forked from calamity-inc/gta-v-joaat-hash-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maid.cpp
288 lines (272 loc) · 7.5 KB
/
maid.cpp
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
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
static uint32_t joaat(const std::string& str)
{
uint32_t hash = 0;
for (const char c : str)
{
hash += c;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
using hash_to_string_t = std::string(*)(uint32_t);
static std::string hex(uint32_t hash)
{
std::stringstream ss = {};
ss << "0x" << std::setfill('0') << std::setw(8) << std::uppercase << std::hex << hash;
return ss.str();
}
static std::string dec_unsigned(uint32_t hash)
{
return std::to_string(hash);
}
static std::string dec_signed(uint32_t hash)
{
return std::to_string((int32_t)hash);
}
static void for_each_hash_to_string_algo(std::function<void(const char*, hash_to_string_t)>&& func)
{
func("hex", &hex);
func("dec_unsigned", &dec_unsigned);
func("dec_signed", &dec_signed);
}
static void output_db(const std::string& name, const std::map<uint32_t, std::optional<std::string>>& database, hash_to_string_t hash_to_string)
{
std::ofstream ofstream(std::string("out/").append(name).append(".csv"));
ofstream << "hash,key" << std::endl;
for (const auto& entry : database)
{
ofstream << hash_to_string(entry.first) << ",";
if (entry.second.has_value())
{
ofstream << entry.second.value();
}
ofstream << std::endl;
}
}
static void output_db(const std::string& db_name, const std::map<uint32_t, std::optional<std::string>>& database)
{
for_each_hash_to_string_algo([&](const char* hts_name, hash_to_string_t hts_algo)
{
output_db(std::string(db_name).append(1, '-').append(hts_name), database, hts_algo);
});
}
static void output_dict(const char* name, const std::map<uint32_t, std::vector<std::string>>& dictionary, hash_to_string_t hash_to_string)
{
std::ofstream ofstream(std::string("out/dictionary-").append(name).append(".tsv"));
ofstream << "hash\tkey(s)" << std::endl;
for (const auto& entry : dictionary)
{
ofstream << hash_to_string(entry.first) << "\t";
for (auto key_it = entry.second.begin(); key_it != entry.second.end(); ++key_it)
{
if (key_it != entry.second.begin())
{
ofstream << ", ";
}
ofstream << *key_it;
}
ofstream << std::endl;
}
}
static void output_dict(const std::map<uint32_t, std::vector<std::string>>& dictionary)
{
for_each_hash_to_string_algo([&](const char* hts_name, hash_to_string_t hts_algo)
{
output_dict(hts_name, dictionary, hts_algo);
});
}
static void save_db(const std::string& name, const std::map<uint32_t, std::optional<std::string>>& database)
{
std::set<std::string> lines = {};
for (const auto& entry : database)
{
if (entry.second.has_value())
{
lines.emplace(entry.second.value());
}
else
{
lines.emplace(hex(entry.first));
}
}
std::ofstream ofstream(std::string("raw/").append(name).append(".txt"));
for (auto&& line : lines)
{
ofstream << line << std::endl;
}
}
static void load_db(const std::string& name, std::map<uint32_t, std::optional<std::string>>& database)
{
std::ifstream ifstream(std::string("raw/").append(name).append(".txt"));
for (std::string line; std::getline(ifstream, line); )
{
if (line.length() > 4 && line.substr(0, 4) == "dec:")
{
database.emplace((uint32_t)std::stol(line.substr(4)), std::nullopt);
}
else if (line.length() > 2 && line.substr(0, 2) == "0x")
{
database.emplace(std::stoul(line.substr(2), nullptr, 16), std::nullopt);
}
else if (!line.empty())
{
std::transform(line.begin(), line.end(), line.begin(), [](unsigned char c) { return std::tolower(c); });
uint32_t hash = joaat(line);
auto entry = database.find(hash);
if (entry == database.end())
{
database.emplace(std::move(hash), std::move(line));
}
else if (!entry->second.has_value())
{
entry->second = std::move(line);
}
else if (entry->second.value() != line)
{
std::cout << "COLLISION: " << entry->second.value() << " <-> " << line << std::endl;
}
}
}
}
static std::map<std::string, std::map<uint32_t, std::optional<std::string>>> databases;
static void load_db(const std::string& name)
{
std::map<uint32_t, std::optional<std::string>> database = {};
load_db(name, database);
databases.emplace(name, std::move(database));
}
static void copy_db(std::map<uint32_t, std::optional<std::string>>& target, const char* source)
{
if(databases.find(source) == databases.end())
{
load_db(source);
}
auto database = databases.at(source);
target.insert(database.begin(), database.end());
}
int main()
{
try
{
databases = {};
{
std::map<uint32_t, std::optional<std::string>> objects_crash = {};
load_db("objects_crash", objects_crash);
databases.emplace("objects_crash", objects_crash);
std::map<uint32_t, std::optional<std::string>> objects = {};
load_db("objects", objects);
for (auto i = objects.begin(); i != objects.end(); )
{
if (objects_crash.find(i->first) != objects_crash.end())
{
i = objects.erase(i);
}
else
{
++i;
}
}
databases.emplace("objects", objects);
copy_db(objects, "objects_downtown");
copy_db(objects, "objects_drawable");
databases.emplace("objects_complete", std::move(objects));
}
{
std::map<uint32_t, std::optional<std::string>> weapon_types = {};
load_db("weapons", weapon_types);
databases.emplace("weapons", weapon_types);
load_db("weapon_types", weapon_types);
databases.emplace("weapon_types", std::move(weapon_types));
}
{
std::unordered_set<std::string> outputed_dbs = {};
for (const auto& database : databases)
{
output_db(database.first, database.second);
outputed_dbs.emplace(database.first);
}
{
auto weapons = databases.at("weapons");
auto i = databases.at("weapon_types").begin();
while (i != databases.at("weapon_types").end())
{
if (weapons.find(i->first) != weapons.end())
{
i = databases.at("weapon_types").erase(i);
}
else
{
i++;
}
}
}
for (const auto& file : std::filesystem::directory_iterator("raw"))
{
auto filename = file.path().filename().u8string();
if (filename.length() > 4 && filename.substr(filename.length() - 4) == ".txt")
{
auto database_name = filename.substr(0, filename.length() - 4);
if (databases.find(database_name) == databases.end())
{
load_db(database_name);
}
save_db(database_name, databases.at(database_name));
if (outputed_dbs.find(database_name) == outputed_dbs.end())
{
output_db(database_name, databases.at(database_name));
}
}
}
}
{
std::map<uint32_t, std::vector<std::string>> dictionary = {};
for (const auto& database : databases)
{
for (const auto& db_entry : database.second)
{
if (!db_entry.second.has_value())
{
continue;
}
auto dict_entry = dictionary.find(db_entry.first);
if (dict_entry == dictionary.end())
{
dictionary.emplace(db_entry.first, std::vector<std::string>(1, db_entry.second.value()));
}
else if (std::find(dict_entry->second.begin(), dict_entry->second.end(), db_entry.second.value()) == dict_entry->second.end())
{
dict_entry->second.emplace_back(db_entry.second.value());
}
}
}
output_dict(dictionary);
}
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
return 1;
}
catch (...)
{
std::cout << "Good luck figuring this out!" << std::endl;
return 1;
}
return 0;
}