mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
Two related fixes to the embedded-addon precedence so users stop running a stale/mismatched on-disk !!!ClassicAPI without realizing it, while keeping the developer's disk-wins workflow intact. Version is now a pure build-flag artifact. The embedded copy's `## Version:` is stamped from CLASSICAPI_TAG at embed time (cmake/embed_addon.cmake rewrites the line in-memory before hex encoding, portable via a temp-file round-trip). The committed source toc stays `## Version: DEV` and is never edited, so a release build is `-DCLASSICAPI_TAG=vX.Y.Z` and nothing else. Dropped the CI sed step that previously mutated the source toc. Moved the version-derivation block above the embed step so CLASSICAPI_TOC_VERSION is available to it. Dev override moved from the toc version to an explicit marker. A `.classicapi-dev` file in the on-disk addon folder now forces the disk copy to win regardless of version (checked via the original FUN_FILE_READ, so it reflects a real on-disk file). Consequently `DEV` no longer sorts as "newer than any release" in CompareVersions — it sorts oldest — so a stray `DEV` source/clone copy on a user's disk no longer silently shadows a newer embedded release. The normal local loop still works without the marker (local DEV DLL + DEV disk = tie = disk wins); the marker is only needed to prefer disk against a released DLL. The marker is gitignored and excluded from the embed (both globs), and the release ships only the DLL, so it can exist only in a developer working tree.
139 lines
4.7 KiB
CMake
139 lines
4.7 KiB
CMake
# embed_addon.cmake — bundle an addon directory into a generated C++ header.
|
|
#
|
|
# Invoked from CMakeLists.txt via `cmake -P` as a custom command. Reads every
|
|
# file under SRC_DIR recursively and emits OUT_HEADER containing:
|
|
#
|
|
# namespace Addons::Embedded {
|
|
# struct File {
|
|
# const char *path;
|
|
# const unsigned char *data;
|
|
# size_t size;
|
|
# };
|
|
# extern const File kFiles[];
|
|
# extern const size_t kFileCount;
|
|
# }
|
|
#
|
|
# Paths are normalized to lowercase backslash-form relative to
|
|
# `Interface\AddOns\` so they match what the engine's TOC parser
|
|
# constructs when it asks for the addon's files (see
|
|
# `s_Interface_AddOns__s__s_toc_00853840` in the binary). The
|
|
# manifest is hash-table-friendly for fast lookup but the lookup
|
|
# walks the array linearly — fine for 25 files.
|
|
#
|
|
# Usage:
|
|
# cmake -DSRC_DIR=... -DOUT_HEADER=... -DADDON_NAME=... -P embed_addon.cmake
|
|
|
|
if(NOT SRC_DIR)
|
|
message(FATAL_ERROR "embed_addon.cmake: SRC_DIR required")
|
|
endif()
|
|
if(NOT OUT_HEADER)
|
|
message(FATAL_ERROR "embed_addon.cmake: OUT_HEADER required")
|
|
endif()
|
|
if(NOT ADDON_NAME)
|
|
message(FATAL_ERROR "embed_addon.cmake: ADDON_NAME required")
|
|
endif()
|
|
|
|
file(GLOB_RECURSE INPUT_FILES RELATIVE "${SRC_DIR}" "${SRC_DIR}/*")
|
|
# Never embed the developer marker — it must reflect on-disk presence
|
|
# only (see src/addons/Embedded.cpp DiskHasDevMarker), and it is
|
|
# gitignored so it only exists in a developer working tree anyway.
|
|
list(FILTER INPUT_FILES EXCLUDE REGEX "(^|/)\\.classicapi-dev$")
|
|
list(SORT INPUT_FILES) # deterministic codegen
|
|
|
|
set(BODY "")
|
|
set(MANIFEST "")
|
|
set(INDEX 0)
|
|
foreach(REL_PATH ${INPUT_FILES})
|
|
set(ABS_PATH "${SRC_DIR}/${REL_PATH}")
|
|
|
|
# Stamp the addon's `## Version:` line from the build flag rather
|
|
# than the committed source (which stays `DEV`). This is what makes
|
|
# a release build a pure function of `-DCLASSICAPI_TAG` — the source
|
|
# toc is never edited. Only the `.toc` file is rewritten, in-memory,
|
|
# before hex-encoding; every other file is embedded verbatim.
|
|
if(ADDON_VERSION AND REL_PATH MATCHES "\\.toc$")
|
|
file(READ "${ABS_PATH}" TOC_TEXT)
|
|
string(REGEX REPLACE "## Version:[^\n\r]*"
|
|
"## Version: ${ADDON_VERSION}" TOC_TEXT "${TOC_TEXT}")
|
|
# Round-trip through a temp file to hex-encode the modified text
|
|
# (portable to CMake 3.10; avoids requiring string(HEX), 3.18+).
|
|
set(TOC_TMP "${OUT_HEADER}.toc.tmp")
|
|
file(WRITE "${TOC_TMP}" "${TOC_TEXT}")
|
|
file(READ "${TOC_TMP}" CONTENT_HEX HEX)
|
|
file(REMOVE "${TOC_TMP}")
|
|
else()
|
|
file(READ "${ABS_PATH}" CONTENT_HEX HEX)
|
|
endif()
|
|
|
|
string(LENGTH "${CONTENT_HEX}" HEX_LEN)
|
|
math(EXPR FILE_SIZE "${HEX_LEN} / 2")
|
|
|
|
# Convert hex string to comma-separated byte literals. 16 bytes per line
|
|
# keeps the generated header readable.
|
|
set(BYTES "")
|
|
set(COL 0)
|
|
set(POS 0)
|
|
while(POS LESS HEX_LEN)
|
|
string(SUBSTRING "${CONTENT_HEX}" ${POS} 2 BYTE)
|
|
if(COL EQUAL 0)
|
|
string(APPEND BYTES " ")
|
|
endif()
|
|
string(APPEND BYTES "0x${BYTE},")
|
|
math(EXPR POS "${POS} + 2")
|
|
math(EXPR COL "${COL} + 1")
|
|
if(COL EQUAL 16)
|
|
string(APPEND BYTES "\n")
|
|
set(COL 0)
|
|
else()
|
|
string(APPEND BYTES " ")
|
|
endif()
|
|
endwhile()
|
|
if(NOT COL EQUAL 0)
|
|
string(APPEND BYTES "\n")
|
|
endif()
|
|
|
|
# Normalize path to engine-style: `<AddonName>\Sub\Path.lua` with
|
|
# backslashes. Engine constructs full path as
|
|
# `Interface\AddOns\<AddonName>\<file>` — we strip the
|
|
# `Interface\AddOns\<AddonName>\` prefix at lookup time so storing
|
|
# just the relative form keeps the manifest small.
|
|
string(REPLACE "/" "\\\\" REL_PATH_BS "${REL_PATH}")
|
|
|
|
string(APPEND BODY "static const unsigned char kData_${INDEX}[] = {\n${BYTES}};\n\n")
|
|
string(APPEND MANIFEST " { \"${REL_PATH_BS}\", kData_${INDEX}, ${FILE_SIZE} },\n")
|
|
|
|
math(EXPR INDEX "${INDEX} + 1")
|
|
endforeach()
|
|
|
|
set(HEADER_CONTENT "// Auto-generated by embed_addon.cmake — do not edit.
|
|
// Source: ${SRC_DIR}
|
|
//
|
|
// Addon \"${ADDON_NAME}\" embedded as a fallback for users who don't have
|
|
// it installed on disk. See src/addons/Embedded.cpp for the file-read
|
|
// hook that surfaces these buffers to the engine.
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
|
|
namespace Addons::Embedded {
|
|
|
|
namespace ${ADDON_NAME}Files {
|
|
|
|
${BODY}static const struct File {
|
|
const char *path;
|
|
const unsigned char *data;
|
|
size_t size;
|
|
} kFiles[] = {
|
|
${MANIFEST}};
|
|
|
|
static constexpr size_t kFileCount = sizeof(kFiles) / sizeof(kFiles[0]);
|
|
|
|
} // namespace ${ADDON_NAME}Files
|
|
|
|
} // namespace Addons::Embedded
|
|
")
|
|
|
|
file(WRITE "${OUT_HEADER}" "${HEADER_CONTENT}")
|
|
message(STATUS "embed_addon: wrote ${OUT_HEADER} (${INDEX} files)")
|