// SPDX-License-Identifier: GPL-3.0-or-later // The per-area simulated-turf settings (gpu/vk/sim_turf_settings.h) over a sparse stand-in for the guest window, without Vulkan // or the game. ((Triforce) The area comes from the host's word, a byte at a time (HOS_OPT_SIM_TURF_AREA_TEXT), and the file is // written whole by file_whole.h, as the renderer writes it.) #define NOMINMAX #ifdef _WIN32 #include #else #include #include #endif #include #include #include #include #include #include #include #include #include "vk_api.h" #include "file_whole.h" namespace mkwvk { static uint8_t* memory; static int64_t clock_ns; static std::filesystem::path fixture; static uint8_t* mock_base() { return memory; } static uint64_t mock_size() { return 0x85000000ull; } static int64_t mock_now() { return clock_ns; } static MkwVkHost mock; const MkwVkHost* host = &mock; void logf(const char*, ...) {} } // namespace mkwvk #include "sim_turf_settings.h" namespace mkwvk::sim_turf_settings { bool write_config(const std::string& path, const std::string& text) { return file_writers::write_whole(path, text); } } namespace { using namespace mkwvk; using namespace mkwvk::sim_turf_settings; template void put(uint64_t at, T value) { std::memcpy(memory + at, &value, sizeof value); } template T get(uint64_t at) { T value{}; assert(read(at, value)); return value; } void scene_data(uint64_t address, float start, float density, int blades, float far_power) { put(address, vtable); put(address + 8, address + 1); put(address + 0x7c, start); put(address + 0x68, density); put(address + 0x70, blades); put(address + 0x6c, far_power); for (const auto& field : fields) put(address + field.flag, uint8_t{1}); } void load(uint64_t address, const char* area) { put(0x14150, address); for (const char* c = area; *c; ++c) area_byte((unsigned char)*c); // (the host's word: HOS_OPT_SIM_TURF_AREA_TEXT, then the end) area_end(1); for (int i = 0; i < 5; ++i) { clock_ns += 250'000'000; tick(clock_ns); } assert(ready); } std::string config_text() { std::ifstream in(config_file); return {std::istreambuf_iterator(in), std::istreambuf_iterator()}; } } // namespace int main(int argc, char** argv) { assert(argc == 2); using namespace mkwvk; using namespace mkwvk::sim_turf_settings; // ((Triforce) the options file's folder by its ANSI path, as GetModuleFileNameA gives it: the options file's own way) fixture = std::filesystem::path(argv[1]) / "sim-turf-test"; assert(std::filesystem::create_directories(fixture)); config_file = (fixture / "sim_turf_settings.ini").string(); assert(!std::filesystem::exists(config_file)); mock.guest_base = mock_base; mock.guest_size = mock_size; mock.now_ns = mock_now; #ifdef _WIN32 memory = static_cast(VirtualAlloc(nullptr, mock_size(), MEM_RESERVE, PAGE_READWRITE)); assert(memory); assert(VirtualAlloc(memory + 0x10000, 0x10000, MEM_COMMIT, PAGE_READWRITE)); assert(VirtualAlloc(memory + 0x84e42000, 4096, MEM_COMMIT, PAGE_READWRITE)); #else void* reserved = mmap(nullptr, mock_size(), PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); assert(reserved != MAP_FAILED); memory = static_cast(reserved); assert(mprotect(memory + 0x10000, 0x10000, PROT_READ | PROT_WRITE) == 0); assert(mprotect(memory + 0x84e42000, 4096, PROT_READ | PROT_WRITE) == 0); #endif put(0x84e42540, 0x10000); put(0x100f0, 0x11000); put(0x13a90, 0x14000); constexpr uint64_t field = 0x18000, market = 0x19000, parent = 0x1a000; scene_data(field, 20.0f, 100.0f, 24, 0.8f); scene_data(market, 8.0f, 200.0f, 2, 0.0f); scene_data(parent, 8.0f, 200.0f, 2, 2.0f); put(market + 0xb1, uint8_t{0}); put(market + 0x10, 1); put(market + 0x18, 0x1b000); put(0x1b000, parent); put(market + 0x20, 1); put(0x1b000 + 0xc, 1); load(field, "HyruleField_Adult (adult)"); assert((values == std::array{200, 100, 24, 80})); // The explicit low-end mode writes both zero controls live and saves them for only this area. values[0] = 0; area_changed(0); values[2] = 0; area_changed(2); flush_config(); assert(get(field + 0x7c) == 0.0f && get(field + 0x70) == 0); assert(!config_dirty && config_text().find("HyruleField_Adult") != std::string::npos); assert(edited_now.load()); // ((Triforce) an edit of this area: the renderer's own turf lists in use) // Global multipliers temporarily replace the area edit, including inherited values, then restore it. global = 1; mode_changed(); multipliers[1] = 20; multiplier_changed(); assert(get(field + 0x70) == 24 && get(field + 0x68) == 50.0f); load(market, "Market_Adult (adult)"); assert(get(market + 0x68) == 100.0f && values[3] == 200); multipliers[3] = 20; multiplier_changed(); assert(get(market + 0x6c) == 4.0f); global = 0; mode_changed(); assert(get(market + 0x68) == 200.0f && get(market + 0xb1) == 0); // A failed atomic reset keeps both the saved edit and the live values; a successful one restores the install. values[2] = 4; area_changed(2); values[3] = 300; area_changed(3); flush_config(); assert(can_reset_area() && get(market + 0x70) == 4 && get(market + 0x6c) == 3.0f); const auto before_reset = config_text(); const std::filesystem::path blocked_temp = config_file + ".part"; // (file_whole.h's: a folder there, the write fails) assert(std::filesystem::create_directory(blocked_temp)); reset_area(); assert(save_failed && can_reset_area() && config_text() == before_reset); assert(get(market + 0x70) == 4 && get(market + 0x6c) == 3.0f); assert(!std::filesystem::exists(blocked_temp) || std::filesystem::remove(blocked_temp)); reset_area(); assert(!save_failed && !can_reset_area()); assert(!edited_now.load()); // ((Triforce) the game's own values again: the game's own turf lists) assert(get(market + 0x70) == 2 && get(market + 0xb1) == 0); // Returning to the first area reapplies its own zero-turf edit, not Market's values. load(field, "HyruleField_Adult (adult)"); assert(get(field + 0x7c) == 0.0f && get(field + 0x70) == 0); load(market, "Market_Adult (adult)"); assert(get(market + 0x70) == 2 && !can_reset_area()); #ifdef _WIN32 assert(VirtualFree(memory, 0, MEM_RELEASE)); #else assert(munmap(memory, mock_size()) == 0); #endif std::puts("PASS: per-area simulated-turf settings, zero-turf mode, inheritance, persistence and reset rollback"); }