# Start of tes3mp addition
#
# The content updater and the crash reporter's files, built as one static library in their own
# directory scope. They are the same sources the standalone launcher (apps/siltstrider)
# compiles, and they are plain Win32: UpdateScreen.cpp draws with GDI+, CrashWindow.cpp opens
# native windows, every wide call is spelled out with its W. 0.51's root CMakeLists defines
# NOMB, NOGDI, UNICODE and _UNICODE for every Windows target; they are taken back here only, so
# the engine keeps them and these files compile as they do in the launcher.
if (WIN32)
    get_directory_property(SILT_DIR_DEFS COMPILE_DEFINITIONS)
    list(REMOVE_ITEM SILT_DIR_DEFS NOMB NOGDI UNICODE _UNICODE)
    set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "${SILT_DIR_DEFS}")
endif ()

set(TES3MP_CONTENT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(TES3MP_MWMP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..)

# Every file is platform-guarded internally, so listing the *Posix files everywhere is safe:
# each compiles empty on the other platform.
add_library(tes3mp-content STATIC
    ${TES3MP_CONTENT_DIR}/Crypto.cpp
    ${TES3MP_CONTENT_DIR}/CryptoPosix.cpp
    ${TES3MP_CONTENT_DIR}/DiscordAuth.cpp
    ${TES3MP_CONTENT_DIR}/Http.cpp
    ${TES3MP_CONTENT_DIR}/HttpPosix.cpp
    ${TES3MP_CONTENT_DIR}/Json.cpp
    ${TES3MP_CONTENT_DIR}/Morrowind.cpp
    ${TES3MP_CONTENT_DIR}/MorrowindPosix.cpp
    ${TES3MP_CONTENT_DIR}/Sync.cpp
    ${TES3MP_CONTENT_DIR}/Text.cpp
    ${TES3MP_CONTENT_DIR}/TextPosix.cpp
    ${TES3MP_CONTENT_DIR}/UpdateScreen.cpp
    # the crash and freeze reporter (CR1, CR2)
    ${TES3MP_MWMP_DIR}/CrashSession.cpp
    ${TES3MP_MWMP_DIR}/CrashReport.cpp
    ${TES3MP_MWMP_DIR}/CrashUpload.cpp
    ${TES3MP_MWMP_DIR}/CrashWindow.cpp
    ${TES3MP_MWMP_DIR}/CrashFlow.cpp
    ${TES3MP_MWMP_DIR}/ClientPlatform.cpp
    # the bug report window's OS half (-): plain Win32, no engine types
    ${TES3MP_MWMP_DIR}/GUI/BugReportPlatform.cpp
)

target_include_directories(tes3mp-content PUBLIC
    ${TES3MP_CONTENT_DIR}
    ${TES3MP_MWMP_DIR}
    ${OpenMW_SOURCE_DIR}/apps/openmw
    ${OpenMW_BINARY_DIR}) # SiltVersions.hpp

# SDL2 for the launcher window, zlib for CrashReport's gzip.
target_link_libraries(tes3mp-content PUBLIC SDL2::SDL2 ZLIB::ZLIB)

# SiltVersions.hpp (the engine and launcher revision stamps) is generated by the silt-version
# target, defined in apps/openmw/CMakeLists.txt.
if (TARGET silt-version)
    add_dependencies(tes3mp-content silt-version)
endif ()

if (WIN32)
    # bcrypt and crypt32: signature and hash checks. winhttp: transport. ws2_32: the sign-in's
    # loopback listener. gdiplus: the backdrop. They ship with the OS.
    target_link_libraries(tes3mp-content PUBLIC bcrypt crypt32 winhttp gdiplus ws2_32 shell32)
endif ()

# The POSIX ports: libcurl for transport (transparent gzip included, load-bearing, see
# HttpPosix.cpp), OpenSSL for hashing, ECDSA and the certificate pin, FreeType for the launcher's
# text, libpng and libjpeg for its art. libcurl must be the OpenSSL flavour for the pin.
if (UNIX AND NOT APPLE)
    find_package(CURL REQUIRED)
    find_package(OpenSSL REQUIRED)
    find_package(Freetype REQUIRED)
    find_package(PNG REQUIRED)
    find_package(JPEG REQUIRED)
    target_include_directories(tes3mp-content PUBLIC
        ${CURL_INCLUDE_DIRS} ${OPENSSL_INCLUDE_DIR} ${FREETYPE_INCLUDE_DIRS}
        ${PNG_INCLUDE_DIRS} ${JPEG_INCLUDE_DIRS})
    target_link_libraries(tes3mp-content PUBLIC
        ${CURL_LIBRARIES} ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY}
        ${FREETYPE_LIBRARIES} ${PNG_LIBRARIES} ${JPEG_LIBRARIES})
endif ()
# End of tes3mp addition
