# svr2009 - ReXGlue Recompiled Project
#
# SDK boilerplate lives in generated/rexglue.cmake.

cmake_minimum_required(VERSION 3.25)
# The release version (shown in the in-game settings menu; package_release.ps1 uses it too).
project(svr2009 VERSION 2.1.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(generated/rexglue.cmake)

# Sources
set(SVR2009_SOURCES
    src/main.cpp
    src/fps_hooks.cpp
    src/game_locator.cpp
    src/input_script.cpp
    src/timer_resolution.cpp
    src/loading_hooks.cpp
    src/widescreen.cpp
    src/updater.cpp
    src/nobk_online.cpp
    src/net_trace.cpp
)

if(WIN32)
    add_executable(svr2009 WIN32 ${SVR2009_SOURCES})
else()
    add_executable(svr2009 ${SVR2009_SOURCES})
endif()

rexglue_setup_target(svr2009 GPU_PLUGINS xenos)
target_compile_definitions(svr2009 PRIVATE SVR_VERSION="${PROJECT_VERSION}")
if(WIN32)
    # Game file picker (src/game_locator.cpp).
    target_link_libraries(svr2009 PRIVATE comdlg32)
    # 1 ms timer resolution (src/timer_resolution.cpp).
    target_link_libraries(svr2009 PRIVATE winmm)
    # Update check and download (src/updater.cpp).
    target_link_libraries(svr2009 PRIVATE winhttp)
    # NOBK Online sign-in token, encrypted for the Windows user (src/nobk_online.cpp).
    target_link_libraries(svr2009 PRIVATE crypt32)
endif()
# The updater reads GitHub's release info with nlohmann/json and unpacks the release zip with
# stb_image's zlib decoder, both header-only and already in the SDK's third-party tree.
target_include_directories(svr2009 PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/third_party/rexglue-sdk/thirdparty/inja/third_party/include
    ${CMAKE_CURRENT_SOURCE_DIR}/third_party/rexglue-sdk/thirdparty/stb)
# fps_hooks.cpp uses the header-only guest memory helpers (core/memory_helpers.h) in every build,
# including the shader-dump one without the native renderer.
target_include_directories(svr2009 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/reblue)

# Developer call census of the D3D library functions, for native
# renderer reverse engineering. Off by default; never ship a build with it on.
option(SVR_D3D_CENSUS "Count calls to the game's D3D library functions" OFF)
# Native renderer (cmake/native_renderer.cmake). Its hooks override the
# same D3D functions the census wraps, so the two are exclusive.
option(SVR_NATIVE_RENDERER "Draw through plume instead of emulating the Xenos GPU" OFF)
if(SVR_NATIVE_RENDERER AND SVR_D3D_CENSUS)
    message(FATAL_ERROR "SVR_NATIVE_RENDERER and SVR_D3D_CENSUS hook the same functions; pick one")
endif()
if(SVR_D3D_CENSUS)
    target_sources(svr2009 PRIVATE src/dev/d3d_census.cpp src/dev/shader_dump.cpp)
endif()
if(SVR_NATIVE_RENDERER)
    include(cmake/native_renderer.cmake)
    svr_add_native_renderer(svr2009)
endif()

# Overlay fonts (Press Start 2P for the FPS counter, SIL OFL 1.1) next to the executable.
add_custom_command(TARGET svr2009 POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            "${CMAKE_CURRENT_SOURCE_DIR}/res/fonts" "$<TARGET_FILE_DIR:svr2009>/fonts")
