upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/mods_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/mods_test.cpp')
-rw-r--r--runtime/test/mods_test.cpp55
1 files changed, 52 insertions, 3 deletions
diff --git a/runtime/test/mods_test.cpp b/runtime/test/mods_test.cpp
index c61182c3c..a29979b25 100644
--- a/runtime/test/mods_test.cpp
+++ b/runtime/test/mods_test.cpp
@@ -1,5 +1,6 @@
1// mods.h: mod.json manifests and archetype swaps (docs/modding.md). No game files. 1// mods.h: mod.json manifests and archetype swaps (docs/modding.md). No game files.
2#include "../src/mods.h" 2#include "../src/mods.h"
3#include "test_temp_dir.h"
3 4
4#include <cstdio> 5#include <cstdio>
5#include <filesystem> 6#include <filesystem>
@@ -56,6 +57,36 @@ int main() {
56 check(!parse_mod("[1,2]", "m", mod, error), "a manifest that is not an object is refused"); 57 check(!parse_mod("[1,2]", "m", mod, error), "a manifest that is not an object is refused");
57 check(parse_mod("{}", "folder_name", mod, error) && mod.name == "folder_name", "the name defaults to the folder"); 58 check(parse_mod("{}", "folder_name", mod, error) && mod.name == "folder_name", "the name defaults to the folder");
58 59
60 // The encodings Windows editors and shells write. A UTF-8 byte-order mark is skipped; UTF-16 is named.
61 const std::string manifest =
62 R"({"name":"Mandy","archetype_swaps":[{"from":"player_story000_00","to":"cs_mandy"}]})";
63 const std::string bom = "\xEF\xBB\xBF";
64 check(parse_mod(bom + manifest, "m", mod, error) && mod.name == "Mandy" && mod.archetype_swaps.size() == 1 &&
65 mod.archetype_swaps[0].to == 0xD7C0CDFFu,
66 "a manifest saved as UTF-8 with a byte-order mark parses");
67 check(parse_mod(bom + "{\r\n \"name\": \"crlf\"\r\n}\r\n", "m", mod, error) && mod.name == "crlf",
68 "a byte-order mark before CRLF text parses");
69 check(!parse_mod(bom + "[1,2]", "m", mod, error) && error == "mod.json is not a JSON object",
70 "a byte-order mark does not make a list a manifest");
71 check(!parse_mod(bom, "m", mod, error) && !parse_mod(bom + bom + manifest, "m", mod, error),
72 "a byte-order mark alone, or two of them, is still refused");
73 const auto utf16 = [&manifest](bool little_endian, bool mark) {
74 std::string out;
75 if (mark) out = little_endian ? std::string("\xFF\xFE", 2) : std::string("\xFE\xFF", 2);
76 for (const char c : manifest) {
77 if (!little_endian) out.push_back('\0');
78 out.push_back(c);
79 if (little_endian) out.push_back('\0');
80 }
81 return out;
82 };
83 for (const std::string& text : {utf16(true, true), utf16(false, true), utf16(true, false), utf16(false, false)})
84 check(!parse_mod(text, "m", mod, error) && error == "mod.json is UTF-16; save it as UTF-8",
85 "a UTF-16 manifest is refused by name, with or without a byte-order mark");
86 check(!parse_mod("", "m", mod, error) && !parse_mod("{", "m", mod, error) &&
87 error == "mod.json is not a JSON object",
88 "an empty or one-byte manifest is not mistaken for UTF-16");
89
59 // "camera": first-person view (docs/modding.md, "First-person view") 90 // "camera": first-person view (docs/modding.md, "First-person view")
60 check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4,"contexts":[0]}})", "fp", mod, 91 check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4,"contexts":[0]}})", "fp", mod,
61 error) && mod.camera.set && mod.camera.first_person && mod.camera.first_person_value == 4 && 92 error) && mod.camera.set && mod.camera.first_person && mod.camera.first_person_value == 4 &&
@@ -69,6 +100,25 @@ int main() {
69 "a fractional enum value is refused"); 100 "a fractional enum value is refused");
70 check(!parse_mod(R"({"camera":{"view_mode":"first_person","contexts":"all"}})", "x", mod, error), 101 check(!parse_mod(R"({"camera":{"view_mode":"first_person","contexts":"all"}})", "x", mod, error),
71 "contexts must be a list"); 102 "contexts must be a list");
103 // A context is a uint32: every whole value up to 4294967295 is taken, and nothing is cast before it is bounded.
104 check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[2147483647,2147483648,4294967295]}})", "x",
105 mod, error) && mod.camera.contexts.size() == 3 && mod.camera.contexts[0] == 2147483647u &&
106 mod.camera.contexts[1] == 2147483648u && mod.camera.contexts[2] == 4294967295u,
107 "contexts past INT_MAX are whole numbers too");
108 for (const char* value : {"4294967296", "1e300", "-1", "1.5", "-0.5"}) {
109 const std::string text = std::string(R"({"camera":{"view_mode":"first_person","contexts":[)") + value + "]}}";
110 check(!parse_mod(text, "x", mod, error) &&
111 error.find("whole numbers from 0 to 4294967295") != std::string::npos,
112 "a context that is not a whole uint32 is refused");
113 }
114 for (const char* value : {"256", "-1", "1e300", "4.5"}) {
115 const std::string text =
116 std::string(R"({"camera":{"view_mode":"first_person","first_person_value":)") + value + "}}";
117 check(!parse_mod(text, "x", mod, error), "a first_person_value outside 0-255 is refused");
118 }
119 check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":255}})", "x", mod, error) &&
120 mod.camera.first_person_value == 255,
121 "255 is the largest first_person_value");
72 122
73 Mod fp_on_foot, fp_everywhere, off; 123 Mod fp_on_foot, fp_everywhere, off;
74 check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[0]}})", "a", fp_on_foot, error) && 124 check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[0]}})", "a", fp_on_foot, error) &&
@@ -84,8 +134,8 @@ int main() {
84 set_active_camera(camera_of({})); 134 set_active_camera(camera_of({}));
85 check(override_view_mode(3, 0) == 3, "with no camera mod the game's view mode passes through"); 135 check(override_view_mode(3, 0) == 3, "with no camera mod the game's view mode passes through");
86 136
87 const auto dir = std::filesystem::temp_directory_path() / "leonida_mods_test"; 137 const testfiles::ScratchDir scratch("leonida_mods_test");
88 std::filesystem::remove_all(dir); 138 const auto& dir = scratch.path();
89 write(dir / "a_mandy" / "mod.json", 139 write(dir / "a_mandy" / "mod.json",
90 R"({"name":"Mandy","archetype_swaps":[{"from":"player_story000_00","to":"cs_mandy","when":"always"}]})"); 140 R"({"name":"Mandy","archetype_swaps":[{"from":"player_story000_00","to":"cs_mandy","when":"always"}]})");
91 write(dir / "b_off" / "mod.json", 141 write(dir / "b_off" / "mod.json",
@@ -107,7 +157,6 @@ int main() {
107 check(mandy_only.remap(0xE472B687u) == 0xD7C0CDFFu, "the Mandy mod swaps Lucia's player model for cs_mandy"); 157 check(mandy_only.remap(0xE472B687u) == 0xD7C0CDFFu, "the Mandy mod swaps Lucia's player model for cs_mandy");
108 158
109 check(load_mods(dir / "missing").mods.empty(), "no mods folder is no mods"); 159 check(load_mods(dir / "missing").mods.empty(), "no mods folder is no mods");
110 std::filesystem::remove_all(dir);
111 160
112 std::printf("mods_test: %d checks, %d failures\n", g_checks, g_failures); 161 std::printf("mods_test: %d checks, %d failures\n", g_checks, g_failures);
113 return g_failures == 0 ? 0 : 1; 162 return g_failures == 0 ? 0 : 1;