DeclManager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __DECLMANAGER_H__
  21. #define __DECLMANAGER_H__
  22. /*
  23. ===============================================================================
  24. Declaration Manager
  25. All "small text" data types, like materials, sound shaders, fx files,
  26. entity defs, etc. are managed uniformly, allowing reloading, purging,
  27. listing, printing, etc. All "large text" data types that never have more
  28. than one declaration in a given file, like maps, models, AAS files, etc.
  29. are not handled here.
  30. A decl will never, ever go away once it is created. The manager is
  31. garranteed to always return the same decl pointer for a decl type/name
  32. combination. The index of a decl in the per type list also stays the
  33. same throughout the lifetime of the engine. Although the pointer to
  34. a decl always stays the same, one should never maintain pointers to
  35. data inside decls. The data stored in a decl is not garranteed to stay
  36. the same for more than one engine frame.
  37. The decl indexes of explicitely defined decls are garrenteed to be
  38. consistent based on the parsed decl files. However, the indexes of
  39. implicit decls may be different based on the order in which levels
  40. are loaded.
  41. The decl namespaces are separate for each type. Comments for decls go
  42. above the text definition to keep them associated with the proper decl.
  43. During decl parsing, errors should never be issued, only warnings
  44. followed by a call to MakeDefault().
  45. ===============================================================================
  46. */
  47. typedef enum {
  48. DECL_TABLE = 0,
  49. DECL_MATERIAL,
  50. DECL_SKIN,
  51. DECL_SOUND,
  52. DECL_ENTITYDEF,
  53. DECL_MODELDEF,
  54. DECL_FX,
  55. DECL_PARTICLE,
  56. DECL_AF,
  57. DECL_PDA,
  58. DECL_VIDEO,
  59. DECL_AUDIO,
  60. DECL_EMAIL,
  61. DECL_MODELEXPORT,
  62. DECL_MAPDEF,
  63. // new decl types can be added here
  64. DECL_MAX_TYPES = 32
  65. } declType_t;
  66. typedef enum {
  67. DS_UNPARSED,
  68. DS_DEFAULTED, // set if a parse failed due to an error, or the lack of any source
  69. DS_PARSED
  70. } declState_t;
  71. const int DECL_LEXER_FLAGS = LEXFL_NOSTRINGCONCAT | // multiple strings seperated by whitespaces are not concatenated
  72. LEXFL_NOSTRINGESCAPECHARS | // no escape characters inside strings
  73. LEXFL_ALLOWPATHNAMES | // allow path seperators in names
  74. LEXFL_ALLOWMULTICHARLITERALS | // allow multi character literals
  75. LEXFL_ALLOWBACKSLASHSTRINGCONCAT | // allow multiple strings seperated by '\' to be concatenated
  76. LEXFL_NOFATALERRORS; // just set a flag instead of fatal erroring
  77. class idDeclBase {
  78. public:
  79. virtual ~idDeclBase() {};
  80. virtual const char * GetName() const = 0;
  81. virtual declType_t GetType() const = 0;
  82. virtual declState_t GetState() const = 0;
  83. virtual bool IsImplicit() const = 0;
  84. virtual bool IsValid() const = 0;
  85. virtual void Invalidate() = 0;
  86. virtual void Reload() = 0;
  87. virtual void EnsureNotPurged() = 0;
  88. virtual int Index() const = 0;
  89. virtual int GetLineNum() const = 0;
  90. virtual const char * GetFileName() const = 0;
  91. virtual void GetText( char *text ) const = 0;
  92. virtual int GetTextLength() const = 0;
  93. virtual void SetText( const char *text ) = 0;
  94. virtual bool ReplaceSourceFileText() = 0;
  95. virtual bool SourceFileChanged() const = 0;
  96. virtual void MakeDefault() = 0;
  97. virtual bool EverReferenced() const = 0;
  98. virtual bool SetDefaultText() = 0;
  99. virtual const char * DefaultDefinition() const = 0;
  100. virtual bool Parse( const char *text, const int textLength, bool allowBinaryVersion ) = 0;
  101. virtual void FreeData() = 0;
  102. virtual size_t Size() const = 0;
  103. virtual void List() const = 0;
  104. virtual void Print() const = 0;
  105. };
  106. class idDecl {
  107. public:
  108. // The constructor should initialize variables such that
  109. // an immediate call to FreeData() does no harm.
  110. idDecl() { base = NULL; }
  111. virtual ~idDecl() {};
  112. // Returns the name of the decl.
  113. const char * GetName() const { return base->GetName(); }
  114. // Returns the decl type.
  115. declType_t GetType() const { return base->GetType(); }
  116. // Returns the decl state which is usefull for finding out if a decl defaulted.
  117. declState_t GetState() const { return base->GetState(); }
  118. // Returns true if the decl was defaulted or the text was created with a call to SetDefaultText.
  119. bool IsImplicit() const { return base->IsImplicit(); }
  120. // The only way non-manager code can have an invalid decl is if the *ByIndex()
  121. // call was used with forceParse = false to walk the lists to look at names
  122. // without touching the media.
  123. bool IsValid() const { return base->IsValid(); }
  124. // Sets state back to unparsed.
  125. // Used by decl editors to undo any changes to the decl.
  126. void Invalidate() { base->Invalidate(); }
  127. // if a pointer might possible be stale from a previous level,
  128. // call this to have it re-parsed
  129. void EnsureNotPurged() { base->EnsureNotPurged(); }
  130. // Returns the index in the per-type list.
  131. int Index() const { return base->Index(); }
  132. // Returns the line number the decl starts.
  133. int GetLineNum() const { return base->GetLineNum(); }
  134. // Returns the name of the file in which the decl is defined.
  135. const char * GetFileName() const { return base->GetFileName(); }
  136. // Returns the decl text.
  137. void GetText( char *text ) const { base->GetText( text ); }
  138. // Returns the length of the decl text.
  139. int GetTextLength() const { return base->GetTextLength(); }
  140. // Sets new decl text.
  141. void SetText( const char *text ) { base->SetText( text ); }
  142. // Saves out new text for the decl.
  143. // Used by decl editors to replace the decl text in the source file.
  144. bool ReplaceSourceFileText() { return base->ReplaceSourceFileText(); }
  145. // Returns true if the source file changed since it was loaded and parsed.
  146. bool SourceFileChanged() const { return base->SourceFileChanged(); }
  147. // Frees data and makes the decl a default.
  148. void MakeDefault() { base->MakeDefault(); }
  149. // Returns true if the decl was ever referenced.
  150. bool EverReferenced() const { return base->EverReferenced(); }
  151. public:
  152. // Sets textSource to a default text if necessary.
  153. // This may be overridden to provide a default definition based on the
  154. // decl name. For instance materials may default to an implicit definition
  155. // using a texture with the same name as the decl.
  156. virtual bool SetDefaultText() { return base->SetDefaultText(); }
  157. // Each declaration type must have a default string that it is guaranteed
  158. // to parse acceptably. When a decl is not explicitly found, is purged, or
  159. // has an error while parsing, MakeDefault() will do a FreeData(), then a
  160. // Parse() with DefaultDefinition(). The defaultDefintion should start with
  161. // an open brace and end with a close brace.
  162. virtual const char * DefaultDefinition() const { return base->DefaultDefinition(); }
  163. // The manager will have already parsed past the type, name and opening brace.
  164. // All necessary media will be touched before return.
  165. // The manager will have called FreeData() before issuing a Parse().
  166. // The subclass can call MakeDefault() internally at any point if
  167. // there are parse errors.
  168. virtual bool Parse( const char *text, const int textLength, bool allowBinaryVersion = false ) { return base->Parse( text, textLength, allowBinaryVersion ); }
  169. // Frees any pointers held by the subclass. This may be called before
  170. // any Parse(), so the constructor must have set sane values. The decl will be
  171. // invalid after issuing this call, but it will always be immediately followed
  172. // by a Parse()
  173. virtual void FreeData() { base->FreeData(); }
  174. // Returns the size of the decl in memory.
  175. virtual size_t Size() const { return base->Size(); }
  176. // If this isn't overridden, it will just print the decl name.
  177. // The manager will have printed 7 characters on the line already,
  178. // containing the reference state and index number.
  179. virtual void List() const { base->List(); }
  180. // The print function will already have dumped the text source
  181. // and common data, subclasses can override this to dump more
  182. // explicit data.
  183. virtual void Print() const { base->Print(); }
  184. public:
  185. idDeclBase * base;
  186. };
  187. template< class type >
  188. ID_INLINE idDecl *idDeclAllocator() {
  189. return new (TAG_DECL) type;
  190. }
  191. class idMaterial;
  192. class idDeclSkin;
  193. class idSoundShader;
  194. class idDeclManager {
  195. public:
  196. virtual ~idDeclManager() {}
  197. virtual void Init() = 0;
  198. virtual void Init2() = 0;
  199. virtual void Shutdown() = 0;
  200. virtual void Reload( bool force ) = 0;
  201. virtual void BeginLevelLoad() = 0;
  202. virtual void EndLevelLoad() = 0;
  203. // Registers a new decl type.
  204. virtual void RegisterDeclType( const char *typeName, declType_t type, idDecl *(*allocator)() ) = 0;
  205. // Registers a new folder with decl files.
  206. virtual void RegisterDeclFolder( const char *folder, const char *extension, declType_t defaultType ) = 0;
  207. // Returns a checksum for all loaded decl text.
  208. virtual int GetChecksum() const = 0;
  209. // Returns the number of decl types.
  210. virtual int GetNumDeclTypes() const = 0;
  211. // Returns the type name for a decl type.
  212. virtual const char * GetDeclNameFromType( declType_t type ) const = 0;
  213. // Returns the decl type for a type name.
  214. virtual declType_t GetDeclTypeFromName( const char *typeName ) const = 0;
  215. // If makeDefault is true, a default decl of appropriate type will be created
  216. // if an explicit one isn't found. If makeDefault is false, NULL will be returned
  217. // if the decl wasn't explcitly defined.
  218. virtual const idDecl * FindType( declType_t type, const char *name, bool makeDefault = true ) = 0;
  219. virtual const idDecl* FindDeclWithoutParsing( declType_t type, const char *name, bool makeDefault = true ) = 0;
  220. virtual void ReloadFile( const char* filename, bool force ) = 0;
  221. // Returns the number of decls of the given type.
  222. virtual int GetNumDecls( declType_t type ) = 0;
  223. // The complete lists of decls can be walked to populate editor browsers.
  224. // If forceParse is set false, you can get the decl to check name / filename / etc.
  225. // without causing it to parse the source and load media.
  226. virtual const idDecl * DeclByIndex( declType_t type, int index, bool forceParse = true ) = 0;
  227. // List and print decls.
  228. virtual void ListType( const idCmdArgs &args, declType_t type ) = 0;
  229. virtual void PrintType( const idCmdArgs &args, declType_t type ) = 0;
  230. // Creates a new default decl of the given type with the given name in
  231. // the given file used by editors to create a new decls.
  232. virtual idDecl * CreateNewDecl( declType_t type, const char *name, const char *fileName ) = 0;
  233. // BSM - Added for the material editors rename capabilities
  234. virtual bool RenameDecl( declType_t type, const char* oldName, const char* newName ) = 0;
  235. // When media files are loaded, a reference line can be printed at a
  236. // proper indentation if decl_show is set
  237. virtual void MediaPrint( VERIFY_FORMAT_STRING const char *fmt, ... ) = 0;
  238. virtual void WritePrecacheCommands( idFile *f ) = 0;
  239. // Convenience functions for specific types.
  240. virtual const idMaterial * FindMaterial( const char *name, bool makeDefault = true ) = 0;
  241. virtual const idDeclSkin * FindSkin( const char *name, bool makeDefault = true ) = 0;
  242. virtual const idSoundShader * FindSound( const char *name, bool makeDefault = true ) = 0;
  243. virtual const idMaterial * MaterialByIndex( int index, bool forceParse = true ) = 0;
  244. virtual const idDeclSkin * SkinByIndex( int index, bool forceParse = true ) = 0;
  245. virtual const idSoundShader * SoundByIndex( int index, bool forceParse = true ) = 0;
  246. virtual void Touch( const idDecl * decl ) = 0;
  247. };
  248. extern idDeclManager * declManager;
  249. template< declType_t type >
  250. ID_INLINE void idListDecls_f( const idCmdArgs &args ) {
  251. declManager->ListType( args, type );
  252. }
  253. template< declType_t type >
  254. ID_INLINE void idPrintDecls_f( const idCmdArgs &args ) {
  255. declManager->PrintType( args, type );
  256. }
  257. #endif /* !__DECLMANAGER_H__ */