Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
PrefabScanner.h
Go to the documentation of this file.
1/*
2 * PrefabScanner.h - Prefab Directory Scanner for Phase 2
3 *
4 * Responsible for scanning GameData/Prefab directory and building a registry
5 * of available prefabs with their resource dependencies.
6 *
7 * Cross-platform implementation (Windows/Unix) without std::filesystem.
8 */
9
10#pragma once
11
12#include <string>
13#include <vector>
14#include <map>
15#include <algorithm>
16#include "ComponentDefinition.h"
17
18// Resource references extracted from prefab
20{
21 std::vector<std::string> spriteRefs;
22 std::vector<std::string> audioRefs;
23 std::vector<std::string> modelRefs;
24};
25
26// Complete prefab blueprint with component definitions
28{
29 std::string prefabName;
30 std::string prefabType;
31 std::string filePath;
32 std::string version;
33 std::string description;
34 std::vector<ComponentDefinition> components;
36 bool isValid;
37 bool isDynamic = false;
38 std::vector<std::string> errors;
39 std::vector<std::string> categories; // Entity categories (RequiresRegistration, HasAI, etc.)
40
42
43 // Check if this prefab has a specific category
44 bool HasCategory(const std::string& category) const
45 {
46 return std::find(categories.begin(), categories.end(), category) != categories.end();
47 }
48
49 // Add a category to this prefab
50 void AddCategory(const std::string& category)
51 {
52 if (!HasCategory(category))
53 {
54 categories.push_back(category);
55 }
56 }
57};
58
59// Synonym information for a canonical type
61{
62 std::string canonicalType;
63 std::string description;
64 std::string prefabFile;
65 std::vector<std::string> synonyms;
66};
67
68// Registry of all discovered prefabs
70{
71public:
72 PrefabRegistry() = default;
73
75 const PrefabBlueprint* Find(const std::string& name) const;
76 std::vector<const PrefabBlueprint*> FindByType(const std::string& type) const;
77 std::vector<std::string> GetAllPrefabNames() const;
78 int GetCount() const { return static_cast<int>(m_blueprints.size()); }
79
80private:
81 std::map<std::string, PrefabBlueprint> m_blueprints; // name -> blueprint
82 std::map<std::string, std::string> m_typeToName; // type -> name
83};
84
85// PrefabScanner: Scan directory for prefabs
87{
88public:
91
92 // ========================================================================
93 // Modern API: Unified initialization with synonym support
94 // ========================================================================
95
96 /// Initialize the prefab system (call once at startup)
97 /// - Loads synonym registry
98 /// - Scans prefab directory recursively
99 /// - Builds registry
100 PrefabRegistry Initialize(const std::string& prefabDirectory = "Gamedata/EntityPrefab");
101
102 /// Normalize a type string to canonical form
103 std::string NormalizeType(const std::string& type) const;
104
105 /// Check if two types are equivalent
106 bool AreTypesEquivalent(const std::string& type1, const std::string& type2) const;
107
108 /// Check if a type is registered
109 bool IsTypeRegistered(const std::string& type) const;
110
111 /// Get canonical type info (for debugging)
112 bool GetCanonicalInfo(const std::string& type, std::string& outCanonical,
113 std::string& outPrefabFile) const;
114
115 // ========================================================================
116 // Legacy API (for backward compatibility)
117 // ========================================================================
118
119 std::vector<PrefabBlueprint> ScanDirectory(const std::string& rootPath);
120
121 PrefabBlueprint ParsePrefab(const std::string& filepath);
122
123 // Synonym registry methods
124 bool LoadSynonymRegistry(const std::string& directory);
125 std::string ExtractPrefabType(const nlohmann::json& prefabJson);
126 float FuzzyMatch(const std::string& str1, const std::string& str2) const;
127
128 // Recursive directory scanning (platform-specific)
129#ifdef _WIN32
130 void ScanDirectoryRecursive_Windows(const std::string& path, std::vector<std::string>& outFiles);
131#else
132 void ScanDirectoryRecursive_Unix(const std::string& path, std::vector<std::string>& outFiles);
133#endif
134
135 // Helper methods
136 std::string DetectComponentType(const std::string& typeName);
138 std::string GetFilename(const std::string& filepath);
139 std::string RemoveExtension(const std::string& filename);
140 std::string ToUpper(const std::string& str) const;
141
142private:
143 // Internal data for synonym system
144 std::map<std::string, std::string> m_synonymToCanonical; // synonym/type -> canonical
145 std::map<std::string, SynonymInfo> m_canonicalTypes; // canonical -> info
146 std::map<std::string, std::vector<std::string>> m_categoryToTypes; // category -> list of types
147 bool m_caseSensitive = false;
149 float m_fuzzyThreshold = 0.8f;
151};
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
const PrefabBlueprint * Find(const std::string &name) const
PrefabRegistry()=default
std::map< std::string, std::string > m_typeToName
std::vector< const PrefabBlueprint * > FindByType(const std::string &type) const
std::vector< std::string > GetAllPrefabNames() const
void Register(const PrefabBlueprint &blueprint)
int GetCount() const
std::map< std::string, PrefabBlueprint > m_blueprints
std::string NormalizeType(const std::string &type) const
Normalize a type string to canonical form.
PrefabBlueprint ParsePrefab(const std::string &filepath)
std::vector< PrefabBlueprint > ScanDirectory(const std::string &rootPath)
std::map< std::string, std::string > m_synonymToCanonical
std::string DetectComponentType(const std::string &typeName)
std::string ToUpper(const std::string &str) const
std::string ExtractPrefabType(const nlohmann::json &prefabJson)
bool LoadSynonymRegistry(const std::string &directory)
bool GetCanonicalInfo(const std::string &type, std::string &outCanonical, std::string &outPrefabFile) const
Get canonical type info (for debugging)
std::map< std::string, std::vector< std::string > > m_categoryToTypes
PrefabRegistry Initialize(const std::string &prefabDirectory="Gamedata/EntityPrefab")
Initialize the prefab system (call once at startup)
bool IsTypeRegistered(const std::string &type) const
Check if a type is registered.
void ExtractResources(const nlohmann::json &componentsJson, ResourceRefs &outResources)
bool AreTypesEquivalent(const std::string &type1, const std::string &type2) const
Check if two types are equivalent.
bool m_enableFuzzyMatching
std::string RemoveExtension(const std::string &filename)
std::string GetFilename(const std::string &filepath)
float FuzzyMatch(const std::string &str1, const std::string &str2) const
void ScanDirectoryRecursive_Unix(const std::string &path, std::vector< std::string > &outFiles)
std::map< std::string, SynonymInfo > m_canonicalTypes
nlohmann::json json
std::vector< std::string > errors
void AddCategory(const std::string &category)
std::string prefabName
std::vector< ComponentDefinition > components
std::string prefabType
std::string filePath
std::string version
std::string description
bool HasCategory(const std::string &category) const
ResourceRefs resources
std::vector< std::string > categories
std::vector< std::string > audioRefs
std::vector< std::string > modelRefs
std::vector< std::string > spriteRefs
std::string canonicalType
std::string description
std::string prefabFile
std::vector< std::string > synonyms