cmake_minimum_required(VERSION 3.20)
set(PSXRECOMP_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/psxrecomp" CACHE PATH
    "Path to psxrecomp")
if(NOT EXISTS "${PSXRECOMP_ROOT}/runtime/runtime.cmake")
    message(FATAL_ERROR
        "PSXRecomp runtime is missing: ${PSXRECOMP_ROOT}/runtime/runtime.cmake\n"
        "This project expects the psxrecomp framework checkout at ${PSXRECOMP_ROOT}.\n"
        "Run: git submodule update --init --recursive\n"
        "If psxrecomp/ is not a submodule, set -DPSXRECOMP_ROOT=<path-to-psxrecomp>.")
endif()

project(BattleArenaToshindenRecomp LANGUAGES C CXX)

# This title exposes the shared launcher, setup workflow, and 1v1 netplay.
# Set before runtime.cmake so recomp-net and the lobby client are real targets.
set(PSX_NETPLAY ON CACHE BOOL "Enable Battle Arena Toshinden netplay" FORCE)
set(PSX_SETUP_WIZARD ON CACHE BOOL
    "Advertise first-run setup wizard + Generate & rebuild in recomp-ui" FORCE)
include("${PSXRECOMP_ROOT}/runtime/runtime.cmake")

file(GLOB TOSHINDEN_MOD_PLUGIN_SOURCES CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/src/mods/*.c")

psxrecomp_add_game_runtime(psx-runtime
    ENABLE_NETPLAY_IF_PRESENT
    NETPLAY_LOBBY_URL "ws://netplay.retcomm.net:8765"
    ENABLE_SETUP_WIZARD
    WINDOW_TITLE "BattleArenaToshinden Recompiled"
    DEFAULT_GAME_CONFIG_PATH "game.toml"
    MAX_PLAYERS 2
    LAUNCHER_BOXART "${CMAKE_CURRENT_SOURCE_DIR}/launcher_assets/img/boxart.tga"
    APP_ICON "${CMAKE_CURRENT_SOURCE_DIR}/assets/psxrecomp.ico"
    PRELOADED_MODS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mods/preloaded"
    EXCLUDE_BUILTIN_MODS
        psx.enhancement.cd-speed
        psx.enhancement.fast-loading
    GEN_MARKER "generated/SCUS_942.00_dispatch.c"
    GEN_FULL_GLOB "generated/SCUS_942.00_full_*.c"
    CODEGEN_SETUP_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/codegen_setup.c"
)

target_sources(psx-runtime PRIVATE ${TOSHINDEN_MOD_PLUGIN_SOURCES})

if(BUILD_TESTING)
    enable_testing()
    add_executable(toshinden_preloaded_mod_targets_test
        tests/test_preloaded_mod_targets.cpp
        psxrecomp/runtime/src/mod_packages.cpp
        psxrecomp/runtime/src/mod_media.cpp
        psxrecomp/runtime/src/iso_reader.cpp
        psxrecomp/runtime/src/cue_sheet.cpp
        psxrecomp/runtime/src/crc32.c
        psxrecomp/runtime/src/psx_sha256.c)
    target_include_directories(toshinden_preloaded_mod_targets_test PRIVATE
        psxrecomp/runtime/include
        psxrecomp/recompiler/include
        psxrecomp/recompiler/lib/toml11)
    target_link_libraries(toshinden_preloaded_mod_targets_test PRIVATE chdr-static)
    target_compile_definitions(toshinden_preloaded_mod_targets_test PRIVATE
        TOSHINDEN_SOURCE_ROOT="${CMAKE_CURRENT_SOURCE_DIR}")
    add_test(NAME toshinden_preloaded_mod_targets_test
             COMMAND toshinden_preloaded_mod_targets_test)
endif()

# Stage runtime configs next to the exe for direct build/ launches. Only copy
# files that exist; never fail POST_BUILD when game_options.toml is absent.
foreach(_PSX_RUNTIME_CONFIG game.toml game_options.toml)
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_PSX_RUNTIME_CONFIG}")
        add_custom_command(TARGET psx-runtime POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${CMAKE_CURRENT_SOURCE_DIR}/${_PSX_RUNTIME_CONFIG}"
                "$<TARGET_FILE_DIR:psx-runtime>/${_PSX_RUNTIME_CONFIG}"
            COMMENT "Staging ${_PSX_RUNTIME_CONFIG}"
            VERBATIM)
    endif()
endforeach()
