DeclManager.cpp 67 KB

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