refactor(tools): replace name_space parser with ResourceLocation

This commit is contained in:
2026-08-08 14:56:52 +08:00
parent 1baa2d6672
commit bc4bbe1ff2
4 changed files with 55 additions and 39 deletions

View File

@@ -1,19 +0,0 @@
#pragma once
#include <string_view>
#include <vector>
namespace Cubed {
inline std::vector<std::string_view> parse_namespace(std::string_view str) {
std::vector<std::string_view> space;
space.reserve(4);
std::size_t p = str.find(':');
std::size_t start = 0;
while (p != std::string_view::npos) {
space.emplace_back(str.substr(start, p));
start = p + 1;
p = str.find(':', p + 1);
}
space.emplace_back(str.substr(start));
return space;
}
} // namespace Cubed

View File

@@ -0,0 +1,36 @@
#pragma once
#include <cstddef>
#include <functional>
#include <optional>
#include <regex>
#include <string>
#include <string_view>
namespace Cubed {
struct ResourceLocation {
static constexpr std::string_view DEFAULT_NAMESPACE = "cubed";
std::string ns = std::string(DEFAULT_NAMESPACE);
std::string path;
std::string to_string() const { return ns + ":" + path; }
// Parses "ns:path"; ns defaults to "cubed" when no colon present.
static std::optional<ResourceLocation> parse(std::string_view str) {
std::regex pattern(R"([a-zA-Z0-9._:/-]+)");
if (!std::regex_match(str.begin(), str.end(), pattern)) {
return std::nullopt;
}
auto it = str.find(":");
if (it == std::string_view::npos) {
return ResourceLocation{std::string(DEFAULT_NAMESPACE),
std::string(str)};
}
return ResourceLocation{std::string(str.substr(0, it)),
std::string(str.substr(it + 1))};
}
bool operator==(const ResourceLocation& o) const {
return (ns == o.ns) && (path == o.path);
}
std::size_t hash() const { return std::hash<std::string>()(to_string()); }
};
} // namespace Cubed

View File

@@ -2,7 +2,7 @@
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
#include "Cubed/tools/name_space.hpp" #include "Cubed/tools/resource_location.hpp"
#include <rapidjson/document.h> #include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h> #include <rapidjson/istreamwrapper.h>
@@ -57,15 +57,18 @@ HitboxManager::Handle HitboxManager::get_hitbox(const std::string& name) {
} }
HitboxManager::Handle HitboxManager::load(std::string_view name) { HitboxManager::Handle HitboxManager::load(std::string_view name) {
auto space = parse_namespace(name); auto space = ResourceLocation::parse(name);
ASSERT(space.size() >= 2); if (!space) {
Logger::error("Can't load hitbox {}", name);
ASSERT(false);
}
fs::path p; fs::path p;
if (space[0] == "cubed") { if (space->ns == "cubed") {
p = std::format("{}model/creature/{}/collision.json", ASSETS_PATH, p = std::format("{}model/creature/{}/collision.json", ASSETS_PATH,
space[1]); space->path);
} else { } else {
p = std::format("{}/model/creature/{}/collision.json", space[0], p = std::format("{}/model/creature/{}/collision.json", space->ns,
space[1]); space->path);
} }
try { try {

View File

@@ -2,7 +2,7 @@
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
#include "Cubed/tools/name_space.hpp" #include "Cubed/tools/resource_location.hpp"
#include <filesystem> #include <filesystem>
#include <rapidjson/document.h> #include <rapidjson/document.h>
@@ -67,23 +67,19 @@ const std::string& ModelManager::get_model_name(ModelID id) {
} }
ModelManager::Handle ModelManager::load_model(std::string_view model_name) { ModelManager::Handle ModelManager::load_model(std::string_view model_name) {
auto p = model_name.find(':');
if (p == std::string::npos) { auto space = ResourceLocation::parse(model_name);
Logger::error("Can't Parse Model name {}", model_name); if (!space) {
ASSERT(false);
}
auto space = parse_namespace(model_name);
if (space.size() < 2) {
Logger::error("Can't Parse Model name {}", model_name); Logger::error("Can't Parse Model name {}", model_name);
ASSERT(false); ASSERT(false);
} }
std::string path; std::string path;
if (space[0] == "cubed") { if (space->ns == "cubed") {
path = std::format("{}model/creature/{}/{}.glb", ASSETS_PATH, space[1], path = std::format("{}model/creature/{}/{}.glb", ASSETS_PATH,
space[1]); space->path, space->path);
} else { } else {
path = std::format("./{}/model/creature/{}/{}.glb", space[0], space[1], path = std::format("./{}/model/creature/{}/{}.glb", space->ns,
space[1]); space->path, space->path);
} }
auto model = m_loader.load(path); auto model = m_loader.load(path);
fs::path anim_path = path; fs::path anim_path = path;