RegExp.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include "RegExp.h"
  23. #include "DeviceContext.h"
  24. #include "Window.h"
  25. #include "UserInterfaceLocal.h"
  26. int idRegister::REGCOUNT[NUMTYPES] = {4, 1, 1, 1, 0, 2, 3, 4};
  27. /*
  28. ====================
  29. idRegister::SetToRegs
  30. ====================
  31. */
  32. void idRegister::SetToRegs( float *registers ) {
  33. int i;
  34. idVec4 v;
  35. idVec2 v2;
  36. idVec3 v3;
  37. idRectangle rect;
  38. if ( !enabled || var == NULL || ( var && ( var->GetDict() || !var->GetEval() ) ) ) {
  39. return;
  40. }
  41. switch( type ) {
  42. case VEC4: {
  43. v = *static_cast<idWinVec4*>(var);
  44. break;
  45. }
  46. case RECTANGLE: {
  47. rect = *static_cast<idWinRectangle*>(var);
  48. v = rect.ToVec4();
  49. break;
  50. }
  51. case VEC2: {
  52. v2 = *static_cast<idWinVec2*>(var);
  53. v[0] = v2[0];
  54. v[1] = v2[1];
  55. break;
  56. }
  57. case VEC3: {
  58. v3 = *static_cast<idWinVec3*>(var);
  59. v[0] = v3[0];
  60. v[1] = v3[1];
  61. v[2] = v3[2];
  62. break;
  63. }
  64. case FLOAT: {
  65. v[0] = *static_cast<idWinFloat*>(var);
  66. break;
  67. }
  68. case INT: {
  69. v[0] = *static_cast<idWinInt*>(var);
  70. break;
  71. }
  72. case BOOL: {
  73. v[0] = *static_cast<idWinBool*>(var);
  74. break;
  75. }
  76. default: {
  77. common->FatalError( "idRegister::SetToRegs: bad reg type" );
  78. break;
  79. }
  80. }
  81. for ( i = 0; i < regCount; i++ ) {
  82. registers[ regs[ i ] ] = v[i];
  83. }
  84. }
  85. /*
  86. =================
  87. idRegister::GetFromRegs
  88. =================
  89. */
  90. void idRegister::GetFromRegs( float *registers ) {
  91. idVec4 v;
  92. idRectangle rect;
  93. if (!enabled || var == NULL || (var && (var->GetDict() || !var->GetEval()))) {
  94. return;
  95. }
  96. for ( int i = 0; i < regCount; i++ ) {
  97. v[i] = registers[regs[i]];
  98. }
  99. switch( type ) {
  100. case VEC4: {
  101. *dynamic_cast<idWinVec4*>(var) = v;
  102. break;
  103. }
  104. case RECTANGLE: {
  105. rect.x = v.x;
  106. rect.y = v.y;
  107. rect.w = v.z;
  108. rect.h = v.w;
  109. *static_cast<idWinRectangle*>(var) = rect;
  110. break;
  111. }
  112. case VEC2: {
  113. *static_cast<idWinVec2*>(var) = v.ToVec2();
  114. break;
  115. }
  116. case VEC3: {
  117. *static_cast<idWinVec3*>(var) = v.ToVec3();
  118. break;
  119. }
  120. case FLOAT: {
  121. *static_cast<idWinFloat*>(var) = v[0];
  122. break;
  123. }
  124. case INT: {
  125. *static_cast<idWinInt*>(var) = v[0];
  126. break;
  127. }
  128. case BOOL: {
  129. *static_cast<idWinBool*>(var) = ( v[0] != 0.0f );
  130. break;
  131. }
  132. default: {
  133. common->FatalError( "idRegister::GetFromRegs: bad reg type" );
  134. break;
  135. }
  136. }
  137. }
  138. /*
  139. =================
  140. idRegister::ReadFromDemoFile
  141. =================
  142. */
  143. void idRegister::ReadFromDemoFile(idDemoFile *f) {
  144. f->ReadBool( enabled );
  145. f->ReadShort( type );
  146. f->ReadInt( regCount );
  147. for ( int i = 0; i < 4; i++ )
  148. f->ReadUnsignedShort( regs[i] );
  149. name = f->ReadHashString();
  150. }
  151. /*
  152. =================
  153. idRegister::WriteToDemoFile
  154. =================
  155. */
  156. void idRegister::WriteToDemoFile( idDemoFile *f ) {
  157. f->WriteBool( enabled );
  158. f->WriteShort( type );
  159. f->WriteInt( regCount );
  160. for (int i = 0; i < 4; i++)
  161. f->WriteUnsignedShort( regs[i] );
  162. f->WriteHashString( name );
  163. }
  164. /*
  165. =================
  166. idRegister::WriteToSaveGame
  167. =================
  168. */
  169. void idRegister::WriteToSaveGame( idFile *savefile ) {
  170. int len;
  171. savefile->Write( &enabled, sizeof( enabled ) );
  172. savefile->Write( &type, sizeof( type ) );
  173. savefile->Write( &regCount, sizeof( regCount ) );
  174. savefile->Write( &regs[0], sizeof( regs ) );
  175. len = name.Length();
  176. savefile->Write( &len, sizeof( len ) );
  177. savefile->Write( name.c_str(), len );
  178. var->WriteToSaveGame( savefile );
  179. }
  180. /*
  181. ================
  182. idRegister::ReadFromSaveGame
  183. ================
  184. */
  185. void idRegister::ReadFromSaveGame( idFile *savefile ) {
  186. int len;
  187. savefile->Read( &enabled, sizeof( enabled ) );
  188. savefile->Read( &type, sizeof( type ) );
  189. savefile->Read( &regCount, sizeof( regCount ) );
  190. savefile->Read( &regs[0], sizeof( regs ) );
  191. savefile->Read( &len, sizeof( len ) );
  192. name.Fill( ' ', len );
  193. savefile->Read( &name[0], len );
  194. var->ReadFromSaveGame( savefile );
  195. }
  196. /*
  197. ====================
  198. idRegisterList::AddReg
  199. ====================
  200. */
  201. void idRegisterList::AddReg( const char *name, int type, idVec4 data, idWindow *win, idWinVar *var ) {
  202. if ( FindReg( name ) == NULL ) {
  203. assert( type >= 0 && type < idRegister::NUMTYPES );
  204. int numRegs = idRegister::REGCOUNT[type];
  205. idRegister *reg = new idRegister( name, type );
  206. reg->var = var;
  207. for ( int i = 0; i < numRegs; i++ ) {
  208. reg->regs[i] = win->ExpressionConstant(data[i]);
  209. }
  210. int hash = regHash.GenerateKey( name, false );
  211. regHash.Add( hash, regs.Append( reg ) );
  212. }
  213. }
  214. /*
  215. ====================
  216. idRegisterList::AddReg
  217. ====================
  218. */
  219. void idRegisterList::AddReg( const char *name, int type, idParser *src, idWindow *win, idWinVar *var ) {
  220. idRegister* reg;
  221. reg = FindReg( name );
  222. if ( reg == NULL ) {
  223. assert(type >= 0 && type < idRegister::NUMTYPES);
  224. int numRegs = idRegister::REGCOUNT[type];
  225. reg = new idRegister( name, type );
  226. reg->var = var;
  227. if ( type == idRegister::STRING ) {
  228. idToken tok;
  229. if ( src->ReadToken( &tok ) ) {
  230. tok = common->GetLanguageDict()->GetString( tok );
  231. var->Init( tok, win );
  232. }
  233. } else {
  234. for ( int i = 0; i < numRegs; i++ ) {
  235. reg->regs[i] = win->ParseExpression(src, NULL);
  236. if ( i < numRegs-1 ) {
  237. src->ExpectTokenString(",");
  238. }
  239. }
  240. }
  241. int hash = regHash.GenerateKey( name, false );
  242. regHash.Add( hash, regs.Append( reg ) );
  243. } else {
  244. int numRegs = idRegister::REGCOUNT[type];
  245. reg->var = var;
  246. if ( type == idRegister::STRING ) {
  247. idToken tok;
  248. if ( src->ReadToken( &tok ) ) {
  249. var->Init( tok, win );
  250. }
  251. } else {
  252. for ( int i = 0; i < numRegs; i++ ) {
  253. reg->regs[i] = win->ParseExpression( src, NULL );
  254. if ( i < numRegs-1 ) {
  255. src->ExpectTokenString(",");
  256. }
  257. }
  258. }
  259. }
  260. }
  261. /*
  262. ====================
  263. idRegisterList::GetFromRegs
  264. ====================
  265. */
  266. void idRegisterList::GetFromRegs(float *registers) {
  267. for ( int i = 0; i < regs.Num(); i++ ) {
  268. regs[i]->GetFromRegs( registers );
  269. }
  270. }
  271. /*
  272. ====================
  273. idRegisterList::SetToRegs
  274. ====================
  275. */
  276. void idRegisterList::SetToRegs( float *registers ) {
  277. int i;
  278. for ( i = 0; i < regs.Num(); i++ ) {
  279. regs[i]->SetToRegs( registers );
  280. }
  281. }
  282. /*
  283. ====================
  284. idRegisterList::FindReg
  285. ====================
  286. */
  287. idRegister *idRegisterList::FindReg( const char *name ) {
  288. int hash = regHash.GenerateKey( name, false );
  289. for ( int i = regHash.First( hash ); i != -1; i = regHash.Next( i ) ) {
  290. if ( regs[i]->name.Icmp( name ) == 0 ) {
  291. return regs[i];
  292. }
  293. }
  294. return NULL;
  295. }
  296. /*
  297. ====================
  298. idRegisterList::Reset
  299. ====================
  300. */
  301. void idRegisterList::Reset() {
  302. regs.DeleteContents( true );
  303. regHash.Clear();
  304. }
  305. /*
  306. ====================
  307. idRegisterList::ReadFromSaveGame
  308. ====================
  309. */
  310. void idRegisterList::ReadFromDemoFile(idDemoFile *f) {
  311. int c;
  312. f->ReadInt( c );
  313. regs.DeleteContents( true );
  314. for ( int i = 0; i < c; i++ ) {
  315. idRegister *reg = new idRegister;
  316. reg->ReadFromDemoFile( f );
  317. regs.Append( reg );
  318. }
  319. }
  320. /*
  321. ====================
  322. idRegisterList::ReadFromSaveGame
  323. ====================
  324. */
  325. void idRegisterList::WriteToDemoFile(idDemoFile *f) {
  326. int c = regs.Num();
  327. f->WriteInt( c );
  328. for ( int i = 0 ; i < c; i++ ) {
  329. regs[i]->WriteToDemoFile(f);
  330. }
  331. }
  332. /*
  333. =====================
  334. idRegisterList::WriteToSaveGame
  335. =====================
  336. */
  337. void idRegisterList::WriteToSaveGame( idFile *savefile ) {
  338. int i, num;
  339. num = regs.Num();
  340. savefile->Write( &num, sizeof( num ) );
  341. for ( i = 0; i < num; i++ ) {
  342. regs[i]->WriteToSaveGame( savefile );
  343. }
  344. }
  345. /*
  346. ====================
  347. idRegisterList::ReadFromSaveGame
  348. ====================
  349. */
  350. void idRegisterList::ReadFromSaveGame( idFile *savefile ) {
  351. int i, num;
  352. savefile->Read( &num, sizeof( num ) );
  353. for ( i = 0; i < num; i++ ) {
  354. regs[i]->ReadFromSaveGame( savefile );
  355. }
  356. }