simSerialize.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #include "simSerialize_ScriptBinding.h"
  28. //////////////////////////////////////////////////////////////////////////
  29. // SimObject Methods
  30. //////////////////////////////////////////////////////////////////////////
  31. bool SimObject::writeObject(Stream *stream)
  32. {
  33. clearFieldFilters();
  34. buildFilterList();
  35. stream->writeString(getName() ? getName() : "");
  36. // Static fields
  37. AbstractClassRep *rep = getClassRep();
  38. AbstractClassRep::FieldList &fieldList = rep->mFieldList;
  39. AbstractClassRep::FieldList::iterator itr;
  40. U32 savePos = stream->getPosition();
  41. U32 numFields = fieldList.size();
  42. stream->write(numFields);
  43. for(itr = fieldList.begin();itr != fieldList.end();itr++)
  44. {
  45. if(itr->type >= AbstractClassRep::StartGroupFieldType || isFiltered(itr->pFieldname))
  46. {
  47. numFields--;
  48. continue;
  49. }
  50. const char *field = getDataField(itr->pFieldname, NULL);
  51. if(field == NULL)
  52. field = "";
  53. stream->writeString(itr->pFieldname);
  54. stream->writeString(field);
  55. }
  56. // Dynamic Fields
  57. if(mCanSaveFieldDictionary)
  58. {
  59. SimFieldDictionary * fieldDictionary = getFieldDictionary();
  60. for(SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr)
  61. {
  62. SimFieldDictionary::Entry * entry = (*ditr);
  63. if(isFiltered(entry->slotName))
  64. continue;
  65. stream->writeString(entry->slotName);
  66. stream->writeString(entry->value);
  67. numFields++;
  68. }
  69. }
  70. // Overwrite the number of fields with the correct value
  71. U32 savePos2 = stream->getPosition();
  72. stream->setPosition(savePos);
  73. stream->write(numFields);
  74. stream->setPosition(savePos2);
  75. return true;
  76. }
  77. bool SimObject::readObject(Stream *stream)
  78. {
  79. const char *name = stream->readSTString(true);
  80. if(name && *name)
  81. assignName(name);
  82. U32 numFields;
  83. stream->read(&numFields);
  84. for(U32 i = 0;i < numFields;i++)
  85. {
  86. const char *fieldName = stream->readSTString();
  87. const char *data = stream->readSTString();
  88. setDataField(fieldName, NULL, data);
  89. }
  90. return true;
  91. }
  92. //////////////////////////////////////////////////////////////////////////
  93. void SimObject::buildFilterList()
  94. {
  95. Con::executef(this, 1, "buildFilterList");
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. void SimObject::addFieldFilter( const char *fieldName )
  99. {
  100. StringTableEntry st = StringTable->insert(fieldName);
  101. for(S32 i = 0;i < mFieldFilter.size();i++)
  102. {
  103. if(mFieldFilter[i] == st)
  104. return;
  105. }
  106. mFieldFilter.push_back(st);
  107. }
  108. void SimObject::removeFieldFilter( const char *fieldName )
  109. {
  110. StringTableEntry st = StringTable->insert(fieldName);
  111. for(S32 i = 0;i < mFieldFilter.size();i++)
  112. {
  113. if(mFieldFilter[i] == st)
  114. {
  115. mFieldFilter.erase(i);
  116. return;
  117. }
  118. }
  119. }
  120. void SimObject::clearFieldFilters()
  121. {
  122. mFieldFilter.clear();
  123. }
  124. //////////////////////////////////////////////////////////////////////////
  125. bool SimObject::isFiltered( const char *fieldName )
  126. {
  127. StringTableEntry st = StringTable->insert(fieldName);
  128. for(S32 i = 0;i < mFieldFilter.size();i++)
  129. {
  130. if(mFieldFilter[i] == st)
  131. return true;
  132. }
  133. return false;
  134. }
  135. //////////////////////////////////////////////////////////////////////////
  136. // SimSet Methods
  137. //////////////////////////////////////////////////////////////////////////
  138. bool SimSet::writeObject( Stream *stream )
  139. {
  140. if(! Parent::writeObject(stream))
  141. return false;
  142. stream->write(size());
  143. for(SimSet::iterator i = begin();i < end();++i)
  144. {
  145. if(! Sim::saveObject((*i), stream))
  146. return false;
  147. }
  148. return true;
  149. }
  150. bool SimSet::readObject( Stream *stream )
  151. {
  152. if(! Parent::readObject(stream))
  153. return false;
  154. U32 numObj;
  155. stream->read(&numObj);
  156. for(U32 i = 0;i < numObj;i++)
  157. {
  158. SimObject *obj = Sim::loadObjectStream(stream);
  159. if(obj == NULL)
  160. return false;
  161. addObject(obj);
  162. }
  163. return true;
  164. }
  165. //////////////////////////////////////////////////////////////////////////
  166. // Sim Functions
  167. //////////////////////////////////////////////////////////////////////////
  168. namespace Sim
  169. {
  170. bool saveObject(SimObject *obj, const char *filename)
  171. {
  172. FileStream fs;
  173. if(ResourceManager->openFileForWrite(fs, filename, FileStream::Write))
  174. {
  175. bool ret = saveObject(obj, &fs);
  176. fs.close();
  177. return ret;
  178. }
  179. return false;
  180. }
  181. bool saveObject(SimObject *obj, Stream *stream)
  182. {
  183. stream->writeString(obj->getClassName());
  184. return obj->writeObject(stream);
  185. }
  186. //////////////////////////////////////////////////////////////////////////
  187. SimObject *loadObjectStream(const char *filename)
  188. {
  189. Stream * stream = ResourceManager->openStream(filename);
  190. if (stream)
  191. {
  192. SimObject *ret = loadObjectStream(stream);
  193. ResourceManager->closeStream(stream);
  194. return ret;
  195. }
  196. return NULL;
  197. }
  198. SimObject *loadObjectStream(Stream *stream)
  199. {
  200. const char *className = stream->readSTString(true);
  201. ConsoleObject *conObj = ConsoleObject::create(className);
  202. if(conObj == NULL)
  203. {
  204. Con::errorf("Sim::restoreObjectStream - Could not create object of class \"%s\"", className);
  205. return NULL;
  206. }
  207. SimObject *simObj = dynamic_cast<SimObject *>(conObj);
  208. if(simObj == NULL)
  209. {
  210. Con::errorf("Sim::restoreObjectStream - Object of class \"%s\" is not a SimObject", className);
  211. delete simObj;
  212. return NULL;
  213. }
  214. if(simObj->readObject(stream))
  215. {
  216. simObj->registerObject();
  217. return simObj;
  218. }
  219. delete simObj;
  220. return NULL;
  221. }
  222. } // end namespace Sim