simSerialize.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "console/simBase.h"
  25. #include "core/stream/bitStream.h"
  26. #include "core/stream/fileStream.h"
  27. #include "console/engineAPI.h"
  28. //-----------------------------------------------------------------------------
  29. // SimObject Methods
  30. //-----------------------------------------------------------------------------
  31. bool SimObject::writeObject(Stream *stream)
  32. {
  33. stream->writeString(getName() ? getName() : "");
  34. // Static fields
  35. AbstractClassRep *rep = getClassRep();
  36. AbstractClassRep::FieldList &fieldList = rep->mFieldList;
  37. AbstractClassRep::FieldList::iterator itr;
  38. U32 savePos = stream->getPosition();
  39. U32 numFields = fieldList.size();
  40. stream->write(numFields);
  41. for(itr = fieldList.begin();itr != fieldList.end();itr++)
  42. {
  43. if( itr->type >= AbstractClassRep::ARCFirstCustomField )
  44. {
  45. numFields--;
  46. continue;
  47. }
  48. const char *field = getDataField(itr->pFieldname, NULL);
  49. if(field == NULL)
  50. field = "";
  51. stream->writeString(itr->pFieldname);
  52. stream->writeString(field);
  53. }
  54. // Dynamic Fields
  55. if(mCanSaveFieldDictionary)
  56. {
  57. SimFieldDictionary * fieldDictionary = getFieldDictionary();
  58. for(SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr)
  59. {
  60. SimFieldDictionary::Entry * entry = (*ditr);
  61. stream->writeString(entry->slotName);
  62. stream->writeString(entry->value);
  63. numFields++;
  64. }
  65. }
  66. // Overwrite the number of fields with the correct value
  67. U32 savePos2 = stream->getPosition();
  68. stream->setPosition(savePos);
  69. stream->write(numFields);
  70. stream->setPosition(savePos2);
  71. return true;
  72. }
  73. bool SimObject::readObject(Stream *stream)
  74. {
  75. const char *name = stream->readSTString(true);
  76. if(name && *name)
  77. assignName(name);
  78. U32 numFields;
  79. stream->read(&numFields);
  80. for(S32 i = 0;i < numFields;i++)
  81. {
  82. const char *fieldName = stream->readSTString();
  83. const char *data = stream->readSTString();
  84. setDataField(fieldName, NULL, data);
  85. }
  86. return true;
  87. }
  88. //-----------------------------------------------------------------------------
  89. // SimSet Methods
  90. //-----------------------------------------------------------------------------
  91. bool SimSet::writeObject( Stream *stream )
  92. {
  93. if(! Parent::writeObject(stream))
  94. return false;
  95. stream->write(size());
  96. for(SimSet::iterator i = begin();i < end();++i)
  97. {
  98. if(! Sim::saveObject((*i), stream))
  99. return false;
  100. }
  101. return true;
  102. }
  103. bool SimSet::readObject( Stream *stream )
  104. {
  105. if(! Parent::readObject(stream))
  106. return false;
  107. U32 numObj;
  108. stream->read(&numObj);
  109. for(U32 i = 0;i < numObj;i++)
  110. {
  111. SimObject *obj = Sim::loadObjectStream(stream);
  112. if(obj == NULL)
  113. return false;
  114. addObject(obj);
  115. }
  116. return true;
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Sim Functions
  120. //-----------------------------------------------------------------------------
  121. namespace Sim
  122. {
  123. bool saveObject(SimObject *obj, const char *filename)
  124. {
  125. FileStream *stream;
  126. if((stream = FileStream::createAndOpen( filename, Torque::FS::File::Write )) == NULL)
  127. return false;
  128. bool ret = saveObject(obj, stream);
  129. delete stream;
  130. return ret;
  131. }
  132. bool saveObject(SimObject *obj, Stream *stream)
  133. {
  134. stream->writeString(obj->getClassName());
  135. return obj->writeObject(stream);
  136. }
  137. //-----------------------------------------------------------------------------
  138. SimObject *loadObjectStream(const char *filename)
  139. {
  140. FileStream * stream;
  141. if((stream = FileStream::createAndOpen( filename, Torque::FS::File::Read )) == NULL)
  142. return NULL;
  143. SimObject * ret = loadObjectStream(stream);
  144. delete stream;
  145. return ret;
  146. }
  147. SimObject *loadObjectStream(Stream *stream)
  148. {
  149. const char *className = stream->readSTString(true);
  150. ConsoleObject *conObj = ConsoleObject::create(className);
  151. if(conObj == NULL)
  152. {
  153. Con::errorf("Sim::restoreObjectStream - Could not create object of class \"%s\"", className);
  154. return NULL;
  155. }
  156. SimObject *simObj = dynamic_cast<SimObject *>(conObj);
  157. if(simObj == NULL)
  158. {
  159. Con::errorf("Sim::restoreObjectStream - Object of class \"%s\" is not a SimObject", className);
  160. delete simObj;
  161. return NULL;
  162. }
  163. if( simObj->readObject(stream)
  164. && simObj->registerObject() )
  165. return simObj;
  166. delete simObj;
  167. return NULL;
  168. }
  169. } // end namespace Sim
  170. //-----------------------------------------------------------------------------
  171. // Console Functions
  172. //-----------------------------------------------------------------------------
  173. DefineEngineFunction(saveObject, bool, ( SimObject *object, const char *filename ),,
  174. "@brief Serialize the object to a file.\n\n"
  175. "@param object The object to serialize.\n"
  176. "@param filename The file name and path.\n"
  177. "@ingroup Console\n")
  178. {
  179. return object && Sim::saveObject(object, filename);
  180. }
  181. DefineEngineFunction(loadObject, SimObject*, ( const char *filename ),,
  182. "@brief Loads a serialized object from a file.\n\n"
  183. "@param Name and path to text file containing the object\n"
  184. "@ingroup Console\n")
  185. {
  186. return Sim::loadObjectStream(filename);
  187. }