2
0

simSerialize.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/console.h"
  23. #include "sim/simBase.h"
  24. #include "io/bitStream.h"
  25. #include "io/fileStream.h"
  26. #include "io/resource/resourceManager.h"
  27. //////////////////////////////////////////////////////////////////////////
  28. // SimObject Methods
  29. //////////////////////////////////////////////////////////////////////////
  30. bool SimObject::writeObject(Stream *stream)
  31. {
  32. clearFieldFilters();
  33. buildFilterList();
  34. stream->writeString(getName() ? getName() : "");
  35. // Static fields
  36. AbstractClassRep *rep = getClassRep();
  37. AbstractClassRep::FieldList &fieldList = rep->mFieldList;
  38. AbstractClassRep::FieldList::iterator itr;
  39. U32 savePos = stream->getPosition();
  40. U32 numFields = fieldList.size();
  41. stream->write(numFields);
  42. for(itr = fieldList.begin();itr != fieldList.end();itr++)
  43. {
  44. if(itr->type >= AbstractClassRep::StartGroupFieldType || isFiltered(itr->pFieldname))
  45. {
  46. numFields--;
  47. continue;
  48. }
  49. const char *field = getDataField(itr->pFieldname, NULL);
  50. if(field == NULL)
  51. field = "";
  52. stream->writeString(itr->pFieldname);
  53. stream->writeString(field);
  54. }
  55. // Dynamic Fields
  56. if(mCanSaveFieldDictionary)
  57. {
  58. SimFieldDictionary * fieldDictionary = getFieldDictionary();
  59. for(SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr)
  60. {
  61. SimFieldDictionary::Entry * entry = (*ditr);
  62. if(isFiltered(entry->slotName))
  63. continue;
  64. stream->writeString(entry->slotName);
  65. stream->writeString(entry->value);
  66. numFields++;
  67. }
  68. }
  69. // Overwrite the number of fields with the correct value
  70. U32 savePos2 = stream->getPosition();
  71. stream->setPosition(savePos);
  72. stream->write(numFields);
  73. stream->setPosition(savePos2);
  74. return true;
  75. }
  76. bool SimObject::readObject(Stream *stream)
  77. {
  78. const char *name = stream->readSTString(true);
  79. if(name && *name)
  80. assignName(name);
  81. U32 numFields;
  82. stream->read(&numFields);
  83. for(U32 i = 0;i < numFields;i++)
  84. {
  85. const char *fieldName = stream->readSTString();
  86. const char *data = stream->readSTString();
  87. setDataField(fieldName, NULL, data);
  88. }
  89. return true;
  90. }
  91. //////////////////////////////////////////////////////////////////////////
  92. void SimObject::buildFilterList()
  93. {
  94. Con::executef(this, 1, "buildFilterList");
  95. }
  96. //////////////////////////////////////////////////////////////////////////
  97. void SimObject::addFieldFilter( const char *fieldName )
  98. {
  99. StringTableEntry st = StringTable->insert(fieldName);
  100. for(S32 i = 0;i < mFieldFilter.size();i++)
  101. {
  102. if(mFieldFilter[i] == st)
  103. return;
  104. }
  105. mFieldFilter.push_back(st);
  106. }
  107. void SimObject::removeFieldFilter( const char *fieldName )
  108. {
  109. StringTableEntry st = StringTable->insert(fieldName);
  110. for(S32 i = 0;i < mFieldFilter.size();i++)
  111. {
  112. if(mFieldFilter[i] == st)
  113. {
  114. mFieldFilter.erase(i);
  115. return;
  116. }
  117. }
  118. }
  119. void SimObject::clearFieldFilters()
  120. {
  121. mFieldFilter.clear();
  122. }
  123. //////////////////////////////////////////////////////////////////////////
  124. bool SimObject::isFiltered( const char *fieldName )
  125. {
  126. StringTableEntry st = StringTable->insert(fieldName);
  127. for(S32 i = 0;i < mFieldFilter.size();i++)
  128. {
  129. if(mFieldFilter[i] == st)
  130. return true;
  131. }
  132. return false;
  133. }
  134. //////////////////////////////////////////////////////////////////////////
  135. // SimSet Methods
  136. //////////////////////////////////////////////////////////////////////////
  137. bool SimSet::writeObject( Stream *stream )
  138. {
  139. if(! Parent::writeObject(stream))
  140. return false;
  141. stream->write(size());
  142. for(SimSet::iterator i = begin();i < end();++i)
  143. {
  144. if(! Sim::saveObject((*i), stream))
  145. return false;
  146. }
  147. return true;
  148. }
  149. bool SimSet::readObject( Stream *stream )
  150. {
  151. if(! Parent::readObject(stream))
  152. return false;
  153. U32 numObj;
  154. stream->read(&numObj);
  155. for(U32 i = 0;i < numObj;i++)
  156. {
  157. SimObject *obj = Sim::loadObjectStream(stream);
  158. if(obj == NULL)
  159. return false;
  160. addObject(obj);
  161. }
  162. return true;
  163. }
  164. //////////////////////////////////////////////////////////////////////////
  165. // Sim Functions
  166. //////////////////////////////////////////////////////////////////////////
  167. namespace Sim
  168. {
  169. bool saveObject(SimObject *obj, const char *filename)
  170. {
  171. FileStream fs;
  172. if(ResourceManager->openFileForWrite(fs, filename, FileStream::Write))
  173. {
  174. bool ret = saveObject(obj, &fs);
  175. fs.close();
  176. return ret;
  177. }
  178. return false;
  179. }
  180. bool saveObject(SimObject *obj, Stream *stream)
  181. {
  182. stream->writeString(obj->getClassName());
  183. return obj->writeObject(stream);
  184. }
  185. //////////////////////////////////////////////////////////////////////////
  186. SimObject *loadObjectStream(const char *filename)
  187. {
  188. Stream * stream = ResourceManager->openStream(filename);
  189. if (stream)
  190. {
  191. SimObject *ret = loadObjectStream(stream);
  192. ResourceManager->closeStream(stream);
  193. return ret;
  194. }
  195. return NULL;
  196. }
  197. SimObject *loadObjectStream(Stream *stream)
  198. {
  199. const char *className = stream->readSTString(true);
  200. ConsoleObject *conObj = ConsoleObject::create(className);
  201. if(conObj == NULL)
  202. {
  203. Con::errorf("Sim::restoreObjectStream - Could not create object of class \"%s\"", className);
  204. return NULL;
  205. }
  206. SimObject *simObj = dynamic_cast<SimObject *>(conObj);
  207. if(simObj == NULL)
  208. {
  209. Con::errorf("Sim::restoreObjectStream - Object of class \"%s\" is not a SimObject", className);
  210. delete simObj;
  211. return NULL;
  212. }
  213. if(simObj->readObject(stream))
  214. {
  215. simObj->registerObject();
  216. return simObj;
  217. }
  218. delete simObj;
  219. return NULL;
  220. }
  221. } // end namespace Sim
  222. //////////////////////////////////////////////////////////////////////////
  223. // Console Methods
  224. //////////////////////////////////////////////////////////////////////////
  225. ConsoleMethod(SimObject, addFieldFilter, void, 3, 3, "(fieldName) Adds the given field filter to the list of table entries.\n"
  226. "@param fieldName The name of the field filter to add.\n"
  227. "@return No return value")
  228. {
  229. object->addFieldFilter(argv[2]);
  230. }
  231. ConsoleMethod(SimObject, removeFieldFilter, void, 3, 3, "(fieldName) Removes the specified field filter from the table\n"
  232. "@param fieldName The name of the field filter to remove.\n"
  233. "@return No return value.")
  234. {
  235. object->removeFieldFilter(argv[2]);
  236. }
  237. //////////////////////////////////////////////////////////////////////////
  238. // Console Functions
  239. //////////////////////////////////////////////////////////////////////////
  240. ConsoleFunction(saveObject, bool, 3, 3, "(object, filename) Saves the given object to the given filename\n"
  241. "@param object The SimObject to save\n"
  242. "@param filename The name of the file in which to save the object\n"
  243. "@return Returns true on success and flase on failure")
  244. {
  245. SimObject *obj = dynamic_cast<SimObject *>(Sim::findObject(argv[1]));
  246. if(obj == NULL)
  247. return false;
  248. return Sim::saveObject(obj, argv[2]);
  249. }
  250. ConsoleFunction(loadObject, S32, 2, 2, "(filename) Loads an object from the specified filename\n"
  251. "@param filename The name of the file to load the object from\n"
  252. "@return Returns the ID of the object on success, or 0 on failure.")
  253. {
  254. SimObject *obj = Sim::loadObjectStream(argv[1]);
  255. return obj ? obj->getId() : 0;
  256. }