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
8 changes: 4 additions & 4 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<Config>/` (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.

Expand Down
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/$<CONFIG>")
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand Down Expand Up @@ -95,6 +95,7 @@ target_compile_definitions(gMod PRIVATE
"VC_EXTRALEAN"
DIRECT_INJECTION
LOG_MESSAGE
$<$<CONFIG:Debug>:_DEBUG>
)

add_subdirectory(TpfConvert)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<Config>/` (for example `bin/Debug/`)

**TpfConvert**
Small utility to convert old .tpf files with invalid images into .zip files with working images.
Expand Down
26 changes: 23 additions & 3 deletions header/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,34 @@ 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
// const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//[[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
}
Expand All @@ -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
}
Expand All @@ -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
}
7 changes: 4 additions & 3 deletions header/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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;
}
Expand Down
2 changes: 2 additions & 0 deletions modules/ModfileLoader.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ void ParseTexmodArchive(std::vector<std::string>& lines, libzippp::ZipArchive& a
continue;

const auto data_ptr = static_cast<BYTE*>(entry.readAsBinary());
if (!data_ptr)
continue;
const auto size = static_cast<size_t>(entry.getSize());
std::vector vec(data_ptr, data_ptr + size);
const auto tex_name = std::filesystem::path(entry.getName());
Expand Down
4 changes: 2 additions & 2 deletions scripts/build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
"""Builds gMod.dll and TpfConvert with llvm-mingw into build/<host>/<preset>/, 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/<Config>/ (CMakeLists.txt sets CMAKE_RUNTIME_OUTPUT_DIRECTORY to bin/$<CONFIG>).

python3 scripts/build.py [--preset relwithdebinfo|debug|release] [--target gMod|TpfConvert]
[--clean | --rebuild]
Expand Down Expand Up @@ -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/<Config>/")
finally:
lock.release()
log.close()
Expand Down
34 changes: 29 additions & 5 deletions source/dll_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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
}
Loading