-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_probe.cpp
More file actions
176 lines (151 loc) · 5.55 KB
/
Copy pathsave_probe.cpp
File metadata and controls
176 lines (151 loc) · 5.55 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
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
namespace fs = std::filesystem;
static std::vector<std::uint8_t> ReadFileBytes(const fs::path& path) {
std::ifstream in(path, std::ios::binary);
if (!in) {
return {};
}
in.seekg(0, std::ios::end);
const std::streamsize size = in.tellg();
in.seekg(0, std::ios::beg);
std::vector<std::uint8_t> bytes(static_cast<size_t>(size));
if (size > 0) {
in.read(reinterpret_cast<char*>(bytes.data()), size);
}
return bytes;
}
static std::uint16_t ReadU16LE(const std::vector<std::uint8_t>& b, size_t off) {
return static_cast<std::uint16_t>(b[off] | (static_cast<std::uint16_t>(b[off + 1]) << 8));
}
static std::map<int, std::string> LoadMissionNames(const fs::path& missionsPath) {
std::map<int, std::string> out;
std::ifstream in(missionsPath);
if (!in) {
return out;
}
std::regex rowRe(R"(^\s*(\d{3})\s+(.+?)\s*$)");
std::string line;
while (std::getline(in, line)) {
std::smatch m;
if (!std::regex_match(line, m, rowRe)) {
continue;
}
const int id = std::stoi(m[1].str());
out[id] = m[2].str();
}
return out;
}
struct SaveInfo {
fs::path path;
int profileFromName = -1;
int slotFromName = -1;
std::uint16_t missionDecoded = 0;
std::uint16_t missionCheckD1 = 0;
std::uint16_t missionCheckD2 = 0;
bool validGvaS = false;
bool hasHeader = false;
};
static SaveInfo ParseGvaSSave(const fs::path& path) {
SaveInfo info;
info.path = path;
std::regex nameRe(R"(^mafia(\d+)\.(\d+)$)", std::regex::icase);
std::smatch nameMatch;
const std::string baseName = path.filename().string();
if (std::regex_match(baseName, nameMatch, nameRe)) {
info.profileFromName = std::stoi(nameMatch[1].str());
info.slotFromName = std::stoi(nameMatch[2].str());
}
const auto bytes = ReadFileBytes(path);
if (bytes.size() < 44) {
return info;
}
info.hasHeader = true;
info.validGvaS = (bytes[0] == 'G' && bytes[1] == 'v' && bytes[2] == 'a' && bytes[3] == 'S');
if (!info.validGvaS) {
return info;
}
const std::uint8_t d0b0 = bytes[32];
const std::uint8_t d0b1 = bytes[33];
const std::uint16_t d1 = ReadU16LE(bytes, 36);
const std::uint16_t d2 = ReadU16LE(bytes, 40);
const std::uint16_t missionFromD0 =
static_cast<std::uint16_t>(((static_cast<std::uint16_t>(d0b1 ^ 0x1C) << 8) | (d0b0 ^ 0x30)));
const std::uint16_t missionFromD1 = static_cast<std::uint16_t>(d1 - 0x9D95);
const std::uint16_t missionFromD2Half = static_cast<std::uint16_t>((d2 - 0x1EFA) / 2);
info.missionDecoded = missionFromD0;
info.missionCheckD1 = missionFromD1;
info.missionCheckD2 = missionFromD2Half;
return info;
}
int main(int argc, char** argv) {
const fs::path saveDir = (argc > 1) ? fs::path(argv[1]) : fs::path("savegame");
const fs::path missionsPath = (argc > 2) ? fs::path(argv[2]) : fs::path("missions.txt");
const auto missionNames = LoadMissionNames(missionsPath);
if (!fs::exists(saveDir) || !fs::is_directory(saveDir)) {
std::cerr << "Save directory not found: " << saveDir << "\n";
return 1;
}
std::vector<SaveInfo> rows;
std::regex fileNameRe(R"(^mafia\d+\.\d+$)", std::regex::icase);
for (const auto& entry : fs::directory_iterator(saveDir)) {
if (!entry.is_regular_file()) {
continue;
}
const std::string baseName = entry.path().filename().string();
if (!std::regex_match(baseName, fileNameRe)) {
continue;
}
rows.push_back(ParseGvaSSave(entry.path()));
}
std::sort(rows.begin(), rows.end(), [](const SaveInfo& a, const SaveInfo& b) {
return a.path.filename().string() < b.path.filename().string();
});
std::cout << "file,size,profile,slot,decoded_mission,d1_mission,d2_mission,mission_name,notes\n";
for (const auto& r : rows) {
const auto fileName = r.path.filename().string();
const std::uintmax_t fileSize = fs::file_size(r.path);
std::string missionName;
if (r.validGvaS) {
auto it = missionNames.find(r.missionDecoded);
if (it != missionNames.end()) {
missionName = it->second;
}
}
std::vector<std::string> notes;
if (!r.hasHeader) {
notes.emplace_back("too_small");
} else if (!r.validGvaS) {
notes.emplace_back("not_gvas");
} else {
if (r.slotFromName >= 0 && r.slotFromName != static_cast<int>(r.missionDecoded)) {
notes.emplace_back("slot_mismatch");
}
if (r.missionDecoded != r.missionCheckD1 || r.missionDecoded != r.missionCheckD2) {
notes.emplace_back("header_check_failed");
}
if (missionName.empty()) {
notes.emplace_back("unknown_mission_name");
}
}
std::ostringstream noteJoin;
for (size_t i = 0; i < notes.size(); ++i) {
if (i) {
noteJoin << '|';
}
noteJoin << notes[i];
}
std::cout << fileName << "," << fileSize << "," << r.profileFromName << "," << r.slotFromName << ","
<< r.missionDecoded << "," << r.missionCheckD1 << "," << r.missionCheckD2 << ","
<< missionName << "," << noteJoin.str() << "\n";
}
return 0;
}