simFieldDictionary.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "console/simFieldDictionary.h"
  28. #include "console/console.h"
  29. #include "console/consoleInternal.h"
  30. #include "core/frameAllocator.h"
  31. SimFieldDictionary::Entry *SimFieldDictionary::smFreeList = NULL;
  32. static Chunker<SimFieldDictionary::Entry> fieldChunker;
  33. U32 SimFieldDictionary::getHashValue(StringTableEntry slotName)
  34. {
  35. return HashPointer(slotName) % HashTableSize;
  36. }
  37. U32 SimFieldDictionary::getHashValue(const String& fieldName)
  38. {
  39. return getHashValue(StringTable->insert(fieldName));
  40. }
  41. SimFieldDictionary::Entry *SimFieldDictionary::addEntry(U32 bucket, StringTableEntry slotName, ConsoleBaseType* type, char* value)
  42. {
  43. Entry* ret;
  44. if (smFreeList)
  45. {
  46. ret = smFreeList;
  47. smFreeList = ret->next;
  48. }
  49. else
  50. ret = fieldChunker.alloc();
  51. ret->next = mHashTable[bucket];
  52. ret->slotName = slotName;
  53. ret->type = type;
  54. ret->value = value;
  55. mHashTable[bucket] = ret;
  56. mNumFields++;
  57. mVersion++;
  58. return ret;
  59. }
  60. void SimFieldDictionary::freeEntry(SimFieldDictionary::Entry *ent)
  61. {
  62. ent->next = smFreeList;
  63. smFreeList = ent;
  64. mNumFields--;
  65. }
  66. SimFieldDictionary::SimFieldDictionary()
  67. : mNumFields(0),
  68. mVersion(0)
  69. {
  70. dMemset(mHashTable, 0, sizeof(mHashTable));
  71. }
  72. SimFieldDictionary::~SimFieldDictionary()
  73. {
  74. for (U32 i = 0; i < HashTableSize; i++)
  75. {
  76. for (Entry *walk = mHashTable[i]; walk;)
  77. {
  78. Entry *temp = walk;
  79. walk = temp->next;
  80. if (temp->value)
  81. dFree(temp->value);
  82. freeEntry(temp);
  83. }
  84. }
  85. AssertFatal(mNumFields == 0, "Incorrect count on field dictionary");
  86. }
  87. void SimFieldDictionary::setFieldType(StringTableEntry slotName, const char *typeString)
  88. {
  89. ConsoleBaseType *cbt = ConsoleBaseType::getTypeByName(typeString);
  90. setFieldType(slotName, cbt);
  91. }
  92. void SimFieldDictionary::setFieldType(StringTableEntry slotName, const U32 typeId)
  93. {
  94. ConsoleBaseType *cbt = ConsoleBaseType::getType(typeId);
  95. setFieldType(slotName, cbt);
  96. }
  97. void SimFieldDictionary::setFieldType(StringTableEntry slotName, ConsoleBaseType *type)
  98. {
  99. // If the field exists on the object, set the type
  100. U32 bucket = getHashValue(slotName);
  101. for (Entry *walk = mHashTable[bucket]; walk; walk = walk->next)
  102. {
  103. if (walk->slotName == slotName)
  104. {
  105. // Found and type assigned, let's bail
  106. walk->type = type;
  107. return;
  108. }
  109. }
  110. // Otherwise create the field, and set the type. Assign a null value.
  111. addEntry(bucket, slotName, type);
  112. }
  113. U32 SimFieldDictionary::getFieldType(StringTableEntry slotName) const
  114. {
  115. U32 bucket = getHashValue(slotName);
  116. for (Entry *walk = mHashTable[bucket]; walk; walk = walk->next)
  117. if (walk->slotName == slotName)
  118. return walk->type ? walk->type->getTypeID() : TypeString;
  119. return TypeString;
  120. }
  121. SimFieldDictionary::Entry *SimFieldDictionary::findDynamicField(const String &fieldName) const
  122. {
  123. U32 bucket = getHashValue(fieldName);
  124. for (Entry *walk = mHashTable[bucket]; walk; walk = walk->next)
  125. {
  126. if (fieldName.equal(walk->slotName, String::NoCase))
  127. return walk;
  128. }
  129. return NULL;
  130. }
  131. SimFieldDictionary::Entry *SimFieldDictionary::findDynamicField(StringTableEntry fieldName) const
  132. {
  133. U32 bucket = getHashValue(fieldName);
  134. for (Entry *walk = mHashTable[bucket]; walk; walk = walk->next)
  135. {
  136. if (walk->slotName == fieldName)
  137. {
  138. return walk;
  139. }
  140. }
  141. return NULL;
  142. }
  143. void SimFieldDictionary::setFieldValue(StringTableEntry slotName, const char *value)
  144. {
  145. U32 bucket = getHashValue(slotName);
  146. Entry **walk = &mHashTable[bucket];
  147. while (*walk && (*walk)->slotName != slotName)
  148. walk = &((*walk)->next);
  149. Entry *field = *walk;
  150. if (!value || !*value)
  151. {
  152. if (field)
  153. {
  154. mVersion++;
  155. if (field->value)
  156. dFree(field->value);
  157. *walk = field->next;
  158. freeEntry(field);
  159. }
  160. }
  161. else
  162. {
  163. if (field)
  164. {
  165. if (field->value)
  166. dFree(field->value);
  167. field->value = dStrdup(value);
  168. }
  169. else
  170. addEntry(bucket, slotName, 0, dStrdup(value));
  171. }
  172. }
  173. const char *SimFieldDictionary::getFieldValue(StringTableEntry slotName)
  174. {
  175. U32 bucket = getHashValue(slotName);
  176. for (Entry *walk = mHashTable[bucket]; walk; walk = walk->next)
  177. if (walk->slotName == slotName)
  178. return walk->value;
  179. return NULL;
  180. }
  181. void SimFieldDictionary::assignFrom(SimFieldDictionary *dict)
  182. {
  183. mVersion++;
  184. for (U32 i = 0; i < HashTableSize; i++)
  185. {
  186. for (Entry *walk = dict->mHashTable[i]; walk; walk = walk->next)
  187. {
  188. setFieldValue(walk->slotName, walk->value);
  189. setFieldType(walk->slotName, walk->type);
  190. }
  191. }
  192. }
  193. static S32 QSORT_CALLBACK compareEntries(const void* a, const void* b)
  194. {
  195. SimFieldDictionary::Entry *fa = *((SimFieldDictionary::Entry **)a);
  196. SimFieldDictionary::Entry *fb = *((SimFieldDictionary::Entry **)b);
  197. return dStricmp(fa->slotName, fb->slotName);
  198. }
  199. void SimFieldDictionary::writeFields(SimObject *obj, Stream &stream, U32 tabStop)
  200. {
  201. const AbstractClassRep::FieldList &list = obj->getFieldList();
  202. Vector<Entry *> flist(__FILE__, __LINE__);
  203. for (U32 curEntry = 0; curEntry < HashTableSize; curEntry++)
  204. {
  205. for (Entry *walk = mHashTable[curEntry]; walk; walk = walk->next)
  206. {
  207. // make sure we haven't written this out yet:
  208. U32 curField;
  209. for (curField = 0; curField < list.size(); curField++)
  210. if (list[curField].pFieldname == walk->slotName)
  211. break;
  212. if (curField != list.size())
  213. continue;
  214. if (!obj->writeField(walk->slotName, walk->value))
  215. continue;
  216. flist.push_back(walk);
  217. }
  218. }
  219. // Sort Entries to prevent version control conflicts
  220. dQsort(flist.address(), flist.size(), sizeof(Entry *), compareEntries);
  221. // Save them out
  222. for (Vector<Entry *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  223. {
  224. U32 nBufferSize = (dStrlen((*itr)->value) * 2) + dStrlen((*itr)->slotName) + 16;
  225. FrameTemp<char> expandedBuffer(nBufferSize);
  226. stream.writeTabs(tabStop + 1);
  227. const char *typeName = (*itr)->type && (*itr)->type->getTypeID() != TypeString ? (*itr)->type->getTypeName() : "";
  228. dSprintf(expandedBuffer, nBufferSize, "%s%s%s = \"", typeName, *typeName ? " " : "", (*itr)->slotName);
  229. if ((*itr)->value)
  230. expandEscape((char*)expandedBuffer + dStrlen(expandedBuffer), (*itr)->value);
  231. dStrcat(expandedBuffer, "\";\r\n", nBufferSize);
  232. stream.write(dStrlen(expandedBuffer), expandedBuffer);
  233. }
  234. }
  235. void SimFieldDictionary::printFields(SimObject *obj)
  236. {
  237. const AbstractClassRep::FieldList &list = obj->getFieldList();
  238. char expandedBuffer[4096];
  239. Vector<Entry *> flist(__FILE__, __LINE__);
  240. for (U32 curEntry = 0; curEntry < HashTableSize; curEntry++)
  241. {
  242. for (Entry *walk = mHashTable[curEntry]; walk; walk = walk->next)
  243. {
  244. // make sure we haven't written this out yet:
  245. U32 curField;
  246. for (curField = 0; curField < list.size(); curField++)
  247. if (list[curField].pFieldname == walk->slotName)
  248. break;
  249. if (curField != list.size())
  250. continue;
  251. flist.push_back(walk);
  252. }
  253. }
  254. dQsort(flist.address(), flist.size(), sizeof(Entry *), compareEntries);
  255. for (Vector<Entry *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  256. {
  257. const char* type = "string";
  258. if ((*itr)->type)
  259. type = (*itr)->type->getTypeClassName();
  260. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s %s = \"", type, (*itr)->slotName);
  261. if ((*itr)->value)
  262. expandEscape(expandedBuffer + dStrlen(expandedBuffer), (*itr)->value);
  263. Con::printf("%s\"", expandedBuffer);
  264. }
  265. }
  266. SimFieldDictionary::Entry *SimFieldDictionary::operator[](U32 index)
  267. {
  268. AssertFatal(index < mNumFields, "out of range");
  269. if (index > mNumFields)
  270. return NULL;
  271. SimFieldDictionaryIterator itr(this);
  272. for (S32 i = 0; i < index && *itr; i++)
  273. ++itr;
  274. return (*itr);
  275. }
  276. //------------------------------------------------------------------------------
  277. SimFieldDictionaryIterator::SimFieldDictionaryIterator(SimFieldDictionary * dictionary)
  278. {
  279. mDictionary = dictionary;
  280. mHashIndex = -1;
  281. mEntry = 0;
  282. operator++();
  283. }
  284. SimFieldDictionary::Entry* SimFieldDictionaryIterator::operator++()
  285. {
  286. if (!mDictionary)
  287. return(mEntry);
  288. if (mEntry)
  289. mEntry = mEntry->next;
  290. while (!mEntry && (mHashIndex < (SimFieldDictionary::HashTableSize - 1)))
  291. mEntry = mDictionary->mHashTable[++mHashIndex];
  292. return(mEntry);
  293. }
  294. SimFieldDictionary::Entry* SimFieldDictionaryIterator::operator*()
  295. {
  296. return(mEntry);
  297. }
  298. // A variation of the stock SimFieldDictionary::setFieldValue(), this method adds the
  299. // <no_replace> argument which, when true, prohibits the replacement of fields that
  300. // already have a value.
  301. //
  302. // AFX uses this when an effects-choreographer (afxMagicSpell, afxEffectron) is created
  303. // using the new operator. It prevents any in-line effect parameters from being overwritten
  304. // by default parameters that are copied over later.
  305. void SimFieldDictionary::setFieldValue(StringTableEntry slotName, const char *value, ConsoleBaseType *type, bool no_replace)
  306. {
  307. if (!no_replace)
  308. {
  309. setFieldValue(slotName, value);
  310. return;
  311. }
  312. if (!value || !*value)
  313. return;
  314. U32 bucket = getHashValue(slotName);
  315. Entry **walk = &mHashTable[bucket];
  316. while (*walk && (*walk)->slotName != slotName)
  317. walk = &((*walk)->next);
  318. Entry *field = *walk;
  319. if (field)
  320. return;
  321. addEntry(bucket, slotName, type, dStrdup(value));
  322. }
  323. // A variation of the stock SimFieldDictionary::assignFrom(), this method adds <no_replace>
  324. // and <filter> arguments. When true, <no_replace> prohibits the replacement of fields that already
  325. // have a value. When <filter> is specified, only fields with leading characters that exactly match
  326. // the characters in <filter> are copied.
  327. void SimFieldDictionary::assignFrom(SimFieldDictionary *dict, const char* filter, bool no_replace)
  328. {
  329. dsize_t filter_len = (filter) ? dStrlen(filter) : 0;
  330. if (filter_len == 0 && !no_replace)
  331. {
  332. assignFrom(dict);
  333. return;
  334. }
  335. mVersion++;
  336. if (filter_len == 0)
  337. {
  338. for (U32 i = 0; i < HashTableSize; i++)
  339. for (Entry *walk = dict->mHashTable[i]; walk; walk = walk->next)
  340. setFieldValue(walk->slotName, walk->value, walk->type, no_replace);
  341. }
  342. else
  343. {
  344. for (U32 i = 0; i < HashTableSize; i++)
  345. for (Entry *walk = dict->mHashTable[i]; walk; walk = walk->next)
  346. if (dStrncmp(walk->slotName, filter, filter_len) == 0)
  347. setFieldValue(walk->slotName, walk->value, walk->type, no_replace);
  348. }
  349. }