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(TwistedMetal2Recomp LANGUAGES C CXX)

# --- Opt-in product surfaces (OFF by default in the framework) -------------
# Scaffold prompts / flags fill these. Without recomp-ui, PSX_RECOMP_UI is OFF.
# Setup-host CI requires WIZARD_CMAKE_BLOCK / ENABLE_SETUP_WIZARD (FORCE_SETUP_HOST
# alone does not open first-run / Generate & rebuild).
# recomp-ui submodule present (PSX_RECOMP_UI defaults ON).
# Full netplay UI -- enable after adding recomp-ui + testing:
# if(EXISTS "${PSXRECOMP_ROOT}/lib/recomp-net/CMakeLists.txt")
#     set(PSX_NETPLAY ON CACHE BOOL "..." FORCE)
# endif()
set(PSX_SETUP_WIZARD ON CACHE BOOL
    "Advertise first-run setup wizard + Generate & rebuild in recomp-ui" FORCE)
include("${PSXRECOMP_ROOT}/runtime/runtime.cmake")

# All custom code for this project lives under src/mods/ and is compiled into
# psx-runtime here. The recompiled game C in generated/ stays vanilla; the mod
# system decides at resolve time which of these plugins actually run.
file(GLOB TM2_MOD_SOURCES CONFIGURE_DEPENDS
     "${CMAKE_CURRENT_SOURCE_DIR}/src/mods/*.c")

psxrecomp_add_game_runtime(psx-runtime
    EXTRAS_SOURCES ${TM2_MOD_SOURCES}
    # ENABLE_NETPLAY_IF_PRESENT
    # NETPLAY_LOBBY_URL ws://netplay.retcomm.net:8765
    ENABLE_SETUP_WIZARD
    WINDOW_TITLE "TwistedMetal2 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"
    GEN_MARKER "generated/SCUS_943.06_dispatch.c"
    GEN_FULL_GLOB "generated/SCUS_943.06_full_*.c"
    CODEGEN_SETUP_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/codegen_setup.c"
)

# --- External debug GUI ----------------------------------------------------
# A separate process, deliberately: the cheat engine's callback runs on the
# emulation thread at guest VBlank, so an in-process panel would have to build
# a window and borrow the game's GL context from inside guest execution. This
# reuses the Dear ImGui and SDL3 already pulled in for recomp-ui, so it costs
# no new dependencies. Off by default -- it is a development tool.
option(TM2_BUILD_DEBUG_GUI "Build the external ImGui debug panel" OFF)
if(TM2_BUILD_DEBUG_GUI)
    set(TM2_IMGUI "${CMAKE_CURRENT_SOURCE_DIR}/recomp-ui/src/third_party/imgui")
    if(NOT EXISTS "${TM2_IMGUI}/imgui.cpp")
        message(FATAL_ERROR
            "TM2_BUILD_DEBUG_GUI needs the recomp-ui submodule (vendored ImGui) "
            "at ${TM2_IMGUI}. Run: git submodule update --init --recursive")
    endif()
    add_executable(tm2-debug-gui
        "${CMAKE_CURRENT_SOURCE_DIR}/tools/tm2_debug_gui/main.cpp"
        "${TM2_IMGUI}/imgui.cpp"
        "${TM2_IMGUI}/imgui_draw.cpp"
        "${TM2_IMGUI}/imgui_tables.cpp"
        "${TM2_IMGUI}/imgui_widgets.cpp"
        "${TM2_IMGUI}/backends/imgui_impl_sdl3.cpp"
        "${TM2_IMGUI}/backends/imgui_impl_opengl3.cpp")
    target_include_directories(tm2-debug-gui PRIVATE "${TM2_IMGUI}")
    target_link_libraries(tm2-debug-gui PRIVATE SDL3::SDL3)
    if(WIN32)
        target_link_libraries(tm2-debug-gui PRIVATE opengl32 ws2_32)
    else()
        find_package(OpenGL REQUIRED)
        target_link_libraries(tm2-debug-gui PRIVATE OpenGL::GL)
    endif()
    set_target_properties(tm2-debug-gui PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "$<TARGET_FILE_DIR:psx-runtime>")
endif()

# Stage this project's own mod catalog next to the executable. runtime.cmake
# stages the framework-owned packages first and merges rather than replaces, so
# this must run after it -- which it does, being registered later on the same
# target. Player-installed .psxmod archives and mods/state.toml are untouched.
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/mods/preloaded/packages")
    add_custom_command(TARGET psx-runtime POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
            "${CMAKE_CURRENT_SOURCE_DIR}/mods/preloaded"
            "$<TARGET_FILE_DIR:psx-runtime>/mods"
        COMMENT "Staging TwistedMetal2 mod catalog"
        VERBATIM)
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()
