# MassBuilderSaveTool
# Copyright (C) 2021-2024 Guillaume Jacquemin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(SAVETOOL_PROJECT_VERSION 1.6.0-pre)

find_package(Corrade REQUIRED Containers Utility)
if(CORRADE_TARGET_WINDOWS)
    find_package(Corrade REQUIRED Main)
endif()
find_package(Magnum REQUIRED GL Sdl2Application)
find_package(MagnumIntegration REQUIRED ImGui)

if(SAVETOOL_USE_SYSTEM_LIBZIP)
    find_package(libzip REQUIRED)
endif(SAVETOOL_USE_SYSTEM_LIBZIP)

if(SAVETOOL_USE_SYSTEM_EFSW)
    find_package(efsw REQUIRED)
endif(SAVETOOL_USE_SYSTEM_EFSW)

if(SAVETOOL_USE_SYSTEM_LIBCURL)
    find_package(CURL REQUIRED HTTPS)
endif(SAVETOOL_USE_SYSTEM_LIBCURL)

set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON)

corrade_add_resource(Assets assets.conf)

set(Logger_SOURCES
    Logger/Logger.h
    Logger/Logger.cpp
    Logger/EntryType.h
    Logger/MagnumLogBuffer.h
    Logger/MagnumLogBuffer.cpp
)

set(BinaryIo_SOURCES
    BinaryIo/BinaryIo.h
    BinaryIo/Reader.h
    BinaryIo/Reader.cpp
    BinaryIo/Writer.h
    BinaryIo/Writer.cpp
)

set(Gvas_SOURCES
    Gvas/Serialisers/Serialisers.h
    Gvas/Serialisers/AbstractUnrealCollectionProperty.h
    Gvas/Serialisers/AbstractUnrealProperty.h
    Gvas/Serialisers/AbstractUnrealStruct.h
    Gvas/Serialisers/ArrayProperty.h
    Gvas/Serialisers/ArrayProperty.cpp
    Gvas/Serialisers/BoolProperty.h
    Gvas/Serialisers/BoolProperty.cpp
    Gvas/Serialisers/ByteProperty.h
    Gvas/Serialisers/ByteProperty.cpp
    Gvas/Serialisers/ColourProperty.h
    Gvas/Serialisers/ColourProperty.cpp
    Gvas/Serialisers/DateTimeProperty.h
    Gvas/Serialisers/DateTimeProperty.cpp
    Gvas/Serialisers/EnumProperty.h
    Gvas/Serialisers/EnumProperty.cpp
    Gvas/Serialisers/FloatProperty.h
    Gvas/Serialisers/FloatProperty.cpp
    Gvas/Serialisers/GuidProperty.h
    Gvas/Serialisers/GuidProperty.cpp
    Gvas/Serialisers/IntProperty.h
    Gvas/Serialisers/IntProperty.cpp
    Gvas/Serialisers/MapProperty.h
    Gvas/Serialisers/MapProperty.cpp
    Gvas/Serialisers/ResourceProperty.h
    Gvas/Serialisers/ResourceProperty.cpp
    Gvas/Serialisers/RotatorProperty.h
    Gvas/Serialisers/RotatorProperty.cpp
    Gvas/Serialisers/StringProperty.h
    Gvas/Serialisers/StringProperty.cpp
    Gvas/Serialisers/SetProperty.h
    Gvas/Serialisers/SetProperty.cpp
    Gvas/Serialisers/Struct.h
    Gvas/Serialisers/Struct.cpp
    Gvas/Serialisers/TextProperty.h
    Gvas/Serialisers/TextProperty.cpp
    Gvas/Serialisers/UnrealProperty.h
    Gvas/Serialisers/VectorProperty.h
    Gvas/Serialisers/VectorProperty.cpp
    Gvas/Serialisers/Vector2DProperty.h
    Gvas/Serialisers/Vector2DProperty.cpp

    Gvas/Types/Types.h
    Gvas/Types/ArrayProperty.h
    Gvas/Types/BoolProperty.h
    Gvas/Types/ByteProperty.h
    Gvas/Types/ColourStructProperty.h
    Gvas/Types/DateTimeStructProperty.h
    Gvas/Types/EnumProperty.h
    Gvas/Types/FloatProperty.h
    Gvas/Types/GenericStructProperty.h
    Gvas/Types/GuidStructProperty.h
    Gvas/Types/IntProperty.h
    Gvas/Types/MapProperty.h
    Gvas/Types/NoneProperty.h
    Gvas/Types/RotatorStructProperty.h
    Gvas/Types/SetProperty.h
    Gvas/Types/StringProperty.h
    Gvas/Types/StructProperty.h
    Gvas/Types/ResourceItemValue.h
    Gvas/Types/TextProperty.h
    Gvas/Types/UnrealProperty.h
    Gvas/Types/UnrealPropertyBase.h
    Gvas/Types/Vector2DStructProperty.h
    Gvas/Types/VectorStructProperty.h

    Gvas/Gvas.h
    Gvas/Debug.h
    Gvas/Debug.cpp
    Gvas/File.h
    Gvas/File.cpp
    Gvas/PropertySerialiser.h
    Gvas/PropertySerialiser.cpp
)

if(CORRADE_TARGET_WINDOWS)
    set(SAVETOOL_RC_FILE resource.rc)
endif()

add_executable(MassBuilderSaveTool
    main.cpp
    Application/Application.h
    Application/Application.cpp
    Application/Application_drawAbout.cpp
    Application/Application_drawMainMenu.cpp
    Application/Application_FileWatcher.cpp
    Application/Application_Initialisation.cpp
    Application/Application_MainManager.cpp
    Application/Application_MassViewer.cpp
    Application/Application_MassViewer_Frame.cpp
    Application/Application_MassViewer_Armour.cpp
    Application/Application_MassViewer_Weapons.cpp
    Application/Application_ProfileManager.cpp
    Application/Application_UpdateChecker.cpp
    Configuration/Configuration.h
    Configuration/Configuration.cpp
    FontAwesome/IconsFontAwesome5.h
    FontAwesome/IconsFontAwesome5Brands.h
    GameData/Accessories.h
    GameData/ArmourSets.h
    GameData/LastMissionId.h
    GameData/ResourceIDs.h
    GameData/StoryProgress.h
    GameData/StyleNames.h
    GameData/WeaponParts.h
    GameObjects/Accessory.h
    GameObjects/ArmourPart.h
    GameObjects/BulletLauncherAttachment.h
    GameObjects/CustomStyle.h
    GameObjects/Decal.h
    GameObjects/Joints.h
    GameObjects/Mass.h
    GameObjects/Mass.cpp
    GameObjects/Mass_Frame.cpp
    GameObjects/Mass_Armour.cpp
    GameObjects/Mass_Weapons.cpp
    GameObjects/Mass_Styles.cpp
    GameObjects/Mass_DecalsAccessories.cpp
    GameObjects/Profile.h
    GameObjects/Profile.cpp
    GameObjects/PropertyNames.h
    GameObjects/Weapon.h
    GameObjects/Weapon.cpp
    GameObjects/WeaponPart.h
    Managers/Backup.h
    Managers/BackupManager.h
    Managers/BackupManager.cpp
    Managers/MassManager.h
    Managers/MassManager.cpp
    Managers/ProfileManager.h
    Managers/ProfileManager.cpp
    Managers/StagedMass.h
    Managers/StagedMassManager.h
    Managers/StagedMassManager.cpp
    Managers/Vfs/Directory.h
    Maps/ArmourSlots.hpp
    Maps/BulletLauncherAttachmentStyles.hpp
    Maps/BulletLauncherSockets.hpp
    Maps/DamageTypes.hpp
    Maps/EffectColourModes.hpp
    Maps/WeaponTypes.hpp
    ToastQueue/ToastQueue.h
    ToastQueue/ToastQueue.cpp
    UpdateChecker/UpdateChecker.h
    UpdateChecker/UpdateChecker.cpp
    Utilities/Crc32.h
    Utilities/Crc32.cpp
    Utilities/Temp.h
    Utilities/Temp.cpp
    Version/Version.h
    Version/Version.cpp
    ${Logger_SOURCES}
    ${BinaryIo_SOURCES}
    ${Gvas_SOURCES}
    ${SAVETOOL_RC_FILE}
    ${Assets}
)

if(CORRADE_TARGET_WINDOWS)
    set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE $<CONFIG:Release>)
endif()

target_compile_definitions(MassBuilderSaveTool PRIVATE
    SAVETOOL_VERSION_STRING="${SAVETOOL_PROJECT_VERSION}"
    SAVETOOL_VERSION_MAJOR=1
    SAVETOOL_VERSION_MINOR=6
    SAVETOOL_VERSION_PATCH=0
    SAVETOOL_VERSION_PRERELEASE=true
    SAVETOOL_CODENAME="Glorious Fuckery"
    SAVETOOL_SUPPORTED_GAME_VERSION="0.12.x"
)

if(CMAKE_BUILD_TYPE STREQUAL Debug)
    target_compile_definitions(MassBuilderSaveTool PRIVATE SAVETOOL_DEBUG_BUILD)
endif()

if(CMAKE_BUILD_TYPE STREQUAL Release)
    set_target_properties(MassBuilderSaveTool PROPERTIES OUTPUT_NAME MassBuilderSaveTool-${SAVETOOL_PROJECT_VERSION})
endif()

target_link_options(MassBuilderSaveTool PRIVATE -static -static-libgcc -static-libstdc++)

if(CMAKE_BUILD_TYPE STREQUAL Release)
    target_link_options(MassBuilderSaveTool PRIVATE -Wl,-S)
endif()

target_link_libraries(MassBuilderSaveTool PRIVATE
    Corrade::Containers
    Corrade::Utility
    Magnum::Magnum
    Magnum::GL
    Magnum::Sdl2Application
    MagnumIntegration::ImGui
    CURL::libcurl_static
)

if(SAVETOOL_USE_SYSTEM_LIBZIP)
    target_link_libraries(MassBuilderSaveTool PRIVATE libzip::zip)
else()
    target_link_libraries(MassBuilderSaveTool PRIVATE zip)
endif()

if(SAVETOOL_USE_SYSTEM_EFSW)
    target_link_libraries(MassBuilderSaveTool PRIVATE efsw::efsw)
else()
    target_link_libraries(MassBuilderSaveTool PRIVATE efsw)
endif()

if(CORRADE_TARGET_WINDOWS)
    target_link_libraries(MassBuilderSaveTool PRIVATE
        Corrade::Main
        imm32
        wtsapi32
    )
endif()