From 7c9477d2d1721d6b843c11e2ef7b3d989ad305d7 Mon Sep 17 00:00:00 2001 From: ineed bots Date: Tue, 29 Aug 2023 18:57:26 -0600 Subject: [PATCH] Added caching for sigs --- src/component/signatures.cpp | 77 ++++++++++++++++++++++++++++++++++++ src/stdinc.hpp | 1 + 2 files changed, 78 insertions(+) diff --git a/src/component/signatures.cpp b/src/component/signatures.cpp index 643515d..216e430 100644 --- a/src/component/signatures.cpp +++ b/src/component/signatures.cpp @@ -1,9 +1,75 @@ #include #include "signatures.hpp" #include +#include +#include +#include namespace signatures { + std::string read_sigs_file() + { + return utils::io::read_file("t4sp-server-plugin/sigs.json"); + } + + bool write_sigs_file(const std::string& f) + { + return utils::io::write_file("t4sp-server-plugin/sigs.json", f); + } + + const char* get_current_version() + { + return *reinterpret_cast(0x4FF72D + 4); + } + + std::unordered_map get_cache_info_for_our_version() + { + std::unordered_map answer; + + auto* version = get_current_version(); + + nlohmann::json cache_json = nlohmann::json::parse(read_sigs_file(), nullptr, false, true); + if (!cache_json.is_discarded() && cache_json.is_object()) + { + for (const auto& [key, value] : cache_json.items()) + { + if (key != version) + { + continue; + } + + if (!value.is_object()) + { + continue; + } + + answer = value.get>(); + break; + } + } + + return answer; + } + + bool save_cache_info_for_our_version(const std::unordered_map& cache_info) + { + auto* version = get_current_version(); + + nlohmann::json cache_json = nlohmann::json::parse(read_sigs_file(), nullptr, false, true); + if (cache_json.is_discarded() || !cache_json.is_object()) + { + cache_json = nlohmann::json::parse("{}", nullptr, false, true); + + if (cache_json.is_discarded() || !cache_json.is_object()) + { + return false; // can't happen? + } + } + + cache_json[version] = cache_info; + return write_sigs_file(cache_json.dump()); + } + size_t load_image_size() { MODULEINFO info{}; @@ -53,6 +119,14 @@ namespace signatures bool process_printf() { + auto cache_info = get_cache_info_for_our_version(); + + if (cache_info.contains("printf")) + { + game::plutonium::printf.set(std::atoi(cache_info.at("printf").c_str())); + return true; + } + const auto string_ref = find_string_ref("A critical exception occured!\n"); if (!string_ref) { @@ -62,6 +136,9 @@ namespace signatures const auto offset = *reinterpret_cast(string_ref + 5); game::plutonium::printf.set(string_ref + 4 + 5 + offset); + cache_info.insert_or_assign("printf", std::to_string(string_ref + 4 + 5 + offset)); + save_cache_info_for_our_version(cache_info); + return true; } diff --git a/src/stdinc.hpp b/src/stdinc.hpp index c49690f..847b07a 100644 --- a/src/stdinc.hpp +++ b/src/stdinc.hpp @@ -8,6 +8,7 @@ #pragma warning(disable: 28182) #pragma warning(disable: 4324) +#pragma warning(disable: 4459) #define DLL_EXPORT extern "C" __declspec(dllexport) #define WIN32_LEAN_AND_MEAN