# ------------------------------
# cmake config / project
# ------------------------------
include(FetchContent)
cmake_minimum_required(VERSION 3.25.0)

set(CMAKE_CXX_STANDARD 20)
if(MSVC)
	set(CMAKE_CXX_FLAGS "/EHsc /D_AMD64_ /DWIN32_LEAN_AND_MEAN")
	set(CMAKE_CXX_FLAGS_RELEASE "/O2 /DNDEBUG")
else()
	set(CMAKE_CXX_FLAGS "-D_AMD64_ -DWIN32_LEAN_AND_MEAN")
	set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
endif()

project(hex-unlocked LANGUAGES CXX)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# ------------------------------
# options
# ------------------------------
option(UNLOCKER_DUMPER "Build with dumper" OFF)


if (UNLOCKER_DUMPER)
    if (NOT CMAKE_GENERATOR MATCHES "Visual Studio")
        message(FATAL_ERROR "Dumper is only compatible with MSVC")
    endif()
    enable_language(CSharp)
endif()

# ------------------------------
# msvc stuff
# ------------------------------
if(CMAKE_GENERATOR MATCHES "Visual Studio")
	string(APPEND CMAKE_CXX_FLAGS " /EHsc /DWIN32_LEAN_AND_MEAN /utf-8 /Zc:char8_t")
	if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
		string(APPEND CMAKE_CXX_FLAGS " /D_AMD64_")
	elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "Win32")
		string(APPEND CMAKE_CXX_FLAGS " /D_WIN32_")
	else()
		message(FATAL_ERROR "unhandled architecture")
	endif()
endif()

# ---------------------
# dependencies
# ---------------------
if (UNLOCKER_DUMPER)
    FetchContent_Declare(
        cue4parse
        GIT_REPOSITORY https://github.com/FabianFG/CUE4Parse
        GIT_TAG master
    )
    FetchContent_MakeAvailable(cue4parse)

    # ---------------------
    # CUE4Parse implementation bc importing just the .csproj has no compatibility with the native stuff 
    # ---------------------

    # native
    set(CUE4PARSE_NATIVE_DIR "${cue4parse_SOURCE_DIR}/CUE4Parse-Natives")
    add_subdirectory(${CUE4PARSE_NATIVE_DIR})

    include_external_msproject(
        CUE4Parse-Net 
        "${cue4parse_SOURCE_DIR}/CUE4Parse/CUE4Parse.csproj"
        TYPE "9A19103F-16F7-4668-BE54-9A1E7A4F7556"
        PLATFORM "Any CPU"
    )
    add_dependencies(CUE4Parse-Net CUE4Parse-Natives)
endif()

set(SEALLIB_ASSERT ON CACHE BOOL "" FORCE)
set(SEALLIB_LOG ON CACHE BOOL "" FORCE)
set(SEALLIB_EVENTS ON CACHE BOOL "" FORCE) # required for tinymitm
FetchContent_Declare(
    seallib
    GIT_REPOSITORY https://git.neru.rip/neru/seallib
    GIT_TAG main
)
FetchContent_MakeAvailable(seallib)

FetchContent_Declare(
    tinymitm
    GIT_REPOSITORY https://git.neru.rip/neru/tinymitm
    GIT_TAG main
)
FetchContent_MakeAvailable(tinymitm)

FetchContent_Declare(
    glaze
    GIT_REPOSITORY https://github.com/stephenberry/glaze
    GIT_TAG main
)
FetchContent_MakeAvailable(glaze)

# ---------------------
# warnings
# ---------------------
add_library(hex-warned INTERFACE)
if(CMAKE_GENERATOR MATCHES "Visual Studio")
	target_compile_options(hex-warned INTERFACE /W4 /WX)
else()
	target_compile_options(hex-warned INTERFACE -Wall -Wextra -Werror)
endif()

# ------------------------------
# dumper
# ------------------------------
if (UNLOCKER_DUMPER)
    include_external_msproject(hex-dumped "${CMAKE_CURRENT_SOURCE_DIR}/src/dumper/hex-dumped.csproj" TYPE "9A19103F-16F7-4668-BE54-9A1E7A4F7556")
    add_dependencies(hex-dumped CUE4Parse-Net)
endif()

# ------------------------------
# unlocker
# ------------------------------
file(GLOB_RECURSE UNLOCKER_SOURCES CONFIGURE_DEPENDS "src/unlocker/*.cpp" "src/unlocker/*.h")

add_executable(hex-unlocked ${UNLOCKER_SOURCES})
target_link_libraries(hex-unlocked PRIVATE dbd-unlocker-warnings seallib glaze::glaze tinymitm)

# rnd exe name
string(RANDOM LENGTH 12 ALPHABET "abcdefghijklmnopqrstuvwxyz0123456789" RANDOM_EXE_NAME)
set_target_properties(hex-unlocked PROPERTIES OUTPUT_NAME "${RANDOM_EXE_NAME}")

# compiler / IDE config
if(NOT MSVC)
    target_link_options(hex-unlocked PRIVATE -static -static-libgcc -static-libstdc++)
else()
    set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT hex-unlocked)
endif()

source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src/unlocker" FILES ${UNLOCKER_SOURCES})