cmake_minimum_required(VERSION 3.20)
project(DevourUnlocker VERSION 2.0.0 LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

option(BUILD_WINHTTP_PROXY "Build winhttp.dll proxy (Windows/Proton)" ON)
option(BUILD_TESTS "Build tests" OFF)

# --- Dependencies ---
include(FetchContent)

# MinHook (Windows hooking) — correct owner is TsudaKageyu
if(WIN32)
  FetchContent_Declare(minhook
    GIT_REPOSITORY https://github.com/TsudaKageyu/minhook.git
    GIT_TAG master
  )
  FetchContent_MakeAvailable(minhook)
endif()

# Dear ImGui
FetchContent_Declare(imgui
  GIT_REPOSITORY https://github.com/ocornut/imgui.git
  GIT_TAG v1.90.4
)
FetchContent_MakeAvailable(imgui)

add_library(imgui STATIC
  ${imgui_SOURCE_DIR}/imgui.cpp
  ${imgui_SOURCE_DIR}/imgui_draw.cpp
  ${imgui_SOURCE_DIR}/imgui_tables.cpp
  ${imgui_SOURCE_DIR}/imgui_widgets.cpp
  ${imgui_SOURCE_DIR}/imgui_demo.cpp
)
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)

# ImGui backends (platform-specific sources added per-target)

# --- Core static lib: resolver + patches + gui + config ---
add_library(devour_core STATIC
  src/il2cpp_resolver.cpp
  src/patches.cpp
  src/config.cpp
  src/gui/overlay.cpp
  src/platform/common.cpp
)

target_include_directories(devour_core PUBLIC src ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)
target_link_libraries(devour_core PUBLIC imgui)

if(WIN32)
  target_sources(devour_core PRIVATE
    src/platform/win32.cpp
    ${imgui_SOURCE_DIR}/backends/imgui_impl_win32.cpp
    ${imgui_SOURCE_DIR}/backends/imgui_impl_dx11.cpp
  )
  target_link_libraries(devour_core PUBLIC d3d11 dxgi minhook dwmapi d3dcompiler)
  target_compile_definitions(devour_core PUBLIC DEVOUR_WINDOWS=1)
else()
  # Linux build (for testing + .so plugin). Under Proton the Windows DLL is used.
  # Keep Linux build dependency-free (no SDL required) — native .so is not primary.
  target_sources(devour_core PRIVATE src/platform/linux.cpp)
  target_compile_definitions(devour_core PUBLIC DEVOUR_LINUX=1)
endif()

# --- winhttp.dll proxy (Windows) ---
if(BUILD_WINHTTP_PROXY AND WIN32)
  add_library(winhttp SHARED
    src/platform/winhttp_proxy.cpp
    src/main.cpp
    src/platform/winhttp_exports.def
  )
  target_link_libraries(winhttp PRIVATE devour_core)
  set_target_properties(winhttp PROPERTIES OUTPUT_NAME "winhttp" PREFIX "" SUFFIX ".dll")
  # Ensure exports forwarding — actual winhttp forwarded at runtime via LoadLibrary
  target_compile_definitions(winhttp PRIVATE WINHTTP_PROXY=1)

  add_library(DevourUnlocker SHARED src/main.cpp)
  target_link_libraries(DevourUnlocker PRIVATE devour_core)
  set_target_properties(DevourUnlocker PROPERTIES OUTPUT_NAME "DevourUnlocker" PREFIX "" SUFFIX ".dll")
endif()

# Linux .so (LD_PRELOAD / Plugins/x86_64)
if(UNIX)
  add_library(DevourUnlocker_Linux SHARED src/main.cpp)
  target_link_libraries(DevourUnlocker_Linux PRIVATE devour_core)
  set_target_properties(DevourUnlocker_Linux PROPERTIES OUTPUT_NAME "DevourUnlocker" PREFIX "" SUFFIX ".so")
endif()

if(BUILD_TESTS)
  enable_testing()
  add_subdirectory(tests)
endif()
