upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/mods_test.cpp
blob: c61182c3cb1f291d7fe955a2e90674c6ff56cd40 (plain)
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
// mods.h: mod.json manifests and archetype swaps (docs/modding.md). No game files.
#include "../src/mods.h"

#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>

namespace {

int g_checks = 0, g_failures = 0;

void check(bool ok, const char* what) {
    ++g_checks;
    if (!ok) {
        ++g_failures;
        std::printf("FAIL %s\n", what);
    }
}

void write(const std::filesystem::path& p, const std::string& text) {
    std::filesystem::create_directories(p.parent_path());
    std::ofstream(p, std::ios::binary) << text;
}

}  // namespace

int main() {
    using namespace leonida::mods;

    // The model ids docs/character_skeleton.md measured in the packs tables are the joaat of their names.
    static_assert(joaat("player_story000_00") == 0xE472B687u, "Lucia's player model id");
    static_assert(joaat("cs_mandy") == 0xD7C0CDFFu, "cs_mandy's model id");
    static_assert(joaat("mandy") == 0x03A885E6u, "Mandy's PERSCHARS name hash");
    static_assert(joaat("CS_Mandy") == joaat("cs_mandy"), "joaat lower-cases");

    uint32_t id = 0;
    check(parse_model_id("0xD7C0CDFF", id) && id == 0xD7C0CDFFu, "a 0x hash is taken as it is");
    check(parse_model_id("cs_monalisa", id) && id == 0x9CB9BCC6u, "a name is hashed");
    check(!parse_model_id("0x123456789", id), "a hash wider than 32 bits is refused");
    check(!parse_model_id("0xZZ", id), "a malformed hash is refused");
    check(!parse_model_id("", id), "an empty name is refused");

    Mod mod;
    std::string error;
    check(parse_mod(R"({"name":"Mandy","archetype_swaps":[{"from":"player_story000_00","to":"cs_mandy"}]})", "m",
                    mod, error) && mod.enabled && mod.archetype_swaps.size() == 1 &&
              mod.archetype_swaps[0].from == 0xE472B687u && mod.archetype_swaps[0].to == 0xD7C0CDFFu,
          "a swap parses into the two model ids");
    check(!parse_mod(R"({"archetype_swaps":[{"from":"a","to":"b","when":"mission:intro"}]})", "m", mod, error) &&
              error.find("\"when\"") != std::string::npos,
          "a condition the runtime cannot honour yet is refused, not ignored");
    check(!parse_mod(R"({"archetype_swaps":[{"from":"cs_mandy","to":"0xD7C0CDFF"}]})", "m", mod, error),
          "a swap of a model with itself is refused");
    check(!parse_mod(R"({"enabled":"yes"})", "m", mod, error), "\"enabled\" must be a boolean");
    check(!parse_mod("[1,2]", "m", mod, error), "a manifest that is not an object is refused");
    check(parse_mod("{}", "folder_name", mod, error) && mod.name == "folder_name", "the name defaults to the folder");

    // "camera": first-person view (docs/modding.md, "First-person view")
    check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4,"contexts":[0]}})", "fp", mod,
                    error) && mod.camera.set && mod.camera.first_person && mod.camera.first_person_value == 4 &&
              mod.camera.contexts.size() == 1 && mod.camera.contexts[0] == 0,
          "a first-person camera section parses");
    check(parse_mod(R"({"camera":{"view_mode":"default"}})", "d", mod, error) && mod.camera.set &&
              !mod.camera.first_person,
          "\"default\" leaves the game's view mode");
    check(!parse_mod(R"({"camera":{"view_mode":"top_down"}})", "x", mod, error), "an unknown view mode is refused");
    check(!parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4.5}})", "x", mod, error),
          "a fractional enum value is refused");
    check(!parse_mod(R"({"camera":{"view_mode":"first_person","contexts":"all"}})", "x", mod, error),
          "contexts must be a list");

    Mod fp_on_foot, fp_everywhere, off;
    check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[0]}})", "a", fp_on_foot, error) &&
              parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":7}})", "b", fp_everywhere,
                        error) &&
              parse_mod(R"({"enabled":false,"camera":{"view_mode":"default"}})", "c", off, error),
          "three camera mods parse");
    set_active_camera(camera_of({fp_on_foot}));
    check(override_view_mode(2, 0) == 4, "on foot (context 0) the view becomes first person");
    check(override_view_mode(2, 1) == 2, "a context the mod does not list keeps the game's view");
    set_active_camera(camera_of({fp_on_foot, fp_everywhere, off}));
    check(override_view_mode(1, 5) == 7, "the last enabled camera mod decides (a disabled one is skipped)");
    set_active_camera(camera_of({}));
    check(override_view_mode(3, 0) == 3, "with no camera mod the game's view mode passes through");

    const auto dir = std::filesystem::temp_directory_path() / "leonida_mods_test";
    std::filesystem::remove_all(dir);
    write(dir / "a_mandy" / "mod.json",
          R"({"name":"Mandy","archetype_swaps":[{"from":"player_story000_00","to":"cs_mandy","when":"always"}]})");
    write(dir / "b_off" / "mod.json",
          R"({"name":"Off","enabled":false,"archetype_swaps":[{"from":"cs_mandy","to":"cs_monalisa"}]})");
    write(dir / "c_broken" / "mod.json", "{not json");
    write(dir / "d_notes" / "readme.txt", "not a mod");
    write(dir / "e_later" / "mod.json", R"({"archetype_swaps":[{"from":"player_story000_00","to":"cs_monalisa"}]})");

    const LoadResult loaded = load_mods(dir);
    check(loaded.mods.size() == 3, "three manifests load (the broken one and the folder without one do not)");
    check(loaded.errors.size() == 1 && loaded.errors[0].rfind("c_broken:", 0) == 0, "the broken one is reported");

    const ArchetypeSwaps swaps(loaded.mods);
    check(swaps.remap(0xE472B687u) == joaat("cs_monalisa"), "a later folder wins for the same model");
    check(swaps.remap(0xD7C0CDFFu) == 0xD7C0CDFFu, "a disabled mod swaps nothing");
    check(swaps.remap(0x12345678u) == 0x12345678u, "a model no mod names is left alone");

    const ArchetypeSwaps mandy_only({loaded.mods[0]});
    check(mandy_only.remap(0xE472B687u) == 0xD7C0CDFFu, "the Mandy mod swaps Lucia's player model for cs_mandy");

    check(load_mods(dir / "missing").mods.empty(), "no mods folder is no mods");
    std::filesystem::remove_all(dir);

    std::printf("mods_test: %d checks, %d failures\n", g_checks, g_failures);
    return g_failures == 0 ? 0 : 1;
}