### One-time setup for Linux cross-compilation Source: https://github.com/alandtse/commonlibvr/blob/ng/examples/linux-cross-compile/README.md Install required toolchain packages for Arch and Debian, set up Wine and llvm-mingw for the fxc2 shader-compile stand-in, install xwin pinned to version 0.10.0, fetch the Windows SDK/CRT sysroot with required flags, and clone vcpkg. Adjust commands for your distro. ```bash # Toolchain adjust for your distro # Arch sudo pacman -S clang lld llvm cmake ninja rust git # Debian 13 "Trixie" sudo apt-get install clang lld llvm cmake ninja-build rustup git rustup default stable # Wine + llvm-mingw: needed only to build/run the fxc2 shader-compile # stand-in (see below), not for the main build itself. # Arch sudo pacman -S wine llvm-mingw # Debian; Use manual install of llvm-mingw. sudo apt-get install wine wget # Manual llvm-mingw install for Distros that don't have a native package. cd /tmp wget https://github.com/mstorsjo/llvm-mingw/releases/download/20260826/llvm-mingw-20260826-ucrt-ubuntu-22.04-x86_64.tar.xz tar -xf llvm-mingw-20260826-ucrt-ubuntu-22.04-x86_64.tar.xz sudo mkdir /opt/llvm-mingw/ sudo mv llvm-mingw-20260826-ucrt-ubuntu-22.04-x86_64/* /opt/llvm-mingw/ export LLVM_MINGW_BIN=/opt/llvm-mingw/bin # xwin, pinned for reproducibility cargo install xwin --version 0.10.0 --locked export PATH="$HOME/.cargo/bin:$PATH" # Windows SDK + MSVC CRT sysroot. Both flags are required: # without --use-winsysroot-style, /winsysroot finds nothing; # without --preserve-ms-arch-notation, folders are named x86_64 instead of x64 and lld-link's auto-search fails. mkdir -p ~/xwin-cache ~/xwin-out xwin --accept-license --http-retry 10 --cache-dir ~/xwin-cache splat \ --output ~/xwin-out --include-debug-libs \ --use-winsysroot-style --preserve-ms-arch-notation # vcpkg git clone https://github.com/microsoft/vcpkg ~/.local/share/vcpkg ~/.local/share/vcpkg/bootstrap-vcpkg.sh -disableMetrics export VCPKG_ROOT=~/.local/share/vcpkg ``` -------------------------------- ### WSL/Linux: Build VR preset with VS Developer Shell Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use powershell.exe to launch the VS Developer Shell and build the VR preset. Adjust the VS installation path to your system; the example uses F: drive. ```bash powershell.exe "& 'F:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1' -Arch amd64; cmake --preset build-release-msvc-vcpkg-vr; cmake --build build/release-msvc-vcpkg-vr" ``` -------------------------------- ### Install and set up pre-commit hooks Source: https://github.com/alandtse/commonlibvr/blob/ng/CONTRIBUTING.md Installs pre-commit and sets up the hooks that run before each commit. The hooks format C/C++ code with clang-format and regenerate cmake/sourcelist.cmake. ```bash pip install pre-commit pre-commit install ``` -------------------------------- ### Step 3: Example migration for a typical NOP patch Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Shows replacing an unverified safe_fill with a verified version using a pattern and error logging. ```cpp // Example migration for a typical NOP patch // Before: REL::safe_fill(REL::RelocationID(12345, 67890).address() + 0x10, REL::NOP, 5); // After: auto address = REL::RelocationID(12345, 67890).address() + 0x10; auto pattern = make_pattern<"E8 ?? ?? ?? ?? 48">(); // call + mov if (!REL::safe_fill(address, REL::NOP, 5, pattern)) { logger::error("Failed to patch function call at {:x}", address); } ``` -------------------------------- ### WSL/Linux: Build ALL preset with VS Developer Shell Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use powershell.exe to launch the VS Developer Shell and build the ALL preset (SE + AE + VR). Adjust the VS installation path to your system. ```bash powershell.exe "& ':\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1' -Arch amd64; cmake --preset build-release-msvc-vcpkg-all; cmake --build build/release-msvc-vcpkg-all" ``` -------------------------------- ### CMakeLists.txt: Precompile headers, install targets, and configure package Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt Configures precompiled headers, installs the library target and exports, generates and installs the CMake package config, and installs public headers. The comment notes that consumer-visible PUBLIC definitions are placed above to avoid macro collisions. ```cmake # consumer-visible PUBLIC definitions above to avoid macro collisions. target_precompile_headers( "${PROJECT_NAME}" PRIVATE include/SKSE/Impl/PCH.h ) install( TARGETS "${PROJECT_NAME}" EXPORT "${PROJECT_NAME}-targets" ) install( EXPORT "${PROJECT_NAME}-targets" NAMESPACE "${PROJECT_NAME}::" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) configure_file( cmake/config.cmake.in "${PROJECT_NAME}Config.cmake" @ONLY ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) install( DIRECTORY "include/RE" "include/REL" "include/REX" "include/SKSE" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) ``` -------------------------------- ### Before: Unverified safe_write and safe_fill Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Original unverified calls that are vulnerable to game updates. ```cpp // No verification - vulnerable to game updates REL::safe_write(address, patch_data, sizeof(patch_data)); REL::safe_fill(address, REL::NOP, 7); ``` -------------------------------- ### Native Windows: Build VR preset with VS Dev Shell launcher (Option 2) Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use the VS Dev Shell launcher in one command to build the VR preset. Adjust the VS installation path to your system. ```powershell & ":\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1" -Arch amd64 cmake --preset build-release-msvc-vcpkg-vr cmake --build build/release-msvc-vcpkg-vr ``` -------------------------------- ### CMake build dependency Source: https://github.com/alandtse/commonlibvr/blob/ng/README.md Finds the CommonLibSSE package and links it to the project target in CMakeLists.txt. ```cmake find_package(CommonLibSSE REQUIRED) target_link_libraries(${PROJECT_NAME} PUBLIC CommonLibSSE::CommonLibSSE) ``` -------------------------------- ### CMake plugin declaration using add_commonlibsse_plugin Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use this simplified CMake function instead of manual plugin setup. Requires CommonLibSSE and links the plugin against CommonLibSSE::CommonLibSSE. ```cmake find_package(CommonLibSSE REQUIRED) add_commonlibsse_plugin(${PROJECT_NAME} SOURCES ${sources}) target_link_libraries(${PROJECT_NAME} PUBLIC CommonLibSSE::CommonLibSSE) ``` -------------------------------- ### WSL/Linux: Build FLATRIM preset with VS Developer Shell Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use powershell.exe to launch the VS Developer Shell and build the FLATRIM preset (SE + AE, no VR). Adjust the VS installation path to your system. ```bash powershell.exe "& ':\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1' -Arch amd64; cmake --preset build-release-msvc-vcpkg-flatrim; cmake --build build/release-msvc-vcpkg-flatrim" ``` -------------------------------- ### Update project submodules Source: https://github.com/alandtse/commonlibvr/blob/ng/README.md Initializes and updates all submodules recursively, including CommonLibSSE-NG, to fetch the latest changes. ```powershell PS E:\Git\foo-bar> git submodule update --init --recursive ``` -------------------------------- ### Initialize a Git repository Source: https://github.com/alandtse/commonlibvr/blob/ng/README.md Run this command in the mod folder to initialize a git repository before adding CommonLibSSE as a submodule. The output shows the repository was created at E:/Git/foo-bar/.git/. ```powershell PS E:\Git\foo-bar> git init Initialized empty Git repository in E:/Git/foo-bar/.git/ ``` -------------------------------- ### After: Verified safe_write with raw bytes, PatternMatcher, and macros Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Three methods to add verification: raw byte array, PatternMatcher (recommended), and VERIFY_AND_FILL macro. ```cpp // Method 1: Raw byte verification std::uint8_t expected[] = { 0x48, 0x8B, 0x05, 0xF9, 0xAA, 0x10, 0x00 }; REL::safe_write(address, patch_data, sizeof(patch_data), expected, sizeof(expected)); // Method 2: PatternMatcher (recommended) auto pattern = make_pattern<"48 8B 05 ?? ?? ?? ??">(); REL::safe_write(address, patch_data, sizeof(patch_data), pattern); // Method 3: Using macros auto pattern = make_pattern<"48 8B 05 F9 AA 10 00">(); VERIFY_AND_FILL(address, REL::NOP, 7, pattern, "Hook description"); ``` -------------------------------- ### Use PatternMatcher for Cross-Version Compatibility Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Use wildcard patterns to handle address differences across game versions. Hardcoded addresses break on updates. ```cpp // Good: Wildcards handle address differences auto pattern = make_pattern<"48 8B 05 ?? ?? ?? ??">(); // mov rax, [rip+offset] // Bad: Hardcoded addresses break on updates std::uint8_t expected[] = { 0x48, 0x8B, 0x05, 0xF9, 0xAA, 0x10, 0x00 }; ``` -------------------------------- ### Build and run the live verification harness Source: https://github.com/alandtse/commonlibvr/blob/ng/test_consumer_live/README.md Builds the SKSE plugin with CMake, deploys the DLL to the Skyrim SE/AE plugins folder, launches the game, and reads the log. Requires a real Skyrim install and is not part of CI. ```powershell cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake cmake --build build --config Release # copy build/CommonLibSSELiveVerify.dll to /Data/SKSE/Plugins/ # launch skse64_loader.exe, load into a save (kDataLoaded fires once per process) # read /My Games/Skyrim Special Edition/SKSE/CommonLibSSELiveVerify.log ``` -------------------------------- ### Memory Access Patches Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Pattern for mov rax, [rip+offset] instructions. ```cpp // Pattern: 48 8B 05 XX XX XX XX (mov rax, [rip+offset]) auto mov_pattern = make_pattern<"48 8B 05 ?? ?? ?? ??">(); ``` -------------------------------- ### RelocateVirtual for Shifted Functions Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Every derived class that overrides functions after a runtime-exclusive function must implement RelocateVirtual for those functions. The example shows Update and GetRotation with SE and VR slot numbers shifted by Unk_03. ```cpp #ifdef SKYRIM_CROSS_VR void DerivedClass::Update(BSTSmartPointer& a_nextState) { REL::RelocateVirtual(0x03, 0x04, this, a_nextState); // SE^ VR^ (shifted by Unk_03) } void DerivedClass::GetRotation(NiQuaternion& a_rotation) { REL::RelocateVirtual(0x04, 0x05, this, a_rotation); // SE^ VR^ (shifted by Unk_03) } #endif ``` -------------------------------- ### Conventional commit examples Source: https://github.com/alandtse/commonlibvr/blob/ng/CONTRIBUTING.md Examples of conventional commit messages used for automated versioning. The type determines the version bump: feat for minor, fix/perf/refactor/style for patch, and BREAKING CHANGE or ! for major. ```bash feat(actor): add new combat utility functions fix(memory): resolve heap allocation bug in ScrapHeap style: apply automated maintenance (formatting & file lists) ``` -------------------------------- ### Quick Conversion: Find & Replace Example Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/RuntimeDataAccessors.md Shows the before and after of converting upstream members to macro format using VSCode regex find & replace. The regex captures whitespace, type, name, and offset comment, then replaces with a macro-style comment. After applying, wrap with #define and remove trailing backslash from last line. ```cpp // Before (upstream): float value; // 0x10 int count; // 0x14 // After (apply regex, then add #define): float value; /* SE 0x10, VR 0x10 */ \ int count; /* SE 0x14, VR 0x14 */ ``` -------------------------------- ### Vtable Slot Alignment Example Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Shows how the vtable slots shift when a runtime-exclusive function is inserted. SE/AE has Begin(01), End(02), Update(03), GetRotation(04); VR has Begin(01), End(02), Unk_03(03), Update(04), GetRotation(05). ```cpp // SE/AE vtable: Begin(01) -> End(02) -> Update(03) -> GetRotation(04) // VR vtable: Begin(01) -> End(02) -> Unk_03(03) -> Update(04) -> GetRotation(05) ``` -------------------------------- ### Verify Instruction Boundaries Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Verify complete instructions to avoid partial matches. Use safe_fill with a pattern to NOP only the intended instruction. ```cpp // Good: Verify complete instruction auto pattern = make_pattern<"48 8B 05 ?? ?? ?? ?? 48 89">(); // mov + mov REL::safe_fill(address, REL::NOP, 7, pattern); // NOP first instruction only // Bad: Partial instruction verification auto pattern = make_pattern<"48 8B 05">(); // Incomplete instruction ``` -------------------------------- ### Add CommonLibSSE-NG as a git submodule Source: https://github.com/alandtse/commonlibvr/blob/ng/README.md Adds the CommonLibSSE-NG repository as a submodule on the 'ng' branch, storing it in lib/commonlibsse-ng. The output shows the cloning progress and completion. ```powershell PS E:\Git\foo-bar> git submodule add -b ng https://github.com/alandtse/CommonLibSSE-NG.git lib/commonlibsse-ng Cloning into 'E:/Git/foo-bar/lib/commonlibsse-ng'... remote: Enumerating objects: 66210, done. remote: Counting objects: 100% (2526/2526), done. remote: Compressing objects: 100% (197/197), done. remote: Total 66210 (delta 2397), reused 2335 (delta 2329), pack-reused 63684 (from 1) Receiving objects: 100% (66210/66210), 18.99 MiB | 19.58 MiB/s, done. Resolving deltas: 100% (51252/51252), done. PS E:\Git\foo-bar ``` -------------------------------- ### Use Descriptive Logging Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Use descriptive error messages for easier debugging. Macros like VERIFY_AND_FILL offer less control over error handling. ```cpp // Good: Descriptive error messages if (!REL::safe_fill(address, REL::NOP, 5, pattern)) { logger::error("Failed to disable texture streaming at {:x}", address); } // OK: Using macros (but less control over error handling) VERIFY_AND_FILL(address, REL::NOP, 5, pattern, "Disable texture streaming"); ``` -------------------------------- ### Jump Patches Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Patterns for jump relative (E9) and jump short (EB) instructions. ```cpp // Pattern: E9 XX XX XX XX (jmp relative) or EB XX (jmp short) auto jmp_pattern = make_pattern<"E9 ?? ?? ?? ??">(); auto jmp_short_pattern = make_pattern<"EB ??">(); ``` -------------------------------- ### Conditional Jumps Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Patterns for je short (74) and je near (0F 84) instructions. ```cpp // Pattern: 74 XX (je short) or 0F 84 XX XX XX XX (je near) auto je_short_pattern = make_pattern<"74 ??">(); auto je_near_pattern = make_pattern<"0F 84 ?? ?? ?? ??">(); ``` -------------------------------- ### Function Call Patches Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Pattern for call relative instructions (E8). Use safe_fill to NOP the call. ```cpp // Pattern: E8 XX XX XX XX (call relative) auto call_pattern = make_pattern<"E8 ?? ?? ?? ??">(); REL::safe_fill(call_address, REL::NOP, 5, call_pattern); ``` -------------------------------- ### STATIC_ASSERT_SIZE usage examples Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/RuntimeDataAccessors.md Validates class sizes across runtimes using STATIC_ASSERT_SIZE from REL/Common.h. Supports single size, Flat vs VR, SE/AE/VR, and all five presets. Use 0x0 for unknown sizes to skip assertion. ```cpp // Single size for all runtimes STATIC_ASSERT_SIZE(MyClass, 0x100); ``` ```cpp // Different sizes for Flat vs VR STATIC_ASSERT_SIZE(CursorMenu, 0x40, 0x50); // Flat=0x40, VR=0x50 ``` ```cpp // SE, AE, VR specified STATIC_ASSERT_SIZE(PlayerCharacter, 0xBE0, 0x9A8, 0x12F0); ``` ```cpp // All five presets specified STATIC_ASSERT_SIZE(ButtonEvent, 0x30, 0x30, 0x38, 0x18, 0x20); // SE AE VR ALL FLAT ``` -------------------------------- ### Handle Verification Failures Gracefully Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Check the return value of safe_write and log a warning if verification fails. Return false to skip the patch instead of crashing. ```cpp auto pattern = make_pattern<"E8 ?? ?? ?? ??">(); if (!REL::safe_write(address, patch, sizeof(patch), pattern)) { logger::warn("Patch verification failed at {:x} - game may have updated", address); return false; // Don't crash, just skip the patch } ``` -------------------------------- ### Xmake build dependency for SKSE plugin Source: https://github.com/alandtse/commonlibvr/blob/ng/README.md Adds CommonLibSSE-NG as a build dependency and applies the custom plugin rule for SKSE plugins in an Xmake target. The rule requires name, author, and description fields. ```xmake target("my_plugin") set_kind("shared") --specifies this is a DLL add_deps("commonlibsse-ng") --adds the dependency add_rules("commonlibsse-ng.plugin", { --adds a custom build rule, whose details are in CommonLibSSE name = "my_plugin", author = "Dovahkiin", description = "SKSE64 plugin using CommonLibSSE-NG" }) ``` -------------------------------- ### Upstream NewBaseClass Accessor Example Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/RuntimeDataAccessors.md Shows the upstream CommonLibSSE pattern for adding a new cast accessor with two overloads (non-const and const) using REL::RelocateMember. Convert this to the RUNTIME_CAST_ACCESSOR macro. ```cpp [[nodiscard]] inline NewBaseClass* AsNewBaseClass() noexcept { return &REL::RelocateMember(this, 0xXX, 0xYY); } [[nodiscard]] inline const NewBaseClass* AsNewBaseClass() const noexcept { return const_cast(this)->AsNewBaseClass(); } ``` -------------------------------- ### Conditional static_assert for Three-Way Conditional Classes Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use conditional `static_assert` for classes with three-way conditionals to verify size. The example checks `sizeof(MyClass) == 0x170` only for the flat (SE/AE) build. ```cpp #if defined(EXCLUSIVE_SKYRIM_FLAT) static_assert(sizeof(MyClass) == 0x170); #endif ``` -------------------------------- ### CMake plugin declaration with add_commonlibsse_plugin Source: https://github.com/alandtse/commonlibvr/blob/ng/README.md Replace add_library with add_commonlibsse_plugin in CMakeLists.txt to auto-generate SKSE plugin version and query functions. Requires find_package(CommonLibSSE REQUIRED) first. ```cmake find_package(CommonLibSSE REQUIRED) add_commonlibsse_plugin(${PROJECT_NAME} SOURCES ${headers} ${sources}) ``` -------------------------------- ### Enable Audit Mode with CMake Source: https://github.com/alandtse/commonlibvr/blob/ng/docs/VERIFICATION_MIGRATION.md Add this compile definition to enable audit mode, which shows compiler warnings and logs runtime warnings for unverified patches. ```cmake target_compile_definitions(${PROJECT_NAME} PRIVATE REL_AUDIT_UNVERIFIED_PATCHES) ``` -------------------------------- ### Quick Build Script (Windows and WSL/Linux) Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use this script for fast iteration and error detection. It builds all presets (SE, AE, VR, Flatrim, All) in sequence, stops at first failure, and shows only errors. ```bash # From Windows .\scripts\build-all-presets.cmd # From WSL/Linux cmd.exe /c "scripts\build-all-presets.cmd" ``` -------------------------------- ### Build Script: .\scripts\build-all-presets.cmd Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Runs all runtime configurations, stops at first error, and shows only error messages for fast iteration. ```bash .\scripts\build-all-presets.cmd ``` -------------------------------- ### Native Windows: Build ALL preset (Option 1) Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Launch VS Developer PowerShell manually, then configure and build the ALL preset. ```powershell cmake --preset build-release-msvc-vcpkg-all cmake --build build/release-msvc-vcpkg-all ``` -------------------------------- ### Build all presets with Clang Source: https://github.com/alandtse/commonlibvr/blob/ng/AGENTS.md Fast iteration build using Clang for all presets. ```bash .\.\scripts\build-all-presets.cmd --clang # fast iteration ``` -------------------------------- ### Build all presets with MSVC Source: https://github.com/alandtse/commonlibvr/blob/ng/AGENTS.md Full MSVC matrix build for all presets. ```bash .\.\scripts\build-all-presets.cmd # full MSVC matrix ``` -------------------------------- ### xmake configuration for consuming prebuilt CommonLibVR Source: https://github.com/alandtse/commonlibvr/blob/ng/PREBUILT.md Replace the source submodule with the extracted bundle at the same path. The includes() call runs the bundled xmake.lua, which detects the prebuilt library and switches to prebuilt mode, keeping the same target, rule, and transitive dependencies. ```lua set_config("rex_ini", true) -- match the bundle set_config("skse_xbyak", true) includes("lib/commonlibsse-ng") -- now a prebuilt bundle, not source target("my-plugin") add_deps("commonlibsse-ng") add_rules("commonlibsse-ng.plugin", { name = "my-plugin", author = "you" }) add_files("src/**.cpp") ``` -------------------------------- ### Linux-host cross-compile: Build ALL preset Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Targets the same Windows PE/MSVC ABI output using clang-cl and lld-link against an xwin-generated sysroot. Runs from an ordinary Linux shell without VS Developer Shell or Windows tooling. ```bash cmake --preset build-release-linux-clangcl-vcpkg-all cmake --build --preset release-linux-clangcl-vcpkg-all ``` -------------------------------- ### Verify the built executable Source: https://github.com/alandtse/commonlibvr/blob/ng/examples/linux-cross-compile/README.md Use the file command to confirm the executable is a Windows PE32+ x86-64 binary. Expect output: PE32+ executable ... x86-64, for MS Windows. ```bash file build/release-linux-clangcl-vcpkg-all/CommonLibSSETests.exe # expect: PE32+ executable ... x86-64, for MS Windows ``` -------------------------------- ### WSL/Linux: Run tests with ctest Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Run tests from the Windows side after building. Uses the 'all' preset. ```bash powershell.exe "ctest --preset all" ``` -------------------------------- ### CMakeLists.txt: Resolve and link prebuilt CommonLibSSE binary Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt Includes cmake/Prebuilt.cmake and calls commonlib_resolve_prebuilt() to prefer a published prebuilt binary over a source compile. If resolved, defines an imported static library target with Release and Debug configurations, maps release-like configs, sets compile features, include directories, compile definitions, MSVC options, and links dependencies. Falls through to source build if not resolved. ```cmake include(cmake/Prebuilt.cmake) commonlib_resolve_prebuilt(COMMONLIB_PREBUILT_RESOLVED) if(COMMONLIB_PREBUILT_RESOLVED) message(STATUS "CommonLibSSE: linking prebuilt binary from ${COMMONLIB_PREBUILT_RESOLVED}") add_library("${PROJECT_NAME}" STATIC IMPORTED GLOBAL) # Pin the lib to Release and map the other release-like configs onto it. Point Debug at a # non-existent sentinel rather than nothing: a multi-config generator validates every config # at generate time, so an empty Debug map fails the whole configure — the sentinel keeps # generate happy while making a Debug *build* fail loudly with a self-documenting name (the # Release-only bundle can't serve Debug; build it from source). set_target_properties( "${PROJECT_NAME}" PROPERTIES IMPORTED_CONFIGURATIONS "RELEASE;DEBUG" IMPORTED_LOCATION_RELEASE "${COMMONLIB_PREBUILT_RESOLVED}/lib/CommonLibSSE.lib" IMPORTED_LOCATION_DEBUG "${COMMONLIB_PREBUILT_RESOLVED}/lib/CommonLibSSE-debug-build-from-source.lib" MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release MAP_IMPORTED_CONFIG_MINSIZEREL Release ) add_library("${PROJECT_NAME}::${PROJECT_NAME}" ALIAS "${PROJECT_NAME}") target_compile_features("${PROJECT_NAME}" INTERFACE cxx_std_23) target_include_directories( "${PROJECT_NAME}" INTERFACE "${COMMONLIB_PREBUILT_RESOLVED}/include" "${COMMONLIB_PREBUILT_RESOLVED}/extern/openvr/headers" ) # Skyrim runtime set is layout-critical and required by the resolver, so define it # unconditionally to match the lib. target_compile_definitions( "${PROJECT_NAME}" INTERFACE WINVER=0x0601 _WIN32_WINNT=0x0601 ENABLE_SKYRIM_SE=1 ENABLE_SKYRIM_AE=1 ENABLE_SKYRIM_VR=1 HAS_SKYRIM_MULTI_TARGETING=1 ) # The lib bakes every REX config + xbyak, but they're additive — expose only the ones the # consumer opted into, so it gets exactly the API (and headers) it asked for. foreach(_opt REX_OPTION_INI REX_OPTION_JSON REX_OPTION_TOML SKSE_SUPPORT_XBYAK) if(${_opt}) target_compile_definitions("${PROJECT_NAME}" INTERFACE ${_opt}=1) endif() endforeach() if(MSVC) target_compile_options( "${PROJECT_NAME}" INTERFACE /Zc:preprocessor /wd4005 /wd4061 /wd4068 /wd4200 /wd4201 /wd4265 /wd4266 /wd4371 /wd4514 /wd4582 /wd4583 /wd4623 /wd4625 /wd4626 /wd4702 /wd4710 /wd4711 /wd4820 /wd4996 /wd5026 /wd5027 /wd5045 /wd5053 /wd5204 /wd5220 ) endif() target_link_libraries( "${PROJECT_NAME}" INTERFACE spdlog::spdlog Microsoft::DirectXTK advapi32.lib bcrypt.lib D3D11.lib d3dcompiler.lib dbghelp.lib DXGI.lib ole32.lib version.lib "${COMMONLIB_PREBUILT_RESOLVED}/lib/openvr_api.lib" ) include(cmake/CommonLibSSE.cmake) # add_commonlibsse_plugin() helper for consumers return() endif() ``` -------------------------------- ### Enable IPO for Release builds with COMMONLIB_ENABLE_IPO option Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt Adds a CMake option to control interprocedural optimization. When enabled, checks if IPO is supported and sets CMAKE_INTERPROCEDURAL_OPTIMIZATION for Release configurations. Off by default in CI to speed up compile and link times. ```cmake option(COMMONLIB_ENABLE_IPO "Enable interprocedural (link-time) optimization for Release builds" ON) if(COMMONLIB_ENABLE_IPO) check_ipo_supported(RESULT USE_IPO OUTPUT IPO_OUTPUT) if(USE_IPO) set(CMAKE_INTERPROCEDURAL_OPTIMIZATION "$<$:ON>") endif() endif() ``` -------------------------------- ### CMake configuration for live verification plugin Source: https://github.com/alandtse/commonlibvr/blob/ng/test_consumer_live/CMakeLists.txt Builds a real SKSE plugin for manual testing against a live Skyrim process. Not part of CI. Supports flat SE/AE by default, with an option to build the VR-only variant. Forces CommonLib build options and disables tests. ```cmake cmake_minimum_required(VERSION 3.21) project( CommonLibSSELiveVerify LANGUAGES CXX VERSION 1.0.0 ) # NOT part of CI (see README.md) -- builds a real SKSE plugin, meant to be # deployed to a real game install and run against a real running Skyrim # process. CI has no game to launch, so this only runs locally/manually. set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) # flat (SE+AE) by default; -DLIVE_VERIFY_VR=ON builds the VR-only variant instead. option(LIVE_VERIFY_VR "Build the VR-only variant instead of flat SE/AE" OFF) if(LIVE_VERIFY_VR) set(ENABLE_SKYRIM_SE OFF CACHE BOOL " " FORCE) set(ENABLE_SKYRIM_AE OFF CACHE BOOL " " FORCE) set(ENABLE_SKYRIM_VR ON CACHE BOOL " " FORCE) else() set(ENABLE_SKYRIM_SE ON CACHE BOOL " " FORCE) set(ENABLE_SKYRIM_AE ON CACHE BOOL " " FORCE) set(ENABLE_SKYRIM_VR OFF CACHE BOOL " " FORCE) endif() set(BUILD_TESTS OFF CACHE BOOL "Disable CommonLib tests in consumer" FORCE) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/.. CommonLibSSE EXCLUDE_FROM_ALL) get_target_property(COMMONLIB_SRC_DIR CommonLibSSE SOURCE_DIR) include(${COMMONLIB_SRC_DIR}/cmake/CommonLibSSE.cmake) add_commonlibsse_plugin(${PROJECT_NAME} AUTHOR "CommonLibVR" SOURCES src/plugin.cpp ) target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_23) target_compile_definitions(${PROJECT_NAME} PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) target_precompile_headers(${PROJECT_NAME} PRIVATE include/PCH.h) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) ``` -------------------------------- ### REL::Relocation Must Be Static Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Shows the correct way to declare a `REL::Relocation` singleton. The wrong version is non-static and will be reinitialized on each call; the correct version uses `static` to ensure initialization once and persistence across calls. ```cpp // WRONG: REL::Relocation singleton{ REL::ID(12345) }; // CORRECT: static REL::Relocation singleton{ REL::ID(12345) }; ``` -------------------------------- ### Define static library target and alias Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt Creates a static library target from SOURCES and additional files, then adds an alias target for convenient referencing. The alias allows using ${PROJECT_NAME}::${PROJECT_NAME} in other CMake targets. ```cmake add_library( "${PROJECT_NAME}" STATIC ${SOURCES} .clang-format CommonLibSSE.natvis ) add_library("${PROJECT_NAME}::${PROJECT_NAME}" ALIAS "${PROJECT_NAME}") ``` -------------------------------- ### CMakeLists.txt: Include source and test lists Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt Includes cmake/sourcelist.cmake and cmake/testlist.cmake to define SOURCES and test targets for the source build fallback. ```cmake include(cmake/sourcelist.cmake) include(cmake/testlist.cmake) ``` -------------------------------- ### CMakeLists.txt: Build tests with Catch2 and copy test data Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt Conditionally builds the test executable when BUILD_TESTS is enabled. Finds Catch2, includes CTest and Catch, creates the test target, sets include directories and compile definitions, links Catch2, adds MSVC debug PDB options, precompiles headers, copies test data files to the build directory, and registers tests via catch_discover_tests and add_test. ```cmake if(BUILD_TESTS) find_package(Catch2 CONFIG REQUIRED) include(CTest) include(Catch) add_executable( "${PROJECT_NAME}Tests" ${TESTS} ) target_include_directories( "${PROJECT_NAME}Tests" PUBLIC "$" "$" ) target_include_directories( "${PROJECT_NAME}Tests" PRIVATE ${Catch2_INCLUDE_DIRS} ${spdlog_INCLUDE_DIRS} ${RAPIDCSV_INCLUDE_DIRS} ) target_compile_definitions( "${PROJECT_NAME}Tests" PUBLIC ENABLE_COMMONLIBSSE_TESTING=1 ) target_link_libraries( "${PROJECT_NAME}Tests" PRIVATE ${PROJECT_NAME} Catch2::Catch2 ) if(MSVC) target_link_options( "${PROJECT_NAME}Tests" PRIVATE # Generate complete PDB with all type information for reverse engineering "$<$:/DEBUG:FULL>" ) endif() target_precompile_headers( "${PROJECT_NAME}Tests" PRIVATE include/SKSE/Impl/PCH.h ) file(COPY tests/REL/input-queue.csv tests/REL/version-1-4-15-0.csv tests/REL/version-1-5-97-0.bin tests/REL/versionlib-1-6-353-0.bin tests/REL/versionlib-1-6-629-0.bin tests/REL/versionlib-1-6-1130-0.bin tests/REL/versionlib-1-6-1170-0.bin tests/REL/versionlib-1-6-1179-0.bin tests/REL/versionlib-1-7-99-0.bin tests/REL/version-invalid-format.bin DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/Data/SKSE/Plugins/" ) catch_discover_tests("${PROJECT_NAME}Tests") add_test(NAME "${PROJECT_NAME}Tests" COMMAND "${PROJECT_NAME}Tests") endif() ``` -------------------------------- ### CMakeLists.txt: Fetch and vendor hde64 for patch safety Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt When SKSE_SUPPORT_PATCH_SAFETY is enabled, enables C language, declares and fetches hde64 from MinHook v1.3.4 with SOURCE_SUBDIR src/hde to vendor the instruction-length decoder without adding MinHook's full library. ```cmake if(SKSE_SUPPORT_PATCH_SAFETY) # hde64 has no vcpkg port -- vendor MinHook's 3-file instruction-length decoder directly. # LANGUAGE C (set below) needs an explicit enable_language(C), or Ninja-based # generators fail at generate time (the VS generator tolerates the omission). enable_language(C) include(FetchContent) FetchContent_Declare( hde64 GIT_REPOSITORY https://github.com/TsudaKageyu/minhook.git GIT_TAG v1.3.4 GIT_SHALLOW TRUE # Populate source only -- MinHook's own root CMakeLists.txt would otherwise add # its full hooking library as a subdirectory/target, which we don't want. SOURCE_SUBDIR src/hde ) FetchContent_MakeAvailable(hde64) endif() ``` -------------------------------- ### CMake configuration for consumer test Source: https://github.com/alandtse/commonlibvr/blob/ng/test_consumer/README.md Configures the consumer test project with vcpkg toolchain and enables Skyrim SE, AE, and VR editions. Requires vcpkg dependencies because CommonLib is built from source. ```bash # With vcpkg toolchain file cmake -B build -S . \ -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake \ -DENABLE_SKYRIM_SE=ON \ -DENABLE_SKYRIM_AE=ON \ -DENABLE_SKYRIM_VR=ON cmake --build build --config Release ./build/Release/ConsumerTest.exe ``` -------------------------------- ### Derived Class Pattern for Runtime-Exclusive Virtual Functions Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Must match the base class pattern. The header uses the same three-way conditional with override for VR. The implementation is identical to the base class, using REL::RelocateVirtual for VR and no-op for SE/AE. ```cpp // Header (.h file) - Must match base class pattern #if defined(EXCLUSIVE_SKYRIM_FLAT) // Function doesn't exist in SE/AE-only builds #elif defined(EXCLUSIVE_SKYRIM_VR) void Unk_03() override; // 03 - VR only #else void Unk_03(); // 03 - Multi-runtime #endif // Implementation (.cpp file) - Same as base class #ifdef SKYRIM_CROSS_VR void DerivedClass::Unk_03() { if (REL::Module::IsVR()) { REL::RelocateVirtual(0x03, 0x03, this); } // SE/AE: no-op, this function should never be called } #endif ``` -------------------------------- ### Base Class Pattern for Runtime-Exclusive Virtual Functions Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Use this pattern when a class has the same base class across runtimes but different virtual functions exist in different runtimes. The header uses a three-way conditional to declare the function only for VR or as non-virtual for multi-runtime. The implementation uses SKYRIM_CROSS_VR to call REL::RelocateVirtual for VR and no-op for SE/AE. ```cpp // Header (.h file) - Three-way conditional pattern #if defined(EXCLUSIVE_SKYRIM_FLAT) // Function doesn't exist in SE/AE-only builds #elif defined(EXCLUSIVE_SKYRIM_VR) virtual void Unk_03(); // 03 - VR only #else void Unk_03(); // 03 - Multi-runtime (non-virtual) #endif // Implementation (.cpp file) #ifdef SKYRIM_CROSS_VR void ClassName::Unk_03() { if (REL::Module::IsVR()) { REL::RelocateVirtual(0x03, 0x03, this); } // SE/AE: no-op, this function should never be called } #endif ``` -------------------------------- ### Set public compile definitions for Windows and feature flags Source: https://github.com/alandtse/commonlibvr/blob/ng/CMakeLists.txt Configures public compile definitions on the library target. Sets Windows version macros for Skyrim Special Edition compatibility and conditionally defines feature flags based on CMake options like REX_OPTION_INI, SKSE_SUPPORT_XBYAK, and ENABLE_SKYRIM_SE/AE/VR. Also defines HAS_SKYRIM_MULTI_TARGETING when multiple Skyrim versions are enabled. ```cmake target_compile_definitions( "${PROJECT_NAME}" PUBLIC WINVER=0x0601 # windows 7, minimum supported version by skyrim special edition _WIN32_WINNT=0x0601 "$<$:REX_OPTION_INI=1>" "$<$:REX_OPTION_JSON=1>" "$<$:REX_OPTION_TOML=1>" "$<$:SKSE_SUPPORT_XBYAK=1>" "$<$:SKSE_SUPPORT_PATCH_SAFETY=1>" "$<$:ENABLE_SKYRIM_SE=1>" "$<$:ENABLE_SKYRIM_AE=1>" "$<$:ENABLE_SKYRIM_VR=1>" "$<$,$>:HAS_SKYRIM_MULTI_TARGETING=1>" "$<$,$>:HAS_SKYRIM_MULTI_TARGETING=1>" "$<$,$>:HAS_SKYRIM_MULTI_TARGETING=1>" ) ``` -------------------------------- ### TESNPC::GetFaceTint VR C++ Source: https://github.com/alandtse/commonlibvr/wiki/Home VR version of GetFaceTint. Does not call TESFile::IsESL_14017E160. Formats the same FaceTint DDS path and returns a non-null pointer if the TESFile is valid. ```cpp if (pTVar4 != (TESFile *)0x0) { iVar3 = f_snprintf_1401423D0 (param_2,(char *)(param_3 & 0xffffffff), "data\\Textures\\Actors\\Character\\FaceGenData\\FaceTint\\%s\\%08X.dds", pTVar4->Name_58); return (TESFile *)CONCAT71((int7)(CONCAT44(extraout_var,iVar3) >> 8),1); } return (TESFile *)0x0; ``` -------------------------------- ### Three-Way Conditional Pattern for Virtual Functions Source: https://github.com/alandtse/commonlibvr/blob/ng/CLAUDE.md Standard pattern for runtime-exclusive virtual functions in multi-runtime builds. Uses `#if defined(EXCLUSIVE_SKYRIM_FLAT)` for SE/AE, `#elif defined(EXCLUSIVE_SKYRIM_VR)` for VR (where the function doesn't exist), and `#else` for multi-runtime non-virtual wrapper. ```cpp #if defined(EXCLUSIVE_SKYRIM_FLAT) void MyFunction() override; // SE/AE only - virtual #elif defined(EXCLUSIVE_SKYRIM_VR) // VR has different vtable - function doesn't exist #else void MyFunction(); // Multi-runtime - non-virtual wrapper #endif ``` -------------------------------- ### CMakeLists.txt for CommonLibSSE consumer test Source: https://github.com/alandtse/commonlibvr/blob/ng/test_consumer/CMakeLists.txt Configures a CMake project that consumes CommonLibSSE as a subdirectory, disables CommonLib's own tests, links the library, sets up precompiled headers, and adds a test that runs the consumer executable. ```cmake cmake_minimum_required(VERSION 3.21) project( CommonLibSSEConsumerTest LANGUAGES CXX VERSION 1.0.0 ) # This test project validates that CommonLib works correctly when used as a dependency # It specifically tests that STATIC_ASSERT_SIZE macros compile when consuming CommonLib set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Add CommonLib as a subdirectory dependency (simulates real-world usage) set(BUILD_TESTS OFF CACHE BOOL "Disable CommonLib tests in consumer" FORCE) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/.. CommonLibSSE EXCLUDE_FROM_ALL) # Create a simple test executable that uses CommonLib headers add_executable( ConsumerTest test.cpp ) target_link_libraries( ConsumerTest PRIVATE CommonLibSSE::CommonLibSSE ) # Use CommonLib's precompiled header (critical for RTTI macros, stl namespace, etc.) target_precompile_headers( ConsumerTest PRIVATE ) # Enable testing enable_testing() add_test( NAME ConsumerCompileTest COMMAND ConsumerTest ) ``` -------------------------------- ### CMake configuration for CommonLibSSE plugin Source: https://github.com/alandtse/commonlibvr/blob/ng/test_package/CMakeLists.txt Configures a C++23 SKSE plugin project using CommonLibSSE. Requires CMake 3.21 and the CommonLibSSE package. Defines the plugin target with add_commonlibsse_plugin. ```cmake cmake_minimum_required(VERSION 3.21) project( CommonLibSSESamplePlugin VERSION 1.0.0 DESCRIPTION "A sample SKSE plugin built with CommonLibSSE." LANGUAGES CXX) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(CommonLibSSE CONFIG REQUIRED) add_commonlibsse_plugin(${PROJECT_NAME} SOURCES Main.cpp) ```