更新VFS类以支持大小写不敏感的文件查找,修复文件未找到时的错误处理

This commit is contained in:
2025-03-03 20:34:05 +08:00
parent e6a24c54a6
commit 446f41deac
3 changed files with 35 additions and 7 deletions

View File

@@ -262,9 +262,9 @@ extern "C" __declspec(dllexport) void Attach() {
DetourAttach(&TrueCloseHandle, HookedCloseHandle);
DetourAttach(&TrueGetFileSize, HookedGetFileSize);
DetourAttach(&TrueSetFilePointer, HookedSetFilePointer);
DetourAttach(&OriginalFindFirstFileExW, HookedFindFirstFileExW);
DetourAttach(&OriginalFindNextFileW, HookedFindNextFileW);
DetourAttach(&OriginalFindClose, HookedFindClose);
// DetourAttach(&OriginalFindFirstFileExW, HookedFindFirstFileExW);
// DetourAttach(&OriginalFindNextFileW, HookedFindNextFileW);
// DetourAttach(&OriginalFindClose, HookedFindClose);
DetourTransactionCommit();
#if _DEBUG
while( !::IsDebuggerPresent() )
@@ -284,9 +284,9 @@ extern "C" __declspec(dllexport) void Detach() {
DetourDetach(&TrueCloseHandle, HookedCloseHandle);
DetourDetach(&TrueGetFileSize, HookedGetFileSize);
DetourDetach(&TrueSetFilePointer, HookedSetFilePointer);
DetourDetach(&OriginalFindFirstFileExW, HookedFindFirstFileExW);
DetourDetach(&OriginalFindNextFileW, HookedFindNextFileW);
DetourDetach(&OriginalFindClose, HookedFindClose);
// DetourDetach(&OriginalFindFirstFileExW, HookedFindFirstFileExW);
// DetourDetach(&OriginalFindNextFileW, HookedFindNextFileW);
// DetourDetach(&OriginalFindClose, HookedFindClose);
DetourTransactionCommit();
}

View File

@@ -75,6 +75,12 @@ HANDLE VFS::CreateFileW(std::wstring path) {
}
str = fileop::relpath(str, base_path);
str = str_util::str_replace(str, "\\", "/");
auto c = files.find(str);
if (c == files.end()) {
SetLastError(ERROR_FILE_NOT_FOUND);
return INVALID_HANDLE_VALUE;
}
str = (*c).first;
zip_t* archive = nullptr;
zip_uint64_t index = 0;
for (auto a : archives) {

24
vfs.hpp
View File

@@ -3,6 +3,28 @@
#include <string>
#include <unordered_map>
#include <Windows.h>
#include "str_util.h"
struct CaseInsensitiveHash {
size_t operator()(const std::string& str) const {
// 创建字符串的小写副本
std::string lowercaseStr = str_util::tolower(str);
// 对小写字符串使用标准哈希函数
return std::hash<std::string>{}(lowercaseStr);
}
};
// 比较函数,忽略大小写
struct CaseInsensitiveEqual {
bool operator()(const std::string& left, const std::string& right) const {
return left.size() == right.size() &&
std::equal(left.begin(), left.end(), right.begin(),
[](unsigned char a, unsigned char b) {
return std::tolower(a) == std::tolower(b);
});
}
};
class VFS {
public:
@@ -17,7 +39,7 @@ class VFS {
void CloseHandle(HANDLE hFile);
DWORD GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh);
DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod);
std::unordered_map<std::string, zip_uint64_t> files;
std::unordered_map<std::string, zip_uint64_t, CaseInsensitiveHash, CaseInsensitiveEqual> files;
std::string GetBasePath();
private:
std::string base_path;