consoleDictionary.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/consoleDictionary.h"
  23. #include "console/consoleNamespace.h"
  24. #include "platform/platform.h"
  25. #include "console/console.h"
  26. #include "console/ast.h"
  27. #include "collection/findIterator.h"
  28. #include "io/resource/resourceManager.h"
  29. #include "string/findMatch.h"
  30. #include "io/fileStream.h"
  31. #include "console/compiler.h"
  32. static char scratchBuffer[1024];
  33. #define ST_INIT_SIZE 15
  34. struct StringValue
  35. {
  36. S32 size;
  37. char *val;
  38. operator char *() { return val; }
  39. StringValue &operator=(const char *string);
  40. StringValue() { size = 0; val = NULL; }
  41. ~StringValue() { dFree(val); }
  42. };
  43. StringValue & StringValue::operator=(const char *string)
  44. {
  45. if(!val)
  46. {
  47. val = dStrdup(string);
  48. size = dStrlen(val);
  49. }
  50. else
  51. {
  52. S32 len = dStrlen(string);
  53. if(len < size)
  54. dStrcpy(val, string);
  55. else
  56. {
  57. size = len;
  58. dFree(val);
  59. val = dStrdup(string);
  60. }
  61. }
  62. return *this;
  63. }
  64. static S32 QSORT_CALLBACK varCompare(const void* a,const void* b)
  65. {
  66. return dStricmp( (*((Dictionary::Entry **) a))->name, (*((Dictionary::Entry **) b))->name );
  67. }
  68. void Dictionary::exportVariables(const char *varString, const char *fileName, bool append)
  69. {
  70. const char *searchStr = varString;
  71. Vector<Entry *> sortList(__FILE__, __LINE__);
  72. for(S32 i = 0; i < hashTable->size;i ++)
  73. {
  74. Entry *walk = hashTable->data[i];
  75. while(walk)
  76. {
  77. if(FindMatch::isMatch((char *) searchStr, (char *) walk->name))
  78. sortList.push_back(walk);
  79. walk = walk->nextEntry;
  80. }
  81. }
  82. if(!sortList.size())
  83. return;
  84. dQsort((void *)sortList.address(), sortList.size(), sizeof(Entry *), varCompare);
  85. Vector<Entry *>::iterator s;
  86. char expandBuffer[1024];
  87. FileStream strm;
  88. if(fileName)
  89. {
  90. if(!ResourceManager->openFileForWrite(strm, fileName, append ? FileStream::ReadWrite : FileStream::Write))
  91. {
  92. Con::errorf(ConsoleLogEntry::General, "Unable to open file '%s for writing.", fileName);
  93. return;
  94. }
  95. if(append)
  96. strm.setPosition(strm.getStreamSize());
  97. }
  98. char buffer[1024];
  99. const char *cat = fileName ? "\r\n" : "";
  100. for(s = sortList.begin(); s != sortList.end(); s++)
  101. {
  102. switch((*s)->type)
  103. {
  104. case Entry::TypeInternalInt:
  105. dSprintf(buffer, sizeof(buffer), "%s = %d;%s", (*s)->name, (*s)->ival, cat);
  106. break;
  107. case Entry::TypeInternalFloat:
  108. dSprintf(buffer, sizeof(buffer), "%s = %g;%s", (*s)->name, (*s)->fval, cat);
  109. break;
  110. default:
  111. expandEscape(expandBuffer, (*s)->getStringValue());
  112. dSprintf(buffer, sizeof(buffer), "%s = \"%s\";%s", (*s)->name, expandBuffer, cat);
  113. break;
  114. }
  115. if(fileName)
  116. strm.write(dStrlen(buffer), buffer);
  117. else
  118. Con::printf("%s", buffer);
  119. }
  120. if(fileName)
  121. strm.close();
  122. }
  123. void Dictionary::deleteVariables(const char *varString)
  124. {
  125. const char *searchStr = varString;
  126. for(S32 i = 0; i < hashTable->size; i++)
  127. {
  128. Entry *walk = hashTable->data[i];
  129. while(walk)
  130. {
  131. Entry *matchedEntry = (FindMatch::isMatch((char *) searchStr, (char *) walk->name)) ? walk : NULL;
  132. walk = walk->nextEntry;
  133. if (matchedEntry)
  134. remove(matchedEntry); // assumes remove() is a stable remove (will not reorder entries on remove)
  135. }
  136. }
  137. }
  138. U32 HashPointer(StringTableEntry ptr)
  139. {
  140. return (U32)(((dsize_t)ptr) >> 2);
  141. }
  142. Dictionary::Entry *Dictionary::lookup(StringTableEntry name)
  143. {
  144. Entry *walk = hashTable->data[HashPointer(name) % hashTable->size];
  145. while(walk)
  146. {
  147. if(walk->name == name)
  148. return walk;
  149. else
  150. walk = walk->nextEntry;
  151. }
  152. return NULL;
  153. }
  154. Dictionary::Entry *Dictionary::add(StringTableEntry name)
  155. {
  156. Entry *walk = hashTable->data[HashPointer(name) % hashTable->size];
  157. while(walk)
  158. {
  159. if(walk->name == name)
  160. return walk;
  161. else
  162. walk = walk->nextEntry;
  163. }
  164. Entry *ret;
  165. hashTable->count++;
  166. if(hashTable->count > hashTable->size * 2)
  167. {
  168. Entry head(NULL), *walk;
  169. S32 i;
  170. walk = &head;
  171. walk->nextEntry = 0;
  172. for(i = 0; i < hashTable->size; i++) {
  173. while(walk->nextEntry) {
  174. walk = walk->nextEntry;
  175. }
  176. walk->nextEntry = hashTable->data[i];
  177. }
  178. delete[] hashTable->data;
  179. hashTable->size = hashTable->size * 4 - 1;
  180. hashTable->data = new Entry *[hashTable->size];
  181. for(i = 0; i < hashTable->size; i++)
  182. hashTable->data[i] = NULL;
  183. walk = head.nextEntry;
  184. while(walk)
  185. {
  186. Entry *temp = walk->nextEntry;
  187. S32 idx = HashPointer(walk->name) % hashTable->size;
  188. walk->nextEntry = hashTable->data[idx];
  189. hashTable->data[idx] = walk;
  190. walk = temp;
  191. }
  192. }
  193. ret = new Entry(name);
  194. S32 idx = HashPointer(name) % hashTable->size;
  195. ret->nextEntry = hashTable->data[idx];
  196. hashTable->data[idx] = ret;
  197. return ret;
  198. }
  199. // deleteVariables() assumes remove() is a stable remove (will not reorder entries on remove)
  200. void Dictionary::remove(Dictionary::Entry *ent)
  201. {
  202. Entry **walk = &hashTable->data[HashPointer(ent->name) % hashTable->size];
  203. while(*walk != ent)
  204. walk = &((*walk)->nextEntry);
  205. *walk = (ent->nextEntry);
  206. delete ent;
  207. hashTable->count--;
  208. }
  209. Dictionary::Dictionary()
  210. : hashTable( NULL ),
  211. exprState( NULL ),
  212. scopeName( NULL ),
  213. scopeNamespace( NULL ),
  214. code( NULL ),
  215. ip( 0 )
  216. {
  217. }
  218. Dictionary::Dictionary(ExprEvalState *state, Dictionary* ref)
  219. : hashTable( NULL ),
  220. exprState( NULL ),
  221. scopeName( NULL ),
  222. scopeNamespace( NULL ),
  223. code( NULL ),
  224. ip( 0 )
  225. {
  226. setState(state,ref);
  227. }
  228. void Dictionary::setState(ExprEvalState *state, Dictionary* ref)
  229. {
  230. exprState = state;
  231. if (ref)
  232. hashTable = ref->hashTable;
  233. else
  234. {
  235. hashTable = new HashTableData;
  236. hashTable->owner = this;
  237. hashTable->count = 0;
  238. hashTable->size = ST_INIT_SIZE;
  239. hashTable->data = new Entry *[hashTable->size];
  240. for(S32 i = 0; i < hashTable->size; i++)
  241. hashTable->data[i] = NULL;
  242. }
  243. }
  244. Dictionary::~Dictionary()
  245. {
  246. if ( hashTable->owner == this )
  247. {
  248. reset();
  249. delete [] hashTable->data;
  250. delete hashTable;
  251. }
  252. }
  253. void Dictionary::reset()
  254. {
  255. S32 i;
  256. Entry *walk, *temp;
  257. for(i = 0; i < hashTable->size; i++)
  258. {
  259. walk = hashTable->data[i];
  260. while(walk)
  261. {
  262. temp = walk->nextEntry;
  263. delete walk;
  264. walk = temp;
  265. }
  266. hashTable->data[i] = NULL;
  267. }
  268. hashTable->size = ST_INIT_SIZE;
  269. hashTable->count = 0;
  270. }
  271. const char *Dictionary::tabComplete(const char *prevText, S32 baseLen, bool fForward)
  272. {
  273. S32 i;
  274. const char *bestMatch = NULL;
  275. for(i = 0; i < hashTable->size; i++)
  276. {
  277. Entry *walk = hashTable->data[i];
  278. while(walk)
  279. {
  280. if(Namespace::canTabComplete(prevText, bestMatch, walk->name, baseLen, fForward))
  281. bestMatch = walk->name;
  282. walk = walk->nextEntry;
  283. }
  284. }
  285. return bestMatch;
  286. }
  287. char *typeValueEmpty = (char*)"";
  288. Dictionary::Entry::Entry(StringTableEntry in_name)
  289. {
  290. dataPtr = NULL;
  291. name = in_name;
  292. type = -1;
  293. ival = 0;
  294. fval = 0;
  295. sval = typeValueEmpty;
  296. }
  297. Dictionary::Entry::~Entry()
  298. {
  299. if(sval != typeValueEmpty)
  300. dFree(sval);
  301. }
  302. const char *Dictionary::getVariable(StringTableEntry name, bool *entValid)
  303. {
  304. Entry *ent = lookup(name);
  305. if(ent)
  306. {
  307. if(entValid)
  308. *entValid = true;
  309. return ent->getStringValue();
  310. }
  311. if(entValid)
  312. *entValid = false;
  313. // Warn users when they access a variable that isn't defined.
  314. if(gWarnUndefinedScriptVariables)
  315. Con::warnf(" *** Accessed undefined variable '%s'", name);
  316. return "";
  317. }
  318. void Dictionary::Entry::setStringValue(const char * value)
  319. {
  320. if(type <= TypeInternalString)
  321. {
  322. // Let's not remove empty-string-valued global vars from the dict.
  323. // If we remove them, then they won't be exported, and sometimes
  324. // it could be necessary to export such a global. There are very
  325. // few empty-string global vars so there's no performance-related
  326. // need to remove them from the dict.
  327. /*
  328. if(!value[0] && name[0] == '$')
  329. {
  330. gEvalState.globalVars.remove(this);
  331. return;
  332. }
  333. */
  334. U32 stringLen = dStrlen(value);
  335. // If it's longer than 256 bytes, it's certainly not a number.
  336. //
  337. // (This decision may come back to haunt you. Shame on you if it
  338. // does.)
  339. if(stringLen < 256)
  340. {
  341. fval = dAtof(value);
  342. ival = dAtoi(value);
  343. }
  344. else
  345. {
  346. fval = 0.f;
  347. ival = 0;
  348. }
  349. type = TypeInternalString;
  350. // may as well pad to the next cache line
  351. U32 newLen = ((stringLen + 1) + 15) & ~15;
  352. if(sval == typeValueEmpty)
  353. sval = (char *) dMalloc(newLen);
  354. else if(newLen > bufferLen)
  355. sval = (char *) dRealloc(sval, newLen);
  356. bufferLen = newLen;
  357. dStrcpy(sval, value);
  358. }
  359. else
  360. Con::setData(type, dataPtr, 0, 1, &value);
  361. }
  362. void Dictionary::setVariable(StringTableEntry name, const char *value)
  363. {
  364. Entry *ent = add(name);
  365. if(!value)
  366. value = "";
  367. ent->setStringValue(value);
  368. }
  369. void Dictionary::addVariable(const char *name, S32 type, void *dataPtr)
  370. {
  371. if(name[0] != '$')
  372. {
  373. scratchBuffer[0] = '$';
  374. dStrcpy(scratchBuffer + 1, name);
  375. name = scratchBuffer;
  376. }
  377. Entry *ent = add(StringTable->insert(name));
  378. ent->type = type;
  379. if(ent->sval != typeValueEmpty)
  380. {
  381. dFree(ent->sval);
  382. ent->sval = typeValueEmpty;
  383. }
  384. ent->dataPtr = dataPtr;
  385. }
  386. bool Dictionary::removeVariable(StringTableEntry name)
  387. {
  388. if( Entry *ent = lookup(name) ){
  389. remove( ent );
  390. return true;
  391. }
  392. return false;
  393. }