diff options
| author | Rafael <rafaelalvesp2005@gmail.com> | 2026-11-23 15:36:26 -0300 |
|---|---|---|
| committer | Rafael <rafaelalvesp2005@gmail.com> | 2026-11-23 15:36:26 -0300 |
| commit | 63f0e0dea3a9d44201db408c4e2c3f2f5b2b94d6 (patch) | |
| tree | 86f0dcad8070e2a3a693ef4897aa83950cfeb224 /runtime/src/mods.h | |
| parent | f78f7d6a2a9364cb128c03ac539fe540d5c70df1 (diff) | |
| parent | 6dda0630bd6fc0514baadb94fb98e36bd7d64c0b (diff) | |
Merge main into fix/walk-bink_names
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/mods.h')
| -rw-r--r-- | runtime/src/mods.h | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/runtime/src/mods.h b/runtime/src/mods.h index c308ed7db..10155bf19 100644 --- a/runtime/src/mods.h +++ b/runtime/src/mods.h | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | #pragma once | 15 | #pragma once |
| 16 | 16 | ||
| 17 | #include <algorithm> | 17 | #include <algorithm> |
| 18 | #include <cmath> | ||
| 18 | #include <cstdint> | 19 | #include <cstdint> |
| 19 | #include <filesystem> | 20 | #include <filesystem> |
| 20 | #include <fstream> | 21 | #include <fstream> |
| @@ -94,8 +95,24 @@ inline bool parse_model_id(const std::string& text, uint32_t& out) { | |||
| 94 | return true; | 95 | return true; |
| 95 | } | 96 | } |
| 96 | 97 | ||
| 98 | // A JSON number that is a whole value in [0, max]. Checked before any integer cast: converting a double outside the | ||
| 99 | // target type's range is undefined, so `v != static_cast<int>(v)` cannot be the test for a value past INT_MAX. | ||
| 100 | inline bool whole_number(double v, double max) { return v >= 0 && v <= max && v == std::floor(v); } | ||
| 101 | |||
| 97 | // Parses one mod.json's text. On failure returns false with `error` set; `mod` is then not to be used. | 102 | // Parses one mod.json's text. On failure returns false with `error` set; `mod` is then not to be used. |
| 98 | inline bool parse_mod(std::string_view text, const std::string& folder, Mod& mod, std::string& error) { | 103 | inline bool parse_mod(std::string_view text, const std::string& folder, Mod& mod, std::string& error) { |
| 104 | // Windows editors and shells mark the encoding. Notepad's "UTF-8 with BOM" and Windows PowerShell 5.1's | ||
| 105 | // `-Encoding utf8` put EF BB BF in front of the same JSON, so it is skipped. PowerShell 5.1's `>` writes UTF-16, | ||
| 106 | // which is not readable as bytes: say so, instead of blaming the JSON. A NUL in the first two bytes is UTF-16 | ||
| 107 | // without a mark; no UTF-8 JSON text starts that way. | ||
| 108 | if (text.substr(0, 3) == std::string_view("\xEF\xBB\xBF", 3)) { | ||
| 109 | text.remove_prefix(3); | ||
| 110 | } else if (text.size() >= 2 && (text.substr(0, 2) == std::string_view("\xFF\xFE", 2) || | ||
| 111 | text.substr(0, 2) == std::string_view("\xFE\xFF", 2) || text[0] == '\0' || | ||
| 112 | text[1] == '\0')) { | ||
| 113 | error = "mod.json is UTF-16; save it as UTF-8"; | ||
| 114 | return false; | ||
| 115 | } | ||
| 99 | Json root; | 116 | Json root; |
| 100 | if (!JsonReader(text).parse(root) || root.kind != Json::Kind::object) { | 117 | if (!JsonReader(text).parse(root) || root.kind != Json::Kind::object) { |
| 101 | error = "mod.json is not a JSON object"; | 118 | error = "mod.json is not a JSON object"; |
| @@ -161,7 +178,7 @@ inline bool parse_mod(std::string_view text, const std::string& folder, Mod& mod | |||
| 161 | } | 178 | } |
| 162 | mod.camera.first_person = view->string == "first_person"; | 179 | mod.camera.first_person = view->string == "first_person"; |
| 163 | if (const Json* v = cam->find("first_person_value")) { | 180 | if (const Json* v = cam->find("first_person_value")) { |
| 164 | if (v->kind != Json::Kind::number || v->number < 0 || v->number > 255 || v->number != static_cast<int>(v->number)) { | 181 | if (v->kind != Json::Kind::number || !whole_number(v->number, 255)) { |
| 165 | error = "\"camera\": \"first_person_value\" must be a whole number 0-255"; | 182 | error = "\"camera\": \"first_person_value\" must be a whole number 0-255"; |
| 166 | return false; | 183 | return false; |
| 167 | } | 184 | } |
| @@ -173,8 +190,8 @@ inline bool parse_mod(std::string_view text, const std::string& folder, Mod& mod | |||
| 173 | return false; | 190 | return false; |
| 174 | } | 191 | } |
| 175 | for (const Json& x : c->array) { | 192 | for (const Json& x : c->array) { |
| 176 | if (x.kind != Json::Kind::number || x.number < 0 || x.number != static_cast<int>(x.number)) { | 193 | if (x.kind != Json::Kind::number || !whole_number(x.number, 4294967295.0)) { |
| 177 | error = "\"camera\": \"contexts\" must be a list of whole numbers"; | 194 | error = "\"camera\": \"contexts\" must be a list of whole numbers from 0 to 4294967295"; |
| 178 | return false; | 195 | return false; |
| 179 | } | 196 | } |
| 180 | mod.camera.contexts.push_back(static_cast<uint32_t>(x.number)); | 197 | mod.camera.contexts.push_back(static_cast<uint32_t>(x.number)); |