From 3d8e6a8aadcb0bdf5798a2f3b187889ca99db72d Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 28 Nov 2026 22:39:09 +0000 Subject: [PATCH 1/2] fix texmods that loaded no entries utf8_to_wstring converted with a length of -1, so every modlist path kept a trailing NUL. The .tpf check then failed and each TPF was opened as a plain zip, which failed silently. Pass the real length. ModfileLoader now skips an entry whose data can't be read, rather than building a vector from a null pointer. --- header/Utils.h | 7 ++++--- modules/ModfileLoader.ixx | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/header/Utils.h b/header/Utils.h index 549599a..c489dc1 100644 --- a/header/Utils.h +++ b/header/Utils.h @@ -51,12 +51,13 @@ namespace utils { { if (utf8str.empty()) return std::wstring{}; - // Calculate the number of wide characters needed for the conversion - const int count = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1, nullptr, 0); + // Length is passed explicitly: with -1 the terminating NUL is converted too, and the path then no longer ends in ".tpf". + const int length = static_cast(utf8str.size()); + const int count = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), length, nullptr, 0); if (count == 0) return std::nullopt; std::wstring wstr(count, 0); - MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1, wstr.data(), count); + MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), length, wstr.data(), count); return wstr; } diff --git a/modules/ModfileLoader.ixx b/modules/ModfileLoader.ixx index c3c8bb8..a102b4e 100644 --- a/modules/ModfileLoader.ixx +++ b/modules/ModfileLoader.ixx @@ -189,6 +189,8 @@ void ParseTexmodArchive(std::vector& lines, libzippp::ZipArchive& a continue; const auto data_ptr = static_cast(entry.readAsBinary()); + if (!data_ptr) + continue; const auto size = static_cast(entry.getSize()); std::vector vec(data_ptr, data_ptr + size); const auto tex_name = std::filesystem::path(entry.getName()); From 63a29a1b925ce53fd87914279d884270e2760122 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 28 Nov 2026 22:39:09 +0000 Subject: [PATCH 2/2] build into bin/, log debug output to gMod.log, bump to 1.10.4 CMAKE_RUNTIME_OUTPUT_DIRECTORY is bin/$, as in GWToolbox, and the pipeline reads bin/Release. Debug builds define _DEBUG so the Message, Info and Warning output is compiled in; it is written to gMod.log beside the DLL, flushed every second when there is new output. The Debug-only FreeConsole call no longer uses MSVC SEH, which clang does not support. Ignore *.slnx at any depth. --- .github/workflows/cd.yaml | 8 ++++---- .github/workflows/ci.yaml | 8 ++++---- .gitignore | 2 +- CLAUDE.md | 2 +- CMakeLists.txt | 5 +++-- README.md | 2 +- header/Defines.h | 26 +++++++++++++++++++++++--- scripts/build.py | 4 ++-- source/dll_main.cpp | 34 +++++++++++++++++++++++++++++----- 9 files changed, 68 insertions(+), 23 deletions(-) diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index 44043d0..df9b08f 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -36,11 +36,11 @@ jobs: run: echo "version=$(python3 scripts/version.py)" >> "$GITHUB_OUTPUT" # Same as GWToolbox: keep the PDB and the split DWARF file, gzipped, next to the binaries. gzip -k keeps the - # originals in bin/ for later steps. + # originals in bin/Release/ for later steps. - name: Compress the debug info run: | - gzip -kf bin/gMod.pdb - gzip -kf bin/gMod.debug + gzip -kf bin/Release/gMod.pdb + gzip -kf bin/Release/gMod.debug # TpfConvert.exe and gMod.dll link to d3dx9_43.dll only at run time. It is a Microsoft redistributable that # the MinGW build does not produce, so carry the copy from the latest release forward unchanged. @@ -50,7 +50,7 @@ jobs: run: | mkdir -p release gh release download --repo "$GITHUB_REPOSITORY" --pattern d3dx9_43.dll --dir release --clobber - cp bin/gMod.dll bin/TpfConvert.exe bin/gMod.pdb.gz bin/gMod.debug.gz release/ + cp bin/Release/gMod.dll bin/Release/TpfConvert.exe bin/Release/gMod.pdb.gz bin/Release/gMod.debug.gz release/ - name: Upload unsigned binaries uses: actions/upload-artifact@v4 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4af3ed5..268e88c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -30,9 +30,9 @@ jobs: with: name: gMod-linux-release path: | - bin/gMod.dll - bin/gMod.pdb - bin/TpfConvert.exe - bin/TpfConvert.pdb + bin/Release/gMod.dll + bin/Release/gMod.pdb + bin/Release/TpfConvert.exe + bin/Release/TpfConvert.pdb if-no-files-found: error diff --git a/.gitignore b/.gitignore index 98f32a2..8c12119 100644 --- a/.gitignore +++ b/.gitignore @@ -74,6 +74,6 @@ __pycache__/ # IDE outputs generated by scripts/bootstrap.py (the settings themselves live in bootstrap/) /.vs-projects -/gMod.slnx +*.slnx /obj .vscode diff --git a/CLAUDE.md b/CLAUDE.md index c6d60a6..5b3a8a0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -26,7 +26,7 @@ python3 scripts/build.py # relwithdebinfo preset; --preset debug|release - Standard: C++23, with `import std`. The experimental flag's UUID in `CMakeLists.txt` belongs to one CMake version, so CMake is pinned in `scripts/cmake.version` (4.4.4) and the bootstrap installs that exact version. Change the pin and the UUID together. -- Output: `bin/` (gMod.dll, TpfConvert.exe, each with a .pdb). +- Output: `bin//` (gMod.dll, TpfConvert.exe, each with a .pdb), e.g. `bin/Debug/gMod.dll`. - Version: set in `CMakeLists.txt` (`VERSION_MAJOR/MINOR/PATCH/TWEAK`). - The Windows CI and release workflows (`.github/workflows/`) still use the old MSVC build; see the note in Notes. diff --git a/CMakeLists.txt b/CMakeLists.txt index 18568fc..735d709 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,13 +13,13 @@ endif() set(VERSION_MAJOR 1) set(VERSION_MINOR 10) -set(VERSION_PATCH 3) +set(VERSION_PATCH 4) set(VERSION_TWEAK 0) set(VERSION_RC "${CMAKE_CURRENT_BINARY_DIR}/version.rc") configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in" "${VERSION_RC}" @ONLY) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin/$") set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -95,6 +95,7 @@ target_compile_definitions(gMod PRIVATE "VC_EXTRALEAN" DIRECT_INJECTION LOG_MESSAGE + $<$:_DEBUG> ) add_subdirectory(TpfConvert) diff --git a/README.md b/README.md index 05b9285..52f2f02 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Requirements: git, Python 3.9+ and Ninja. The bootstrap installs the pinned CMak Compile: - `python3 scripts/build.py` (on Windows `python scripts\build.py`), or `--preset debug` / `--preset release` -- The DLL and TpfConvert.exe land in `bin/` +- The DLL and TpfConvert.exe land in `bin//` (for example `bin/Debug/`) **TpfConvert** Small utility to convert old .tpf files with invalid images into .zip files with working images. diff --git a/header/Defines.h b/header/Defines.h index 7d566ec..8927c58 100644 --- a/header/Defines.h +++ b/header/Defines.h @@ -34,6 +34,26 @@ struct TextureFileStruct { HashType crc_hash = 0; // hash value }; +// Debug output goes to the console and to gMod.log beside the DLL; dll_main.cpp opens the file and flushes it every second. +inline FILE* gmod_log_file = nullptr; +inline std::mutex gmod_log_mutex; +inline bool gmod_log_dirty = false; + +inline void GModLog([[maybe_unused]] FILE* console, [[maybe_unused]] const char* format, [[maybe_unused]] va_list args) +{ +#ifdef _DEBUG + va_list console_args; + va_copy(console_args, args); + vfprintf(console, format, console_args); + va_end(console_args); + std::lock_guard lock(gmod_log_mutex); + if (gmod_log_file) { + vfprintf(gmod_log_file, format, args); + gmod_log_dirty = true; + } +#endif +} + inline void Message([[maybe_unused]] const char* format, ...) { #ifdef _DEBUG @@ -41,7 +61,7 @@ inline void Message([[maybe_unused]] const char* format, ...) //[[maybe_unused]] const auto success = SetConsoleTextAttribute(hConsole, 0); // white va_list args; va_start(args, format); - vprintf(format, args); + GModLog(stdout, format, args); va_end(args); #endif } @@ -53,7 +73,7 @@ inline void Info([[maybe_unused]] const char* format, ...) //[[maybe_unused]] const auto success = SetConsoleTextAttribute(hConsole, 0); // white va_list args; va_start(args, format); - vprintf(format, args); + GModLog(stdout, format, args); va_end(args); #endif } @@ -65,7 +85,7 @@ inline void Warning([[maybe_unused]] const char* format, ...) [[maybe_unused]] const auto success = SetConsoleTextAttribute(hConsole, 6); // yellow va_list args; va_start(args, format); - vfprintf(stderr, format, args); + GModLog(stderr, format, args); va_end(args); #endif } diff --git a/scripts/build.py b/scripts/build.py index 9ed2fd8..5674311 100644 --- a/scripts/build.py +++ b/scripts/build.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 """Builds gMod.dll and TpfConvert with llvm-mingw into build///, configuring the preset first when it -has not been configured yet. Output lands in bin/ (the presets point CMAKE_RUNTIME_OUTPUT_DIRECTORY there). +has not been configured yet. Output lands in bin// (CMakeLists.txt sets CMAKE_RUNTIME_OUTPUT_DIRECTORY to bin/$). python3 scripts/build.py [--preset relwithdebinfo|debug|release] [--target gMod|TpfConvert] [--clean | --rebuild] @@ -55,7 +55,7 @@ def main(): for target in targets: command += ["--target", target] run(log, command, "cmake build") - log.line("built into bin/") + log.line("built into bin//") finally: lock.release() log.close() diff --git a/source/dll_main.cpp b/source/dll_main.cpp index 3027314..827c1d3 100644 --- a/source/dll_main.cpp +++ b/source/dll_main.cpp @@ -179,6 +179,23 @@ HRESULT APIENTRY Direct3DCreate9Ex(UINT SDKVersion, IDirect3D9Ex** ppD3D) return ret; } +#ifdef _DEBUG +// Runs until the log is closed on detach, so a crash keeps at most the last second of output. +DWORD WINAPI FlushLogEvery5Seconds(LPVOID) +{ + for (;;) { + Sleep(1000); + std::lock_guard lock(gmod_log_mutex); + if (!gmod_log_file) + return 0; + if (gmod_log_dirty) { + fflush(gmod_log_file); + gmod_log_dirty = false; + } + } +} +#endif + BOOL WINAPI DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { @@ -191,6 +208,11 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserv SetConsoleTitleA("gMod Console"); freopen_s(&stdout_proxy, "CONOUT$", "w", stdout); freopen_s(&stderr_proxy, "CONOUT$", "w", stderr); + { + std::lock_guard lock(gmod_log_mutex); + gmod_log_file = _wfopen((gmod_dll_path.parent_path() / L"gMod.log").c_str(), L"w"); + } + CreateThread(nullptr, 0, FlushLogEvery5Seconds, nullptr, 0, nullptr); #endif InitInstance(hModule); break; @@ -331,14 +353,16 @@ void ExitInstance(bool is_unloading) FreeLibrary(gMod_Loaded_d3d9_Module_Handle); #ifdef _DEBUG + { + std::lock_guard lock(gmod_log_mutex); + if (gmod_log_file) + fclose(gmod_log_file); + gmod_log_file = nullptr; + } if (stdout_proxy) fclose(stdout_proxy); if (stderr_proxy) fclose(stderr_proxy); - __try { - FreeConsole(); - } - __except (EXCEPTION_CONTINUE_EXECUTION) { - } + FreeConsole(); #endif }