# Network-only native bridge: shadPS4 v.0.20.0's network, NP and shadNet sources
# (529ceb32f5546b820588f194b903cc39d08015fe).
# The renderer and the existing shared runtime shims stay authoritative.
find_package(Protobuf CONFIG REQUIRED GLOBAL)
find_package(nlohmann_json CONFIG REQUIRED GLOBAL)
find_package(OpenSSL REQUIRED GLOBAL)
find_package(ZLIB REQUIRED GLOBAL)
set(BBNET_VENDOR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/shadps4)
set(BBNET_PROTO_OUT ${CMAKE_CURRENT_BINARY_DIR}/proto)
file(MAKE_DIRECTORY ${BBNET_PROTO_OUT})
add_custom_command(
    OUTPUT ${BBNET_PROTO_OUT}/shadnet.pb.cc ${BBNET_PROTO_OUT}/shadnet.pb.h
    COMMAND protobuf::protoc --proto_path=${BBNET_VENDOR}/shadnet
            --cpp_out=${BBNET_PROTO_OUT} ${BBNET_VENDOR}/shadnet/shadnet.proto
    DEPENDS ${BBNET_VENDOR}/shadnet/shadnet.proto)
file(GLOB_RECURSE BBNET_SOURCES CONFIGURE_DEPENDS
    vendor/shadps4/core/libraries/net/*.cpp vendor/shadps4/core/libraries/network/*.cpp
    vendor/shadps4/core/libraries/np/*.cpp vendor/shadps4/core/net/*.cpp
    vendor/shadps4/shadnet/*.cpp)
file(GLOB BBNET_THD_SOURCES CONFIGURE_DEPENDS thd/*.cpp)
file(GLOB BBNET_ROOM_SOURCES CONFIGURE_DEPENDS room/*.cpp)
add_library(bbnet STATIC ${BBNET_SOURCES} ${BBNET_THD_SOURCES} ${BBNET_ROOM_SOURCES} ${BBNET_PROTO_OUT}/shadnet.pb.cc bbnet_glue.cpp)
target_include_directories(bbnet PRIVATE include ../shim ${BBNET_VENDOR}
    ${BBNET_VENDOR}/shadnet ../shadps4 ${BBNET_PROTO_OUT} ../third_party/imgui third_party/httplib)
target_compile_definitions(bbnet PRIVATE BOOST_ASIO_STANDALONE NDEBUG
    CPPHTTPLIB_OPENSSL_SUPPORT CPPHTTPLIB_ZLIB_SUPPORT)
target_compile_options(bbnet PRIVATE -Wno-deprecated-declarations -Wno-missing-field-initializers)
target_link_libraries(bbnet PUBLIC Vulkan::Headers fmt::fmt Boost::headers magic_enum::magic_enum
    protobuf::libprotobuf nlohmann_json::nlohmann_json OpenSSL::SSL OpenSSL::Crypto ZLIB::ZLIB)
# miniupnpc, static (MSYS2's on Windows; packaging/linux/setup-ubuntu.sh builds it into /opt/deps).
find_path(BBNET_MINIUPNPC_INCLUDE miniupnpc.h PATH_SUFFIXES miniupnpc REQUIRED)
find_library(BBNET_MINIUPNPC_LIBRARY NAMES libminiupnpc.a miniupnpc REQUIRED)
target_include_directories(bbnet PRIVATE ${BBNET_MINIUPNPC_INCLUDE})
target_compile_definitions(bbnet PRIVATE MINIUPNP_STATICLIB)
target_link_libraries(bbnet PUBLIC ${BBNET_MINIUPNPC_LIBRARY})
if(WIN32)
    target_sources(bbnet PRIVATE third_party/wepoll/wepoll.c)
    target_include_directories(bbnet PRIVATE third_party/wepoll)
    target_compile_definitions(bbnet PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
    target_compile_options(bbnet PRIVATE
        $<$<COMPILE_LANGUAGE:CXX>:-include ${CMAKE_CURRENT_SOURCE_DIR}/include/bb_win_compat.h>)
    target_link_libraries(bbnet PUBLIC ws2_32 iphlpapi bcrypt crypt32)
endif()
add_executable(ip-mask-test EXCLUDE_FROM_ALL ../../tests/test_ip_mask.cpp)
target_compile_options(ip-mask-test PRIVATE -UNDEBUG -Wall -Wextra -Werror)
target_include_directories(ip-mask-test PRIVATE ../..)
add_executable(room-announce-test EXCLUDE_FROM_ALL ../../tests/test_room_announce.cpp ${BBNET_ROOM_SOURCES})
target_compile_options(room-announce-test PRIVATE -UNDEBUG -Wall -Wextra)
target_include_directories(room-announce-test PRIVATE ../.. third_party/httplib)
target_compile_definitions(room-announce-test PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
target_link_libraries(room-announce-test PRIVATE nlohmann_json::nlohmann_json OpenSSL::SSL OpenSSL::Crypto)
if(WIN32)
    target_compile_definitions(room-announce-test PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
    target_link_libraries(room-announce-test PRIVATE ws2_32 crypt32)
endif()
add_executable(voice-gate-test EXCLUDE_FROM_ALL ../../tests/test_voice_gate.cpp)
target_compile_options(voice-gate-test PRIVATE -UNDEBUG -Wall -Wextra)
target_include_directories(voice-gate-test PRIVATE ../..)
add_executable(room-link-bench EXCLUDE_FROM_ALL ../../tests/room_link_bench.cpp ${BBNET_ROOM_SOURCES})
target_compile_options(room-link-bench PRIVATE -Wall -Wextra)
target_include_directories(room-link-bench PRIVATE ../.. third_party/httplib)
target_compile_definitions(room-link-bench PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT ROOM_BENCH_GAME_SEND)
target_link_libraries(room-link-bench PRIVATE nlohmann_json::nlohmann_json OpenSSL::SSL OpenSSL::Crypto)
if(WIN32)
    target_compile_definitions(room-link-bench PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
    target_link_libraries(room-link-bench PRIVATE ws2_32 crypt32)
endif()
add_executable(room-link-test EXCLUDE_FROM_ALL ../../tests/test_room_link.cpp ${BBNET_ROOM_SOURCES})
target_compile_options(room-link-test PRIVATE -UNDEBUG -Wall -Wextra)
target_include_directories(room-link-test PRIVATE ../.. third_party/httplib)
target_compile_definitions(room-link-test PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
target_link_libraries(room-link-test PRIVATE nlohmann_json::nlohmann_json OpenSSL::SSL OpenSSL::Crypto)
add_executable(room-host-test EXCLUDE_FROM_ALL ../../tests/test_room_host.cpp ${BBNET_ROOM_SOURCES})
target_compile_options(room-host-test PRIVATE -UNDEBUG -Wall -Wextra)
target_include_directories(room-host-test PRIVATE ../.. third_party/httplib)
target_compile_definitions(room-host-test PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
target_link_libraries(room-host-test PRIVATE nlohmann_json::nlohmann_json OpenSSL::SSL OpenSSL::Crypto)
if(WIN32)
    target_compile_definitions(room-host-test PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
    target_link_libraries(room-host-test PRIVATE ws2_32 crypt32)
endif()
add_executable(room-status-test EXCLUDE_FROM_ALL ../../tests/test_room_status.cpp room/room_status.cpp room/room_announce.cpp)
target_compile_options(room-status-test PRIVATE -UNDEBUG -Wall -Wextra)
target_include_directories(room-status-test PRIVATE ../..)
add_executable(room-service-test EXCLUDE_FROM_ALL ../../tests/test_room_service.cpp ${BBNET_ROOM_SOURCES})
target_compile_options(room-service-test PRIVATE -UNDEBUG -Wall -Wextra)
target_include_directories(room-service-test PRIVATE ../.. third_party/httplib)
target_compile_definitions(room-service-test PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
target_link_libraries(room-service-test PRIVATE nlohmann_json::nlohmann_json OpenSSL::SSL OpenSSL::Crypto)
if(WIN32)
    target_compile_definitions(room-link-test PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
    target_link_libraries(room-link-test PRIVATE ws2_32 crypt32)
    target_compile_definitions(room-service-test PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
    target_link_libraries(room-service-test PRIVATE ws2_32 crypt32)
endif()
