refactor: eliminar ShapeLoader::resolvePath i BASE_PATH (codi mort)
Cap caller invocava resolvePath fora de la seua pròpia definició. A més, BASE_PATH apuntava a "data/shapes/" mentre que load() ja construeix el path amb el prefix "shapes/" directament — el helper mai s'hauria activat encara que es cridara. Hallazgo #18 de CODE_REVIEW.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,77 +9,62 @@
|
|||||||
|
|
||||||
namespace Graphics {
|
namespace Graphics {
|
||||||
|
|
||||||
// Inicialización de variables estàtiques
|
// Inicialización de variables estàtiques
|
||||||
std::unordered_map<std::string, std::shared_ptr<Shape>> ShapeLoader::cache;
|
std::unordered_map<std::string, std::shared_ptr<Shape>> ShapeLoader::cache;
|
||||||
|
|
||||||
auto ShapeLoader::load(const std::string& filename) -> std::shared_ptr<Shape> {
|
auto ShapeLoader::load(const std::string& filename) -> std::shared_ptr<Shape> {
|
||||||
// Check cache first
|
// Check cache first
|
||||||
auto it = cache.find(filename);
|
auto it = cache.find(filename);
|
||||||
if (it != cache.end()) {
|
if (it != cache.end()) {
|
||||||
std::cout << "[ShapeLoader] Cache hit: " << filename << '\n';
|
std::cout << "[ShapeLoader] Cache hit: " << filename << '\n';
|
||||||
return it->second; // Cache hit
|
return it->second; // Cache hit
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize path: "ship.shp" → "shapes/ship.shp"
|
||||||
|
// "logo/letra_j.shp" → "shapes/logo/letra_j.shp"
|
||||||
|
std::string normalized = filename;
|
||||||
|
if (!normalized.starts_with("shapes/")) {
|
||||||
|
// Doesn't start with "shapes/", so add it
|
||||||
|
normalized = "shapes/" + normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load from resource system
|
||||||
|
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
||||||
|
if (data.empty()) {
|
||||||
|
std::cerr << "[ShapeLoader] Error: no s'ha pogut load " << normalized
|
||||||
|
<< '\n';
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert bytes to string and parse
|
||||||
|
std::string file_content(data.begin(), data.end());
|
||||||
|
auto shape = std::make_shared<Shape>();
|
||||||
|
if (!shape->parseFile(file_content)) {
|
||||||
|
std::cerr << "[ShapeLoader] Error: no s'ha pogut parsejar " << normalized
|
||||||
|
<< '\n';
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify shape is valid
|
||||||
|
if (!shape->isValid()) {
|
||||||
|
std::cerr << "[ShapeLoader] Error: shape invàlida " << normalized << '\n';
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache and return
|
||||||
|
std::cout << "[ShapeLoader] Carregat: " << normalized << " (" << shape->getName()
|
||||||
|
<< ", " << shape->getNumPrimitives() << " primitives)" << '\n';
|
||||||
|
|
||||||
|
cache[filename] = shape;
|
||||||
|
return shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize path: "ship.shp" → "shapes/ship.shp"
|
void ShapeLoader::clearCache() {
|
||||||
// "logo/letra_j.shp" → "shapes/logo/letra_j.shp"
|
std::cout << "[ShapeLoader] Netejant caché (" << cache.size() << " formes)"
|
||||||
std::string normalized = filename;
|
|
||||||
if (!normalized.starts_with("shapes/")) {
|
|
||||||
// Doesn't start with "shapes/", so add it
|
|
||||||
normalized = "shapes/" + normalized;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load from resource system
|
|
||||||
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
|
||||||
if (data.empty()) {
|
|
||||||
std::cerr << "[ShapeLoader] Error: no s'ha pogut load " << normalized
|
|
||||||
<< '\n';
|
<< '\n';
|
||||||
return nullptr;
|
cache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert bytes to string and parse
|
auto ShapeLoader::getCacheSize() -> size_t { return cache.size(); }
|
||||||
std::string file_content(data.begin(), data.end());
|
|
||||||
auto shape = std::make_shared<Shape>();
|
|
||||||
if (!shape->parseFile(file_content)) {
|
|
||||||
std::cerr << "[ShapeLoader] Error: no s'ha pogut parsejar " << normalized
|
|
||||||
<< '\n';
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify shape is valid
|
|
||||||
if (!shape->isValid()) {
|
|
||||||
std::cerr << "[ShapeLoader] Error: shape invàlida " << normalized << '\n';
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cache and return
|
|
||||||
std::cout << "[ShapeLoader] Carregat: " << normalized << " (" << shape->getName()
|
|
||||||
<< ", " << shape->getNumPrimitives() << " primitives)" << '\n';
|
|
||||||
|
|
||||||
cache[filename] = shape;
|
|
||||||
return shape;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShapeLoader::clearCache() {
|
|
||||||
std::cout << "[ShapeLoader] Netejant caché (" << cache.size() << " formes)"
|
|
||||||
<< '\n';
|
|
||||||
cache.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto ShapeLoader::getCacheSize() -> size_t { return cache.size(); }
|
|
||||||
|
|
||||||
auto ShapeLoader::resolvePath(const std::string& filename) -> std::string {
|
|
||||||
// Si es un path absolut (comença con '/'), usar-lo directament
|
|
||||||
if (!filename.empty() && filename[0] == '/') {
|
|
||||||
return filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Si ya conté el prefix base_path, usar-lo directament
|
|
||||||
if (filename.starts_with(BASE_PATH)) {
|
|
||||||
return filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Altrament, añadir base_path (ara suporta subdirectoris)
|
|
||||||
return std::string(BASE_PATH) + filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Graphics
|
} // namespace Graphics
|
||||||
|
|||||||
@@ -11,9 +11,9 @@
|
|||||||
|
|
||||||
namespace Graphics {
|
namespace Graphics {
|
||||||
|
|
||||||
// Carregador estàtic de formes con caché
|
// Carregador estàtic de formes con caché
|
||||||
class ShapeLoader {
|
class ShapeLoader {
|
||||||
public:
|
public:
|
||||||
// No instanciable (tot estàtic)
|
// No instanciable (tot estàtic)
|
||||||
ShapeLoader() = delete;
|
ShapeLoader() = delete;
|
||||||
|
|
||||||
@@ -28,12 +28,8 @@ class ShapeLoader {
|
|||||||
// Estadístiques (debug)
|
// Estadístiques (debug)
|
||||||
static auto getCacheSize() -> size_t;
|
static auto getCacheSize() -> size_t;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::unordered_map<std::string, std::shared_ptr<Shape>> cache;
|
static std::unordered_map<std::string, std::shared_ptr<Shape>> cache;
|
||||||
static constexpr const char* BASE_PATH = "data/shapes/";
|
};
|
||||||
|
|
||||||
// Helpers privats
|
|
||||||
static auto resolvePath(const std::string& filename) -> std::string;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Graphics
|
} // namespace Graphics
|
||||||
|
|||||||
Reference in New Issue
Block a user