Remove unneeded code

This commit is contained in:
2025-03-04 15:54:47 +08:00
parent 58c2d98686
commit 05c99f36b2
2 changed files with 8 additions and 132 deletions

View File

@@ -2,12 +2,10 @@
#include "config.hpp"
#include "detours.h"
#include <stdio.h>
#include <shlwapi.h>
#include "wchar_util.h"
#include "vfs.hpp"
#include "str_util.h"
#include "fileop.h"
#include <algorithm>
#include <fcntl.h>
#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<WIN32_FIND_DATAW> 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<WIN32_FIND_DATAW> 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<WIN32_FIND_DATAW> virtual_entries;
for (const auto& entry : vfs.files) {
std::string file_path = entry.first;
std::vector<std::string> 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<DWORD>(entry.second & 0xFFFFFFFF);
vdata.nFileSizeHigh = static_cast<DWORD>(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<WIN32_FIND_DATAW> 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<WIN32_FIND_DATAW*>(lpFindFileData) = all_entries[1];
ctx->current_index = 2;
}
return reinterpret_cast<HANDLE>(ctx);
}
BOOL WINAPI HookedFindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData) {
CustomFindContext* ctx = reinterpret_cast<CustomFindContext*>(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<CustomFindContext*>(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();
}