diff --git a/assets/svencoop/bettersprays/gameui_english.txt b/assets/svencoop/bettersprays/gameui_english.txt index 114cbd3..9569828 100644 Binary files a/assets/svencoop/bettersprays/gameui_english.txt and b/assets/svencoop/bettersprays/gameui_english.txt differ diff --git a/assets/svencoop/bettersprays/gameui_schinese.txt b/assets/svencoop/bettersprays/gameui_schinese.txt index db9da89..160a5d7 100644 Binary files a/assets/svencoop/bettersprays/gameui_schinese.txt and b/assets/svencoop/bettersprays/gameui_schinese.txt differ diff --git a/cmake/Tests.cmake b/cmake/Tests.cmake index 092150c..e421dc9 100644 --- a/cmake/Tests.cmake +++ b/cmake/Tests.cmake @@ -21,5 +21,32 @@ foreach(scenario PNG WEBP HTML) endforeach() add_test(NAME betterspray_plugin_factory COMMAND BetterSprayPluginLoadSmoke "$" "$" "$" "$") + +# Exercise the actual Windows file APIs with both process code pages, without +# changing the machine's locale. Locale-name manifests require Windows 11. +foreach(variant UTF8 CP936) + if(variant STREQUAL "UTF8") + set(test_locale UTF-8) + set(test_codepage 65001) + else() + set(test_locale zh-CN) + set(test_codepage 936) + endif() + set(test_target BetterSprayUTF8FileSystem${variant}Tests) + set(test_manifest "${PROJECT_BINARY_DIR}/tests/utf8-filesystem-${variant}.manifest") + file(GENERATE OUTPUT "${test_manifest}" CONTENT +" + ${test_locale} + +") + add_executable(${test_target} tests/utf8_filesystem_tests.cpp) + target_compile_features(${test_target} PRIVATE cxx_std_20) + target_include_directories(${test_target} PRIVATE "${METAHOOK_SOURCE_PATH}/include/SourceSDK") + target_compile_options(${test_target} PRIVATE /W4 /WX /UNDEBUG) + target_link_options(${test_target} PRIVATE /MANIFEST:EMBED "/MANIFESTINPUT:${test_manifest}") + set_target_properties(${test_target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/tests") + add_test(NAME betterspray_utf8_filesystem_${variant} COMMAND ${test_target} ${test_codepage}) + set_tests_properties(betterspray_utf8_filesystem_${variant} PROPERTIES SKIP_RETURN_CODE 77) +endforeach() get_property(betterspray_tests DIRECTORY PROPERTY TESTS) set_tests_properties(${betterspray_tests} PROPERTIES TIMEOUT 30) diff --git a/docs/en/build-instruction.md b/docs/en/build-instruction.md index dc8d9a7..484c16e 100644 --- a/docs/en/build-instruction.md +++ b/docs/en/build-instruction.md @@ -98,4 +98,8 @@ python scripts/validate-gamedata.py install/x86/Release/svencoop/metahook/gameda Tests retain assertions in Release. They cover host gamedata calls and error paths, PNG alpha/WebP round trips, UTF-8 HTML/XPath parsing and loading the DLL's two factories. +The filesystem tests create Chinese and supplementary Unicode filenames, enumerate +them, and check directory operations under process ACP 65001 and 936. Test-only +manifests select the code page without changing system settings; a case is skipped +when the OS cannot honor its requested code page (CP936 selection needs Windows 11). Compilation and simulated tests do not establish in-game spray, UI or cloud behavior. diff --git a/docs/zh-CN/build-instruction.md b/docs/zh-CN/build-instruction.md index 2904ea6..3d8830a 100644 --- a/docs/zh-CN/build-instruction.md +++ b/docs/zh-CN/build-instruction.md @@ -92,4 +92,7 @@ python scripts/validate-gamedata.py install/x86/Release/svencoop/metahook/gameda Release 测试保留断言。覆盖主机 gamedata 调用和错误路径、PNG 透明通道/WebP 编解码、 UTF-8 HTML/XPath 解析,以及 DLL 的两个工厂接口装载。 +文件系统测试在进程 ACP 65001 和 936 下创建、枚举中文及补充平面 Unicode 文件名, +并检查目录操作。代码页由测试专用 manifest 指定,不修改系统设置;系统无法采用指定 +代码页时跳过对应测试(CP936 的选择需要 Windows 11)。 编译与模拟测试不能证明游戏内喷漆、UI 或云分享行为已验证。 diff --git a/src/BetterSpraySettingsPage.cpp b/src/BetterSpraySettingsPage.cpp index 3872350..b841fd4 100644 --- a/src/BetterSpraySettingsPage.cpp +++ b/src/BetterSpraySettingsPage.cpp @@ -221,6 +221,7 @@ void CBetterSpraySettingsPage::OpenLoadSparyDialog(void) vgui::surface()->GetScreenSize(sw, sh); auto pFileDialog = new vgui::FileOpenDialog(this, "#GameUI_BetterSpray_LoadSpray", vgui::FileOpenDialogType_t::FOD_OPEN, new KeyValues("FileInfo")); + pFileDialog->AddActionSignalTarget(this); pFileDialog->SetProportional(false); pFileDialog->SetAutoDelete(true); //pFileDialog->SetStartDirectory("svencoop_downloads/"); @@ -264,7 +265,7 @@ void CBetterSpraySettingsPage::OnDataChanged() void CBetterSpraySettingsPage::OnFileSelected(const char* fullpath) { wchar_t wszFullPath[MAX_PATH]{}; - Q_UTF8ToUnicode(fullpath, wszFullPath, MAX_PATH); + Q_UTF8ToUnicode(fullpath, wszFullPath, sizeof(wszFullPath)); auto fiFormat = FreeImage_GetFileTypeU(wszFullPath); diff --git a/tests/utf8_filesystem_tests.cpp b/tests/utf8_filesystem_tests.cpp new file mode 100644 index 0000000..790a6ab --- /dev/null +++ b/tests/utf8_filesystem_tests.cpp @@ -0,0 +1,109 @@ +#include + +#include +#include +#include +#include + +static void TestConversions() +{ + const char* name = "\xE4\xB8\xAD\xE6\x96\x87 \xF0\x9F\x8E\xA8.png"; + const wchar_t* wide = L"\u4E2D\u6587 \U0001F3A8.png"; + assert(std::wstring(wide) == filesystem_utf8::ToWide(name)); + assert(std::string(name) == filesystem_utf8::FromWide(wide)); + assert(std::wstring(L"plain.png") == filesystem_utf8::ToWide("plain.png")); + assert(std::string("plain.png") == filesystem_utf8::FromWide(L"plain.png")); + assert(true == filesystem_utf8::ToWide("\xFF").empty()); + assert(true == filesystem_utf8::ToWide(nullptr).empty()); + assert(true == filesystem_utf8::FromWide(nullptr).empty()); +} + +static void TestFiles() +{ + wchar_t tempPath[MAX_PATH]{}; + assert(0 != GetTempPathW(MAX_PATH, tempPath)); + wchar_t tempFile[MAX_PATH]{}; + assert(0 != GetTempFileNameW(tempPath, L"bsu", 0, tempFile)); + assert(TRUE == DeleteFileW(tempFile)); + assert(TRUE == CreateDirectoryW(tempFile, nullptr)); + + const std::wstring root = tempFile; + const std::wstring directory = root + L"\\\u4E2D\u6587\u6587\u4EF6\u5939"; + const std::string directoryUTF8 = filesystem_utf8::FromWide(directory.c_str()); + assert(true == filesystem_utf8::CreateFolder(directoryUTF8.c_str())); + const auto nestedParent = directory + L"\\\u591A\u7EA7"; + const auto nested = nestedParent + L"\\\u5B50\u76EE\u5F55"; + const auto nestedUTF8 = filesystem_utf8::FromWide(nested.c_str()); + assert(true == filesystem_utf8::CreateFolder(nestedUTF8.c_str())); + assert(true == filesystem_utf8::CreateFolder(nestedUTF8.c_str())); + assert(false == filesystem_utf8::CreateFolder("\xFF")); + // This filename fits Win32's UTF-16 limit while exceeding MAX_PATH UTF-8 bytes. + constexpr size_t longNameLength = 100; + const std::wstring names[] = {L"plain.png", L"\u55B7\u6F06.png", L"\U0001F3A8.png", std::wstring(longNameLength, L'\u6D4B') + L".png"}; + for (const auto& name : names) + { + const auto file = directory + L"\\" + name; + HANDLE handle = CreateFileW(file.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); + assert(INVALID_HANDLE_VALUE != handle); + assert(TRUE == CloseHandle(handle)); + const auto fileUTF8 = filesystem_utf8::FromWide(file.c_str()); + const DWORD fileAttributes = filesystem_utf8::GetAttributes(fileUTF8.c_str()); + assert(INVALID_FILE_ATTRIBUTES != fileAttributes); + assert(0 == (fileAttributes & FILE_ATTRIBUTE_DIRECTORY)); + assert(false == filesystem_utf8::CreateFolder(fileUTF8.c_str())); + } + + const DWORD attributes = filesystem_utf8::GetAttributes(directoryUTF8.c_str()); + assert(INVALID_FILE_ATTRIBUTES != attributes); + assert(FILE_ATTRIBUTE_DIRECTORY == (attributes & FILE_ATTRIBUTE_DIRECTORY)); + assert(INVALID_FILE_ATTRIBUTES == filesystem_utf8::GetAttributes((directoryUTF8 + "\\missing.png").c_str())); + assert(INVALID_FILE_ATTRIBUTES == filesystem_utf8::GetAttributes("\xFF")); + + WIN32_FIND_DATAW data{}; + HANDLE find = filesystem_utf8::FindFirst((directoryUTF8 + "\\*.png").c_str(), &data); + assert(INVALID_HANDLE_VALUE != find); + bool found[std::size(names)]{}; + do + { + const auto nameUTF8 = filesystem_utf8::FromWide(data.cFileName); + const auto name = filesystem_utf8::ToWide(nameUTF8.c_str()); + for (size_t i = 0; i < std::size(names); ++i) + if (names[i] == name) + found[i] = true; + } while (FindNextFileW(find, &data)); + assert(ERROR_NO_MORE_FILES == GetLastError()); + assert(TRUE == FindClose(find)); + for (bool value : found) + assert(true == value); + + find = filesystem_utf8::FindFirst((directoryUTF8 + "\\*.jpg").c_str(), &data); + assert(INVALID_HANDLE_VALUE == find); + assert(INVALID_HANDLE_VALUE == filesystem_utf8::FindFirst("\xFF", &data)); + + const auto previous = filesystem_utf8::ToWide(filesystem_utf8::GetWorkingDirectory().c_str()); + assert(TRUE == SetCurrentDirectoryW(directory.c_str())); + assert(directoryUTF8 == filesystem_utf8::GetWorkingDirectory()); + assert(TRUE == SetCurrentDirectoryW(previous.c_str())); + + for (const auto& name : names) + assert(TRUE == DeleteFileW((directory + L"\\" + name).c_str())); + assert(TRUE == RemoveDirectoryW(nested.c_str())); + assert(TRUE == RemoveDirectoryW(nestedParent.c_str())); + assert(TRUE == RemoveDirectoryW(directory.c_str())); + assert(TRUE == RemoveDirectoryW(root.c_str())); +} + +int main(int argc, char** argv) +{ + const auto codePage = GetACP(); + std::printf("Process ACP: %u\n", codePage); + std::fflush(stdout); + if (argc == 2 && static_cast(std::stoul(argv[1])) != codePage) + { + std::puts("Requested manifest code page is unavailable on this Windows version; skipping."); + return 77; + } + TestConversions(); + TestFiles(); + std::puts("UTF-8 filenames, directory enumeration, attributes and working directory passed."); +}