# OpenZenLoader — the GUI injector.
#
# Pure Win32 + GDI+: no third-party UI toolkit, no vcpkg. Everything links
# against libraries that ship with the Windows SDK, so a fresh checkout
# configures and builds in seconds (the slow part is compiling the injected
# DLL, not this target).

# Stage the freshly-built OpenZen.dll into the loader's binary dir so rc.exe
# can pick it up as an RCDATA resource (embedded in the .exe).
set(EMBED_DIR "${CMAKE_CURRENT_BINARY_DIR}/embedded_dll")
add_custom_command(
    OUTPUT  "${EMBED_DIR}/OpenZen.dll"
    COMMAND ${CMAKE_COMMAND} -E make_directory "${EMBED_DIR}"
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:OpenZen> "${EMBED_DIR}/OpenZen.dll"
    DEPENDS OpenZen
    COMMENT "Staging OpenZen.dll into loader resources"
)
add_custom_target(stage_dll_for_loader DEPENDS "${EMBED_DIR}/OpenZen.dll")

add_executable(OpenZenLoader WIN32
    src/main.cpp
    src/cli.h
    src/cli.cpp
    src/wgfx.h
    src/wgfx.cpp
    src/splash_win.h
    src/splash_win.cpp
    src/main_win.h
    src/main_win.cpp
    src/overlay_win.h
    src/overlay_win.cpp
    src/process_list.cpp
    src/injector.cpp
    src/manual_map.cpp
    src/embedded_dll.cpp
    src/window_title.cpp
    res/loader.rc
)

add_dependencies(OpenZenLoader stage_dll_for_loader)

target_include_directories(OpenZenLoader PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/res
)

# Tell rc.exe to look in EMBED_DIR (where OpenZen.dll was staged) when
# resolving the RCDATA "OpenZen.dll" reference in loader.rc.
set_source_files_properties(res/loader.rc PROPERTIES
    COMPILE_FLAGS "/I\"${EMBED_DIR}\""
)

target_link_libraries(OpenZenLoader PRIVATE
    gdiplus    # owner-drawn UI rendering
    dwmapi     # DwmSetWindowAttribute for Win11 rounded corners
    psapi
    shlwapi
    shell32    # CommandLineToArgvW for the --nogui CLI mode
    user32
    gdi32
)

# Build-time git revision passed in by Gradle (gradle.properties /
# CI). Stamped into the window title so a built loader can be
# traced back to its source commit.
if(DEFINED OPENZEN_BUILD_REVISION AND NOT OPENZEN_BUILD_REVISION STREQUAL "")
    target_compile_definitions(OpenZenLoader PRIVATE
        OPENZEN_BUILD_REVISION="${OPENZEN_BUILD_REVISION}")
endif()

set_target_properties(OpenZenLoader PROPERTIES
    OUTPUT_NAME "OpenZenLoader"
)

if(MSVC)
    # requireAdministrator: lets the loader OpenProcess / VirtualAllocEx /
    # CreateRemoteThread against javaw.exe even when the launcher was
    # itself started elevated. UAC consent prompts once on launch.
    set_target_properties(OpenZenLoader PROPERTIES
        LINK_FLAGS "/SUBSYSTEM:WINDOWS /MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\""
    )
endif()
