From 05c99f36b2eb408c78a94d54836d2696e6b93eb5 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 4 Mar 2025 15:54:47 +0800 Subject: [PATCH] Remove unneeded code --- CMakeLists.txt | 1 - dllmain.cpp | 139 +++---------------------------------------------- 2 files changed, 8 insertions(+), 132 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2713cb..34aaa99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,5 @@ add_library(jewena_patch SHARED dllmain.cpp config.hpp config.cpp vfs.hpp vfs.cp target_link_libraries(jewena_patch "${DETOURS_LIB}") target_link_libraries(jewena_patch utils) target_link_libraries(jewena_patch zip) -target_link_libraries(jewena_patch shlwapi.lib) add_executable(jewena-chs WIN32 main.cpp winres.rc jewena-chs.exe.manifest) diff --git a/dllmain.cpp b/dllmain.cpp index f4e4f1b..052b739 100644 --- a/dllmain.cpp +++ b/dllmain.cpp @@ -2,12 +2,10 @@ #include "config.hpp" #include "detours.h" #include -#include #include "wchar_util.h" #include "vfs.hpp" #include "str_util.h" #include "fileop.h" -#include #include #include "string_replace_file.hpp" @@ -19,133 +17,12 @@ static BOOL(WINAPI *TrueCloseHandle)(HANDLE hObject) = CloseHandle; static DWORD(WINAPI *TrueGetFileSize)(HANDLE hFile, LPDWORD lpFileSizeHigh) = GetFileSize; static decltype(GetFileSizeEx) *TrueGetFileSizeEx = GetFileSizeEx; static DWORD(WINAPI *TrueSetFilePointer)(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod) = SetFilePointer; -static decltype(FindFirstFileExW)* OriginalFindFirstFileExW = FindFirstFileExW; -static decltype(FindNextFileW)* OriginalFindNextFileW = FindNextFileW; -static decltype(FindClose)* OriginalFindClose = FindClose; static Config config; static std::wstring defaultFont; static VFS vfs; static StringReplaceFile replaceFile; -struct CustomFindContext { - std::vector entries; - size_t current_index; -}; - -HANDLE WINAPI HookedFindFirstFileExW( - LPCWSTR lpFileName, - FINDEX_INFO_LEVELS fInfoLevelId, - LPVOID lpFindFileData, - FINDEX_SEARCH_OPS fSearchOp, - LPVOID lpSearchFilter, - DWORD dwAdditionalFlags -) { - std::wstring filename_w(lpFileName); - std::string filename_utf8; - wchar_util::wstr_to_str(filename_utf8, filename_w, CP_UTF8); - - // 解析目录和模式 - std::string dir_part = fileop::dirname(filename_utf8); - std::string pattern = fileop::basename(filename_utf8); - - // 标准化路径,判断是否为目标目录 - std::string abs_dir = fileop::isabs(dir_part) ? dir_part : fileop::join(vfs.GetBasePath(), dir_part); - std::string base = fileop::join(vfs.GetBasePath(), ""); // 确保base_path以/结尾 - - if (abs_dir != vfs.GetBasePath()) { - // 非目标目录,调用原始函数 - return OriginalFindFirstFileExW(lpFileName, fInfoLevelId, lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags); - } - - // 收集实际文件系统的条目 - HANDLE hFindReal = OriginalFindFirstFileExW(lpFileName, fInfoLevelId, lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags); - std::vector real_entries; - WIN32_FIND_DATAW data; - if (hFindReal != INVALID_HANDLE_VALUE) { - while (OriginalFindNextFileW(hFindReal, &data)) { - real_entries.push_back(data); - } - OriginalFindClose(hFindReal); - } - - // 转换模式为宽字符 - std::wstring wpattern; - wchar_util::str_to_wstr(wpattern, pattern, CP_UTF8); - - // 收集虚拟条目 - std::vector virtual_entries; - for (const auto& entry : vfs.files) { - std::string file_path = entry.first; - std::vector components = str_util::str_splitv(file_path, "\\"); - if (components.empty()) continue; - - if (components.size() == 1) { - // 文件条目 - std::wstring wname; - wchar_util::str_to_wstr(wname, components[0], CP_UTF8); - if (PathMatchSpecW(wname.c_str(), wpattern.c_str())) { - WIN32_FIND_DATAW vdata = {0}; - wcsncpy(vdata.cFileName, wname.c_str(), MAX_PATH); - vdata.dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE; - vdata.nFileSizeLow = static_cast(entry.second & 0xFFFFFFFF); - vdata.nFileSizeHigh = static_cast(entry.second >> 32); - virtual_entries.push_back(vdata); - } - } else { - // 目录条目 - std::wstring wdir; - wchar_util::str_to_wstr(wdir, components[0], CP_UTF8); - if (PathMatchSpecW(wdir.c_str(), wpattern.c_str())) { - bool dir_exists = std::any_of(real_entries.begin(), real_entries.end(), - [&](const WIN32_FIND_DATAW& d) { - return (d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && - _wcsicmp(d.cFileName, wdir.c_str()) == 0; - }); - if (!dir_exists) { - WIN32_FIND_DATAW vdata = {0}; - wcsncpy(vdata.cFileName, wdir.c_str(), MAX_PATH); - vdata.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY; - virtual_entries.push_back(vdata); - } - } - } - } - - // 合并条目 - std::vector all_entries; - all_entries.reserve(real_entries.size() + virtual_entries.size()); - all_entries.insert(all_entries.end(), real_entries.begin(), real_entries.end()); - all_entries.insert(all_entries.end(), virtual_entries.begin(), virtual_entries.end()); - - // 创建上下文 - CustomFindContext* ctx = new CustomFindContext{all_entries, 0}; - if (!all_entries.empty()) { - *static_cast(lpFindFileData) = all_entries[1]; - ctx->current_index = 2; - } - return reinterpret_cast(ctx); -} - -BOOL WINAPI HookedFindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData) { - CustomFindContext* ctx = reinterpret_cast(hFindFile); - if (ctx && ctx->current_index < ctx->entries.size()) { - *lpFindFileData = ctx->entries[ctx->current_index++]; - return TRUE; - } - SetLastError(ERROR_NO_MORE_FILES); - return FALSE; -} - -BOOL WINAPI HookedFindClose(HANDLE hFindFile) { - CustomFindContext* ctx = reinterpret_cast(hFindFile); - if (ctx) { - delete ctx; - return TRUE; - } - return OriginalFindClose(hFindFile); -} - char* to_utf8(char* target, const char* source, UINT cp) { int count = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, source, -1, NULL, 0); if (!count) return nullptr; @@ -174,8 +51,14 @@ char* WINAPI jis_to_utf8(char* target, const char* source) { auto re = replaceFile.messages.find(str); if (re != replaceFile.messages.end()) { str = (*re).second; - strcpy(target, str.c_str()); - result = target; + if (target) { + strcpy(target, str.c_str()); + result = target; + } else { + delete[] result; + result = new char[str.size() + 1]; + strcpy(result, str.c_str()); + } } } return result; @@ -285,9 +168,6 @@ extern "C" __declspec(dllexport) void Attach() { DetourAttach(&TrueGetFileSize, HookedGetFileSize); DetourAttach(&TrueGetFileSizeEx, HookedGetFileSizeEx); DetourAttach(&TrueSetFilePointer, HookedSetFilePointer); - // DetourAttach(&OriginalFindFirstFileExW, HookedFindFirstFileExW); - // DetourAttach(&OriginalFindNextFileW, HookedFindNextFileW); - // DetourAttach(&OriginalFindClose, HookedFindClose); DetourTransactionCommit(); std::string stringReplaceFile = config.configs["stringReplaceFile"]; if (!stringReplaceFile.empty()) { @@ -314,9 +194,6 @@ extern "C" __declspec(dllexport) void Detach() { DetourDetach(&TrueGetFileSize, HookedGetFileSize); DetourDetach(&TrueGetFileSizeEx, HookedGetFileSizeEx); DetourDetach(&TrueSetFilePointer, HookedSetFilePointer); - // DetourDetach(&OriginalFindFirstFileExW, HookedFindFirstFileExW); - // DetourDetach(&OriginalFindNextFileW, HookedFindNextFileW); - // DetourDetach(&OriginalFindClose, HookedFindClose); DetourTransactionCommit(); }