cmake_minimum_required(VERSION 3.20)
project(ValkyriaChronicles3 VERSION 0.2.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_subdirectory(portablekit)

# VC3's generated code (generated/, made from the player's own Japanese
# executable; never committed) is patched in place at configure time, once:
# step and frame sites (high frame rate), the morale gauge, level of detail
# and the Modern controls' camera, aim and cursor sites, at VC3's own measured
# addresses (cmake/vc3_sites.cmake).
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/vc3_sites.cmake")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/generated/generated_registry.cpp")
    vc3_patch_step_sites("${CMAKE_CURRENT_SOURCE_DIR}/generated")
    vc3_patch_morale_sites("${CMAKE_CURRENT_SOURCE_DIR}/generated")
    vc3_patch_lod_site("${CMAKE_CURRENT_SOURCE_DIR}/generated")
    vc3_patch_camera_sites("${CMAKE_CURRENT_SOURCE_DIR}/generated")
    vc3_patch_audio_sites("${CMAKE_CURRENT_SOURCE_DIR}/generated")
    vc3_patch_title_sites("${CMAKE_CURRENT_SOURCE_DIR}/generated")
    vc3_patch_options_rows("${CMAKE_CURRENT_SOURCE_DIR}/generated")
    vc3_patch_glyph_sites("${CMAKE_CURRENT_SOURCE_DIR}/generated")
endif()
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/vc3_sites.cmake")

# The game. Its host code is VC2's (profiles/vc2/host in psprecomp-upstream,
# renamed vc3_*), running on VC3's address table (host/vc3_region.cpp). The
# English ReTranslation runs on the same corpus (ProfileVariant::
# shared_code_sha256), so there is no corpus library for it.
portablekit_add_game(VC3Native
    PROFILE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
    SOURCES
        host/vc3_profile.cpp host/vc3_region.cpp host/vc3_native_gu.cpp host/vc3_font.cpp
        host/vc3_tick.cpp host/vc3_census.cpp host/vc3_lod.cpp
        host/vc3_settings.cpp host/vc3_settings_menu.cpp
        host/vc3_camera.cpp host/vc3_controls.cpp host/vc3_glyphs.cpp host/vc3_text.cpp
        host/vc3_defaults.cpp host/vc3_discord.cpp host/vc3_audio.cpp host/vc3_extras.cpp)

# 100% native, as VC2's build (owner, 2026-11-16: VC3's native rendering
# confirmed): every GU entry point, draw and display list is handled on the
# host, and PortableKit's GE emulation (command decoding, primitives, patches,
# the list runner) is left out of the build. VC3_NATIVE=0, VC3_NATIVE_CHECK,
# VC3_NATIVE_GU / VC3_NATIVE_DRAW / VC3_NATIVE_LISTS no longer switch anything.
target_compile_definitions(VC3Native PRIVATE PORTABLEKIT_NO_GE_EMULATION=1 VC3_NATIVE_ONLY=1)

set_target_properties(VC3Native PROPERTIES OUTPUT_NAME "Valkyria Chronicles 3")

# The executable's icon (Explorer, the taskbar, Alt-Tab and the game window,
# which SDL takes from the executable): resources/vc3.ico as icon 1, as VC2's
# vc2native.ico. Compiled by windres on its own in the build folder, with
# names free of spaces: windres hands the target's include folders to the
# preprocessor unquoted, and "Lanseal Academy" broke them (2026-11-16).
set(VC3_ICON "${CMAKE_CURRENT_SOURCE_DIR}/resources/vc3.ico")
get_filename_component(VC3_COMPILER_BIN "${CMAKE_CXX_COMPILER}" DIRECTORY)
find_program(VC3_WINDRES NAMES windres x86_64-w64-mingw32-windres HINTS "${VC3_COMPILER_BIN}")
if(WIN32 AND EXISTS "${VC3_ICON}" AND VC3_WINDRES)
    set(VC3_ICON_OBJECT "${CMAKE_CURRENT_BINARY_DIR}/vc3_icon_res.o")
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/vc3_icon.rc" "// Generated by CMakeLists.txt: the game's icon.\n1 ICON \"vc3.ico\"\n")
    add_custom_command(OUTPUT "${VC3_ICON_OBJECT}"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "${VC3_ICON}" vc3.ico
        COMMAND "${VC3_WINDRES}" -O coff -i vc3_icon.rc -o vc3_icon_res.o
        DEPENDS "${VC3_ICON}" "${CMAKE_CURRENT_BINARY_DIR}/vc3_icon.rc"
        WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
        COMMENT "VC3 icon"
        VERBATIM)
    set_source_files_properties("${VC3_ICON_OBJECT}" PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE)
    target_sources(VC3Native PRIVATE "${VC3_ICON_OBJECT}")
    message(STATUS "VC3 icon: ${VC3_ICON}")
elseif(WIN32)
    message(STATUS "VC3 icon: ${VC3_ICON} or windres not found; the executable has no icon of its own")
endif()

# vc3_font.cpp draws text glyphs with Windows GDI.
if(WIN32)
    target_link_libraries(VC3Native PRIVATE gdi32)
endif()

# The MinGW runtime beside the executable, so the game starts on a PC without
# MinGW installed (GCC Runtime Library Exception).
if(MINGW)
    get_filename_component(VC3_MINGW_BIN "${CMAKE_CXX_COMPILER}" DIRECTORY)
    foreach(dll libgcc_s_seh-1.dll libstdc++-6.dll libwinpthread-1.dll)
        if(EXISTS "${VC3_MINGW_BIN}/${dll}")
            add_custom_command(TARGET VC3Native POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different "${VC3_MINGW_BIN}/${dll}" "$<TARGET_FILE_DIR:VC3Native>/${dll}"
                VERBATIM)
        endif()
    endforeach()
endif()

# A small local preparation tool (Codex): decrypts the measured EBOOT with
# PortableKit's EBOOT decryptor and prints the prepared ELF's hash.
set(VC3_PROBE_SOURCES
    tools/vc3_identity_probe.cpp
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/host/install/executable_preparation.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/host/profile.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/host/crypto_keys_builtin.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/host/save_data/aes128.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/third_party/tiny_aes/aes.c")
set_source_files_properties(
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/third_party/tiny_aes/aes.c"
    PROPERTIES LANGUAGE CXX)
add_executable(vc3_identity_probe EXCLUDE_FROM_ALL ${VC3_PROBE_SOURCES})
target_compile_features(vc3_identity_probe PRIVATE cxx_std_20)
target_include_directories(vc3_identity_probe PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/host"
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/third_party"
    "${CMAKE_CURRENT_SOURCE_DIR}/portablekit/include")
target_link_libraries(vc3_identity_probe PRIVATE psprecomp_core)
