# 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: `\Sub\Path.lua` with # backslashes. Engine constructs full path as # `Interface\AddOns\\` — we strip the # `Interface\AddOns\\` 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 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)")