From aa27c65012d4bc21a591d016cf9958695a834a11 Mon Sep 17 00:00:00 2001 From: Ji O Date: Thu, 22 Sep 2022 02:21:53 +0900 Subject: [PATCH] Support windows system encoding Enhanced unlockSize behavior --- src/hook.cpp | 158 +++++++++++++++++++++++++++----------------- src/local/local.cpp | 24 +++++++ src/local/local.hpp | 2 + src/main.cpp | 2 +- 4 files changed, 124 insertions(+), 62 deletions(-) diff --git a/src/hook.cpp b/src/hook.cpp index ff9b5c8..99b1782 100644 --- a/src/hook.cpp +++ b/src/hook.cpp @@ -39,7 +39,7 @@ namespace MH_RemoveHook(LoadLibraryW); if (!g_custom_title_name.empty()) { - SetWindowText(GetActiveWindow(), g_custom_title_name.data()); + SetWindowText(GetActiveWindow(), local::wide_acp(local::u8_wide(g_custom_title_name)).data()); } // use original function beacuse we have unhooked that @@ -483,7 +483,49 @@ namespace } bool (*is_virt)() = nullptr; - int last_height = 0, last_width = 0; + + Il2CppObject* (*display_get_main)(); + + int (*get_system_width)(Il2CppObject* thisObj); + + int (*get_system_height)(Il2CppObject* thisObj); + + Resolution_t* (*get_resolution)(Resolution_t* buffer); + + void get_resolution_stub(Resolution_t* r) + { + *r = *get_resolution(r); + + int width = min(r->height, r->width) * g_aspect_ratio; + if (r->width > r->height) + r->width = width; + else + r->height = width; + } + + void* set_resolution_orig; + void set_resolution_hook(int width, int height, bool fullscreen) + { + Resolution_t r; + r = *get_resolution(&r); + + if (g_force_landscape && !g_auto_fullscreen) { + return reinterpret_cast(set_resolution_orig)( + height, width, false + ); + } + + bool need_fullscreen = false; + + if (is_virt() && r.width / static_cast(r.height) == (9.0 / 16.0)) + need_fullscreen = true; + else if (!is_virt() && r.width / static_cast(r.height) == (16.0 / 9.0)) + need_fullscreen = true; + + return reinterpret_cast(set_resolution_orig)( + need_fullscreen ? r.width : width, need_fullscreen ? r.height : height, need_fullscreen + ); + } void* wndproc_orig = nullptr; LRESULT wndproc_hook(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) @@ -493,42 +535,56 @@ namespace RECT* rect = reinterpret_cast(lParam); float ratio = is_virt() ? 1.f / g_aspect_ratio : g_aspect_ratio; + + RECT windowRect; + GetWindowRect(hWnd, &windowRect); + + RECT clientRect; + GetClientRect(hWnd, &clientRect); + + int borderWidth = windowRect.right - windowRect.left - (clientRect.right - clientRect.left); + int borderHeight = windowRect.bottom - windowRect.top - (clientRect.bottom - clientRect.top); + float height = rect->bottom - rect->top; float width = rect->right - rect->left; float new_ratio = width / height; - if (new_ratio > ratio && height >= last_height || width < last_width) - height = width / ratio; - else if (new_ratio < ratio && width >= last_width || height < last_height) - width = height * ratio; - - switch (wParam) - { - case WMSZ_TOP: - case WMSZ_TOPLEFT: - case WMSZ_TOPRIGHT: - rect->top = rect->bottom - height; - break; - default: - rect->bottom = rect->top + height; - break; - } + rect->right -= borderWidth; + rect->bottom -= borderHeight; switch (wParam) { case WMSZ_LEFT: - case WMSZ_TOPLEFT: - case WMSZ_BOTTOMLEFT: + case WMSZ_LEFT + WMSZ_BOTTOM: rect->left = rect->right - width; + rect->bottom = rect->top + roundf(width / ratio); break; - default: + case WMSZ_LEFT + WMSZ_TOP: + rect->left = rect->right - width; + rect->top = rect->bottom - roundf(width / ratio); + break; + case WMSZ_RIGHT: + case WMSZ_RIGHT + WMSZ_BOTTOM: + rect->right = rect->left + width; + rect->bottom = rect->top + roundf(width / ratio); + break; + case WMSZ_RIGHT + WMSZ_TOP: rect->right = rect->left + width; + rect->top = rect->bottom - roundf(width / ratio); + break; + case WMSZ_TOP: + rect->top = rect->bottom - height; + rect->right = rect->left + roundf(height * ratio); + break; + case WMSZ_BOTTOM: + rect->bottom = rect->top + height; + rect->right = rect->left + roundf(height * ratio); break; } - last_height = height; - last_width = width; + rect->right += borderWidth; + rect->bottom += borderHeight; return TRUE; } @@ -567,19 +623,6 @@ namespace return size; } - Resolution_t* (*get_resolution)(Resolution_t* buffer); - - void get_resolution_stub(Resolution_t* r) - { - *r = *get_resolution(r); - - int width = min(r->height, r->width) * g_aspect_ratio; - if (r->width > r->height) - r->width = width; - else - r->height = width; - } - void* gallop_get_screenheight_orig; int gallop_get_screenheight_hook() { @@ -809,24 +852,6 @@ namespace onComplete, overrideDuration); } - void* set_resolution_orig; - void set_resolution_hook(int width, int height, bool fullscreen) - { - Resolution_t r; - r = *get_resolution(&r); - - bool need_fullscreen = false; - - if (is_virt() && r.width / static_cast(r.height) == (9.0 / 16.0)) - need_fullscreen = true; - else if (!is_virt() && r.width / static_cast(r.height) == (16.0 / 9.0)) - need_fullscreen = true; - - return reinterpret_cast(set_resolution_orig)( - need_fullscreen ? r.width : width, need_fullscreen ? r.height : height, need_fullscreen - ); - } - void* BootSystem_Awake_orig = nullptr; void BootSystem_Awake_hook(Il2CppObject* _this) { @@ -1041,6 +1066,22 @@ namespace "StandaloneWindowResize", "getOptimizedWindowSizeHori", 2 ); + display_get_main = reinterpret_cast(il2cpp_symbols::get_method_pointer( + "UnityEngine.CoreModule.dll", + "UnityEngine", + "Display", "get_main", -1)); + + get_system_width = reinterpret_cast(il2cpp_symbols::get_method_pointer( + "UnityEngine.CoreModule.dll", + "UnityEngine", + "Display", "get_systemWidth", 0)); + + get_system_height = reinterpret_cast(il2cpp_symbols::get_method_pointer( + "UnityEngine.CoreModule.dll", + "UnityEngine", + "Display", "get_systemHeight", 0)); + is_virt = reinterpret_cast( il2cpp_symbols::get_method_pointer( "umamusume.dll", "Gallop", @@ -1246,12 +1287,7 @@ namespace } } - if (!assets) - { - cout << "Asset not loaded.\n"; - } - else - { + if (assets) { cout << "Asset loaded: " << assets << "\n"; } #pragma endregion @@ -1356,7 +1392,7 @@ namespace ADD_HOOK(canvas_scaler_setres, "UnityEngine.UI.CanvasScaler::set_referenceResolution at %p\n"); } - if (g_auto_fullscreen) + if (g_auto_fullscreen || g_force_landscape) { ADD_HOOK(set_resolution, "UnityEngine.Screen.SetResolution(int, int, bool) at %p\n"); adjust_size(); diff --git a/src/local/local.cpp b/src/local/local.cpp index 95db604..0396cf8 100644 --- a/src/local/local.cpp +++ b/src/local/local.cpp @@ -34,6 +34,30 @@ namespace local return result; } + string wide_acp(wstring str) + { + string result; + result.resize(str.length() * 4); + + int len = WideCharToMultiByte(CP_ACP, 0, str.data(), str.length(), result.data(), result.length(), nullptr, nullptr); + + result.resize(len); + + return result; + } + + wstring acp_wide(string str) + { + wstring result; + result.resize(str.length() * 4); + + int len = MultiByteToWideChar(CP_ACP, 0, str.data(), str.length(), result.data(), result.length()); + + result.resize(len); + + return result; + } + void reload_textdb(const vector* dicts) { text_db.clear(); diff --git a/src/local/local.hpp b/src/local/local.hpp index ff9548c..4c0e653 100644 --- a/src/local/local.hpp +++ b/src/local/local.hpp @@ -4,6 +4,8 @@ namespace local { std::string wide_u8(std::wstring str); std::wstring u8_wide(std::string str); + std::string wide_acp(std::wstring str); + std::wstring acp_wide(std::string str); void load_textdb(const std::vector *dicts); void reload_textdb(const std::vector* dicts); bool localify_text(size_t hash, std::string** result); diff --git a/src/main.cpp b/src/main.cpp index fac813c..bff21d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -144,7 +144,7 @@ namespace { if (file.is_regular_file()) { - g_replace_assets.emplace(file.path().filename().string(), ReplaceAsset{ file.path().string(), nullptr }); + g_replace_assets.emplace(local::wide_u8(local::acp_wide(file.path().filename().string())), ReplaceAsset{ local::wide_u8(local::acp_wide(file.path().string())), nullptr}); } } }