57 lines
1.8 KiB
C++
57 lines
1.8 KiB
C++
// entity_loader.cpp - Implementació del carregador d'entitats YAML
|
|
// © 2026 JailDesigner
|
|
|
|
#include "core/entities/entity_loader.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "core/resources/resource_helper.hpp"
|
|
|
|
namespace Entities {
|
|
|
|
std::unordered_map<std::string, std::shared_ptr<fkyaml::node>> EntityLoader::cache;
|
|
|
|
auto EntityLoader::load(const std::string& name) -> std::shared_ptr<fkyaml::node> {
|
|
// Cache hit
|
|
auto it = cache.find(name);
|
|
if (it != cache.end()) {
|
|
std::cout << "[EntityLoader] Cache hit: " << name << '\n';
|
|
return it->second;
|
|
}
|
|
|
|
const std::string PATH = "entities/" + name + "/" + name + ".yaml";
|
|
|
|
std::vector<uint8_t> data = Resource::Helper::loadFile(PATH);
|
|
if (data.empty()) {
|
|
std::cerr << "[EntityLoader] Error: no s'ha pogut load " << PATH << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
try {
|
|
std::string yaml_content(data.begin(), data.end());
|
|
std::stringstream stream(yaml_content);
|
|
auto node = std::make_shared<fkyaml::node>(fkyaml::node::deserialize(stream));
|
|
|
|
std::cout << "[EntityLoader] Carregat: " << PATH << '\n';
|
|
cache[name] = node;
|
|
return node;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "[EntityLoader] Excepció parsejant " << PATH << ": " << e.what() << '\n';
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
void EntityLoader::clearCache() {
|
|
std::cout << "[EntityLoader] Netejant caché (" << cache.size() << " entitats)" << '\n';
|
|
cache.clear();
|
|
}
|
|
|
|
auto EntityLoader::getCacheSize() -> size_t { return cache.size(); }
|
|
|
|
} // namespace Entities
|