mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
Merge pull request #1061 from MihailRis/preload-patterns
preload.json patterns
This commit is contained in:
commit
acfd1c107f
5 changed files with 378 additions and 66 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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": [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,8 +19,14 @@
|
|||
#include "util/ThreadPool.hpp"
|
||||
#include "voxels/Block.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
|
@ -151,24 +157,14 @@ static std::string assets_def_folder(AssetType tag) {
|
|||
return "<error>";
|
||||
}
|
||||
|
||||
void AssetsLoader::processPreload(
|
||||
AssetType tag, const std::string& name, const dv::value& map
|
||||
static std::shared_ptr<AssetCfg> 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<AssetCfg> 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<SoundCfg>(map.at("keep-pcm").get(keepPCM));
|
||||
break;
|
||||
map.at("keep-pcm").get(keepPCM);
|
||||
return std::make_shared<SoundCfg>(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<AtlasCfg>(type);
|
||||
break;
|
||||
return std::make_shared<AtlasCfg>(type);
|
||||
}
|
||||
case AssetType::POST_EFFECT: {
|
||||
bool advanced = false;
|
||||
map.at("advanced").get(advanced);
|
||||
config = std::make_shared<PostEffectCfg>(advanced);
|
||||
break;
|
||||
return std::make_shared<PostEffectCfg>(advanced);
|
||||
}
|
||||
case AssetType::MODEL: {
|
||||
bool squashed = false;
|
||||
map.at("squash").get(squashed);
|
||||
config = std::make_shared<ModelCfg>(squashed);
|
||||
break;
|
||||
return std::make_shared<ModelCfg>(squashed);
|
||||
}
|
||||
case AssetType::FONT: {
|
||||
int size = DEFAULT_FONT_SIZE;
|
||||
map.at("size").get(size);
|
||||
config = std::make_unique<FontCfg>(size);
|
||||
break;
|
||||
return std::make_shared<FontCfg>(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<std::string> 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<std::string> 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<std::string>& segments,
|
||||
size_t index,
|
||||
const std::string& relative,
|
||||
bool directories,
|
||||
std::vector<std::string>& 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<std::string> asset_search_folders(AssetType tag) {
|
||||
if (tag == AssetType::ANIMATION) {
|
||||
return {ANIMATION_FOLDER, ANIMATION_FOLDER_ALIAS};
|
||||
}
|
||||
return {assets_def_folder(tag)};
|
||||
}
|
||||
|
||||
static std::vector<std::string> 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<std::string> 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<unsigned char>(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<PreloadMatch> 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<PreloadMatch> 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<std::string, std::string> found;
|
||||
for (const auto& searchFolder : asset_search_folders(tag)) {
|
||||
io::path folder = packFolder / searchFolder;
|
||||
std::vector<std::string> 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<PreloadMatch> 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<AssetCfg> 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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue