-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload_study.cpp
More file actions
310 lines (275 loc) · 9.47 KB
/
Copy pathpayload_study.cpp
File metadata and controls
310 lines (275 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "gvas.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <string>
#include <utility>
#include <vector>
namespace fs = std::filesystem;
namespace {
struct SaveRecord {
gvas::SaveFileMeta meta;
std::vector<std::uint8_t> bytes;
bool valid = false;
};
struct PairStats {
int slot = -1;
std::size_t lenA = 0;
std::size_t lenB = 0;
std::size_t equalPrefix = 0;
std::size_t comparedPayloadBytes = 0;
std::size_t equalPayloadBytes = 0;
};
void PrintUsage() {
std::cout << "Usage:\n"
<< " payload_study cross-profile <save_dir> <profileA> <profileB> [max_payload_offsets]\n"
<< " payload_study same-profile <save_dir> <profile> [top_n]\n";
}
bool LoadSaveRecords(const fs::path& saveDir, std::vector<SaveRecord>* out) {
if (out == nullptr) {
return false;
}
out->clear();
if (!fs::exists(saveDir) || !fs::is_directory(saveDir)) {
return false;
}
for (const auto& entry : fs::directory_iterator(saveDir)) {
if (!entry.is_regular_file()) {
continue;
}
gvas::SaveFileMeta meta;
if (!gvas::ParseSaveFileMeta(entry.path(), &meta)) {
continue;
}
SaveRecord rec;
rec.meta = meta;
rec.bytes = gvas::ReadFileBytes(entry.path());
gvas::Header header;
if (gvas::ReadHeader(rec.bytes, &header)) {
std::string checkErr;
const std::uint16_t mission = gvas::DecodeMission(header);
if (gvas::ValidateMissionChecks(header, &checkErr) && meta.slot == static_cast<int>(mission)) {
rec.valid = true;
}
}
out->push_back(std::move(rec));
}
return true;
}
std::size_t CommonPrefix(const std::vector<std::uint8_t>& a, const std::vector<std::uint8_t>& b) {
const std::size_t n = std::min(a.size(), b.size());
std::size_t i = 0;
while (i < n && a[i] == b[i]) {
++i;
}
return i;
}
PairStats ComparePayload(const SaveRecord& a, const SaveRecord& b) {
PairStats st;
st.slot = a.meta.slot;
st.lenA = a.bytes.size();
st.lenB = b.bytes.size();
st.equalPrefix = CommonPrefix(a.bytes, b.bytes);
if (a.bytes.size() <= gvas::kHeaderSize || b.bytes.size() <= gvas::kHeaderSize) {
return st;
}
const std::size_t cmp = std::min(a.bytes.size(), b.bytes.size()) - gvas::kHeaderSize;
st.comparedPayloadBytes = cmp;
for (std::size_t i = 0; i < cmp; ++i) {
if (a.bytes[gvas::kHeaderSize + i] == b.bytes[gvas::kHeaderSize + i]) {
++st.equalPayloadBytes;
}
}
return st;
}
int CmdCrossProfile(const fs::path& saveDir, int profileA, int profileB, int maxPayloadOffsets) {
std::vector<SaveRecord> all;
if (!LoadSaveRecords(saveDir, &all)) {
std::cerr << "Failed to read save directory: " << saveDir << "\n";
return 1;
}
std::map<int, SaveRecord> aBySlot;
std::map<int, SaveRecord> bBySlot;
for (const auto& rec : all) {
if (!rec.valid) {
continue;
}
if (rec.meta.profile == profileA) {
aBySlot[rec.meta.slot] = rec;
} else if (rec.meta.profile == profileB) {
bBySlot[rec.meta.slot] = rec;
}
}
std::vector<PairStats> pairs;
for (const auto& [slot, ra] : aBySlot) {
auto it = bBySlot.find(slot);
if (it == bBySlot.end()) {
continue;
}
pairs.push_back(ComparePayload(ra, it->second));
}
if (pairs.empty()) {
std::cerr << "No common valid missions for profiles " << profileA << " and " << profileB << "\n";
return 1;
}
std::cout << "slot,lenA,lenB,equal_prefix,payload_compared,payload_equal,payload_equal_ratio\n";
for (const auto& st : pairs) {
const double ratio =
st.comparedPayloadBytes ? static_cast<double>(st.equalPayloadBytes) / st.comparedPayloadBytes : 0.0;
std::cout << st.slot << "," << st.lenA << "," << st.lenB << "," << st.equalPrefix << "," << st.comparedPayloadBytes
<< "," << st.equalPayloadBytes << "," << std::fixed << std::setprecision(6) << ratio << "\n";
}
std::size_t minPrefix = std::numeric_limits<std::size_t>::max();
double sumRatio = 0.0;
for (const auto& st : pairs) {
minPrefix = std::min(minPrefix, st.equalPrefix);
const double ratio =
st.comparedPayloadBytes ? static_cast<double>(st.equalPayloadBytes) / st.comparedPayloadBytes : 0.0;
sumRatio += ratio;
}
const double avgRatio = sumRatio / static_cast<double>(pairs.size());
std::cout << "summary,pairs=" << pairs.size() << ",min_prefix=" << minPrefix << ",avg_payload_equal_ratio="
<< std::fixed << std::setprecision(6) << avgRatio << "\n";
if (maxPayloadOffsets <= 0) {
return 0;
}
std::cout << "offset,pairs_with_byte,equal_pairs,equal_ratio\n";
for (int off = 0; off < maxPayloadOffsets; ++off) {
int total = 0;
int eq = 0;
for (const auto& [slot, ra] : aBySlot) {
auto it = bBySlot.find(slot);
if (it == bBySlot.end()) {
continue;
}
const auto& rb = it->second;
const std::size_t ia = gvas::kHeaderSize + static_cast<std::size_t>(off);
if (ia >= ra.bytes.size() || ia >= rb.bytes.size()) {
continue;
}
++total;
if (ra.bytes[ia] == rb.bytes[ia]) {
++eq;
}
}
if (total == 0) {
continue;
}
const double ratio = static_cast<double>(eq) / static_cast<double>(total);
std::cout << off << "," << total << "," << eq << "," << std::fixed << std::setprecision(6) << ratio << "\n";
}
return 0;
}
int CmdSameProfile(const fs::path& saveDir, int profile, int topN) {
std::vector<SaveRecord> all;
if (!LoadSaveRecords(saveDir, &all)) {
std::cerr << "Failed to read save directory: " << saveDir << "\n";
return 1;
}
std::vector<SaveRecord> rows;
for (const auto& rec : all) {
if (rec.valid && rec.meta.profile == profile && rec.meta.slot <= 561) {
rows.push_back(rec);
}
}
if (rows.size() < 2) {
std::cerr << "Not enough valid saves for profile " << profile << "\n";
return 1;
}
struct ClosePair {
int slotA;
int slotB;
std::size_t size;
std::size_t comparedPayload;
std::size_t diffPayload;
double diffRatio;
std::size_t prefix;
};
std::vector<ClosePair> out;
for (std::size_t i = 0; i < rows.size(); ++i) {
for (std::size_t j = i + 1; j < rows.size(); ++j) {
if (rows[i].bytes.size() != rows[j].bytes.size()) {
continue;
}
if (rows[i].bytes.size() <= gvas::kHeaderSize) {
continue;
}
const std::size_t compared = rows[i].bytes.size() - gvas::kHeaderSize;
std::size_t diff = 0;
for (std::size_t k = 0; k < compared; ++k) {
if (rows[i].bytes[gvas::kHeaderSize + k] != rows[j].bytes[gvas::kHeaderSize + k]) {
++diff;
}
}
ClosePair p{};
p.slotA = rows[i].meta.slot;
p.slotB = rows[j].meta.slot;
p.size = rows[i].bytes.size();
p.comparedPayload = compared;
p.diffPayload = diff;
p.diffRatio = static_cast<double>(diff) / static_cast<double>(compared);
p.prefix = CommonPrefix(rows[i].bytes, rows[j].bytes);
out.push_back(p);
}
}
std::sort(out.begin(), out.end(), [](const ClosePair& a, const ClosePair& b) {
if (std::fabs(a.diffRatio - b.diffRatio) > 1e-12) {
return a.diffRatio < b.diffRatio;
}
return a.size < b.size;
});
if (topN < 1) {
topN = 1;
}
if (topN > static_cast<int>(out.size())) {
topN = static_cast<int>(out.size());
}
std::cout << "slotA,slotB,size,payload_compared,payload_diff,payload_diff_ratio,equal_prefix\n";
for (int i = 0; i < topN; ++i) {
const auto& p = out[static_cast<std::size_t>(i)];
std::cout << p.slotA << "," << p.slotB << "," << p.size << "," << p.comparedPayload << "," << p.diffPayload
<< "," << std::fixed << std::setprecision(6) << p.diffRatio << "," << p.prefix << "\n";
}
return 0;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
PrintUsage();
return 1;
}
const std::string cmd = argv[1];
if (cmd == "cross-profile") {
if (argc < 5 || argc > 6) {
PrintUsage();
return 1;
}
const fs::path dir = argv[2];
const int profileA = std::stoi(argv[3]);
const int profileB = std::stoi(argv[4]);
int maxOffsets = 128;
if (argc == 6) {
maxOffsets = std::stoi(argv[5]);
}
return CmdCrossProfile(dir, profileA, profileB, maxOffsets);
}
if (cmd == "same-profile") {
if (argc < 4 || argc > 5) {
PrintUsage();
return 1;
}
const fs::path dir = argv[2];
const int profile = std::stoi(argv[3]);
int topN = 20;
if (argc == 5) {
topN = std::stoi(argv[4]);
}
return CmdSameProfile(dir, profile, topN);
}
PrintUsage();
return 1;
}