# Start of tes3mp addition
#
# Wire-format dump: builds wire_dump.cpp against a source tree's components/openmw-mp and
# runs it. See wire_dump.cpp and run.sh. Stand-alone on purpose (it builds in seconds, not
# with the engine), and the SAME source builds against either tree:
#
#   -DWIRE_FLAVOUR=OLD  -DMP_TREE=<siltstrider fd2785706f checkout/archive>
#       components/openmw-mp plus the ESM sources its headers need (the ESM:: record structs)
#   -DWIRE_FLAVOUR=NEW  -DMP_TREE=<this branch>
#       components/openmw-mp alone (no ESM)
#
# Other inputs: -DRAKNET_ROOT=<dir with include/raknet and lib/RakNetLibStatic.lib>,
# -DBOOST_INCLUDE_DIR=<dir with boost/>, and for OLD -DOSG_INCLUDE_DIR=<dir with osg/>
# (the old ESM headers include osg/Vec3f, as the 0.8.1 layer did).
cmake_minimum_required(VERSION 3.16)
project(WireDump CXX)

set(WIRE_FLAVOUR "NEW" CACHE STRING "OLD (ESM structs) or NEW (wire structs)")
set(MP_TREE "" CACHE PATH "Source tree that has components/openmw-mp")
set(RAKNET_ROOT "" CACHE PATH "RakNet install: include/raknet and lib/RakNetLibStatic.lib")
set(BOOST_INCLUDE_DIR "" CACHE PATH "Directory that contains boost/")
set(OSG_INCLUDE_DIR "" CACHE PATH "OLD only: directory that contains osg/")
set(BOOST_FILESYSTEM_LIB "" CACHE FILEPATH "OLD only: boost_filesystem import library (the old Utils.cpp uses boost::filesystem)")

if (NOT MP_TREE OR NOT RAKNET_ROOT OR NOT BOOST_INCLUDE_DIR)
    message(FATAL_ERROR "Set MP_TREE, RAKNET_ROOT and BOOST_INCLUDE_DIR")
endif ()

file(GLOB_RECURSE WIRE_SOURCES CONFIGURE_DEPENDS ${MP_TREE}/components/openmw-mp/*.cpp)
# Not compiled by any build of the tree (includes a header that does not exist).
list(REMOVE_ITEM WIRE_SOURCES ${MP_TREE}/components/openmw-mp/Packets/Player/PacketPlayerRegionAuthority.cpp)

set(EXTRA_SOURCES)
if (WIRE_FLAVOUR STREQUAL "OLD")
    # The ESM:: structs the old packets use have a few out-of-line constructors and the like.
    # (their reader and writer pull in to_utf8; nothing else of the engine is needed).
    file(GLOB OLD_ESM_SOURCES
        ${MP_TREE}/components/esm/npcstats.cpp
        ${MP_TREE}/components/esm/statstate.cpp
        ${MP_TREE}/components/esm/variant.cpp
        ${MP_TREE}/components/esm/aisequence.cpp
        ${MP_TREE}/components/esm/esmreader.cpp
        ${MP_TREE}/components/esm/esmwriter.cpp
        ${MP_TREE}/components/esm/variantimp.cpp
        ${MP_TREE}/components/esm/loadtes3.cpp
        ${MP_TREE}/components/debug/debuglog.cpp
        ${MP_TREE}/components/to_utf8/to_utf8.cpp)
    set(EXTRA_SOURCES ${OLD_ESM_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/old_stubs.cpp)
endif ()

add_executable(wire_dump wire_dump.cpp ${WIRE_SOURCES} ${EXTRA_SOURCES})
target_compile_features(wire_dump PRIVATE cxx_std_20)
target_include_directories(wire_dump PRIVATE
    ${MP_TREE}
    ${RAKNET_ROOT}/include
    ${RAKNET_ROOT}/include/raknet
    ${BOOST_INCLUDE_DIR}
)
if (WIRE_FLAVOUR STREQUAL "OLD")
    target_include_directories(wire_dump PRIVATE ${OSG_INCLUDE_DIR})
    target_compile_definitions(wire_dump PRIVATE WIRE_FLAVOUR_OLD=1)
    target_link_libraries(wire_dump PRIVATE ${BOOST_FILESYSTEM_LIB})
endif ()
target_compile_definitions(wire_dump PRIVATE _USE_MATH_DEFINES _CRT_SECURE_NO_WARNINGS NOMINMAX)
target_link_libraries(wire_dump PRIVATE ${RAKNET_ROOT}/lib/RakNetLibStatic.lib ws2_32)
# End of tes3mp addition
