From 8cc25938ef8820c1cb3b6bbd1c04ad57fee96b54 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 26 Oct 2026 12:27:03 +0300 Subject: [PATCH 1/3] feat: preload.json patterns support --- src/assets/AssetsLoader.cpp | 320 ++++++++++++++++++++++++++++++++---- src/assets/AssetsLoader.hpp | 9 +- 2 files changed, 291 insertions(+), 38 deletions(-) diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index 6e20f214a..aa996c025 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -19,8 +19,14 @@ #include "util/ThreadPool.hpp" #include "voxels/Block.hpp" +#include +#include +#include #include +#include +#include #include +#include namespace fs = std::filesystem; @@ -151,24 +157,14 @@ static std::string assets_def_folder(AssetType tag) { return ""; } -void AssetsLoader::processPreload( - AssetType tag, const std::string& name, const dv::value& map +static std::shared_ptr create_preload_config( + AssetType tag, const dv::value& map ) { - std::string defFolder = assets_def_folder(tag); - std::string path = defFolder + "/" + name; - if (map == nullptr) { - add(tag, path, name, nullptr, true); - return; - } - std::shared_ptr config = nullptr; - map.at("path").get(path); - logger.debug() << "processing preload " << util::quote(name) - << " path: " << util::quote(path); switch (tag) { case AssetType::SOUND: { bool keepPCM = false; - config = std::make_shared(map.at("keep-pcm").get(keepPCM)); - break; + map.at("keep-pcm").get(keepPCM); + return std::make_shared(keepPCM); } case AssetType::ATLAS: { std::string typeName = "atlas"; @@ -177,44 +173,295 @@ void AssetsLoader::processPreload( if (typeName == "separate") { type = AtlasType::SEPARATE; } - config = std::make_shared(type); - break; + return std::make_shared(type); } case AssetType::POST_EFFECT: { bool advanced = false; map.at("advanced").get(advanced); - config = std::make_shared(advanced); - break; + return std::make_shared(advanced); } case AssetType::MODEL: { bool squashed = false; map.at("squash").get(squashed); - config = std::make_shared(squashed); - break; + return std::make_shared(squashed); } case AssetType::FONT: { int size = DEFAULT_FONT_SIZE; map.at("size").get(size); - config = std::make_unique(size); - break; + return std::make_shared(size); } default: - break; + return nullptr; + } +} + +static bool is_pattern(const std::string& name) { + return name.find('*') != std::string::npos; +} + +static bool match_wildcard(std::string_view pattern, std::string_view str) { + size_t p = 0; + size_t s = 0; + size_t starPos = std::string_view::npos; + size_t starMatch = 0; + while (s < str.length()) { + if (p < pattern.length() && pattern[p] == '*') { + starPos = p++; + starMatch = s; + } else if (p < pattern.length() && pattern[p] == str[s]) { + p++; + s++; + } else if (starPos != std::string_view::npos) { + p = starPos + 1; + s = ++starMatch; + } else { + return false; + } + } + while (p < pattern.length() && pattern[p] == '*') { + p++; + } + return p == pattern.length(); +} + +static std::vector split_pattern(const std::string& pattern) { + if (pattern.find("**") != std::string::npos) { + throw std::runtime_error( + "'**' is not supported in preload pattern " + util::quote(pattern) + ); + } + std::vector segments; + size_t start = 0; + while (start < pattern.length()) { + size_t end = pattern.find('/', start); + if (end == std::string::npos) { + end = pattern.length(); + } + if (end > start) { + auto segment = pattern.substr(start, end - start); + if (segment == "." || segment == "..") { + throw std::runtime_error( + "relative segments are not allowed in preload pattern " + + util::quote(pattern) + ); + } + segments.push_back(std::move(segment)); + } + start = end + 1; + } + return segments; +} + +static void glob( + const io::path& folder, + const std::vector& segments, + size_t index, + const std::string& relative, + bool directories, + std::vector& out +) { + const auto& segment = segments[index]; + bool last = index + 1 == segments.size(); + + auto visit = [&](const std::string& name) { + io::path path = folder / name; + std::string rel = relative.empty() ? name : relative + "/" + name; + if (!last) { + if (io::is_directory(path)) { + glob(path, segments, index + 1, rel, directories, out); + } + } else if (directories ? io::is_directory(path) + : io::is_regular_file(path)) { + out.push_back(std::move(rel)); + } + }; + + if (!is_pattern(segment)) { + visit(segment); + return; + } + if (!io::is_directory(folder)) { + return; + } + for (const auto& entry : io::directory_iterator(folder)) { + auto name = entry.name(); + if (match_wildcard(segment, name)) { + visit(name); + } + } +} + +static std::vector asset_search_folders(AssetType tag) { + if (tag == AssetType::ANIMATION) { + return {ANIMATION_FOLDER, ANIMATION_FOLDER_ALIAS}; + } + return {assets_def_folder(tag)}; +} + +static std::vector asset_extensions(AssetType tag) { + switch (tag) { + case AssetType::ANIMATION: + return {".vca"}; + case AssetType::FONT: + return {".png", ".ttf", ".otf"}; + case AssetType::MODEL: + return {".vec3", ".obj", ".xml", ".vcm"}; + case AssetType::POST_EFFECT: + return {".glsl"}; + case AssetType::SHADER: + return {".glslv", ".glslf"}; + case AssetType::SKELETON: + return {".json"}; + case AssetType::SOUND: + return {".ogg", ".wav"}; + case AssetType::TEXTURE: + return {".png"}; + default: + return {}; + } +} + +static std::optional strip_index_suffix(const std::string& stem) { + size_t pos = stem.rfind('_'); + if (pos == std::string::npos || pos + 1 == stem.length()) { + return std::nullopt; + } + for (size_t i = pos + 1; i < stem.length(); i++) { + if (!std::isdigit(static_cast(stem[i]))) { + return std::nullopt; + } + } + return stem.substr(0, pos); +} + +struct PreloadMatch { + std::string name; + std::string path; +}; + +/// @brief Resolve asset name for a file matched by preload pattern +/// @param folder asset type folder the file was found in +/// @param relative file path relative to the folder +/// @return std::nullopt if the file is not a loadable asset of the type +static std::optional resolve_preload_file( + AssetType tag, const io::path& folder, const std::string& relative +) { + io::path file = relative; + std::string extension = file.extension(); + const auto& extensions = asset_extensions(tag); + if (std::find(extensions.begin(), extensions.end(), extension) == + extensions.end()) { + return std::nullopt; + } + std::string base = relative.substr(0, relative.length() - extension.length()); + std::string defFolder = assets_def_folder(tag); + + if (tag == AssetType::FONT) { + if (extension != ".png") { + return PreloadMatch {base, defFolder + "/" + relative}; + } + auto fontName = strip_index_suffix(base); + if (!fontName || !io::exists(folder / (*fontName + "_0.png"))) { + return std::nullopt; + } + base = std::move(*fontName); + } else if (tag == AssetType::SOUND) { + auto soundName = strip_index_suffix(base); + if (soundName && (io::exists(folder / (*soundName + extension)) || + io::exists(folder / (*soundName + "_0" + extension)))) { + base = std::move(*soundName); + } + } + return PreloadMatch {base, defFolder + "/" + base}; +} + +static std::vector find_preload_matches( + AssetType tag, const io::path& packFolder, const std::string& pattern +) { + auto segments = split_pattern(pattern); + if (segments.empty()) { + return {}; + } + bool directories = pattern.back() == '/' || tag == AssetType::ATLAS; + if (directories && tag != AssetType::ATLAS) { + throw std::runtime_error( + "directory pattern " + util::quote(pattern) + + " is not applicable to assets of type " + assets_def_folder(tag) + ); + } + std::map found; + for (const auto& searchFolder : asset_search_folders(tag)) { + io::path folder = packFolder / searchFolder; + std::vector files; + glob(folder, segments, 0, "", directories, files); + for (const auto& relative : files) { + if (directories) { + found.try_emplace(relative, assets_def_folder(tag) + "/" + relative); + continue; + } + if (auto match = resolve_preload_file(tag, folder, relative)) { + found.try_emplace(std::move(match->name), std::move(match->path)); + } + } + } + std::vector matches; + matches.reserve(found.size()); + for (auto& [name, path] : found) { + matches.push_back(PreloadMatch {name, std::move(path)}); + } + return matches; +} + +void AssetsLoader::processPreload( + AssetType tag, + const std::string& name, + const dv::value& map, + const io::path& packFolder +) { + std::shared_ptr config = + map == nullptr ? nullptr : create_preload_config(tag, map); + + if (is_pattern(name)) { + if (map != nullptr && map.has("path")) { + throw std::runtime_error( + "'path' cannot be used with preload pattern " + util::quote(name) + ); + } + auto matches = find_preload_matches(tag, packFolder, name); + if (matches.empty()) { + logger.warning() << "preload pattern " << util::quote(name) + << " matches nothing in " << packFolder.string(); + } + for (const auto& match : matches) { + logger.debug() << "processing preload " << util::quote(match.name) + << " path: " << util::quote(match.path); + add(tag, match.path, match.name, config, true); + } + return; + } + + std::string path = assets_def_folder(tag) + "/" + name; + if (map != nullptr) { + map.at("path").get(path); + logger.debug() << "processing preload " << util::quote(name) + << " path: " << util::quote(path); } add(tag, path, name, std::move(config), true); } -void AssetsLoader::processPreloadList(AssetType tag, const dv::value& list) { +void AssetsLoader::processPreloadList( + AssetType tag, const dv::value& list, const io::path& packFolder +) { if (list == nullptr) { return; } for (const auto& value : list) { switch (value.getType()) { case dv::value_type::string: - processPreload(tag, value.asString(), nullptr); + processPreload(tag, value.asString(), nullptr, packFolder); break; case dv::value_type::object: - processPreload(tag, value["name"].asString(), value); + processPreload(tag, value["name"].asString(), value, packFolder); break; default: throw std::runtime_error("invalid entry type"); @@ -224,16 +471,17 @@ void AssetsLoader::processPreloadList(AssetType tag, const dv::value& list) { void AssetsLoader::processPreloadConfig(const io::path& file) { auto root = io::read_json(file); - processPreloadList(AssetType::ANIMATION, root["animations"]); - processPreloadList(AssetType::ANIMATION, root["animation"]); - processPreloadList(AssetType::ATLAS, root["atlases"]); - processPreloadList(AssetType::FONT, root["fonts"]); - processPreloadList(AssetType::MODEL, root["models"]); - processPreloadList(AssetType::POST_EFFECT, root["post-effects"]); - processPreloadList(AssetType::SHADER, root["shaders"]); - processPreloadList(AssetType::SKELETON, root["skeletons"]); - processPreloadList(AssetType::SOUND, root["sounds"]); - processPreloadList(AssetType::TEXTURE, root["textures"]); + io::path folder = file.parent(); + processPreloadList(AssetType::ANIMATION, root["animations"], folder); + processPreloadList(AssetType::ANIMATION, root["animation"], folder); + processPreloadList(AssetType::ATLAS, root["atlases"], folder); + processPreloadList(AssetType::FONT, root["fonts"], folder); + processPreloadList(AssetType::MODEL, root["models"], folder); + processPreloadList(AssetType::POST_EFFECT, root["post-effects"], folder); + processPreloadList(AssetType::SHADER, root["shaders"], folder); + processPreloadList(AssetType::SKELETON, root["skeletons"], folder); + processPreloadList(AssetType::SOUND, root["sounds"], folder); + processPreloadList(AssetType::TEXTURE, root["textures"], folder); // layouts are loaded automatically } diff --git a/src/assets/AssetsLoader.hpp b/src/assets/AssetsLoader.hpp index a61c3b59e..477c6aaa1 100644 --- a/src/assets/AssetsLoader.hpp +++ b/src/assets/AssetsLoader.hpp @@ -149,9 +149,14 @@ private: AssetsLoadInfo& assetsLoadInfo; void processPreload( - AssetType tag, const std::string& name, const dv::value& map + AssetType tag, + const std::string& name, + const dv::value& map, + const io::path& packFolder + ); + void processPreloadList( + AssetType tag, const dv::value& list, const io::path& packFolder ); - void processPreloadList(AssetType tag, const dv::value& list); void processPreloadConfig(const io::path& file); void processPreloadConfigs(const Content* content); }; From 07568e6ae8d49d105b3f13eb5028471fa566526c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 26 Oct 2026 12:27:41 +0300 Subject: [PATCH 2/3] update res/preload.json --- res/preload.json | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/res/preload.json b/res/preload.json index 6b0093f36..6bdbbeb3f 100644 --- a/res/preload.json +++ b/res/preload.json @@ -25,37 +25,11 @@ } ], "textures": [ - "gui/menubg", - "gui/delete_icon", - "gui/no_icon", - "gui/no_world_icon", - "gui/warning", - "gui/error", - "gui/cross", - "gui/refresh", - "gui/folder_icon", - "gui/settings_icon", - "misc/rain", - "misc/snow", - "gui/check_mark", - "gui/left_arrow", - "gui/right_arrow", - "gui/lock", - "gui/save", - "gui/block", - "gui/item", - "gui/file", - "gui/module", - "gui/play", - "gui/info", - "gui/world", - "gui/hud", - "gui/entity", - "gui/half_block", { "name": "", "path": "textures/misc/blank" - } + }, + "gui/*" ], "fonts": [ { From 65b40d7f5f3e87bed9711068b656f02713b52ebf Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 26 Oct 2026 12:38:30 +0300 Subject: [PATCH 3/3] update doc/*/assets-preload.md --- doc/en/assets-preload.md | 42 +++++++++++++++++++++++++++++++++++++++ doc/ru/assets-preload.md | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/doc/en/assets-preload.md b/doc/en/assets-preload.md index e40403298..8591c4402 100644 --- a/doc/en/assets-preload.md +++ b/doc/en/assets-preload.md @@ -38,6 +38,48 @@ Additional load settings example: ] } ``` +## Patterns + +An asset name may contain `*` to include multiple assets at once. The `*` matches any sequence of characters within a single path segment. + +Pattern matching is performed within the pack containing `preload.json`, specifically within the folder corresponding to the asset type (e.g., `textures/`, `sounds/`, etc.). +The resulting asset names are the same as if each discovered asset had been specified explicitly. + +```json +{ + "textures": [ + "gui/*", + "mobs/*/skin-*" + ], + "sounds": [ + "steps/*" + ] +} +``` + +> [!NOTE] +> Sound variations (such as `grass_0.ogg`, `grass_1.ogg`, etc.) are added as a single sound entry (e.g., `steps/grass`). + +A pattern ending in `/` matches directories rather than individual files. This is used for atlases: +```json +{ + "atlases": [ + "smth/*/other-*/*/" + ] +} +``` + +Entry parameters apply to all matched assets. The `path` parameter cannot be used in conjunction with a pattern: +```json +{ + "sounds": [ + { + "name": "ambient/*", + "keep-pcm": true + } + ] +} +``` *preload.json* example from `core:` package (`res/preload.json`): ```json diff --git a/doc/ru/assets-preload.md b/doc/ru/assets-preload.md index 1d39d76aa..cfc3a1832 100644 --- a/doc/ru/assets-preload.md +++ b/doc/ru/assets-preload.md @@ -39,6 +39,49 @@ ``` +## Шаблоны + +Имя ассета может содержать `*` для добавления сразу нескольких ассетов. `*` соответствует любой последовательности символов в пределах одного сегмента пути. + +Поиск по шаблону производится в паке, содержащем `preload.json`, в папке типа ассетов (`textures/`, `sounds/`, ...). +Имена ассетов получаются такими же, как если бы каждый найденный ассет был указан явно. + +```json +{ + "textures": [ + "gui/*", + "mobs/*/skin-*" + ], + "sounds": [ + "steps/*" + ] +} +``` + +> [!NOTE] +> Вариации звука (`grass_0.ogg`, `grass_1.ogg`, ...) добавляются как один звук (`steps/grass`). + +Шаблон, оканчивающийся на `/`, соответствует папкам, а не файлам. Используется для атласов: +```json +{ + "atlases": [ + "smth/*/other-*/*/" + ] +} +``` + +Параметры записи применяются ко всем найденным ассетам. Параметр `path` нельзя использовать вместе с шаблоном: +```json +{ + "sounds": [ + { + "name": "ambient/*", + "keep-pcm": true + } + ] +} +``` + Пример файла из пакета `core:` (`res/preload.json`): ```json {