DeclManager.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. GUIs and script remain separately parsed
  24. Following a parse, all referenced media (and other decls) will have been touched.
  25. sinTable and cosTable are required for the rotate material keyword to function
  26. A new FindType on a purged decl will cause it to be reloaded, but a stale pointer to a purged
  27. decl will look like a defaulted decl.
  28. Moving a decl from one file to another will not be handled correctly by a reload, the material
  29. will be defaulted.
  30. NULL or empty decl names will always return NULL
  31. Should probably make a default decl for this
  32. Decls are initially created without a textSource
  33. A parse without textSource set should always just call MakeDefault()
  34. A parse that has an error should internally call MakeDefault()
  35. A purge does nothing to a defaulted decl
  36. Should we have a "purged" media state separate from the "defaulted" media state?
  37. reloading over a decl name that was defaulted
  38. reloading over a decl name that was valid
  39. missing reload over a previously explicit definition
  40. */
  41. #define USE_COMPRESSED_DECLS
  42. //#define GET_HUFFMAN_FREQUENCIES
  43. class idDeclType {
  44. public:
  45. idStr typeName;
  46. declType_t type;
  47. idDecl * (*allocator)( void );
  48. };
  49. class idDeclFolder {
  50. public:
  51. idStr folder;
  52. idStr extension;
  53. declType_t defaultType;
  54. };
  55. class idDeclFile;
  56. class idDeclLocal : public idDeclBase {
  57. friend class idDeclFile;
  58. friend class idDeclManagerLocal;
  59. public:
  60. idDeclLocal();
  61. virtual ~idDeclLocal() {};
  62. virtual const char * GetName( void ) const;
  63. virtual declType_t GetType( void ) const;
  64. virtual declState_t GetState( void ) const;
  65. virtual bool IsImplicit( void ) const;
  66. virtual bool IsValid( void ) const;
  67. virtual void Invalidate( void );
  68. virtual void Reload( void );
  69. virtual void EnsureNotPurged( void );
  70. virtual int Index( void ) const;
  71. virtual int GetLineNum( void ) const;
  72. virtual const char * GetFileName( void ) const;
  73. virtual size_t Size( void ) const;
  74. virtual void GetText( char *text ) const;
  75. virtual int GetTextLength( void ) const;
  76. virtual void SetText( const char *text );
  77. virtual bool ReplaceSourceFileText( void );
  78. virtual bool SourceFileChanged( void ) const;
  79. virtual void MakeDefault( void );
  80. virtual bool EverReferenced( void ) const;
  81. protected:
  82. virtual bool SetDefaultText( void );
  83. virtual const char * DefaultDefinition( void ) const;
  84. virtual bool Parse( const char *text, const int textLength );
  85. virtual void FreeData( void );
  86. virtual void List( void ) const;
  87. virtual void Print( void ) const;
  88. protected:
  89. void AllocateSelf( void );
  90. // Parses the decl definition.
  91. // After calling parse, a decl will be guaranteed usable.
  92. void ParseLocal( void );
  93. // Does a MakeDefualt, but flags the decl so that it
  94. // will Parse() the next time the decl is found.
  95. void Purge( void );
  96. // Set textSource possible with compression.
  97. void SetTextLocal( const char *text, const int length );
  98. private:
  99. idDecl * self;
  100. idStr name; // name of the decl
  101. char * textSource; // decl text definition
  102. int textLength; // length of textSource
  103. int compressedLength; // compressed length
  104. idDeclFile * sourceFile; // source file in which the decl was defined
  105. int sourceTextOffset; // offset in source file to decl text
  106. int sourceTextLength; // length of decl text in source file
  107. int sourceLine; // this is where the actual declaration token starts
  108. int checksum; // checksum of the decl text
  109. declType_t type; // decl type
  110. declState_t declState; // decl state
  111. int index; // index in the per-type list
  112. bool parsedOutsideLevelLoad; // these decls will never be purged
  113. bool everReferenced; // set to true if the decl was ever used
  114. bool referencedThisLevel; // set to true when the decl is used for the current level
  115. bool redefinedInReload; // used during file reloading to make sure a decl that has
  116. // its source removed will be defaulted
  117. idDeclLocal * nextInFile; // next decl in the decl file
  118. };
  119. class idDeclFile {
  120. public:
  121. idDeclFile();
  122. idDeclFile( const char *fileName, declType_t defaultType );
  123. void Reload( bool force );
  124. int LoadAndParse();
  125. public:
  126. idStr fileName;
  127. declType_t defaultType;
  128. ID_TIME_T timestamp;
  129. int checksum;
  130. int fileSize;
  131. int numLines;
  132. idDeclLocal * decls;
  133. };
  134. class idDeclManagerLocal : public idDeclManager {
  135. friend class idDeclLocal;
  136. public:
  137. virtual void Init( void );
  138. virtual void Shutdown( void );
  139. virtual void Reload( bool force );
  140. virtual void BeginLevelLoad();
  141. virtual void EndLevelLoad();
  142. virtual void RegisterDeclType( const char *typeName, declType_t type, idDecl *(*allocator)( void ) );
  143. virtual void RegisterDeclFolder( const char *folder, const char *extension, declType_t defaultType );
  144. virtual int GetChecksum( void ) const;
  145. virtual int GetNumDeclTypes( void ) const;
  146. virtual int GetNumDecls( declType_t type );
  147. virtual const char * GetDeclNameFromType( declType_t type ) const;
  148. virtual declType_t GetDeclTypeFromName( const char *typeName ) const;
  149. virtual const idDecl * FindType( declType_t type, const char *name, bool makeDefault = true );
  150. virtual const idDecl * DeclByIndex( declType_t type, int index, bool forceParse = true );
  151. virtual const idDecl* FindDeclWithoutParsing( declType_t type, const char *name, bool makeDefault = true );
  152. virtual void ReloadFile( const char* filename, bool force );
  153. virtual void ListType( const idCmdArgs &args, declType_t type );
  154. virtual void PrintType( const idCmdArgs &args, declType_t type );
  155. virtual idDecl * CreateNewDecl( declType_t type, const char *name, const char *fileName );
  156. //BSM Added for the material editors rename capabilities
  157. virtual bool RenameDecl( declType_t type, const char* oldName, const char* newName );
  158. virtual void MediaPrint( const char *fmt, ... ) id_attribute((format(printf,2,3)));
  159. virtual void WritePrecacheCommands( idFile *f );
  160. virtual const idMaterial * FindMaterial( const char *name, bool makeDefault = true );
  161. virtual const idDeclSkin * FindSkin( const char *name, bool makeDefault = true );
  162. virtual const idSoundShader * FindSound( const char *name, bool makeDefault = true );
  163. virtual const idMaterial * MaterialByIndex( int index, bool forceParse = true );
  164. virtual const idDeclSkin * SkinByIndex( int index, bool forceParse = true );
  165. virtual const idSoundShader * SoundByIndex( int index, bool forceParse = true );
  166. public:
  167. static void MakeNameCanonical( const char *name, char *result, int maxLength );
  168. idDeclLocal * FindTypeWithoutParsing( declType_t type, const char *name, bool makeDefault = true );
  169. idDeclType * GetDeclType( int type ) const { return declTypes[type]; }
  170. const idDeclFile * GetImplicitDeclFile( void ) const { return &implicitDecls; }
  171. private:
  172. idList<idDeclType *> declTypes;
  173. idList<idDeclFolder *> declFolders;
  174. idList<idDeclFile *> loadedFiles;
  175. idHashIndex hashTables[DECL_MAX_TYPES];
  176. idList<idDeclLocal *> linearLists[DECL_MAX_TYPES];
  177. idDeclFile implicitDecls; // this holds all the decls that were created because explicit
  178. // text definitions were not found. Decls that became default
  179. // because of a parse error are not in this list.
  180. int checksum; // checksum of all loaded decl text
  181. int indent; // for MediaPrint
  182. bool insideLevelLoad;
  183. static idCVar decl_show;
  184. private:
  185. static void ListDecls_f( const idCmdArgs &args );
  186. static void ReloadDecls_f( const idCmdArgs &args );
  187. static void TouchDecl_f( const idCmdArgs &args );
  188. };
  189. idCVar idDeclManagerLocal::decl_show( "decl_show", "0", CVAR_SYSTEM, "set to 1 to print parses, 2 to also print references", 0, 2, idCmdSystem::ArgCompletion_Integer<0,2> );
  190. idDeclManagerLocal declManagerLocal;
  191. idDeclManager * declManager = &declManagerLocal;
  192. /*
  193. ====================================================================================
  194. decl text huffman compression
  195. ====================================================================================
  196. */
  197. const int MAX_HUFFMAN_SYMBOLS = 256;
  198. typedef struct huffmanNode_s {
  199. int symbol;
  200. int frequency;
  201. struct huffmanNode_s * next;
  202. struct huffmanNode_s * children[2];
  203. } huffmanNode_t;
  204. typedef struct huffmanCode_s {
  205. unsigned long bits[8];
  206. int numBits;
  207. } huffmanCode_t;
  208. // compression ratio = 64%
  209. static int huffmanFrequencies[] = {
  210. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  211. 0x00000001, 0x00078fb6, 0x000352a7, 0x00000002, 0x00000001, 0x0002795e, 0x00000001, 0x00000001,
  212. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  213. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  214. 0x00049600, 0x000000dd, 0x00018732, 0x0000005a, 0x00000007, 0x00000092, 0x0000000a, 0x00000919,
  215. 0x00002dcf, 0x00002dda, 0x00004dfc, 0x0000039a, 0x000058be, 0x00002d13, 0x00014d8c, 0x00023c60,
  216. 0x0002ddb0, 0x0000d1fc, 0x000078c4, 0x00003ec7, 0x00003113, 0x00006b59, 0x00002499, 0x0000184a,
  217. 0x0000250b, 0x00004e38, 0x000001ca, 0x00000011, 0x00000020, 0x000023da, 0x00000012, 0x00000091,
  218. 0x0000000b, 0x00000b14, 0x0000035d, 0x0000137e, 0x000020c9, 0x00000e11, 0x000004b4, 0x00000737,
  219. 0x000006b8, 0x00001110, 0x000006b3, 0x000000fe, 0x00000f02, 0x00000d73, 0x000005f6, 0x00000be4,
  220. 0x00000d86, 0x0000014d, 0x00000d89, 0x0000129b, 0x00000db3, 0x0000015a, 0x00000167, 0x00000375,
  221. 0x00000028, 0x00000112, 0x00000018, 0x00000678, 0x0000081a, 0x00000677, 0x00000003, 0x00018112,
  222. 0x00000001, 0x000441ee, 0x000124b0, 0x0001fa3f, 0x00026125, 0x0005a411, 0x0000e50f, 0x00011820,
  223. 0x00010f13, 0x0002e723, 0x00003518, 0x00005738, 0x0002cc26, 0x0002a9b7, 0x0002db81, 0x0003b5fa,
  224. 0x000185d2, 0x00001299, 0x00030773, 0x0003920d, 0x000411cd, 0x00018751, 0x00005fbd, 0x000099b0,
  225. 0x00009242, 0x00007cf2, 0x00002809, 0x00005a1d, 0x00000001, 0x00005a1d, 0x00000001, 0x00000001,
  226. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  227. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  228. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  229. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  230. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  231. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  232. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  233. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  234. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  235. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  236. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  237. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  238. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  239. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  240. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  241. 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001,
  242. };
  243. static huffmanCode_t huffmanCodes[MAX_HUFFMAN_SYMBOLS];
  244. static huffmanNode_t *huffmanTree = NULL;
  245. static int totalUncompressedLength = 0;
  246. static int totalCompressedLength = 0;
  247. static int maxHuffmanBits = 0;
  248. /*
  249. ================
  250. ClearHuffmanFrequencies
  251. ================
  252. */
  253. void ClearHuffmanFrequencies( void ) {
  254. int i;
  255. for( i = 0; i < MAX_HUFFMAN_SYMBOLS; i++ ) {
  256. huffmanFrequencies[i] = 1;
  257. }
  258. }
  259. /*
  260. ================
  261. InsertHuffmanNode
  262. ================
  263. */
  264. huffmanNode_t *InsertHuffmanNode( huffmanNode_t *firstNode, huffmanNode_t *node ) {
  265. huffmanNode_t *n, *lastNode;
  266. lastNode = NULL;
  267. for ( n = firstNode; n; n = n->next ) {
  268. if ( node->frequency <= n->frequency ) {
  269. break;
  270. }
  271. lastNode = n;
  272. }
  273. if ( lastNode ) {
  274. node->next = lastNode->next;
  275. lastNode->next = node;
  276. } else {
  277. node->next = firstNode;
  278. firstNode = node;
  279. }
  280. return firstNode;
  281. }
  282. /*
  283. ================
  284. BuildHuffmanCode_r
  285. ================
  286. */
  287. void BuildHuffmanCode_r( huffmanNode_t *node, huffmanCode_t code, huffmanCode_t codes[MAX_HUFFMAN_SYMBOLS] ) {
  288. if ( node->symbol == -1 ) {
  289. huffmanCode_t newCode = code;
  290. assert( code.numBits < sizeof( codes[0].bits ) * 8 );
  291. newCode.numBits++;
  292. if ( code.numBits > maxHuffmanBits ) {
  293. maxHuffmanBits = newCode.numBits;
  294. }
  295. BuildHuffmanCode_r( node->children[0], newCode, codes );
  296. newCode.bits[code.numBits >> 5] |= 1 << ( code.numBits & 31 );
  297. BuildHuffmanCode_r( node->children[1], newCode, codes );
  298. } else {
  299. assert( code.numBits <= sizeof( codes[0].bits ) * 8 );
  300. codes[node->symbol] = code;
  301. }
  302. }
  303. /*
  304. ================
  305. FreeHuffmanTree_r
  306. ================
  307. */
  308. void FreeHuffmanTree_r( huffmanNode_t *node ) {
  309. if ( node->symbol == -1 ) {
  310. FreeHuffmanTree_r( node->children[0] );
  311. FreeHuffmanTree_r( node->children[1] );
  312. }
  313. delete node;
  314. }
  315. /*
  316. ================
  317. HuffmanHeight_r
  318. ================
  319. */
  320. int HuffmanHeight_r( huffmanNode_t *node ) {
  321. if ( node == NULL ) {
  322. return -1;
  323. }
  324. int left = HuffmanHeight_r( node->children[0] );
  325. int right = HuffmanHeight_r( node->children[1] );
  326. if ( left > right ) {
  327. return left + 1;
  328. }
  329. return right + 1;
  330. }
  331. /*
  332. ================
  333. SetupHuffman
  334. ================
  335. */
  336. void SetupHuffman( void ) {
  337. int i, height;
  338. huffmanNode_t *firstNode, *node;
  339. huffmanCode_t code;
  340. firstNode = NULL;
  341. for( i = 0; i < MAX_HUFFMAN_SYMBOLS; i++ ) {
  342. node = new huffmanNode_t;
  343. node->symbol = i;
  344. node->frequency = huffmanFrequencies[i];
  345. node->next = NULL;
  346. node->children[0] = NULL;
  347. node->children[1] = NULL;
  348. firstNode = InsertHuffmanNode( firstNode, node );
  349. }
  350. for( i = 1; i < MAX_HUFFMAN_SYMBOLS; i++ ) {
  351. node = new huffmanNode_t;
  352. node->symbol = -1;
  353. node->frequency = firstNode->frequency + firstNode->next->frequency;
  354. node->next = NULL;
  355. node->children[0] = firstNode;
  356. node->children[1] = firstNode->next;
  357. firstNode = InsertHuffmanNode( firstNode->next->next, node );
  358. }
  359. maxHuffmanBits = 0;
  360. memset( &code, 0, sizeof( code ) );
  361. BuildHuffmanCode_r( firstNode, code, huffmanCodes );
  362. huffmanTree = firstNode;
  363. height = HuffmanHeight_r( firstNode );
  364. assert( maxHuffmanBits == height );
  365. }
  366. /*
  367. ================
  368. ShutdownHuffman
  369. ================
  370. */
  371. void ShutdownHuffman( void ) {
  372. if ( huffmanTree ) {
  373. FreeHuffmanTree_r( huffmanTree );
  374. }
  375. }
  376. /*
  377. ================
  378. HuffmanCompressText
  379. ================
  380. */
  381. int HuffmanCompressText( const char *text, int textLength, byte *compressed, int maxCompressedSize ) {
  382. int i, j;
  383. idBitMsg msg;
  384. totalUncompressedLength += textLength;
  385. msg.Init( compressed, maxCompressedSize );
  386. msg.BeginWriting();
  387. for ( i = 0; i < textLength; i++ ) {
  388. const huffmanCode_t &code = huffmanCodes[(unsigned char)text[i]];
  389. for ( j = 0; j < ( code.numBits >> 5 ); j++ ) {
  390. msg.WriteBits( code.bits[j], 32 );
  391. }
  392. if ( code.numBits & 31 ) {
  393. msg.WriteBits( code.bits[j], code.numBits & 31 );
  394. }
  395. }
  396. totalCompressedLength += msg.GetSize();
  397. return msg.GetSize();
  398. }
  399. /*
  400. ================
  401. HuffmanDecompressText
  402. ================
  403. */
  404. int HuffmanDecompressText( char *text, int textLength, const byte *compressed, int compressedSize ) {
  405. int i, bit;
  406. idBitMsg msg;
  407. huffmanNode_t *node;
  408. msg.Init( compressed, compressedSize );
  409. msg.SetSize( compressedSize );
  410. msg.BeginReading();
  411. for ( i = 0; i < textLength; i++ ) {
  412. node = huffmanTree;
  413. do {
  414. bit = msg.ReadBits( 1 );
  415. node = node->children[bit];
  416. } while( node->symbol == -1 );
  417. text[i] = node->symbol;
  418. }
  419. text[i] = '\0';
  420. return msg.GetReadCount();
  421. }
  422. /*
  423. ================
  424. ListHuffmanFrequencies_f
  425. ================
  426. */
  427. void ListHuffmanFrequencies_f( const idCmdArgs &args ) {
  428. int i;
  429. float compression;
  430. compression = !totalUncompressedLength ? 100 : 100 * totalCompressedLength / totalUncompressedLength;
  431. common->Printf( "// compression ratio = %d%%\n", (int)compression );
  432. common->Printf( "static int huffmanFrequencies[] = {\n" );
  433. for( i = 0; i < MAX_HUFFMAN_SYMBOLS; i += 8 ) {
  434. common->Printf( "\t0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x,\n",
  435. huffmanFrequencies[i+0], huffmanFrequencies[i+1],
  436. huffmanFrequencies[i+2], huffmanFrequencies[i+3],
  437. huffmanFrequencies[i+4], huffmanFrequencies[i+5],
  438. huffmanFrequencies[i+6], huffmanFrequencies[i+7]);
  439. }
  440. common->Printf( "}\n" );
  441. }
  442. /*
  443. ====================================================================================
  444. idDeclFile
  445. ====================================================================================
  446. */
  447. /*
  448. ================
  449. idDeclFile::idDeclFile
  450. ================
  451. */
  452. idDeclFile::idDeclFile( const char *fileName, declType_t defaultType ) {
  453. this->fileName = fileName;
  454. this->defaultType = defaultType;
  455. this->timestamp = 0;
  456. this->checksum = 0;
  457. this->fileSize = 0;
  458. this->numLines = 0;
  459. this->decls = NULL;
  460. }
  461. /*
  462. ================
  463. idDeclFile::idDeclFile
  464. ================
  465. */
  466. idDeclFile::idDeclFile() {
  467. this->fileName = "<implicit file>";
  468. this->defaultType = DECL_MAX_TYPES;
  469. this->timestamp = 0;
  470. this->checksum = 0;
  471. this->fileSize = 0;
  472. this->numLines = 0;
  473. this->decls = NULL;
  474. }
  475. /*
  476. ================
  477. idDeclFile::Reload
  478. ForceReload will cause it to reload even if the timestamp hasn't changed
  479. ================
  480. */
  481. void idDeclFile::Reload( bool force ) {
  482. // check for an unchanged timestamp
  483. if ( !force && timestamp != 0 ) {
  484. ID_TIME_T testTimeStamp;
  485. fileSystem->ReadFile( fileName, NULL, &testTimeStamp );
  486. if ( testTimeStamp == timestamp ) {
  487. return;
  488. }
  489. }
  490. // parse the text
  491. LoadAndParse();
  492. }
  493. /*
  494. ================
  495. idDeclFile::LoadAndParse
  496. This is used during both the initial load, and any reloads
  497. ================
  498. */
  499. int c_savedMemory = 0;
  500. int idDeclFile::LoadAndParse() {
  501. int i, numTypes;
  502. idLexer src;
  503. idToken token;
  504. int startMarker;
  505. char * buffer;
  506. int length, size;
  507. int sourceLine;
  508. idStr name;
  509. idDeclLocal *newDecl;
  510. bool reparse;
  511. // load the text
  512. common->DPrintf( "...loading '%s'\n", fileName.c_str() );
  513. length = fileSystem->ReadFile( fileName, (void **)&buffer, &timestamp );
  514. if ( length == -1 ) {
  515. common->FatalError( "couldn't load %s", fileName.c_str() );
  516. return 0;
  517. }
  518. if ( !src.LoadMemory( buffer, length, fileName ) ) {
  519. common->Error( "Couldn't parse %s", fileName.c_str() );
  520. Mem_Free( buffer );
  521. return 0;
  522. }
  523. // mark all the defs that were from the last reload of this file
  524. for ( idDeclLocal *decl = decls; decl; decl = decl->nextInFile ) {
  525. decl->redefinedInReload = false;
  526. }
  527. src.SetFlags( DECL_LEXER_FLAGS );
  528. checksum = MD5_BlockChecksum( buffer, length );
  529. fileSize = length;
  530. // scan through, identifying each individual declaration
  531. while( 1 ) {
  532. startMarker = src.GetFileOffset();
  533. sourceLine = src.GetLineNum();
  534. // parse the decl type name
  535. if ( !src.ReadToken( &token ) ) {
  536. break;
  537. }
  538. declType_t identifiedType = DECL_MAX_TYPES;
  539. // get the decl type from the type name
  540. numTypes = declManagerLocal.GetNumDeclTypes();
  541. for ( i = 0; i < numTypes; i++ ) {
  542. idDeclType *typeInfo = declManagerLocal.GetDeclType( i );
  543. if ( typeInfo && typeInfo->typeName.Icmp( token ) == 0 ) {
  544. identifiedType = (declType_t) typeInfo->type;
  545. break;
  546. }
  547. }
  548. if ( i >= numTypes ) {
  549. if ( token.Icmp( "{" ) == 0 ) {
  550. // if we ever see an open brace, we somehow missed the [type] <name> prefix
  551. src.Warning( "Missing decl name" );
  552. src.SkipBracedSection( false );
  553. continue;
  554. } else {
  555. if ( defaultType == DECL_MAX_TYPES ) {
  556. src.Warning( "No type" );
  557. continue;
  558. }
  559. src.UnreadToken( &token );
  560. // use the default type
  561. identifiedType = defaultType;
  562. }
  563. }
  564. // now parse the name
  565. if ( !src.ReadToken( &token ) ) {
  566. src.Warning( "Type without definition at end of file" );
  567. break;
  568. }
  569. if ( !token.Icmp( "{" ) ) {
  570. // if we ever see an open brace, we somehow missed the [type] <name> prefix
  571. src.Warning( "Missing decl name" );
  572. src.SkipBracedSection( false );
  573. continue;
  574. }
  575. // FIXME: export decls are only used by the model exporter, they are skipped here for now
  576. if ( identifiedType == DECL_MODELEXPORT ) {
  577. src.SkipBracedSection();
  578. continue;
  579. }
  580. name = token;
  581. // make sure there's a '{'
  582. if ( !src.ReadToken( &token ) ) {
  583. src.Warning( "Type without definition at end of file" );
  584. break;
  585. }
  586. if ( token != "{" ) {
  587. src.Warning( "Expecting '{' but found '%s'", token.c_str() );
  588. continue;
  589. }
  590. src.UnreadToken( &token );
  591. // now take everything until a matched closing brace
  592. src.SkipBracedSection();
  593. size = src.GetFileOffset() - startMarker;
  594. // look it up, possibly getting a newly created default decl
  595. reparse = false;
  596. newDecl = declManagerLocal.FindTypeWithoutParsing( identifiedType, name, false );
  597. if ( newDecl ) {
  598. // update the existing copy
  599. if ( newDecl->sourceFile != this || newDecl->redefinedInReload ) {
  600. src.Warning( "%s '%s' previously defined at %s:%i", declManagerLocal.GetDeclNameFromType( identifiedType ),
  601. name.c_str(), newDecl->sourceFile->fileName.c_str(), newDecl->sourceLine );
  602. continue;
  603. }
  604. if ( newDecl->declState != DS_UNPARSED ) {
  605. reparse = true;
  606. }
  607. } else {
  608. // allow it to be created as a default, then add it to the per-file list
  609. newDecl = declManagerLocal.FindTypeWithoutParsing( identifiedType, name, true );
  610. newDecl->nextInFile = this->decls;
  611. this->decls = newDecl;
  612. }
  613. newDecl->redefinedInReload = true;
  614. if ( newDecl->textSource ) {
  615. Mem_Free( newDecl->textSource );
  616. newDecl->textSource = NULL;
  617. }
  618. newDecl->SetTextLocal( buffer + startMarker, size );
  619. newDecl->sourceFile = this;
  620. newDecl->sourceTextOffset = startMarker;
  621. newDecl->sourceTextLength = size;
  622. newDecl->sourceLine = sourceLine;
  623. newDecl->declState = DS_UNPARSED;
  624. // if it is currently in use, reparse it immedaitely
  625. if ( reparse ) {
  626. newDecl->ParseLocal();
  627. }
  628. }
  629. numLines = src.GetLineNum();
  630. Mem_Free( buffer );
  631. // any defs that weren't redefinedInReload should now be defaulted
  632. for ( idDeclLocal *decl = decls ; decl ; decl = decl->nextInFile ) {
  633. if ( decl->redefinedInReload == false ) {
  634. decl->MakeDefault();
  635. decl->sourceTextOffset = decl->sourceFile->fileSize;
  636. decl->sourceTextLength = 0;
  637. decl->sourceLine = decl->sourceFile->numLines;
  638. }
  639. }
  640. return checksum;
  641. }
  642. /*
  643. ====================================================================================
  644. idDeclManagerLocal
  645. ====================================================================================
  646. */
  647. const char *listDeclStrings[] = { "current", "all", "ever", NULL };
  648. /*
  649. ===================
  650. idDeclManagerLocal::Init
  651. ===================
  652. */
  653. void idDeclManagerLocal::Init( void ) {
  654. common->Printf( "----- Initializing Decls -----\n" );
  655. checksum = 0;
  656. #ifdef USE_COMPRESSED_DECLS
  657. SetupHuffman();
  658. #endif
  659. #ifdef GET_HUFFMAN_FREQUENCIES
  660. ClearHuffmanFrequencies();
  661. #endif
  662. // decls used throughout the engine
  663. RegisterDeclType( "table", DECL_TABLE, idDeclAllocator<idDeclTable> );
  664. RegisterDeclType( "material", DECL_MATERIAL, idDeclAllocator<idMaterial> );
  665. RegisterDeclType( "skin", DECL_SKIN, idDeclAllocator<idDeclSkin> );
  666. RegisterDeclType( "sound", DECL_SOUND, idDeclAllocator<idSoundShader> );
  667. RegisterDeclType( "entityDef", DECL_ENTITYDEF, idDeclAllocator<idDeclEntityDef> );
  668. RegisterDeclType( "mapDef", DECL_MAPDEF, idDeclAllocator<idDeclEntityDef> );
  669. RegisterDeclType( "fx", DECL_FX, idDeclAllocator<idDeclFX> );
  670. RegisterDeclType( "particle", DECL_PARTICLE, idDeclAllocator<idDeclParticle> );
  671. RegisterDeclType( "articulatedFigure", DECL_AF, idDeclAllocator<idDeclAF> );
  672. RegisterDeclType( "pda", DECL_PDA, idDeclAllocator<idDeclPDA> );
  673. RegisterDeclType( "email", DECL_EMAIL, idDeclAllocator<idDeclEmail> );
  674. RegisterDeclType( "video", DECL_VIDEO, idDeclAllocator<idDeclVideo> );
  675. RegisterDeclType( "audio", DECL_AUDIO, idDeclAllocator<idDeclAudio> );
  676. RegisterDeclFolder( "materials", ".mtr", DECL_MATERIAL );
  677. RegisterDeclFolder( "skins", ".skin", DECL_SKIN );
  678. RegisterDeclFolder( "sound", ".sndshd", DECL_SOUND );
  679. // add console commands
  680. cmdSystem->AddCommand( "listDecls", ListDecls_f, CMD_FL_SYSTEM, "lists all decls" );
  681. cmdSystem->AddCommand( "reloadDecls", ReloadDecls_f, CMD_FL_SYSTEM, "reloads decls" );
  682. cmdSystem->AddCommand( "touch", TouchDecl_f, CMD_FL_SYSTEM, "touches a decl" );
  683. cmdSystem->AddCommand( "listTables", idListDecls_f<DECL_TABLE>, CMD_FL_SYSTEM, "lists tables", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  684. cmdSystem->AddCommand( "listMaterials", idListDecls_f<DECL_MATERIAL>, CMD_FL_SYSTEM, "lists materials", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  685. cmdSystem->AddCommand( "listSkins", idListDecls_f<DECL_SKIN>, CMD_FL_SYSTEM, "lists skins", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  686. cmdSystem->AddCommand( "listSoundShaders", idListDecls_f<DECL_SOUND>, CMD_FL_SYSTEM, "lists sound shaders", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  687. cmdSystem->AddCommand( "listEntityDefs", idListDecls_f<DECL_ENTITYDEF>, CMD_FL_SYSTEM, "lists entity defs", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  688. cmdSystem->AddCommand( "listFX", idListDecls_f<DECL_FX>, CMD_FL_SYSTEM, "lists FX systems", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  689. cmdSystem->AddCommand( "listParticles", idListDecls_f<DECL_PARTICLE>, CMD_FL_SYSTEM, "lists particle systems", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  690. cmdSystem->AddCommand( "listAF", idListDecls_f<DECL_AF>, CMD_FL_SYSTEM, "lists articulated figures", idCmdSystem::ArgCompletion_String<listDeclStrings>);
  691. cmdSystem->AddCommand( "listPDAs", idListDecls_f<DECL_PDA>, CMD_FL_SYSTEM, "lists PDAs", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  692. cmdSystem->AddCommand( "listEmails", idListDecls_f<DECL_EMAIL>, CMD_FL_SYSTEM, "lists Emails", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  693. cmdSystem->AddCommand( "listVideos", idListDecls_f<DECL_VIDEO>, CMD_FL_SYSTEM, "lists Videos", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  694. cmdSystem->AddCommand( "listAudios", idListDecls_f<DECL_AUDIO>, CMD_FL_SYSTEM, "lists Audios", idCmdSystem::ArgCompletion_String<listDeclStrings> );
  695. cmdSystem->AddCommand( "printTable", idPrintDecls_f<DECL_TABLE>, CMD_FL_SYSTEM, "prints a table", idCmdSystem::ArgCompletion_Decl<DECL_TABLE> );
  696. cmdSystem->AddCommand( "printMaterial", idPrintDecls_f<DECL_MATERIAL>, CMD_FL_SYSTEM, "prints a material", idCmdSystem::ArgCompletion_Decl<DECL_MATERIAL> );
  697. cmdSystem->AddCommand( "printSkin", idPrintDecls_f<DECL_SKIN>, CMD_FL_SYSTEM, "prints a skin", idCmdSystem::ArgCompletion_Decl<DECL_SKIN> );
  698. cmdSystem->AddCommand( "printSoundShader", idPrintDecls_f<DECL_SOUND>, CMD_FL_SYSTEM, "prints a sound shader", idCmdSystem::ArgCompletion_Decl<DECL_SOUND> );
  699. cmdSystem->AddCommand( "printEntityDef", idPrintDecls_f<DECL_ENTITYDEF>, CMD_FL_SYSTEM, "prints an entity def", idCmdSystem::ArgCompletion_Decl<DECL_ENTITYDEF> );
  700. cmdSystem->AddCommand( "printFX", idPrintDecls_f<DECL_FX>, CMD_FL_SYSTEM, "prints an FX system", idCmdSystem::ArgCompletion_Decl<DECL_FX> );
  701. cmdSystem->AddCommand( "printParticle", idPrintDecls_f<DECL_PARTICLE>, CMD_FL_SYSTEM, "prints a particle system", idCmdSystem::ArgCompletion_Decl<DECL_PARTICLE> );
  702. cmdSystem->AddCommand( "printAF", idPrintDecls_f<DECL_AF>, CMD_FL_SYSTEM, "prints an articulated figure", idCmdSystem::ArgCompletion_Decl<DECL_AF> );
  703. cmdSystem->AddCommand( "printPDA", idPrintDecls_f<DECL_PDA>, CMD_FL_SYSTEM, "prints an PDA", idCmdSystem::ArgCompletion_Decl<DECL_PDA> );
  704. cmdSystem->AddCommand( "printEmail", idPrintDecls_f<DECL_EMAIL>, CMD_FL_SYSTEM, "prints an Email", idCmdSystem::ArgCompletion_Decl<DECL_EMAIL> );
  705. cmdSystem->AddCommand( "printVideo", idPrintDecls_f<DECL_VIDEO>, CMD_FL_SYSTEM, "prints a Audio", idCmdSystem::ArgCompletion_Decl<DECL_VIDEO> );
  706. cmdSystem->AddCommand( "printAudio", idPrintDecls_f<DECL_AUDIO>, CMD_FL_SYSTEM, "prints an Video", idCmdSystem::ArgCompletion_Decl<DECL_AUDIO> );
  707. cmdSystem->AddCommand( "listHuffmanFrequencies", ListHuffmanFrequencies_f, CMD_FL_SYSTEM, "lists decl text character frequencies" );
  708. common->Printf( "------------------------------\n" );
  709. }
  710. /*
  711. ===================
  712. idDeclManagerLocal::Shutdown
  713. ===================
  714. */
  715. void idDeclManagerLocal::Shutdown( void ) {
  716. int i, j;
  717. idDeclLocal *decl;
  718. // free decls
  719. for ( i = 0; i < DECL_MAX_TYPES; i++ ) {
  720. for ( j = 0; j < linearLists[i].Num(); j++ ) {
  721. decl = linearLists[i][j];
  722. if ( decl->self != NULL ) {
  723. decl->self->FreeData();
  724. delete decl->self;
  725. }
  726. if ( decl->textSource ) {
  727. Mem_Free( decl->textSource );
  728. decl->textSource = NULL;
  729. }
  730. delete decl;
  731. }
  732. linearLists[i].Clear();
  733. hashTables[i].Free();
  734. }
  735. // free decl files
  736. loadedFiles.DeleteContents( true );
  737. // free the decl types and folders
  738. declTypes.DeleteContents( true );
  739. declFolders.DeleteContents( true );
  740. #ifdef USE_COMPRESSED_DECLS
  741. ShutdownHuffman();
  742. #endif
  743. }
  744. /*
  745. ===================
  746. idDeclManagerLocal::Reload
  747. ===================
  748. */
  749. void idDeclManagerLocal::Reload( bool force ) {
  750. for ( int i = 0; i < loadedFiles.Num(); i++ ) {
  751. loadedFiles[i]->Reload( force );
  752. }
  753. }
  754. /*
  755. ===================
  756. idDeclManagerLocal::BeginLevelLoad
  757. ===================
  758. */
  759. void idDeclManagerLocal::BeginLevelLoad() {
  760. insideLevelLoad = true;
  761. // clear all the referencedThisLevel flags and purge all the data
  762. // so the next reference will cause a reparse
  763. for ( int i = 0; i < DECL_MAX_TYPES; i++ ) {
  764. int num = linearLists[i].Num();
  765. for ( int j = 0 ; j < num ; j++ ) {
  766. idDeclLocal *decl = linearLists[i][j];
  767. decl->Purge();
  768. }
  769. }
  770. }
  771. /*
  772. ===================
  773. idDeclManagerLocal::EndLevelLoad
  774. ===================
  775. */
  776. void idDeclManagerLocal::EndLevelLoad() {
  777. insideLevelLoad = false;
  778. // we don't need to do anything here, but the image manager, model manager,
  779. // and sound sample manager will need to free media that was not referenced
  780. }
  781. /*
  782. ===================
  783. idDeclManagerLocal::RegisterDeclType
  784. ===================
  785. */
  786. void idDeclManagerLocal::RegisterDeclType( const char *typeName, declType_t type, idDecl *(*allocator)( void ) ) {
  787. idDeclType *declType;
  788. if ( type < declTypes.Num() && declTypes[(int)type] ) {
  789. common->Warning( "idDeclManager::RegisterDeclType: type '%s' already exists", typeName );
  790. return;
  791. }
  792. declType = new idDeclType;
  793. declType->typeName = typeName;
  794. declType->type = type;
  795. declType->allocator = allocator;
  796. if ( (int)type + 1 > declTypes.Num() ) {
  797. declTypes.AssureSize( (int)type + 1, NULL );
  798. }
  799. declTypes[type] = declType;
  800. }
  801. /*
  802. ===================
  803. idDeclManagerLocal::RegisterDeclFolder
  804. ===================
  805. */
  806. void idDeclManagerLocal::RegisterDeclFolder( const char *folder, const char *extension, declType_t defaultType ) {
  807. int i, j;
  808. idStr fileName;
  809. idDeclFolder *declFolder;
  810. idFileList *fileList;
  811. idDeclFile *df;
  812. // check whether this folder / extension combination already exists
  813. for ( i = 0; i < declFolders.Num(); i++ ) {
  814. if ( declFolders[i]->folder.Icmp( folder ) == 0 && declFolders[i]->extension.Icmp( extension ) == 0 ) {
  815. break;
  816. }
  817. }
  818. if ( i < declFolders.Num() ) {
  819. declFolder = declFolders[i];
  820. } else {
  821. declFolder = new idDeclFolder;
  822. declFolder->folder = folder;
  823. declFolder->extension = extension;
  824. declFolder->defaultType = defaultType;
  825. declFolders.Append( declFolder );
  826. }
  827. // scan for decl files
  828. fileList = fileSystem->ListFiles( declFolder->folder, declFolder->extension, true );
  829. // load and parse decl files
  830. for ( i = 0; i < fileList->GetNumFiles(); i++ ) {
  831. fileName = declFolder->folder + "/" + fileList->GetFile( i );
  832. // check whether this file has already been loaded
  833. for ( j = 0; j < loadedFiles.Num(); j++ ) {
  834. if ( fileName.Icmp( loadedFiles[j]->fileName ) == 0 ) {
  835. break;
  836. }
  837. }
  838. if ( j < loadedFiles.Num() ) {
  839. df = loadedFiles[j];
  840. } else {
  841. df = new idDeclFile( fileName, defaultType );
  842. loadedFiles.Append( df );
  843. }
  844. df->LoadAndParse();
  845. }
  846. fileSystem->FreeFileList( fileList );
  847. }
  848. /*
  849. ===================
  850. idDeclManagerLocal::GetChecksum
  851. ===================
  852. */
  853. int idDeclManagerLocal::GetChecksum( void ) const {
  854. int i, j, total, num;
  855. int *checksumData;
  856. // get the total number of decls
  857. total = 0;
  858. for ( i = 0; i < DECL_MAX_TYPES; i++ ) {
  859. total += linearLists[i].Num();
  860. }
  861. checksumData = (int *) _alloca16( total * 2 * sizeof( int ) );
  862. total = 0;
  863. for ( i = 0; i < DECL_MAX_TYPES; i++ ) {
  864. declType_t type = (declType_t) i;
  865. // FIXME: not particularly pretty but PDAs and associated decls are localized and should not be checksummed
  866. if ( type == DECL_PDA || type == DECL_VIDEO || type == DECL_AUDIO || type == DECL_EMAIL ) {
  867. continue;
  868. }
  869. num = linearLists[i].Num();
  870. for ( j = 0; j < num; j++ ) {
  871. idDeclLocal *decl = linearLists[i][j];
  872. if ( decl->sourceFile == &implicitDecls ) {
  873. continue;
  874. }
  875. checksumData[total*2+0] = total;
  876. checksumData[total*2+1] = decl->checksum;
  877. total++;
  878. }
  879. }
  880. LittleRevBytes( checksumData, sizeof(int), total * 2 );
  881. return MD5_BlockChecksum( checksumData, total * 2 * sizeof( int ) );
  882. }
  883. /*
  884. ===================
  885. idDeclManagerLocal::GetNumDeclTypes
  886. ===================
  887. */
  888. int idDeclManagerLocal::GetNumDeclTypes( void ) const {
  889. return declTypes.Num();
  890. }
  891. /*
  892. ===================
  893. idDeclManagerLocal::GetDeclNameFromType
  894. ===================
  895. */
  896. const char * idDeclManagerLocal::GetDeclNameFromType( declType_t type ) const {
  897. int typeIndex = (int)type;
  898. if ( typeIndex < 0 || typeIndex >= declTypes.Num() || declTypes[typeIndex] == NULL ) {
  899. common->FatalError( "idDeclManager::GetDeclNameFromType: bad type: %i", typeIndex );
  900. }
  901. return declTypes[typeIndex]->typeName;
  902. }
  903. /*
  904. ===================
  905. idDeclManagerLocal::GetDeclTypeFromName
  906. ===================
  907. */
  908. declType_t idDeclManagerLocal::GetDeclTypeFromName( const char *typeName ) const {
  909. int i;
  910. for ( i = 0; i < declTypes.Num(); i++ ) {
  911. if ( declTypes[i] && declTypes[i]->typeName.Icmp( typeName ) == 0 ) {
  912. return (declType_t)declTypes[i]->type;
  913. }
  914. }
  915. return DECL_MAX_TYPES;
  916. }
  917. /*
  918. =================
  919. idDeclManagerLocal::FindType
  920. External users will always cause the decl to be parsed before returning
  921. =================
  922. */
  923. const idDecl *idDeclManagerLocal::FindType( declType_t type, const char *name, bool makeDefault ) {
  924. idDeclLocal *decl;
  925. if ( !name || !name[0] ) {
  926. name = "_emptyName";
  927. //common->Warning( "idDeclManager::FindType: empty %s name", GetDeclType( (int)type )->typeName.c_str() );
  928. }
  929. decl = FindTypeWithoutParsing( type, name, makeDefault );
  930. if ( !decl ) {
  931. return NULL;
  932. }
  933. decl->AllocateSelf();
  934. // if it hasn't been parsed yet, parse it now
  935. if ( decl->declState == DS_UNPARSED ) {
  936. decl->ParseLocal();
  937. }
  938. // mark it as referenced
  939. decl->referencedThisLevel = true;
  940. decl->everReferenced = true;
  941. if ( insideLevelLoad ) {
  942. decl->parsedOutsideLevelLoad = false;
  943. }
  944. return decl->self;
  945. }
  946. /*
  947. ===============
  948. idDeclManagerLocal::FindDeclWithoutParsing
  949. ===============
  950. */
  951. const idDecl* idDeclManagerLocal::FindDeclWithoutParsing( declType_t type, const char *name, bool makeDefault) {
  952. idDeclLocal* decl;
  953. decl = FindTypeWithoutParsing(type, name, makeDefault);
  954. if(decl) {
  955. return decl->self;
  956. }
  957. return NULL;
  958. }
  959. /*
  960. ===============
  961. idDeclManagerLocal::ReloadFile
  962. ===============
  963. */
  964. void idDeclManagerLocal::ReloadFile( const char* filename, bool force ) {
  965. for ( int i = 0; i < loadedFiles.Num(); i++ ) {
  966. if(!loadedFiles[i]->fileName.Icmp(filename)) {
  967. checksum ^= loadedFiles[i]->checksum;
  968. loadedFiles[i]->Reload( force );
  969. checksum ^= loadedFiles[i]->checksum;
  970. }
  971. }
  972. }
  973. /*
  974. ===================
  975. idDeclManagerLocal::GetNumDecls
  976. ===================
  977. */
  978. int idDeclManagerLocal::GetNumDecls( declType_t type ) {
  979. int typeIndex = (int)type;
  980. if ( typeIndex < 0 || typeIndex >= declTypes.Num() || declTypes[typeIndex] == NULL ) {
  981. common->FatalError( "idDeclManager::GetNumDecls: bad type: %i", typeIndex );
  982. }
  983. return linearLists[ typeIndex ].Num();
  984. }
  985. /*
  986. ===================
  987. idDeclManagerLocal::DeclByIndex
  988. ===================
  989. */
  990. const idDecl *idDeclManagerLocal::DeclByIndex( declType_t type, int index, bool forceParse ) {
  991. int typeIndex = (int)type;
  992. if ( typeIndex < 0 || typeIndex >= declTypes.Num() || declTypes[typeIndex] == NULL ) {
  993. common->FatalError( "idDeclManager::DeclByIndex: bad type: %i", typeIndex );
  994. }
  995. if ( index < 0 || index >= linearLists[ typeIndex ].Num() ) {
  996. common->Error( "idDeclManager::DeclByIndex: out of range" );
  997. }
  998. idDeclLocal *decl = linearLists[ typeIndex ][ index ];
  999. decl->AllocateSelf();
  1000. if ( forceParse && decl->declState == DS_UNPARSED ) {
  1001. decl->ParseLocal();
  1002. }
  1003. return decl->self;
  1004. }
  1005. /*
  1006. ===================
  1007. idDeclManagerLocal::ListType
  1008. list*
  1009. Lists decls currently referenced
  1010. list* ever
  1011. Lists decls that have been referenced at least once since app launched
  1012. list* all
  1013. Lists every decl declared, even if it hasn't been referenced or parsed
  1014. FIXME: alphabetized, wildcards?
  1015. ===================
  1016. */
  1017. void idDeclManagerLocal::ListType( const idCmdArgs &args, declType_t type ) {
  1018. bool all, ever;
  1019. if ( !idStr::Icmp( args.Argv( 1 ), "all" ) ) {
  1020. all = true;
  1021. } else {
  1022. all = false;
  1023. }
  1024. if ( !idStr::Icmp( args.Argv( 1 ), "ever" ) ) {
  1025. ever = true;
  1026. } else {
  1027. ever = false;
  1028. }
  1029. common->Printf( "--------------------\n" );
  1030. int printed = 0;
  1031. int count = linearLists[ (int)type ].Num();
  1032. for ( int i = 0 ; i < count ; i++ ) {
  1033. idDeclLocal *decl = linearLists[ (int)type ][ i ];
  1034. if ( !all && decl->declState == DS_UNPARSED ) {
  1035. continue;
  1036. }
  1037. if ( !all && !ever && !decl->referencedThisLevel ) {
  1038. continue;
  1039. }
  1040. if ( decl->referencedThisLevel ) {
  1041. common->Printf( "*" );
  1042. } else if ( decl->everReferenced ) {
  1043. common->Printf( "." );
  1044. } else {
  1045. common->Printf( " " );
  1046. }
  1047. if ( decl->declState == DS_DEFAULTED ) {
  1048. common->Printf( "D" );
  1049. } else {
  1050. common->Printf( " " );
  1051. }
  1052. common->Printf( "%4i: ", decl->index );
  1053. printed++;
  1054. if ( decl->declState == DS_UNPARSED ) {
  1055. // doesn't have any type specific data yet
  1056. common->Printf( "%s\n", decl->GetName() );
  1057. } else {
  1058. decl->self->List();
  1059. }
  1060. }
  1061. common->Printf( "--------------------\n" );
  1062. common->Printf( "%i of %i %s\n", printed, count, declTypes[type]->typeName.c_str() );
  1063. }
  1064. /*
  1065. ===================
  1066. idDeclManagerLocal::PrintType
  1067. ===================
  1068. */
  1069. void idDeclManagerLocal::PrintType( const idCmdArgs &args, declType_t type ) {
  1070. // individual decl types may use additional command parameters
  1071. if ( args.Argc() < 2 ) {
  1072. common->Printf( "USAGE: Print<decl type> <decl name> [type specific parms]\n" );
  1073. return;
  1074. }
  1075. // look it up, skipping the public path so it won't parse or reference
  1076. idDeclLocal *decl = FindTypeWithoutParsing( type, args.Argv( 1 ), false );
  1077. if ( !decl ) {
  1078. common->Printf( "%s '%s' not found.\n", declTypes[ type ]->typeName.c_str(), args.Argv( 1 ) );
  1079. return;
  1080. }
  1081. // print information common to all decls
  1082. common->Printf( "%s %s:\n", declTypes[ type ]->typeName.c_str(), decl->name.c_str() );
  1083. common->Printf( "source: %s:%i\n", decl->sourceFile->fileName.c_str(), decl->sourceLine );
  1084. common->Printf( "----------\n" );
  1085. if ( decl->textSource != NULL ) {
  1086. char *declText = (char *)_alloca( decl->textLength + 1 );
  1087. decl->GetText( declText );
  1088. common->Printf( "%s\n", declText );
  1089. } else {
  1090. common->Printf( "NO SOURCE\n" );
  1091. }
  1092. common->Printf( "----------\n" );
  1093. switch( decl->declState ) {
  1094. case DS_UNPARSED:
  1095. common->Printf( "Unparsed.\n" );
  1096. break;
  1097. case DS_DEFAULTED:
  1098. common->Printf( "<DEFAULTED>\n" );
  1099. break;
  1100. case DS_PARSED:
  1101. common->Printf( "Parsed.\n" );
  1102. break;
  1103. }
  1104. if ( decl->referencedThisLevel ) {
  1105. common->Printf( "Currently referenced this level.\n" );
  1106. } else if ( decl->everReferenced ) {
  1107. common->Printf( "Referenced in a previous level.\n" );
  1108. } else {
  1109. common->Printf( "Never referenced.\n" );
  1110. }
  1111. // allow type-specific data to be printed
  1112. if ( decl->self != NULL ) {
  1113. decl->self->Print();
  1114. }
  1115. }
  1116. /*
  1117. ===================
  1118. idDeclManagerLocal::CreateNewDecl
  1119. ===================
  1120. */
  1121. idDecl *idDeclManagerLocal::CreateNewDecl( declType_t type, const char *name, const char *_fileName ) {
  1122. int typeIndex = (int)type;
  1123. int i, hash;
  1124. if ( typeIndex < 0 || typeIndex >= declTypes.Num() || declTypes[typeIndex] == NULL ) {
  1125. common->FatalError( "idDeclManager::CreateNewDecl: bad type: %i", typeIndex );
  1126. }
  1127. char canonicalName[MAX_STRING_CHARS];
  1128. MakeNameCanonical( name, canonicalName, sizeof( canonicalName ) );
  1129. idStr fileName = _fileName;
  1130. fileName.BackSlashesToSlashes();
  1131. // see if it already exists
  1132. hash = hashTables[typeIndex].GenerateKey( canonicalName, false );
  1133. for ( i = hashTables[typeIndex].First( hash ); i >= 0; i = hashTables[typeIndex].Next( i ) ) {
  1134. if ( linearLists[typeIndex][i]->name.Icmp( canonicalName ) == 0 ) {
  1135. linearLists[typeIndex][i]->AllocateSelf();
  1136. return linearLists[typeIndex][i]->self;
  1137. }
  1138. }
  1139. idDeclFile *sourceFile;
  1140. // find existing source file or create a new one
  1141. for ( i = 0; i < loadedFiles.Num(); i++ ) {
  1142. if ( loadedFiles[i]->fileName.Icmp( fileName ) == 0 ) {
  1143. break;
  1144. }
  1145. }
  1146. if ( i < loadedFiles.Num() ) {
  1147. sourceFile = loadedFiles[i];
  1148. } else {
  1149. sourceFile = new idDeclFile( fileName, type );
  1150. loadedFiles.Append( sourceFile );
  1151. }
  1152. idDeclLocal *decl = new idDeclLocal;
  1153. decl->name = canonicalName;
  1154. decl->type = type;
  1155. decl->declState = DS_UNPARSED;
  1156. decl->AllocateSelf();
  1157. idStr header = declTypes[typeIndex]->typeName;
  1158. idStr defaultText = decl->self->DefaultDefinition();
  1159. int size = header.Length() + 1 + idStr::Length( canonicalName ) + 1 + defaultText.Length();
  1160. char *declText = ( char * ) _alloca( size + 1 );
  1161. memcpy( declText, header, header.Length() );
  1162. declText[header.Length()] = ' ';
  1163. memcpy( declText + header.Length() + 1, canonicalName, idStr::Length( canonicalName ) );
  1164. declText[header.Length() + 1 + idStr::Length( canonicalName )] = ' ';
  1165. memcpy( declText + header.Length() + 1 + idStr::Length( canonicalName ) + 1, defaultText, defaultText.Length() + 1 );
  1166. decl->SetTextLocal( declText, size );
  1167. decl->sourceFile = sourceFile;
  1168. decl->sourceTextOffset = sourceFile->fileSize;
  1169. decl->sourceTextLength = 0;
  1170. decl->sourceLine = sourceFile->numLines;
  1171. decl->ParseLocal();
  1172. // add this decl to the source file list
  1173. decl->nextInFile = sourceFile->decls;
  1174. sourceFile->decls = decl;
  1175. // add it to the hash table and linear list
  1176. decl->index = linearLists[typeIndex].Num();
  1177. hashTables[typeIndex].Add( hash, linearLists[typeIndex].Append( decl ) );
  1178. return decl->self;
  1179. }
  1180. /*
  1181. ===============
  1182. idDeclManagerLocal::RenameDecl
  1183. ===============
  1184. */
  1185. bool idDeclManagerLocal::RenameDecl( declType_t type, const char* oldName, const char* newName ) {
  1186. char canonicalOldName[MAX_STRING_CHARS];
  1187. MakeNameCanonical( oldName, canonicalOldName, sizeof( canonicalOldName ));
  1188. char canonicalNewName[MAX_STRING_CHARS];
  1189. MakeNameCanonical( newName, canonicalNewName, sizeof( canonicalNewName ) );
  1190. idDeclLocal *decl = NULL;
  1191. // make sure it already exists
  1192. int typeIndex = (int)type;
  1193. int i, hash;
  1194. hash = hashTables[typeIndex].GenerateKey( canonicalOldName, false );
  1195. for ( i = hashTables[typeIndex].First( hash ); i >= 0; i = hashTables[typeIndex].Next( i ) ) {
  1196. if ( linearLists[typeIndex][i]->name.Icmp( canonicalOldName ) == 0 ) {
  1197. decl = linearLists[typeIndex][i];
  1198. break;
  1199. }
  1200. }
  1201. if(!decl)
  1202. return false;
  1203. //if ( !hashTables[(int)type].Get( canonicalOldName, &declPtr ) )
  1204. // return false;
  1205. //decl = *declPtr;
  1206. //Change the name
  1207. decl->name = canonicalNewName;
  1208. // add it to the hash table
  1209. //hashTables[(int)decl->type].Set( decl->name, decl );
  1210. int newhash = hashTables[typeIndex].GenerateKey( canonicalNewName, false );
  1211. hashTables[typeIndex].Add( newhash, decl->index );
  1212. //Remove the old hash item
  1213. hashTables[typeIndex].Remove(hash, decl->index);
  1214. return true;
  1215. }
  1216. /*
  1217. ===================
  1218. idDeclManagerLocal::MediaPrint
  1219. This is just used to nicely indent media caching prints
  1220. ===================
  1221. */
  1222. void idDeclManagerLocal::MediaPrint( const char *fmt, ... ) {
  1223. if ( !decl_show.GetInteger() ) {
  1224. return;
  1225. }
  1226. for ( int i = 0 ; i < indent ; i++ ) {
  1227. common->Printf( " " );
  1228. }
  1229. va_list argptr;
  1230. char buffer[1024];
  1231. va_start (argptr,fmt);
  1232. idStr::vsnPrintf( buffer, sizeof(buffer), fmt, argptr );
  1233. va_end (argptr);
  1234. buffer[sizeof(buffer)-1] = '\0';
  1235. common->Printf( "%s", buffer );
  1236. }
  1237. /*
  1238. ===================
  1239. idDeclManagerLocal::WritePrecacheCommands
  1240. ===================
  1241. */
  1242. void idDeclManagerLocal::WritePrecacheCommands( idFile *f ) {
  1243. for ( int i = 0; i < declTypes.Num(); i++ ) {
  1244. int num;
  1245. if ( declTypes[i] == NULL ) {
  1246. continue;
  1247. }
  1248. num = linearLists[i].Num();
  1249. for ( int j = 0 ; j < num ; j++ ) {
  1250. idDeclLocal *decl = linearLists[i][j];
  1251. if ( !decl->referencedThisLevel ) {
  1252. continue;
  1253. }
  1254. char str[1024];
  1255. sprintf( str, "touch %s %s\n", declTypes[i]->typeName.c_str(), decl->GetName() );
  1256. common->Printf( "%s", str );
  1257. f->Printf( "%s", str );
  1258. }
  1259. }
  1260. }
  1261. /********************************************************************/
  1262. const idMaterial *idDeclManagerLocal::FindMaterial( const char *name, bool makeDefault ) {
  1263. return static_cast<const idMaterial *>( FindType( DECL_MATERIAL, name, makeDefault ) );
  1264. }
  1265. const idMaterial *idDeclManagerLocal::MaterialByIndex( int index, bool forceParse ) {
  1266. return static_cast<const idMaterial *>( DeclByIndex( DECL_MATERIAL, index, forceParse ) );
  1267. }
  1268. /********************************************************************/
  1269. const idDeclSkin *idDeclManagerLocal::FindSkin( const char *name, bool makeDefault ) {
  1270. return static_cast<const idDeclSkin *>( FindType( DECL_SKIN, name, makeDefault ) );
  1271. }
  1272. const idDeclSkin *idDeclManagerLocal::SkinByIndex( int index, bool forceParse ) {
  1273. return static_cast<const idDeclSkin *>( DeclByIndex( DECL_SKIN, index, forceParse ) );
  1274. }
  1275. /********************************************************************/
  1276. const idSoundShader *idDeclManagerLocal::FindSound( const char *name, bool makeDefault ) {
  1277. return static_cast<const idSoundShader *>( FindType( DECL_SOUND, name, makeDefault ) );
  1278. }
  1279. const idSoundShader *idDeclManagerLocal::SoundByIndex( int index, bool forceParse ) {
  1280. return static_cast<const idSoundShader *>( DeclByIndex( DECL_SOUND, index, forceParse ) );
  1281. }
  1282. /*
  1283. ===================
  1284. idDeclManagerLocal::MakeNameCanonical
  1285. ===================
  1286. */
  1287. void idDeclManagerLocal::MakeNameCanonical( const char *name, char *result, int maxLength ) {
  1288. int i, lastDot;
  1289. lastDot = -1;
  1290. for ( i = 0; i < maxLength && name[i] != '\0'; i++ ) {
  1291. int c = name[i];
  1292. if ( c == '\\' ) {
  1293. result[i] = '/';
  1294. } else if ( c == '.' ) {
  1295. lastDot = i;
  1296. result[i] = c;
  1297. } else {
  1298. result[i] = idStr::ToLower( c );
  1299. }
  1300. }
  1301. if ( lastDot != -1 ) {
  1302. result[lastDot] = '\0';
  1303. } else {
  1304. result[i] = '\0';
  1305. }
  1306. }
  1307. /*
  1308. ================
  1309. idDeclManagerLocal::ListDecls_f
  1310. ================
  1311. */
  1312. void idDeclManagerLocal::ListDecls_f( const idCmdArgs &args ) {
  1313. int i, j;
  1314. int totalDecls = 0;
  1315. int totalText = 0;
  1316. int totalStructs = 0;
  1317. for ( i = 0; i < declManagerLocal.declTypes.Num(); i++ ) {
  1318. int size, num;
  1319. if ( declManagerLocal.declTypes[i] == NULL ) {
  1320. continue;
  1321. }
  1322. num = declManagerLocal.linearLists[i].Num();
  1323. totalDecls += num;
  1324. size = 0;
  1325. for ( j = 0; j < num; j++ ) {
  1326. size += declManagerLocal.linearLists[i][j]->Size();
  1327. if ( declManagerLocal.linearLists[i][j]->self != NULL ) {
  1328. size += declManagerLocal.linearLists[i][j]->self->Size();
  1329. }
  1330. }
  1331. totalStructs += size;
  1332. common->Printf( "%4ik %4i %s\n", size >> 10, num, declManagerLocal.declTypes[i]->typeName.c_str() );
  1333. }
  1334. for ( i = 0 ; i < declManagerLocal.loadedFiles.Num() ; i++ ) {
  1335. idDeclFile *df = declManagerLocal.loadedFiles[i];
  1336. totalText += df->fileSize;
  1337. }
  1338. common->Printf( "%i total decls is %i decl files\n", totalDecls, declManagerLocal.loadedFiles.Num() );
  1339. common->Printf( "%iKB in text, %iKB in structures\n", totalText >> 10, totalStructs >> 10 );
  1340. }
  1341. /*
  1342. ===================
  1343. idDeclManagerLocal::ReloadDecls_f
  1344. Reload will not find any new files created in the directories, it
  1345. will only reload existing files.
  1346. A reload will never cause anything to be purged.
  1347. ===================
  1348. */
  1349. void idDeclManagerLocal::ReloadDecls_f( const idCmdArgs &args ) {
  1350. bool force;
  1351. if ( !idStr::Icmp( args.Argv( 1 ), "all" ) ) {
  1352. force = true;
  1353. common->Printf( "reloading all decl files:\n" );
  1354. } else {
  1355. force = false;
  1356. common->Printf( "reloading changed decl files:\n" );
  1357. }
  1358. soundSystem->SetMute( true );
  1359. declManagerLocal.Reload( force );
  1360. soundSystem->SetMute( false );
  1361. }
  1362. /*
  1363. ===================
  1364. idDeclManagerLocal::TouchDecl_f
  1365. ===================
  1366. */
  1367. void idDeclManagerLocal::TouchDecl_f( const idCmdArgs &args ) {
  1368. int i;
  1369. if ( args.Argc() != 3 ) {
  1370. common->Printf( "usage: touch <type> <name>\n" );
  1371. common->Printf( "valid types: " );
  1372. for ( int i = 0 ; i < declManagerLocal.declTypes.Num() ; i++ ) {
  1373. if ( declManagerLocal.declTypes[i] ) {
  1374. common->Printf( "%s ", declManagerLocal.declTypes[i]->typeName.c_str() );
  1375. }
  1376. }
  1377. common->Printf( "\n" );
  1378. return;
  1379. }
  1380. for ( i = 0; i < declManagerLocal.declTypes.Num(); i++ ) {
  1381. if ( declManagerLocal.declTypes[i] && declManagerLocal.declTypes[i]->typeName.Icmp( args.Argv( 1 ) ) == 0 ) {
  1382. break;
  1383. }
  1384. }
  1385. if ( i >= declManagerLocal.declTypes.Num() ) {
  1386. common->Printf( "unknown decl type '%s'\n", args.Argv( 1 ) );
  1387. return;
  1388. }
  1389. const idDecl *decl = declManagerLocal.FindType( (declType_t)i, args.Argv( 2 ), false );
  1390. if ( !decl ) {
  1391. common->Printf( "%s '%s' not found\n", declManagerLocal.declTypes[i]->typeName.c_str(), args.Argv( 2 ) );
  1392. }
  1393. }
  1394. /*
  1395. ===================
  1396. idDeclManagerLocal::FindTypeWithoutParsing
  1397. This finds or creats the decl, but does not cause a parse. This is only used internally.
  1398. ===================
  1399. */
  1400. idDeclLocal *idDeclManagerLocal::FindTypeWithoutParsing( declType_t type, const char *name, bool makeDefault ) {
  1401. int typeIndex = (int)type;
  1402. int i, hash;
  1403. if ( typeIndex < 0 || typeIndex >= declTypes.Num() || declTypes[typeIndex] == NULL ) {
  1404. common->FatalError( "idDeclManager::FindTypeWithoutParsing: bad type: %i", typeIndex );
  1405. }
  1406. char canonicalName[MAX_STRING_CHARS];
  1407. MakeNameCanonical( name, canonicalName, sizeof( canonicalName ) );
  1408. // see if it already exists
  1409. hash = hashTables[typeIndex].GenerateKey( canonicalName, false );
  1410. for ( i = hashTables[typeIndex].First( hash ); i >= 0; i = hashTables[typeIndex].Next( i ) ) {
  1411. if ( linearLists[typeIndex][i]->name.Icmp( canonicalName ) == 0 ) {
  1412. // only print these when decl_show is set to 2, because it can be a lot of clutter
  1413. if ( decl_show.GetInteger() > 1 ) {
  1414. MediaPrint( "referencing %s %s\n", declTypes[ type ]->typeName.c_str(), name );
  1415. }
  1416. return linearLists[typeIndex][i];
  1417. }
  1418. }
  1419. if ( !makeDefault ) {
  1420. return NULL;
  1421. }
  1422. idDeclLocal *decl = new idDeclLocal;
  1423. decl->self = NULL;
  1424. decl->name = canonicalName;
  1425. decl->type = type;
  1426. decl->declState = DS_UNPARSED;
  1427. decl->textSource = NULL;
  1428. decl->textLength = 0;
  1429. decl->sourceFile = &implicitDecls;
  1430. decl->referencedThisLevel = false;
  1431. decl->everReferenced = false;
  1432. decl->parsedOutsideLevelLoad = !insideLevelLoad;
  1433. // add it to the linear list and hash table
  1434. decl->index = linearLists[typeIndex].Num();
  1435. hashTables[typeIndex].Add( hash, linearLists[typeIndex].Append( decl ) );
  1436. return decl;
  1437. }
  1438. /*
  1439. ====================================================================================
  1440. idDeclLocal
  1441. ====================================================================================
  1442. */
  1443. /*
  1444. =================
  1445. idDeclLocal::idDeclLocal
  1446. =================
  1447. */
  1448. idDeclLocal::idDeclLocal( void ) {
  1449. name = "unnamed";
  1450. textSource = NULL;
  1451. textLength = 0;
  1452. compressedLength = 0;
  1453. sourceFile = NULL;
  1454. sourceTextOffset = 0;
  1455. sourceTextLength = 0;
  1456. sourceLine = 0;
  1457. checksum = 0;
  1458. type = DECL_ENTITYDEF;
  1459. index = 0;
  1460. declState = DS_UNPARSED;
  1461. parsedOutsideLevelLoad = false;
  1462. referencedThisLevel = false;
  1463. everReferenced = false;
  1464. redefinedInReload = false;
  1465. nextInFile = NULL;
  1466. }
  1467. /*
  1468. =================
  1469. idDeclLocal::GetName
  1470. =================
  1471. */
  1472. const char *idDeclLocal::GetName( void ) const {
  1473. return name.c_str();
  1474. }
  1475. /*
  1476. =================
  1477. idDeclLocal::GetType
  1478. =================
  1479. */
  1480. declType_t idDeclLocal::GetType( void ) const {
  1481. return type;
  1482. }
  1483. /*
  1484. =================
  1485. idDeclLocal::GetState
  1486. =================
  1487. */
  1488. declState_t idDeclLocal::GetState( void ) const {
  1489. return declState;
  1490. }
  1491. /*
  1492. =================
  1493. idDeclLocal::IsImplicit
  1494. =================
  1495. */
  1496. bool idDeclLocal::IsImplicit( void ) const {
  1497. return ( sourceFile == declManagerLocal.GetImplicitDeclFile() );
  1498. }
  1499. /*
  1500. =================
  1501. idDeclLocal::IsValid
  1502. =================
  1503. */
  1504. bool idDeclLocal::IsValid( void ) const {
  1505. return ( declState != DS_UNPARSED );
  1506. }
  1507. /*
  1508. =================
  1509. idDeclLocal::Invalidate
  1510. =================
  1511. */
  1512. void idDeclLocal::Invalidate( void ) {
  1513. declState = DS_UNPARSED;
  1514. }
  1515. /*
  1516. =================
  1517. idDeclLocal::EnsureNotPurged
  1518. =================
  1519. */
  1520. void idDeclLocal::EnsureNotPurged( void ) {
  1521. if ( declState == DS_UNPARSED ) {
  1522. ParseLocal();
  1523. }
  1524. }
  1525. /*
  1526. =================
  1527. idDeclLocal::Index
  1528. =================
  1529. */
  1530. int idDeclLocal::Index( void ) const {
  1531. return index;
  1532. }
  1533. /*
  1534. =================
  1535. idDeclLocal::GetLineNum
  1536. =================
  1537. */
  1538. int idDeclLocal::GetLineNum( void ) const {
  1539. return sourceLine;
  1540. }
  1541. /*
  1542. =================
  1543. idDeclLocal::GetFileName
  1544. =================
  1545. */
  1546. const char *idDeclLocal::GetFileName( void ) const {
  1547. return ( sourceFile ) ? sourceFile->fileName.c_str() : "*invalid*";
  1548. }
  1549. /*
  1550. =================
  1551. idDeclLocal::Size
  1552. =================
  1553. */
  1554. size_t idDeclLocal::Size( void ) const {
  1555. return sizeof( idDecl ) + name.Allocated();
  1556. }
  1557. /*
  1558. =================
  1559. idDeclLocal::GetText
  1560. =================
  1561. */
  1562. void idDeclLocal::GetText( char *text ) const {
  1563. #ifdef USE_COMPRESSED_DECLS
  1564. HuffmanDecompressText( text, textLength, (byte *)textSource, compressedLength );
  1565. #else
  1566. memcpy( text, textSource, textLength+1 );
  1567. #endif
  1568. }
  1569. /*
  1570. =================
  1571. idDeclLocal::GetTextLength
  1572. =================
  1573. */
  1574. int idDeclLocal::GetTextLength( void ) const {
  1575. return textLength;
  1576. }
  1577. /*
  1578. =================
  1579. idDeclLocal::SetText
  1580. =================
  1581. */
  1582. void idDeclLocal::SetText( const char *text ) {
  1583. SetTextLocal( text, idStr::Length( text ) );
  1584. }
  1585. /*
  1586. =================
  1587. idDeclLocal::SetTextLocal
  1588. =================
  1589. */
  1590. void idDeclLocal::SetTextLocal( const char *text, const int length ) {
  1591. Mem_Free( textSource );
  1592. checksum = MD5_BlockChecksum( text, length );
  1593. #ifdef GET_HUFFMAN_FREQUENCIES
  1594. for( int i = 0; i < length; i++ ) {
  1595. huffmanFrequencies[((const unsigned char *)text)[i]]++;
  1596. }
  1597. #endif
  1598. #ifdef USE_COMPRESSED_DECLS
  1599. int maxBytesPerCode = ( maxHuffmanBits + 7 ) >> 3;
  1600. byte *compressed = (byte *)_alloca( length * maxBytesPerCode );
  1601. compressedLength = HuffmanCompressText( text, length, compressed, length * maxBytesPerCode );
  1602. textSource = (char *)Mem_Alloc( compressedLength );
  1603. memcpy( textSource, compressed, compressedLength );
  1604. #else
  1605. compressedLength = length;
  1606. textSource = (char *) Mem_Alloc( length + 1 );
  1607. memcpy( textSource, text, length );
  1608. textSource[length] = '\0';
  1609. #endif
  1610. textLength = length;
  1611. }
  1612. /*
  1613. =================
  1614. idDeclLocal::ReplaceSourceFileText
  1615. =================
  1616. */
  1617. bool idDeclLocal::ReplaceSourceFileText( void ) {
  1618. int oldFileLength, newFileLength;
  1619. char *buffer;
  1620. idFile *file;
  1621. common->Printf( "Writing \'%s\' to \'%s\'...\n", GetName(), GetFileName() );
  1622. if ( sourceFile == &declManagerLocal.implicitDecls ) {
  1623. common->Warning( "Can't save implicit declaration %s.", GetName() );
  1624. return false;
  1625. }
  1626. // get length and allocate buffer to hold the file
  1627. oldFileLength = sourceFile->fileSize;
  1628. newFileLength = oldFileLength - sourceTextLength + textLength;
  1629. buffer = (char *) Mem_Alloc( Max( newFileLength, oldFileLength ) );
  1630. // read original file
  1631. if ( sourceFile->fileSize ) {
  1632. file = fileSystem->OpenFileRead( GetFileName() );
  1633. if ( !file ) {
  1634. Mem_Free( buffer );
  1635. common->Warning( "Couldn't open %s for reading.", GetFileName() );
  1636. return false;
  1637. }
  1638. if ( file->Length() != sourceFile->fileSize || file->Timestamp() != sourceFile->timestamp ) {
  1639. Mem_Free( buffer );
  1640. common->Warning( "The file %s has been modified outside of the engine.", GetFileName() );
  1641. return false;
  1642. }
  1643. file->Read( buffer, oldFileLength );
  1644. fileSystem->CloseFile( file );
  1645. if ( MD5_BlockChecksum( buffer, oldFileLength ) != sourceFile->checksum ) {
  1646. Mem_Free( buffer );
  1647. common->Warning( "The file %s has been modified outside of the engine.", GetFileName() );
  1648. return false;
  1649. }
  1650. }
  1651. // insert new text
  1652. char *declText = (char *) _alloca( textLength + 1 );
  1653. GetText( declText );
  1654. memmove( buffer + sourceTextOffset + textLength, buffer + sourceTextOffset + sourceTextLength, oldFileLength - sourceTextOffset - sourceTextLength );
  1655. memcpy( buffer + sourceTextOffset, declText, textLength );
  1656. // write out new file
  1657. file = fileSystem->OpenFileWrite( GetFileName(), "fs_devpath" );
  1658. if ( !file ) {
  1659. Mem_Free( buffer );
  1660. common->Warning( "Couldn't open %s for writing.", GetFileName() );
  1661. return false;
  1662. }
  1663. file->Write( buffer, newFileLength );
  1664. fileSystem->CloseFile( file );
  1665. // set new file size, checksum and timestamp
  1666. sourceFile->fileSize = newFileLength;
  1667. sourceFile->checksum = MD5_BlockChecksum( buffer, newFileLength );
  1668. fileSystem->ReadFile( GetFileName(), NULL, &sourceFile->timestamp );
  1669. // free buffer
  1670. Mem_Free( buffer );
  1671. // move all decls in the same file
  1672. for ( idDeclLocal *decl = sourceFile->decls; decl; decl = decl->nextInFile ) {
  1673. if (decl->sourceTextOffset > sourceTextOffset) {
  1674. decl->sourceTextOffset += textLength - sourceTextLength;
  1675. }
  1676. }
  1677. // set new size of text in source file
  1678. sourceTextLength = textLength;
  1679. return true;
  1680. }
  1681. /*
  1682. =================
  1683. idDeclLocal::SourceFileChanged
  1684. =================
  1685. */
  1686. bool idDeclLocal::SourceFileChanged( void ) const {
  1687. int newLength;
  1688. ID_TIME_T newTimestamp;
  1689. if ( sourceFile->fileSize <= 0 ) {
  1690. return false;
  1691. }
  1692. newLength = fileSystem->ReadFile( GetFileName(), NULL, &newTimestamp );
  1693. if ( newLength != sourceFile->fileSize || newTimestamp != sourceFile->timestamp ) {
  1694. return true;
  1695. }
  1696. return false;
  1697. }
  1698. /*
  1699. =================
  1700. idDeclLocal::MakeDefault
  1701. =================
  1702. */
  1703. void idDeclLocal::MakeDefault() {
  1704. static int recursionLevel;
  1705. const char *defaultText;
  1706. declManagerLocal.MediaPrint( "DEFAULTED\n" );
  1707. declState = DS_DEFAULTED;
  1708. AllocateSelf();
  1709. defaultText = self->DefaultDefinition();
  1710. // a parse error inside a DefaultDefinition() string could
  1711. // cause an infinite loop, but normal default definitions could
  1712. // still reference other default definitions, so we can't
  1713. // just dump out on the first recursion
  1714. if ( ++recursionLevel > 100 ) {
  1715. common->FatalError( "idDecl::MakeDefault: bad DefaultDefinition(): %s", defaultText );
  1716. }
  1717. // always free data before parsing
  1718. self->FreeData();
  1719. // parse
  1720. self->Parse( defaultText, strlen( defaultText ) );
  1721. // we could still eventually hit the recursion if we have enough Error() calls inside Parse...
  1722. --recursionLevel;
  1723. }
  1724. /*
  1725. =================
  1726. idDeclLocal::SetDefaultText
  1727. =================
  1728. */
  1729. bool idDeclLocal::SetDefaultText( void ) {
  1730. return false;
  1731. }
  1732. /*
  1733. =================
  1734. idDeclLocal::DefaultDefinition
  1735. =================
  1736. */
  1737. const char *idDeclLocal::DefaultDefinition() const {
  1738. return "{ }";
  1739. }
  1740. /*
  1741. =================
  1742. idDeclLocal::Parse
  1743. =================
  1744. */
  1745. bool idDeclLocal::Parse( const char *text, const int textLength ) {
  1746. idLexer src;
  1747. src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
  1748. src.SetFlags( DECL_LEXER_FLAGS );
  1749. src.SkipUntilString( "{" );
  1750. src.SkipBracedSection( false );
  1751. return true;
  1752. }
  1753. /*
  1754. =================
  1755. idDeclLocal::FreeData
  1756. =================
  1757. */
  1758. void idDeclLocal::FreeData() {
  1759. }
  1760. /*
  1761. =================
  1762. idDeclLocal::List
  1763. =================
  1764. */
  1765. void idDeclLocal::List() const {
  1766. common->Printf( "%s\n", GetName() );
  1767. }
  1768. /*
  1769. =================
  1770. idDeclLocal::Print
  1771. =================
  1772. */
  1773. void idDeclLocal::Print() const {
  1774. }
  1775. /*
  1776. =================
  1777. idDeclLocal::Reload
  1778. =================
  1779. */
  1780. void idDeclLocal::Reload( void ) {
  1781. this->sourceFile->Reload( false );
  1782. }
  1783. /*
  1784. =================
  1785. idDeclLocal::AllocateSelf
  1786. =================
  1787. */
  1788. void idDeclLocal::AllocateSelf( void ) {
  1789. if ( self == NULL ) {
  1790. self = declManagerLocal.GetDeclType( (int)type )->allocator();
  1791. self->base = this;
  1792. }
  1793. }
  1794. /*
  1795. =================
  1796. idDeclLocal::ParseLocal
  1797. =================
  1798. */
  1799. void idDeclLocal::ParseLocal( void ) {
  1800. bool generatedDefaultText = false;
  1801. AllocateSelf();
  1802. // always free data before parsing
  1803. self->FreeData();
  1804. declManagerLocal.MediaPrint( "parsing %s %s\n", declManagerLocal.declTypes[type]->typeName.c_str(), name.c_str() );
  1805. // if no text source try to generate default text
  1806. if ( textSource == NULL ) {
  1807. generatedDefaultText = self->SetDefaultText();
  1808. }
  1809. // indent for DEFAULTED or media file references
  1810. declManagerLocal.indent++;
  1811. // no text immediately causes a MakeDefault()
  1812. if ( textSource == NULL ) {
  1813. MakeDefault();
  1814. declManagerLocal.indent--;
  1815. return;
  1816. }
  1817. declState = DS_PARSED;
  1818. // parse
  1819. char *declText = (char *) _alloca( ( GetTextLength() + 1 ) * sizeof( char ) );
  1820. GetText( declText );
  1821. self->Parse( declText, GetTextLength() );
  1822. // free generated text
  1823. if ( generatedDefaultText ) {
  1824. Mem_Free( textSource );
  1825. textSource = 0;
  1826. textLength = 0;
  1827. }
  1828. declManagerLocal.indent--;
  1829. }
  1830. /*
  1831. =================
  1832. idDeclLocal::Purge
  1833. =================
  1834. */
  1835. void idDeclLocal::Purge( void ) {
  1836. // never purge things that were referenced outside level load,
  1837. // like the console and menu graphics
  1838. if ( parsedOutsideLevelLoad ) {
  1839. return;
  1840. }
  1841. referencedThisLevel = false;
  1842. MakeDefault();
  1843. // the next Find() for this will re-parse the real data
  1844. declState = DS_UNPARSED;
  1845. }
  1846. /*
  1847. =================
  1848. idDeclLocal::EverReferenced
  1849. =================
  1850. */
  1851. bool idDeclLocal::EverReferenced( void ) const {
  1852. return everReferenced;
  1853. }