cmake_minimum_required(VERSION 3.5)
project(ISXMr VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set the project source directory
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)

# Set the project include directory
include_directories(${SOURCE_DIR})

file(GLOB_RECURSE SCRIPT_FILES "scripts/*.iss")
file(GLOB_RECURSE LGUI2_FILES "lgui2/*.json")

foreach (FILE ${SCRIPT_FILES})
    get_filename_component(FILE_NAME ${FILE} NAME)
    string(REGEX REPLACE "[^a-zA-Z0-9]" "_" FILE_IDENTIFIER ${FILE_NAME})
    set(HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/scripts/${FILE_NAME}.h")


    add_custom_command(
            OUTPUT ${HEADER_FILE}
            COMMENT "Generating ${HEADER_FILE} from ${FILE}"
            COMMAND xxd -i -n ${FILE_IDENTIFIER} ${FILE} ${HEADER_FILE}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            DEPENDS ${FILE}
    )

    list(APPEND GENERATED_HEADERS ${HEADER_FILE})
endforeach ()

foreach (FILE ${LGUI2_FILES})
    get_filename_component(FILE_NAME ${FILE} NAME)
    string(REGEX REPLACE "[^a-zA-Z0-9]" "_" FILE_IDENTIFIER ${FILE_NAME})
    set(HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/lgui2/${FILE_NAME}.h")
    file(TIMESTAMP "${FILE}" MODIFICATION_TIME "%Y-%m-%d %H:%M:%S")

    add_custom_command(
            OUTPUT ${HEADER_FILE}
            COMMENT "Generating ${HEADER_FILE} from ${FILE}"
            COMMAND ${CMAKE_COMMAND} -E remove ${HEADER_FILE}
            COMMAND xxd -i -n ${FILE_IDENTIFIER} ${FILE} ${HEADER_FILE}
            COMMAND ${CMAKE_COMMAND} -D IDENTIFIER:string="${FILE_IDENTIFIER}" -D HEADER_FILE:string="${HEADER_FILE}" -D MODIFICATION_TIME:string="${MODIFICATION_TIME}" -P ${CMAKE_CURRENT_SOURCE_DIR}/append_timestamp.cmake
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            DEPENDS ${FILE}
    )


    list(APPEND GENERATED_HEADERS ${HEADER_FILE})
endforeach ()
add_custom_target(GenerateHeaders ALL DEPENDS ${GENERATED_HEADERS})

add_library(ISXMr SHARED ${SOURCE_DIR}/ISXMr.cpp
        ${SOURCE_DIR}/Commands.cpp
        ${SOURCE_DIR}/Services.cpp
        ${SOURCE_DIR}/TopLevelObjects.cpp
        ${SOURCE_DIR}/Commands.h
        ${SOURCE_DIR}/DataTypeList.h
        ${SOURCE_DIR}/ISXMr.h
        ${SOURCE_DIR}/ISXMrServices.h
        ${SOURCE_DIR}/Services.h
        ${SOURCE_DIR}/TopLevelObjects.h
        src/isxeq2/Character.cpp
        src/isxeq2/Character.h
        src/isxeq2/Actor.cpp
        src/isxeq2/Actor.h
        src/isxeq2/LSObject.h
        src/WriteUIFileToDisk.cpp
        src/isxeq2/Point3f.h
        src/isxeq2/Ability.cpp
        src/isxeq2/Ability.h
        src/isxeq2/AbilityInfo.cpp
        src/isxeq2/AbilityInfo.h
        src/isxeq2/CharacterClass.h
        src/isxeq2/AbilityEffect.h
        libs/json/json.h
        src/Logger.h
        src/Commands/ExecutableCommand.h
        src/Commands/ExportCommand.cpp
        src/Commands/ExportCommand.h
        src/isxeq2/ExtensionTLOs.h
        src/Commands/CommandExecutor.cpp
        src/Commands/CommandExecutor.h)

IF (WIN32)
    cmake_host_system_information(
            RESULT InnerspacePath
            QUERY WINDOWS_REGISTRY
            "HKLM/SOFTWARE/WOW6432Node/Microsoft/Windows/CurrentVersion/App Paths/InnerSpace.exe"
            VALUE
            "PATH"
    )
    if (InnerspacePath)
        # Normalize the path (optional, but recommended)
        file(TO_CMAKE_PATH "${InnerspacePath}" NormalizedInnerspacePath)

        add_custom_command(TARGET ISXMr POST_BUILD
                COMMENT "Copying isxmr.dll to ${NormalizedInnerspacePath}"
                COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:ISXMr> "${NormalizedInnerspacePath}/ISXMr.dll"
        )
    endif ()
endif ()

# Set the path to additional libraries
set(LIBS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/)

# Find isxdk library
find_library(ISXDK_LIBRARY ISXDK HINTS ${LIBS_DIR}/isxdk/lib64/vs16)
if (NOT ISXDK_LIBRARY)
    message(FATAL_ERROR "isxdk library not found")
endif ()

find_library(ISUI_LIBRARY ISUI HINTS ${LIBS_DIR}/isxdk/lib64/vs16)
if (NOT ISUI_LIBRARY)
    message(FATAL_ERROR "isxui library not found")
endif ()

# Set include directories for isxdk
include_directories(${LIBS_DIR}/isxdk/include)
include_directories(${LIBS_DIR}/argh)

# Link ISXMr with isxdk library
target_link_libraries(ISXMr PRIVATE ${ISUI_LIBRARY} ${ISXDK_LIBRARY})

# Set the output directory for the shared library
set_target_properties(ISXMr PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin
)

set_property(TARGET ISXMr PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

IF (WIN32)
    cmake_host_system_information(
            RESULT InnerspacePath
            QUERY WINDOWS_REGISTRY
            "HKLM/SOFTWARE/WOW6432Node/Microsoft/Windows/CurrentVersion/App Paths/InnerSpace.exe"
            VALUE
            "PATH"
    )
    if (InnerspacePath)
        message(STATUS "Copying isxmr.dll to ${InnerspacePath}")
        # Normalize the path (optional, but recommended)
        file(TO_CMAKE_PATH "${InnerspacePath}/x64/Extensions/ISXDK35" NormalizedInnerspacePath)

        add_custom_command(TARGET ISXMr POST_BUILD
                COMMENT "Copying isxmr.dll to ${NormalizedInnerspacePath}"
                COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:ISXMr> "${NormalizedInnerspacePath}/ISXMr.dll"
        )
    endif ()
endif ()
