# MeshMC standalone plugin build bootstrap.
# When this plugin is built from MeshMC's main tree, the parent project
# already provides Qt, MeshMC::SDK, output directories, and signing helpers.
# When this directory is configured as its own repository, provide those
# pieces here so the plugin can be built with only an installed MeshMC SDK.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    cmake_minimum_required(VERSION 3.21)
    project(PackPortal LANGUAGES CXX)

    set(CMAKE_AUTOMOC ON)
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)

    find_package(Qt6 REQUIRED COMPONENTS Core Widgets Gui Network)
    find_package(MeshMC_SDK REQUIRED)

    set(MESHMC_PLUGIN_STAGING_DIR "${CMAKE_BINARY_DIR}/mmcmodules" CACHE PATH
        "Directory where built .mmco plugins are placed")
    set(MMCO_MODULES_DEST_DIR "lib/mmcmodules" CACHE PATH
        "Install directory for .mmco plugins")
    set(MMCO_PLUGIN_DATA_DEST_DIR "share/meshmc/mmcmodules" CACHE PATH
        "Install directory for plugin data files")
endif()

# SPDX-FileCopyrightText: 2026 Project Tick
# SPDX-License-Identifier: GPL-3.0-or-later
#
# PackPortal — MeshMC plugin (.mmco)
#
# Round-trip pack import/export for CurseForge zips, Modrinth .mrpack,
# and MultiMC/MeshMC raw zips. Internally each format has its own
# manifest emitter and parser, plus a shared "overrides" copier that
# moves the right slice of the instance into the archive.

# Note: only the EXPORT side of the engine is wired into the plugin.
# Importing mrpack / CurseForge / MultiMC archives is handled by the
# launcher's built-in InstanceImportTask, reachable from
# Add Instance → Import from zip. The legacy ImportEngine files used
# to live here; they were removed once the launcher's importer proved
# capable of all three formats out of the box.
#
# The PackPortalPage (a per-instance BasePage) was likewise dropped:
# the plugin no longer injects any UI surface of its own. It overrides
# the launcher's existing actionExportInstance handler at MAIN_READY
# time and replaces the dialog that pops open. Keeping the page would
# have added a redundant "Pack Export" tab to every instance's
# settings panel.
set(PACKPORTAL_SOURCES
    PackPortalPlugin.cpp
    PackFormat.h
    formats/MrPackFormat.h
    formats/MrPackFormat.cpp
    formats/CurseForgeFormat.h
    formats/CurseForgeFormat.cpp
    formats/MultiMCFormat.h
    formats/MultiMCFormat.cpp
    ExportEngine.h
    ExportEngine.cpp
)

add_library(PackPortal MODULE ${PACKPORTAL_SOURCES})

target_link_libraries(PackPortal PRIVATE
    MeshMC::SDK
)

target_include_directories(PackPortal PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
)

set_target_properties(PackPortal PROPERTIES
    PREFIX ""
    SUFFIX ".mmco"
    LIBRARY_OUTPUT_DIRECTORY "${MESHMC_PLUGIN_STAGING_DIR}"
)

foreach(CFG ${CMAKE_CONFIGURATION_TYPES})
    string(TOUPPER "${CFG}" CFG_UPPER)
    set_target_properties(PackPortal PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY_${CFG_UPPER} "${MESHMC_PLUGIN_STAGING_DIR}"
    )
endforeach()

install(TARGETS PackPortal
    LIBRARY DESTINATION "${MMCO_MODULES_DEST_DIR}"
)

if(MeshMC_PLUGIN_SIGN_ALL)
    mmco_sign_plugin(PackPortal)
endif()
