# Saints Reborn - builds saintsrow.exe from the ReXGlue SDK, the code generated
# from your own copy of the game (see scripts/setup.ps1) and the sources in src/.
cmake_minimum_required(VERSION 3.25)
project(saintsrow LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(DEFINED ENV{REXSDK})
    list(PREPEND CMAKE_PREFIX_PATH $ENV{REXSDK})
endif()
find_package(rexglue REQUIRED)

set(SR_GENERATED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../build/generated" CACHE PATH
    "Directory containing the code generated by 'rexglue codegen'")
if(NOT EXISTS "${SR_GENERATED_DIR}/sources.cmake")
    message(FATAL_ERROR "Generated code not found in ${SR_GENERATED_DIR}. Run setup.bat first.")
endif()

# ---------------------------------------------------------------------------
# Whompay's Mod Loader dependencies (downloaded at configure time)
# ---------------------------------------------------------------------------
include(FetchContent)
# SOURCE_SUBDIR points at a folder without a CMakeLists.txt, so the sources
# are only downloaded; the libraries are defined below.
FetchContent_Declare(lua
    GIT_REPOSITORY https://github.com/lua/lua.git
    GIT_TAG v5.4.7
    GIT_SHALLOW TRUE
    SOURCE_SUBDIR no-cmake)
FetchContent_Declare(minhook
    GIT_REPOSITORY https://github.com/TsudaKageyu/minhook.git
    GIT_TAG v1.3.4
    GIT_SHALLOW TRUE
    SOURCE_SUBDIR no-cmake)
FetchContent_Declare(zlib
    GIT_REPOSITORY https://github.com/madler/zlib.git
    GIT_TAG v1.3.1
    GIT_SHALLOW TRUE
    SOURCE_SUBDIR no-cmake)
FetchContent_MakeAvailable(lua minhook zlib)

add_library(wml_zlib STATIC
    ${zlib_SOURCE_DIR}/adler32.c ${zlib_SOURCE_DIR}/compress.c ${zlib_SOURCE_DIR}/crc32.c
    ${zlib_SOURCE_DIR}/deflate.c ${zlib_SOURCE_DIR}/inffast.c ${zlib_SOURCE_DIR}/inflate.c
    ${zlib_SOURCE_DIR}/inftrees.c ${zlib_SOURCE_DIR}/trees.c ${zlib_SOURCE_DIR}/uncompr.c
    ${zlib_SOURCE_DIR}/zutil.c)
target_include_directories(wml_zlib PUBLIC ${zlib_SOURCE_DIR})
target_compile_definitions(wml_zlib PRIVATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)

file(GLOB WML_LUA_SOURCES ${lua_SOURCE_DIR}/*.c)
list(FILTER WML_LUA_SOURCES EXCLUDE REGEX "/(lua|luac|onelua|ltests)\\.c$")
add_library(wml_lua STATIC ${WML_LUA_SOURCES})
target_include_directories(wml_lua PUBLIC ${lua_SOURCE_DIR})
target_compile_definitions(wml_lua PRIVATE _CRT_SECURE_NO_WARNINGS)

add_library(wml_minhook STATIC
    ${minhook_SOURCE_DIR}/src/buffer.c
    ${minhook_SOURCE_DIR}/src/hook.c
    ${minhook_SOURCE_DIR}/src/trampoline.c
    ${minhook_SOURCE_DIR}/src/hde/hde64.c)
target_include_directories(wml_minhook PUBLIC ${minhook_SOURCE_DIR}/include)

set(SAINTSROW_SOURCES
    src/main.cpp
    src/stubs.cpp
    src/fps_overlay.cpp
    src/perf_monitor.cpp
    src/kbm.cpp
    src/options_menu.cpp
    src/hw_profile.cpp
    src/shadow_probe.cpp
    src/coop_menu.cpp
    src/online_integrity.cpp
    src/online_players.cpp
    src/chat.cpp
    src/glyphs.cpp
    src/profiler.cpp
    src/render_fixes.cpp
    src/world_studio_bridge.cpp
    src/wml/mod_list.cpp
    src/wml/mod_loader.cpp
    src/wml/lua_mods.cpp
    src/wml/packfile.cpp
    src/wml/patch_phase.cpp
    src/wml/fair_play.cpp
    src/discord_presence.cpp
    ${REXGLUE_SHARE_DIR}/rex_app.cpp
    res/saintsreborn.rc
)
if(EXISTS "${REXGLUE_SHARE_DIR}/windowed_app_main_sdl.cpp")
    list(APPEND SAINTSROW_SOURCES ${REXGLUE_SHARE_DIR}/windowed_app_main_sdl.cpp)
else()
    list(APPEND SAINTSROW_SOURCES ${REXGLUE_SHARE_DIR}/windowed_app_main_win.cpp)
endif()

include(${SR_GENERATED_DIR}/sources.cmake)
list(APPEND SAINTSROW_SOURCES ${GENERATED_SOURCES})

add_executable(saintsrow WIN32 ${SAINTSROW_SOURCES})

# rex_app.cpp expects REXGLUE_BUILD_CONFIG, which the SDK package doesn't export.
if(CMAKE_BUILD_TYPE)
    target_compile_definitions(saintsrow PRIVATE REXGLUE_BUILD_CONFIG="${CMAKE_BUILD_TYPE}")
else()
    target_compile_definitions(saintsrow PRIVATE REXGLUE_BUILD_CONFIG="Release")
endif()

target_compile_definitions(saintsrow PRIVATE _CRT_SECURE_NO_WARNINGS)

target_include_directories(saintsrow PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/../modding/include
    ${SR_GENERATED_DIR}
)
target_link_libraries(saintsrow PRIVATE winmm rex::runtime rex::gpu-xenos wml_lua wml_minhook wml_zlib)

target_compile_options(saintsrow PRIVATE
    -fno-strict-aliasing
    # precise, not strict: strict turned every float operation of the recompiled
    # code into a constrained operation (no single-precision math, no
    # vectorizing, ~35-40% more float instructions). The results are the same
    # (IEEE, no reassociation; contract=off keeps separate multiply + add).
    -ffp-model=precise
    -ffp-contract=off
    -fno-char8_t
    $<$<CONFIG:Release>:-O3>
    $<$<CONFIG:Release>:-DNDEBUG>
)
if(NOT MSVC)
    # x86-64-v3 (Haswell / Zen and newer: AVX2, BMI2, LZCNT, MOVBE). MOVBE lets
    # the big-endian guest loads/stores of the recompiled code be one
    # instruction instead of load + byte swap.
    # auto (default) = what the CPU doing the build supports: players build the
    # game on their own PC (scripts/setup.ps1), so an old laptop gets v2.
    set(SR_CPU_LEVEL "auto" CACHE STRING "Recompiled code CPU level: auto, v3, v2 or sse41")
    set(SR_CPU_LEVEL_USED "${SR_CPU_LEVEL}")
    if(SR_CPU_LEVEL STREQUAL "auto")
        try_run(SR_CPU_PROBE_RUN SR_CPU_PROBE_COMPILED
            ${CMAKE_BINARY_DIR}/cpu_level_probe
            ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cpu_level_probe.c
            COMPILE_DEFINITIONS -mxsave
            RUN_OUTPUT_VARIABLE SR_CPU_PROBE_OUT)
        if(SR_CPU_PROBE_COMPILED AND SR_CPU_PROBE_OUT STREQUAL "v2")
            set(SR_CPU_LEVEL_USED "v2")
        else()
            set(SR_CPU_LEVEL_USED "v3")
        endif()
        message(STATUS "Recompiled code CPU level (auto): ${SR_CPU_LEVEL_USED}")
    endif()
    if(SR_CPU_LEVEL_USED STREQUAL "v3")
        target_compile_options(saintsrow PRIVATE -march=x86-64-v3)
        # The process starts in cpu_gate.cpp (plain x86-64): on a CPU without
        # the v3 features it runs saintsrow_compat.exe (SR_CPU_LEVEL=v2)
        # instead of crashing on the first AVX2/BMI/MOVBE instruction.
        target_sources(saintsrow PRIVATE src/cpu_gate.cpp)
        set_source_files_properties(src/cpu_gate.cpp PROPERTIES COMPILE_OPTIONS "-march=x86-64;-mtune=generic")
        target_link_options(saintsrow PRIVATE "LINKER:/ENTRY:sr_cpu_gate_entry")
    elseif(SR_CPU_LEVEL_USED STREQUAL "v2")
        # For older CPUs (SSE4.2 + POPCNT: Nehalem / Bulldozer and newer, i.e.
        # everything from ~2009 on). Built as saintsrow_compat.exe.
        target_compile_options(saintsrow PRIVATE -march=x86-64-v2)
        # No FMA3: replace the runtime's slow software std::fma in the
        # recompiled code with an exact fast path (src/fast_fma.h).
        set_source_files_properties(${GENERATED_SOURCES} PROPERTIES COMPILE_OPTIONS
            "-include;${CMAKE_CURRENT_SOURCE_DIR}/src/fast_fma.h")
        # Asked for explicitly = the extra exe for old CPUs next to a v3 one.
        # (auto on an old CPU builds a plain saintsrow.exe.)
        if(SR_CPU_LEVEL STREQUAL "v2")
            set_target_properties(saintsrow PROPERTIES OUTPUT_NAME saintsrow_compat)
        endif()
    else()
        target_compile_options(saintsrow PRIVATE -msse4.1)
    endif()
    # saintsrow.map lets the built-in profiler name functions.
    target_link_options(saintsrow PRIVATE "LINKER:/MAP:saintsrow.map")
endif()

# Profile-guided optimization of the recompiled code (clang PGO):
#   SR_PGO=gen -> instrumented exe, writes pgo_saintsrow.profraw next to where
#                 it runs (every 30 s and on close) - play a few minutes.
#   SR_PGO=use -> optimized with SR_PGO_PROFILE (llvm-profdata merge output).
# Separate build folders (build/game_pgo_gen, build/game_pgo_use) keep the
# normal build untouched.
set(SR_PGO "off" CACHE STRING "Profile-guided optimization: off, gen or use")
set(SR_PGO_PROFILE "${CMAKE_CURRENT_SOURCE_DIR}/../pgo/saintsrow.profdata" CACHE FILEPATH "Merged PGO profile")
if(SR_PGO STREQUAL "gen")
    target_compile_options(saintsrow PRIVATE -fprofile-generate)
    target_link_options(saintsrow PRIVATE -fprofile-generate)
    target_compile_definitions(saintsrow PRIVATE SR_PGO_GEN=1)
elseif(SR_PGO STREQUAL "use")
    target_compile_options(saintsrow PRIVATE "-fprofile-use=${SR_PGO_PROFILE}"
        -Wno-profile-instr-unprofiled -Wno-profile-instr-out-of-date -Wno-profile-instr-missing
        -Wno-backend-plugin)
endif()

# /force:multiple lets stubs.cpp override the SDK's kernel export implementations.
target_link_options(saintsrow PRIVATE "LINKER:/force:multiple" "LINKER:/STACK:16777216")

# Put the SDK's DLLs next to the exe so it also runs from the build folder.
add_custom_command(TARGET saintsrow POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:saintsrow> $<TARGET_FILE_DIR:saintsrow>
    COMMAND_EXPAND_LISTS
)

# ---------------------------------------------------------------------------
# Whompay's Mod Loader launcher
# ---------------------------------------------------------------------------
add_executable(WhompaysModLoader WIN32
    launcher/launcher.cpp
    src/wml/mod_list.cpp
    res/modloader.rc)
target_include_directories(WhompaysModLoader PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_compile_definitions(WhompaysModLoader PRIVATE _CRT_SECURE_NO_WARNINGS UNICODE _UNICODE)
target_compile_options(WhompaysModLoader PRIVATE -fno-char8_t)
target_link_libraries(WhompaysModLoader PRIVATE user32 gdi32 comctl32 shell32 ole32 uxtheme)

# Example native mod (built so the examples folder has a ready-to-use DLL).
add_library(example_native_mod SHARED ../modding/examples/ExampleNative/example_native.c)
target_include_directories(example_native_mod PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../modding/include)
set_target_properties(example_native_mod PROPERTIES OUTPUT_NAME "ExampleNative" PREFIX "")

add_library(whompays_trainer SHARED ../modding/mods/WhompaysTrainer/trainer.cpp)
target_include_directories(whompays_trainer PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../modding/include)
target_link_libraries(whompays_trainer PRIVATE rex::runtime user32 gdi32)
target_compile_options(whompays_trainer PRIVATE -msse4.1)
set_target_properties(whompays_trainer PROPERTIES OUTPUT_NAME "WhompaysTrainer" PREFIX "")

# Co-op (built in: dist/core/WhompaysCoop). Online co-op by join code needs the
# Epic Online Services SDK (SR_EOS = folder with SDK\ and eos.ini, kept out of
# the repository); without it co-op works by direct IP only.
add_library(whompays_coop SHARED ../core/WhompaysCoop/coop.cpp)
target_include_directories(whompays_coop PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../modding/include)
target_compile_definitions(whompays_coop PRIVATE _CRT_SECURE_NO_WARNINGS)
target_link_libraries(whompays_coop PRIVATE rex::runtime ws2_32 user32)
target_compile_options(whompays_coop PRIVATE -msse4.1)
set(SR_EOS "" CACHE PATH "Epic Online Services folder (SDK\\ + eos.ini), optional")
if(SR_EOS AND EXISTS "${SR_EOS}/SDK/Include/eos_sdk.h")
    target_compile_definitions(whompays_coop PRIVATE WHOMPAYS_EOS)
    target_include_directories(whompays_coop PRIVATE ${SR_EOS}/SDK/Include)
endif()
set_target_properties(whompays_coop PROPERTIES OUTPUT_NAME "WhompaysCoop" PREFIX "")

# Keyboard/mouse button pictures: setup runs glyphgen on the player's own game
# files to make dist/kbm_ui.bin (see tools/glyphgen/README.md).
add_executable(glyphgen ../tools/glyphgen/glyphgen.cpp src/wml/packfile.cpp)
target_include_directories(glyphgen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/wml)
target_compile_definitions(glyphgen PRIVATE _CRT_SECURE_NO_WARNINGS)
target_link_libraries(glyphgen PRIVATE wml_zlib)
