Debian Codegraph @

include/ideclmanager.h

raw
1 #pragma once
2
3 #include <map>
4 #include <sigc++/signal.h>
5 #include "imodule.h"
6 #include "ifilesystem.h"
7 #include "igameresource.h"
8 #include "idecltypes.h"
9
10 namespace decl
11 {
12
13 // Represents a declaration block as found in the various decl files
14 // Holds the name of the block, its typename and the raw block contents
15 // including whitespace and comments but exluding the outermost brace pair
16 struct DeclarationBlockSyntax : game::IResource
17 {
18 // The type name of this block (e.g. "table")
19 std::string typeName;
20
21 // The name of this block (e.g. "sinTable" or "textures/common/caulk"
22 std::string name;
23
24 // The block contents (excluding braces)
25 std::string contents;
26
27 // The mod this syntax has been defined in
28 std::string modName;
29
30 // The VFS info of the file this syntax is located
31 vfs::FileInfo fileInfo;
32
33 std::string getModName() const override
34 {
35 return modName;
36 }
37 };
38
39 // Common interface shared by all the declarations supported by a certain game type
40 class IDeclaration :
41 public game::IResource
42 {
43 public:
44 virtual ~IDeclaration()
45 {}
46
47 using Ptr = std::shared_ptr<IDeclaration>;
48
49 // The full name of this declaration, e.g. "textures/common/caulk"
50 virtual const std::string& getDeclName() const = 0;
51
52 // Change the name of this decl. Don't use this directly in client code, use
53 // GlobalDeclarationManager().renameDeclaration() instead.
54 virtual void setDeclName(const std::string& newName) = 0;
55
56 // The original full name of this declaration, e.g. "textures/common/caulk",
57 // as parsed from the decl file. This is used internally for saving/deleting
58 // declarations, client code usually wants to use getDeclName().
59 virtual const std::string& getOriginalDeclName() const = 0;
60
61 // Update the name of this decl as it appears in the decl file
62 // Used internally after saving a renamed decl.
63 virtual void setOriginalDeclName(const std::string& newName) = 0;
64
65 // The type of this declaration
66 virtual Type getDeclType() const = 0;
67
68 // The raw syntax block (without the outer curly braces) used to construct this decl
69 virtual const DeclarationBlockSyntax& getBlockSyntax() = 0;
70
71 // Set the block contents of this declaration.
72 // Implementations are free to either (re-)parse immediately or deferred.
73 virtual void setBlockSyntax(const DeclarationBlockSyntax& block) = 0;
74
75 // Returns the mod-relative path to the file this decl has been declared in
76 virtual std::string getDeclFilePath() const = 0;
77
78 // Sets the file info as contained in the block syntax
79 virtual void setFileInfo(const vfs::FileInfo& fileInfo) = 0;
80
81 // Returns the value of the internally used parse epoch counter
82 virtual std::size_t getParseStamp() const = 0;
83
84 // Sets the internally used parse epoch counter
85 virtual void setParseStamp(std::size_t parseStamp) = 0;
86
87 // Fired when this declaration changed (i.e. as result of a reloadDecls
88 // operation or a change in an editor).
89 virtual sigc::signal<void>& signal_DeclarationChanged() = 0;
90 };
91
92 // Factory interface being able to create a single declaration type
93 class IDeclarationCreator
94 {
95 public:
96 virtual ~IDeclarationCreator()
97 {}
98
99 using Ptr = std::shared_ptr<IDeclarationCreator>;
100
101 // Returns the declaration type this creator can handle
102 virtual Type getDeclType() const = 0;
103
104 // Creates an empty declaration with the given name
105 virtual IDeclaration::Ptr createDeclaration(const std::string& name) = 0;
106 };
107
108 // Central service class holding all the declarations in the active game,
109 // like entityDefs, materials, skins, etc.
110 // Searches registered VFS folders and parses the contents of the matching files
111 // making use of the associated IDeclarationParser instances.
112 class IDeclarationManager :
113 public RegisterableModule
114 {
115 public:
116 virtual ~IDeclarationManager() override
117 {}
118
119 // Registers the declaration typename (e.g. "material") and associates it with the given creator
120 // It's not allowed to register more than one creator for a single typename
121 // Note: The given typename will be treated case-insensitively by the decl parser.
122 virtual void registerDeclType(const std::string& typeName, const IDeclarationCreator::Ptr& creator) = 0;
123
124 // Unregisters the given typename and the associated creator
125 virtual void unregisterDeclType(const std::string& typeName) = 0;
126
127 // Associates the given VFS folder (with trailing slash) to a certain declaration type
128 // all files matching the given file extension (without dot) will be searched and parsed.
129 // The folder will not be recursively searched for files, only immediate children will be processed.
130 // Any untyped declaration blocks found in the files will be assumed to be of the given defaultType.
131 // All explicitly typed resources will be processed using the parser that has been previously
132 // associated in registerDeclType()
133 // Registering a folder will immediately trigger parsing of all contained files matching the criteria.
134 virtual void registerDeclFolder(Type defaultType, const std::string& vfsFolder, const std::string& extension) = 0;
135
136 // Find the declaration with the given type and name
137 // Returns an empty reference if no declaration with that name could be found
138 virtual IDeclaration::Ptr findDeclaration(Type type, const std::string& name) = 0;
139
140 // Find the declaration with the given type and name, or creates a default declaration
141 // of the given type if nothing was found. Will always return a non-empty reference.
142 // Throws std::invalid_argument exception of the type has not even been registered
143 virtual IDeclaration::Ptr findOrCreateDeclaration(Type type, const std::string& name) = 0;
144
145 // Iterate over all known declarations, using the given visitor
146 virtual void foreachDeclaration(Type type, const std::function<void(const IDeclaration::Ptr&)>& functor) = 0;
147
148 // Renames the declaration from oldName to newName. The new name must not be in use by any other declaration,
149 // and it must be different from oldName, otherwise renaming will fail.
150 // Returns true if the old declaration existed and could successfully be renamed, false on any failure.
151 virtual bool renameDeclaration(Type type, const std::string& oldName, const std::string& newName) = 0;
152
153 // Removes the given declaration both from memory and the decl file, if there is one.
154 // Only declarations stored in physical files (or unsaved ones) can be removed.
155 // Attempting to remove a read-only declaration (e.g. decls saved in PK4 archives)
156 // will cause a std::logic_error to be thrown.
157 virtual void removeDeclaration(Type type, const std::string& name) = 0;
158
159 // Re-load all declarations.
160 // All declaration references will stay intact, only their contents will be refreshed
161 virtual void reloadDeclarations() = 0;
162
163 // Saves the given declaration to a physical declaration file. Depending on the original location
164 // of the declaration the outcome will be different.
165 //
166 // It is required for the declaration to have valid file information set on its syntax block,
167 // otherwise the declaration manager will not know where to save it to and throws std::invalid_argument.
168 //
169 // Case #1: Newly created declarations (created through findOrCreateDeclaration):
170 // The decl will be appended to the given file. The file will be created if required.
171 // Case #2: Existing declarations (with their original file being a physical file):
172 // The decl in the file will be replaced with its new syntax block. All the content before and
173 // after the declaration will be left untouched.
174 // Case #3: Existing declarations (with their original file being stored within a PK4):
175 // The decl file will be copied and saved to its corresponding physical location, effectively
176 // creating a file that will override the one in the PK4. The decl will be merged into the file
177 // just like in case #2
178 virtual void saveDeclaration(const IDeclaration::Ptr& decl) = 0;
179
180 // Signal emitted right before decls are being reloaded
181 virtual sigc::signal<void>& signal_DeclsReloading(Type type) = 0;
182
183 // Signal emitted when the decls of the given type have been (re-)loaded
184 // Note that this signal can be fired on an arbitrary thread
185 virtual sigc::signal<void>& signal_DeclsReloaded(Type type) = 0;
186
187 // Signal emitted when a declaration is renamed
188 // The type, the old name and the new name will be passed as arguments
189 virtual sigc::signal<void(Type, const std::string&, const std::string&)>& signal_DeclRenamed() = 0;
190
191 // Signal emitted when a declaration has been created (e.g. by findOrCreateDeclaration),
192 // passing the type and name of the created decl as argument
193 virtual sigc::signal<void(Type, const std::string&)>& signal_DeclCreated() = 0;
194
195 // Signal emitted when a declaration has been removed (by removeDeclaration),
196 // passing the type and name of the removed decl as argument
197 virtual sigc::signal<void(Type, const std::string&)>& signal_DeclRemoved() = 0;
198 };
199
200 }
201
202 constexpr const char* const MODULE_DECLMANAGER("DeclarationManager");
203
204 inline decl::IDeclarationManager& GlobalDeclarationManager()
205 {
206 static module::InstanceReference<decl::IDeclarationManager> _reference(MODULE_DECLMANAGER);
207 return _reference;
208 }