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

set(CMAKE_CXX_STANDARD 20)


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

enable_language(RC)

if(CMAKE_GENERATOR MATCHES "Visual Studio")
    add_compile_options(/EHsc /utf-8 /Zc:char8_t)
else()
	add_compile_options(-m64)
    add_link_options(-static -static-libgcc -static-libstdc++)
endif()

# ------------------------------
# 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")
	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})

    # net
    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)

    # conv
    include_external_msproject(
        CUE4Parse-Conversion 
        "${cue4parse_SOURCE_DIR}/CUE4Parse-Conversion/CUE4Parse-Conversion.csproj"
        TYPE "9A19103F-16F7-4668-BE54-9A1E7A4F7556"
        PLATFORM "Any CPU"
    )
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)

set(USE_ZLIB OFF CACHE BOOL "" FORCE)
FetchContent_Declare(
    ixwebsocket
    GIT_REPOSITORY https://github.com/machinezone/IXWebSocket
    GIT_TAG master
)
FetchContent_MakeAvailable(ixwebsocket)
# ---------------------
# 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 CUE4Parse-Conversion)
endif()

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

add_executable(hex-unlocked WIN32 ${UNLOCKER_SOURCES} "res/resources.rc")
target_compile_definitions(hex-unlocked PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_link_libraries(hex-unlocked PRIVATE hex-warned seallib glaze::glaze tinymitm wininet ixwebsocket crypt32 user32 shell32)

# 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++)
    if(CMAKE_BUILD_TYPE MATCHES "Release|MinSizeRel")
        target_link_options(hex-unlocked PRIVATE -s)
    endif()
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})