Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/svencoop/bettersprays/gameui_english.txt
Binary file not shown.
Binary file modified assets/svencoop/bettersprays/gameui_schinese.txt
Binary file not shown.
27 changes: 27 additions & 0 deletions cmake/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,32 @@ foreach(scenario PNG WEBP HTML)
endforeach()
add_test(NAME betterspray_plugin_factory COMMAND BetterSprayPluginLoadSmoke "$<TARGET_FILE:BetterSpray>"
"$<TARGET_FILE:FreeImage>" "$<TARGET_FILE:LibXml2>" "$<TARGET_FILE:SteamAPIBridge>")

# 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
"<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\" xmlns:asmv3=\"urn:schemas-microsoft-com:asm.v3\">
<asmv3:application><asmv3:windowsSettings xmlns=\"http://schemas.microsoft.com/SMI/2019/WindowsSettings\"><activeCodePage>${test_locale}</activeCodePage></asmv3:windowsSettings></asmv3:application>
</assembly>
")
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)
4 changes: 4 additions & 0 deletions docs/en/build-instruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 3 additions & 0 deletions docs/zh-CN/build-instruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 或云分享行为已验证。
3 changes: 2 additions & 1 deletion src/BetterSpraySettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
Expand Down Expand Up @@ -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);

Expand Down
109 changes: 109 additions & 0 deletions tests/utf8_filesystem_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <utf8filesystem.h>

#include <cassert>
#include <cstdio>
#include <iterator>
#include <string>

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<UINT>(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.");
}
Loading