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
|
// Mods: folders under mods/ with a mod.json that the runtime reads at start (docs/modding.md).
//
// A mod never ships game data. It names things the game already has, by the names (or joaat hashes) the game uses,
// and says how to swap them. The first kind is an archetype swap: whenever the game asks for the model archetype
// `from`, it gets `to` instead (e.g. Lucia's player model player_story000_00 -> cs_mandy). Applying a swap needs one
// hook in the game: its archetype lookup by hash, whose address is per executable version and is filled in once the
// executable is analysed. Until then the swaps are loaded, checked and logged, and `remap_archetype` is ready for it.
//
// mod.json:
// { "name": "Mandy in the prison intro", "enabled": true, "game_version": "1.0.2.1",
// "archetype_swaps": [ { "from": "player_story000_00", "to": "cs_mandy", "when": "always" } ] }
// `from`/`to`: a model name (hashed with joaat, lower case, as the game does) or a 32-bit hash written "0x...".
// `when`: "always" (default). Narrower conditions (one mission, one cutscene) need the game's mission state and are
// rejected until a hook provides it, so a mod never silently does more than it says.
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
#include "shader_library.h" // leonida::Json / JsonReader
namespace leonida::mods {
// RAGE's joaat (Jenkins one-at-a-time over the lower-cased name): joaat("player_story000_00") = 0xE472B687,
// joaat("cs_mandy") = 0xD7C0CDFF, the model ids docs/character_skeleton.md measured in the packs tables.
constexpr uint32_t joaat(std::string_view name) {
uint32_t h = 0;
for (char c : name) {
const auto u = static_cast<unsigned char>(c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c);
h += u;
h += h << 10;
h ^= h >> 6;
}
h += h << 3;
h ^= h >> 11;
h += h << 15;
return h;
}
struct ArchetypeSwap {
uint32_t from = 0, to = 0;
std::string from_name, to_name; // as written in mod.json
std::string when = "always";
};
// "camera": the view mode the game's view-mode getter answers (docs/modding.md, "First-person view"). The getter
// takes a context (on foot, in a vehicle, ...) and returns an enum; `first_person_value` is that enum's first-person
// value, 4 in GTA V's camViewModeSettings, to be confirmed in the executable. `contexts`: the getter's first-argument
// values to override; empty = every context.
struct CameraView {
bool set = false; // the mod has a "camera" section
bool first_person = false; // "view_mode": "first_person" (else "default": leave the game's choice)
uint32_t first_person_value = 4;
std::vector<uint32_t> contexts;
};
struct Mod {
std::string folder; // the folder name under mods/
std::string name;
std::string game_version;
bool enabled = true;
std::vector<ArchetypeSwap> archetype_swaps;
CameraView camera;
};
struct LoadResult {
std::vector<Mod> mods; // every mod.json that parsed, enabled or not
std::vector<std::string> errors; // "<folder>: what was wrong" (that mod is skipped)
};
// "0x..." -> that value; anything else -> joaat(name). Returns false for an empty name or a malformed hex value.
inline bool parse_model_id(const std::string& text, uint32_t& out) {
if (text.empty()) return false;
if (text.size() >= 2 && text[0] == '0' && (text[1] == 'x' || text[1] == 'X')) {
if (text.size() == 2 || text.size() > 10) return false;
uint32_t v = 0;
for (std::size_t i = 2; i < text.size(); ++i) {
const char c = text[i];
const int d = c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10
: c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1;
if (d < 0) return false;
v = v << 4 | static_cast<uint32_t>(d);
}
out = v;
return true;
}
out = joaat(text);
return true;
}
// A JSON number that is a whole value in [0, max]. Checked before any integer cast: converting a double outside the
// target type's range is undefined, so `v != static_cast<int>(v)` cannot be the test for a value past INT_MAX.
inline bool whole_number(double v, double max) { return v >= 0 && v <= max && v == std::floor(v); }
// Parses one mod.json's text. On failure returns false with `error` set; `mod` is then not to be used.
inline bool parse_mod(std::string_view text, const std::string& folder, Mod& mod, std::string& error) {
// Windows editors and shells mark the encoding. Notepad's "UTF-8 with BOM" and Windows PowerShell 5.1's
// `-Encoding utf8` put EF BB BF in front of the same JSON, so it is skipped. PowerShell 5.1's `>` writes UTF-16,
// which is not readable as bytes: say so, instead of blaming the JSON. A NUL in the first two bytes is UTF-16
// without a mark; no UTF-8 JSON text starts that way.
if (text.substr(0, 3) == std::string_view("\xEF\xBB\xBF", 3)) {
text.remove_prefix(3);
} else if (text.size() >= 2 && (text.substr(0, 2) == std::string_view("\xFF\xFE", 2) ||
text.substr(0, 2) == std::string_view("\xFE\xFF", 2) || text[0] == '\0' ||
text[1] == '\0')) {
error = "mod.json is UTF-16; save it as UTF-8";
return false;
}
Json root;
if (!JsonReader(text).parse(root) || root.kind != Json::Kind::object) {
error = "mod.json is not a JSON object";
return false;
}
mod = Mod{};
mod.folder = folder;
const Json* name = root.find("name");
mod.name = name && name->kind == Json::Kind::string ? name->string : folder;
if (const Json* v = root.find("game_version"); v && v->kind == Json::Kind::string) mod.game_version = v->string;
if (const Json* e = root.find("enabled")) {
if (e->kind != Json::Kind::boolean) {
error = "\"enabled\" must be true or false";
return false;
}
mod.enabled = e->boolean;
}
if (const Json* swaps = root.find("archetype_swaps")) {
if (swaps->kind != Json::Kind::array) {
error = "\"archetype_swaps\" must be a list";
return false;
}
for (const Json& s : swaps->array) {
const Json* from = s.find("from");
const Json* to = s.find("to");
if (s.kind != Json::Kind::object || !from || !to || from->kind != Json::Kind::string ||
to->kind != Json::Kind::string) {
error = "each archetype swap needs \"from\" and \"to\" model names";
return false;
}
ArchetypeSwap swap;
swap.from_name = from->string;
swap.to_name = to->string;
if (!parse_model_id(swap.from_name, swap.from) || !parse_model_id(swap.to_name, swap.to)) {
error = "archetype swap " + swap.from_name + " -> " + swap.to_name + ": not a model name or 0x hash";
return false;
}
if (const Json* when = s.find("when")) {
if (when->kind != Json::Kind::string || when->string != "always") {
error = "archetype swap " + swap.from_name + ": \"when\" can only be \"always\" until a game hook "
"provides mission state (docs/modding.md)";
return false;
}
}
if (swap.from == swap.to) {
error = "archetype swap " + swap.from_name + " -> " + swap.to_name + " swaps a model with itself";
return false;
}
mod.archetype_swaps.push_back(std::move(swap));
}
}
if (const Json* cam = root.find("camera")) {
if (cam->kind != Json::Kind::object) {
error = "\"camera\" must be an object";
return false;
}
mod.camera.set = true;
const Json* view = cam->find("view_mode");
if (!view || view->kind != Json::Kind::string ||
(view->string != "first_person" && view->string != "default")) {
error = "\"camera\": \"view_mode\" must be \"first_person\" or \"default\"";
return false;
}
mod.camera.first_person = view->string == "first_person";
if (const Json* v = cam->find("first_person_value")) {
if (v->kind != Json::Kind::number || !whole_number(v->number, 255)) {
error = "\"camera\": \"first_person_value\" must be a whole number 0-255";
return false;
}
mod.camera.first_person_value = static_cast<uint32_t>(v->number);
}
if (const Json* c = cam->find("contexts")) {
if (c->kind != Json::Kind::array) {
error = "\"camera\": \"contexts\" must be a list of numbers";
return false;
}
for (const Json& x : c->array) {
if (x.kind != Json::Kind::number || !whole_number(x.number, 4294967295.0)) {
error = "\"camera\": \"contexts\" must be a list of whole numbers from 0 to 4294967295";
return false;
}
mod.camera.contexts.push_back(static_cast<uint32_t>(x.number));
}
}
}
return true;
}
// Reads every <dir>/<folder>/mod.json, in folder-name order. A missing dir is no mods, not an error.
inline LoadResult load_mods(const std::filesystem::path& dir) {
LoadResult result;
std::error_code ec;
if (!std::filesystem::is_directory(dir, ec)) return result;
std::vector<std::filesystem::path> folders;
for (const auto& entry : std::filesystem::directory_iterator(dir, ec))
if (entry.is_directory(ec)) folders.push_back(entry.path());
std::sort(folders.begin(), folders.end());
for (const auto& folder : folders) {
const std::string name = folder.filename().string();
std::ifstream f(folder / "mod.json", std::ios::binary);
if (!f) continue; // a folder without a manifest is not a mod (notes, assets of another tool)
const std::string text((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
Mod mod;
std::string error;
if (parse_mod(text, name, mod, error))
result.mods.push_back(std::move(mod));
else
result.errors.push_back(name + ": " + error);
}
return result;
}
// The swap table the archetype hook consults: enabled mods only, later folders win for the same `from`.
class ArchetypeSwaps {
public:
explicit ArchetypeSwaps(const std::vector<Mod>& mods) {
for (const Mod& m : mods) {
if (!m.enabled) continue;
for (const ArchetypeSwap& s : m.archetype_swaps) {
bool replaced = false;
for (auto& [from, to] : table_)
if (from == s.from) to = s.to, replaced = true;
if (!replaced) table_.emplace_back(s.from, s.to);
}
}
}
// The archetype the game should get when it asks for `hash` (itself when no mod swaps it). One step only: a swap
// to a model that is itself swapped is not followed, so two mods cannot loop.
uint32_t remap(uint32_t hash) const {
for (const auto& [from, to] : table_)
if (from == hash) return to;
return hash;
}
std::size_t size() const { return table_.size(); }
private:
std::vector<std::pair<uint32_t, uint32_t>> table_;
};
// The process's swaps: set once at start (main.cpp), before any game code runs; read by the archetype hook.
inline ArchetypeSwaps& active_slot() {
static ArchetypeSwaps swaps{std::vector<Mod>{}};
return swaps;
}
inline void set_active(ArchetypeSwaps swaps) { active_slot() = std::move(swaps); }
inline const ArchetypeSwaps& active() { return active_slot(); }
// The camera override of the enabled mods: the last one (in folder order) with a "camera" section decides.
inline CameraView camera_of(const std::vector<Mod>& mods) {
CameraView view;
for (const Mod& m : mods)
if (m.enabled && m.camera.set) view = m.camera;
return view;
}
inline CameraView& camera_slot() {
static CameraView view;
return view;
}
inline void set_active_camera(CameraView view) { camera_slot() = std::move(view); }
// What the hooked view-mode getter returns: the mod's first-person value for an overridden context, else the game's.
inline uint32_t override_view_mode(uint32_t result, uint32_t context) {
const CameraView& v = camera_slot();
if (!v.first_person) return result;
if (!v.contexts.empty()) {
bool listed = false;
for (uint32_t c : v.contexts) listed = listed || c == context;
if (!listed) return result;
}
return v.first_person_value;
}
} // namespace leonida::mods
|