mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
116 lines
3.5 KiB
CMake
116 lines
3.5 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}/*")
|
|
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}")
|
|
file(READ "${ABS_PATH}" CONTENT_HEX HEX)
|
|
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)")
|