Skip to content

feat(sagepatch): casual QoL patch for macOS + Linux (Phase 1)#110

Draft
ebellumat wants to merge 9 commits intofbraz3:mainfrom
ebellumat:feature/sagepatch-qol
Draft

feat(sagepatch): casual QoL patch for macOS + Linux (Phase 1)#110
ebellumat wants to merge 9 commits intofbraz3:mainfrom
ebellumat:feature/sagepatch-qol

Conversation

@ebellumat
Copy link
Copy Markdown

@ebellumat ebellumat commented Apr 25, 2026

Summary

Introduces SagePatch — an optional, drop-in patch for GeneralsX that adds quality-of-life features for casual play, gated by the new CMake flag RTS_BUILD_OPTION_SAGE_PATCH (default OFF, ON in the macos-vulkan preset).

Inspired by the casual subset of the original GenTool, but independently implemented from the public GenTool changelog and SDL3/CoreGraphics/X11 documentation. No code reverse-engineered from the original closed-source d3d8.dll (which is VMProtect-protected anyway). No anti-cheat, no networking, no replay tools, no engine bug fixes — see the doc for the explicit non-goals.

The whole thing is outside the game source tree in Patches/SagePatch/. The only files touched in core are CMakeLists.txt, CMakePresets.json, and cmake/config-build.cmake to wire the option.

What's in

Feature Trigger macOS Linux Windows
Screenshot F11 screencapture -l <id> import (ImageMagick) / gnome-screenshot stub
Cursor lock toggle Scroll Lock SDL3 SDL3 stub
Brightness up / down Ctrl + PageUp / Ctrl + PageDown CoreGraphics gamma XF86VidMode (X11) stub
Window snap (5 positions) Ctrl + 1..5 SDL3 SDL3 stub
Camera height range passive INI override INI override INI override
Camera pitch passive INI override INI override INI override
Keyboard scroll speed passive INI override INI override INI override
FPS counter passive DXVK_HUD=fps default in run.sh same n/a

Architecture

Game process (GeneralsXZH)
    │
    ├── DYLD_INSERT_LIBRARIES (macOS) / LD_PRELOAD (Linux) → libsage_patch.{dylib,so}
    │       └── SDL_PollEvent gets replaced (interpose table on macOS,
    │           symbol override + dlsym RTLD_NEXT on Linux)
    │              └── F11 / Scroll Lock / Ctrl+PgUp/Dn / Ctrl+1..5 → SagePatch handlers
    │                      └── Per-platform: screencapture / ImageMagick,
    │                          CoreGraphics gamma / XF86VidMode, SDL_SetWindowPosition
    │
    └── Engine reads Data/INI/Default/GameData/SagePatch.ini → camera/scroll overrides

No D3D8 proxy, no Vulkan layer, no engine source modifications. Phase 1 patch source is ~600 lines C++ across common/, macos/, linux/, windows/ (stubs).

Layout

Patches/SagePatch/
    CMakeLists.txt
    include/SagePatch/{Hooks.h, Features.h, Logger.h}
    src/
      common/                          # SDL3 — every platform
        Init.cpp / KeyHandler.cpp / CursorLock.cpp / WindowPosition.cpp
      macos/                           # __DATA,__interpose, screencapture, CoreGraphics
        interposers_macos.cpp / Screenshot_macos.cpp / Brightness_macos.cpp
      linux/                           # LD_PRELOAD, ImageMagick, XF86VidMode (lazy dlopen)
        interposers_linux.cpp / Screenshot_linux.cpp / Brightness_linux.cpp
      windows/                         # stubs (Phase 2)
        Stubs_windows.cpp
    resources/Override.ini             # engine INI override
docs/PATCHES/SAGEPATCH.md              # user/dev documentation

What's intentionally not in scope

  • Anti-cheat (MDS, Game File Validator, version check, ergc) — explicit user decision; out of scope.
  • External-server features (CNC Online, GameRanger, Upload Mode, ticker, ladder, GenTool updater) — depend on third-party infrastructure SagePatch does not own.
  • Replay tools and competitive features (Money Display, Player Table, Random Balance, fog-of-war replay, frame stepping, controls bar) — focus is casual play.
  • Engine bug fixes (Scud bug, Tunnel bug, Building bug, multiplayer movement crash) — these are already handled upstream by TheSuperHackers/GeneralsGameCode, which is the parent of GeneralsX and is maintained by the same author who wrote GenTool. The codebase already carries 170+ @bugfix annotations including Tunnel System fixes by xezon. Duplicating them here would create merge conflicts on the next upstream sync.
  • In-game text overlay (clock, match timer, in-game settings menu) — would need a graphics-pipeline hook (D3D8 proxy or Vulkan layer); Phase 2 if there's appetite. The existing DXVK_HUD provides a basic FPS counter as a substitute.
  • Windows preload mechanism — Win32 has no LD_PRELOAD / __interpose equivalent; needs a proxy d3d8.dll like the original GenTool. Stubs are in place so the build still succeeds with RTS_BUILD_OPTION_SAGE_PATCH=ON on Windows, but the runtime is no-op until the proxy is implemented.

Already in the engine, no patch needed

These GenTool-era options are first-class engine flags in Common/CommandLine.cpp. Documented in the SAGEPATCH.md so users know they can call them directly via run.sh:

  • -nologo
  • -noShellAnim
  • -noshellmap
  • -quickstart
  • -xres N -yres N (resolution unlock — engine no longer locks the list)
  • -forcefullviewport
  • -noaudio, -nomusic, -novideo

Test plan

  • Builds on macOS arm64 (macos-vulkan preset, RTS_BUILD_OPTION_SAGE_PATCH=ON)
  • libsage_patch.dylib produced — 55 KB, arm64 native, dynamic SDL3 lookup
  • Deploy script copies the dylib + Override.ini into the game runtime dir
  • Generated run.sh wrapper sets DYLD_INSERT_LIBRARIES correctly (no trailing :)
  • Smoke launch shows the SagePatch banner with all 4 hot-key registrations and the engine loads Data/INI/Default/GameData/SagePatch.ini
  • SAGE_PATCH_DISABLED=1 ./run.sh skips the preload as expected
  • Linux build verification (no Linux host available in this session — config compiles, runtime path needs Linux tester)
  • Each individual feature manually validated (F11 / Scroll Lock / Ctrl+PgUp/Dn / Ctrl+1..5)

Notes on naming

SagePatch is a placeholder picked at scaffolding time (the SAGE engine being what Generals runs on). Trivial to rename via:

git grep -l SagePatch | xargs sed -i '' 's/SagePatch/<NewName>/g; s/sage_patch/<new_name>/g; s/SAGE_PATCH/<NEW_NAME>/g'
mv Patches/SagePatch Patches/<NewName>
mv docs/PATCHES/SAGEPATCH.md docs/PATCHES/<NEWNAME>.md

Commits

  • d42ef875c — initial Phase 1 (macOS-only, mistake)
  • 3efaac65c — cross-platform refactor (macOS + Linux + Windows stubs), window snaps, FPS HUD default
  • 3a87fa2cd — docs scope clarifications

Introduces an optional patch (Patches/SagePatch/) that adds quality-of-life
features for casual play without modifying the game source. Gated by the
new RTS_BUILD_OPTION_SAGE_PATCH cmake flag (default OFF, ON in macos-vulkan
preset).

Architecture: a separate dylib loaded via DYLD_INSERT_LIBRARIES that
replaces SDL_PollEvent at the dyld level (__DATA,__interpose) to capture
hot-keys, plus an engine-side INI override picked up automatically from
Data/INI/Default/GameData/SagePatch.ini. No D3D8 proxy, no Vulkan layer,
no engine source changes.

Features in this initial drop:
  F11           PNG screenshot via /usr/sbin/screencapture (window-only)
  Scroll Lock   cursor lock toggle (SDL_SetWindowMouseGrab)
  Ctrl+PgUp/Dn  display gamma adjustment, range -128..+128
  Override.ini  MaxCameraHeight=800, MinCameraHeight=60, ScrollSpeed=1.0,
                EnforceMaxCameraHeight=No

Anti-cheat features (MDS, Game File Validator, ergc, version validation)
and competitive/networking features (Money Display, Player Table, Random
Balance, replay tools, CNC Online, ticker, Upload Mode, Auto Updater) are
intentionally excluded — focus is casual QoL only.

Smoke test: dylib loads cleanly via DYLD_INSERT_LIBRARIES, engine reads
the INI override, GameMain returns code 0.
…s + FPS

GeneralsX is a multi-platform project; the Phase-1 SagePatch was macOS-only,
which broke the rule. Restructured src/ tree and added Linux backend.

Source tree:
  src/common/   SDL3-only code that works everywhere — Init, KeyHandler,
                CursorLock, WindowPosition.
  src/macos/    __DATA,__interpose hook, screencapture, CoreGraphics gamma.
  src/linux/    LD_PRELOAD + dlsym(RTLD_NEXT), ImageMagick `import` /
                gnome-screenshot, XF86VidMode (lazy dlopen, X11 only —
                no-op under Wayland).
  src/windows/  stubs only (Phase 2 — would need a proxy DLL).

New features in this iteration:
  * Window snap presets — Ctrl+1..5 (center / TL / TR / BL / BR), via
    SDL_SetWindowPosition. Cross-platform.
  * FPS counter — both macOS and Linux deploy scripts default DXVK_HUD=fps
    when SagePatch is active, so casual users get a frame counter without
    extra config. Override with DXVK_HUD=0 or any custom value.
  * Camera pitch override added to resources/Override.ini.

Linux deploy script (scripts/build/linux/deploy-linux-zh.sh) now copies
libsage_patch.so + Override.ini, and the generated run.sh sets LD_PRELOAD
when the library is present. SAGE_PATCH_DISABLED=1 still skips the preload.

Phase 1 complete. Phase 2 items intentionally deferred:
  - Engine bug fixes (scud / tunnel / building / multiplayer crash) require
    edits inside Generals*/Code, defeating SagePatch's "no source change"
    contract.
  - Windows preload is structurally different — needs a d3d8.dll proxy.
  - In-game overlay text (clock, match timer, in-game menu) needs a
    graphics hook (D3D8 proxy or Vulkan layer).
… fixes

Two important clarifications after a code-base inventory:

1. Engine already supports GenTool-style CLI flags natively. Document
   -nologo, -noShellAnim, -noshellmap, -quickstart, -xres/-yres,
   -forcefullviewport, -noaudio/-nomusic/-novideo so users do not assume
   they need SagePatch to get those. They have always been part of the
   engine's CommandLine.cpp; no patch required.

2. Engine bug fixes (scud / tunnel / building / multiplayer movement crash)
   are handled by the upstream TheSuperHackers/GeneralsGameCode project
   which is maintained by the same author who wrote GenTool (xezon). The
   codebase already carries 170+ @BugFix annotations including the
   Tunnel System fixes by xezon himself. SagePatch does not duplicate
   them — that would create merge conflicts on the next upstream sync.

This explicitly scopes SagePatch to QoL features that live outside the
engine source tree.
Local deploy already shipped SagePatch on the previous commit. This wires
the same artifacts (libsage_patch.{dylib,so} + Override.ini) into every
release bundle so users actually get the QoL features when they download
the official zip / tar / flatpak — not just when they build from source.

Touches:

- scripts/build/macos/bundle-macos-zh.sh
- scripts/build/macos/bundle-macos-generals.sh
- scripts/build/linux/bundle-linux-zh.sh
- scripts/build/linux/bundle-linux.sh
- flatpak/com.fbraz3.GeneralsXZH.yml
- flatpak/com.fbraz3.GeneralsX.yml

Each one now:
1. Copies libsage_patch.{dylib,so} into the bundle's lib path (guarded by
   `-f` so the build still succeeds when SAGE_PATCH=OFF).
2. Ships resources/Override.ini into the bundle's data path
   (Resources/Data/... on macOS, /app/share/... in flatpak,
   Data/... in the standalone Linux tar).
3. Generated wrapper scripts now set DYLD_INSERT_LIBRARIES / LD_PRELOAD
   when the lib is present, with a `:` guard so empty existing values do
   not break dyld.
4. Wrapper seeds the INI override into `${CNC_GENERALS_*_PATH}/Data/INI/Default/GameData/SagePatch.ini`
   on first launch (engine reads INIs from cwd, not from inside the bundle).
5. DXVK_HUD defaults to "fps" when SagePatch is active (was: "0").

Flatpak manifests also pick up `-DRTS_BUILD_OPTION_SAGE_PATCH=ON` and run
the `sage_patch` build target after `z_generals` / `g_generals`.

Verified on macOS by running bundle-macos-zh.sh end-to-end:
  GeneralsXZH.app/Contents/Resources/lib/libsage_patch.dylib (55 KB, arm64)
  GeneralsXZH.app/Contents/Resources/Data/INI/Default/GameData/SagePatch.ini
…awIndex

The wrapper scripts shipped DXVK_HUD=fps when SagePatch was active. On
macOS 26 (MoltenVK 1.4.1, current SDK), DXVK's HUD pipeline shader uses
gl_DrawID, which lowers to SPIR-V DrawIndex. SPIRV-Cross to MSL has no
equivalent for that decoration and aborts conversion:

  [mvk-error] SPIR-V to MSL conversion error: DrawIndex is not supported in MSL.
  err:   Failed to create swap chain blit pipeline: VK_ERROR_INITIALIZATION_FAILED

The blit pipeline failure means DXVK can never present a frame; the game
hangs at the EA Games logo (last thing the engine drew before DXVK started
needing the blit pipeline).

Revert the default to DXVK_HUD=0 on the three macOS wrapper scripts.
Users who want the FPS overlay can still opt in with DXVK_HUD=fps.

Linux and Flatpak wrappers are unchanged: native Vulkan drivers handle
DrawIndex correctly there.
The engine resolves Local FS lookups (Data/INI/Default/<subdir>/*.ini overrides,
loose Data/ assets, etc.) relative to the binary's cwd, never the binary's
location. Without an explicit cd, launching the wrapper via absolute path,
Finder, gtimeout, or any other invocation that does not happen to start in the
asset dir caused the engine to miss every loose INI on disk — including
SagePatch.ini — while still loading the BIG-archived defaults via the archive
file system. Symptom: the game runs but the override apparently does nothing.

Wrapper now cds to the script's own directory (which deploy puts the binary,
the dylibs, the override INI, and the .big assets into) and execs the binary
relative to that. Matches the bundle-script wrappers which already did this.
@fbraz3
Copy link
Copy Markdown
Owner

fbraz3 commented Apr 26, 2026

Hey @ebellumat, thanks for your contribution!

I see there are several commits in this PR. Please let me know when it's ready for review.

Regarding the FPS counter feature, how is it different from the native FPS counter located at the top-left corner of the screen?

The engine ships a native FPS overlay at the top-left via
W3DDisplay::drawFPSStats(), gated by #ifdef RTS_DEBUG plus the runtime
-benchmark <seconds> CLI flag. The previous SagePatch revision defaulted
DXVK_HUD=fps in the run wrapper as a release-build alternative, but that
default was already reverted in 55f06b7 because MoltenVK on macOS 26
cannot compile DXVK's HUD pipeline shader (DrawIndex has no MSL equivalent)
and the resulting blit-pipeline failure hangs the game at the EA logo.

Removing the FPS row from the feature table and replacing it with a small
'About FPS counters' section that explains the native option and why our
DXVK_HUD shortcut is parked.
@ebellumat
Copy link
Copy Markdown
Author

You're right, didn't realize the native one existed. The DXVK_HUD trick I had in the wrapper is broken on macOS anyway (MoltenVK can't compile the HUD shader, hangs the game at the EA logo), so I just dropped the FPS claim from the docs in 535c8a1.

Keeping it as draft for now, want to do another pass on macOS at 1440p with assets and get a Linux smoke test before flipping. Will ping when ready.

The engine subsystem init for TheWritableGlobalData scans two parent dirs:
path1 = Data/INI/Default/GameData (loaded first)
path2 = Data/INI/GameData         (loaded second)

Each parsed GameData block overwrites prior values in TheWritableGlobalData
(INI_LOAD_OVERWRITE semantics). The vanilla camera defaults
(MaxCameraHeight = 310, MinCameraHeight = 120, CameraPitch = 37.5) live in
the BIG-archived Data/INI/GameData.ini and are parsed in the SECOND pass,
so an override placed under Data/INI/Default/GameData/ — which is parsed
in the FIRST pass — is silently undone right after.

Verified via temporary instrumentation in parseGameDataDefinition:
  pass 1: file=Data/INI/Default/GameData.ini    max=300/min=100  (debug-only block)
  pass 2: file=Data/INI/GameData.ini line=464   max=310/min=120  (vanilla, was last-write)
  pass 3: file=Data/INI/GameData/SagePatch.ini  max=800/min=60   (now winning)

Deploy scripts on both platforms now write to Data/INI/GameData/SagePatch.ini
(path2 subdir, parsed last) instead of Data/INI/Default/GameData/SagePatch.ini,
and clean up any prior misplaced copy. SAGEPATCH.md gains a short section
explaining the load order so future contributors do not repeat the mistake.
@ebellumat
Copy link
Copy Markdown
Author

Camera distance overrides is working :D

image

MaxCameraHeight=800 was too aggressive — on small maps it pushed the
orthographic frustum past the playable border, exposing void/cull at the
screen edges. Drop to 500 (~1.6x vanilla 310) so the extra range is
useful without breaking small skirmish maps.

Also raise MinCameraHeight back to 80 (slightly closer than vanilla 120
but not as tight as the previous 60), and remove the CameraPitch override
entirely — letting the engine keep its vanilla ~37.5 pitch instead of
forcing 50, which was an arbitrary choice and not a published GenTool
default. Scroll factor stays at 1.0 (2x vanilla).

Reaffirms that no published GenTool tuning numbers are public; these are
conservative casual-friendly bumps.
@fbraz3
Copy link
Copy Markdown
Owner

fbraz3 commented Apr 29, 2026

Camera distance overrides is working :D

Cool! This kind of zoom remember me the older C&C games like RA2.

@fbraz3
Copy link
Copy Markdown
Owner

fbraz3 commented Apr 29, 2026

note: I pushed a bugfix for full screen mode on main branch, you may want to merge it on your fork to be sure it not broke anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants