#include "wmischook.h" #include "util/logging.h" #include "util/detour.h" #include "util/sigscan.h" namespace hooks::wmisc::thumbnail { typedef bool (*ThumExists_t)(void *titleUTF_getter, int32_t id, char flag); static ThumExists_t ThumExists; bool on_thum_exists_(void *titleUTF_getter, int32_t id, char flag) { static std::unordered_map thumids; auto it = thumids.find(id); if (it != thumids.end()) { return it->second; } else { log_misc("hooks::wmisc::thumbnail", "Caching state for [{}]", id); it = thumids.insert({id, ThumExists(titleUTF_getter, id, flag)}).first; } return it->second; } void run(HINSTANCE hmodule) { // Reverse lookup function head (PUSH RDI) for ThumExists() specific signature, quietly fail if invalid intptr_t beg, match, TX_ThumExists; TX_ThumExists = beg = match = find_pattern_from(hmodule, "83f801750c4084ff750732c0", "XXXXXXXXXXXX", 0, 0, 0); while (beg > sizeof(intptr_t) && match >= TX_ThumExists) { beg--; match = find_pattern_from(hmodule, "4057", "XX", 0, 0, beg - reinterpret_cast(hmodule)); } TX_ThumExists = match; log_info("hooks::wmisc::thumbnail", "Hooking ThumExists({})", reinterpret_cast(TX_ThumExists)); detour::trampoline_try(reinterpret_cast(TX_ThumExists), reinterpret_cast(on_thum_exists_), reinterpret_cast(&ThumExists)); } } namespace hooks::wmisc { static constexpr unsigned int by_model(const char *model, int h = 0) { return !model[h] ? 5381 : (by_model(model, h + 1) * 33) ^ model[h]; } static constexpr unsigned int by_ext(const char *ext, int h = 0) { return !ext[h] ? 5381 : (by_ext(ext, h + 1) * 33) ^ ext[h]; } void apply_performance_hacks(HINSTANCE hmodule, const char *model, const char *ext) { log_info("hooks::wmisc", "Enabling performance patches for {}-{}", model, ext); switch (by_model(model)) { case by_model("LDJ"): thumbnail::run(hmodule); break; default: break; } } }