simSerialize.cpp
Engine/source/console/simSerialize.cpp
Namespaces:
namespace
Public Functions
DefineEngineFunction(loadObject , SimObject * , (const char *filename) , "@brief Loads <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> serialized object from <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n\n</a>" "@param Name and path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> text <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> containing the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Console\n</a>" )
DefineEngineFunction(saveObject , bool , (SimObject *object, const char *filename) , "@brief Serialize the object <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n\n</a>" "@param object The object <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">serialize.\n</a>" "@param filename The <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> name and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">path.\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Console\n</a>" )
Detailed Description
Public Functions
DefineEngineFunction(loadObject , SimObject * , (const char *filename) , "@brief Loads <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> serialized object from <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n\n</a>" "@param Name and path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> text <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> containing the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Console\n</a>" )
DefineEngineFunction(saveObject , bool , (SimObject *object, const char *filename) , "@brief Serialize the object <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n\n</a>" "@param object The object <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">serialize.\n</a>" "@param filename The <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> name and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">path.\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Console\n</a>" )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25 26#include "console/console.h" 27#include "console/simBase.h" 28#include "core/stream/bitStream.h" 29#include "core/stream/fileStream.h" 30#include "console/engineAPI.h" 31 32 33//----------------------------------------------------------------------------- 34// SimObject Methods 35//----------------------------------------------------------------------------- 36 37bool SimObject::writeObject(Stream *stream) 38{ 39 stream->writeString(getName() ? getName() : ""); 40 41 // Static fields 42 AbstractClassRep *rep = getClassRep(); 43 AbstractClassRep::FieldList &fieldList = rep->mFieldList; 44 AbstractClassRep::FieldList::iterator itr; 45 46 U32 savePos = stream->getPosition(); 47 U32 numFields = fieldList.size(); 48 stream->write(numFields); 49 50 for(itr = fieldList.begin();itr != fieldList.end();itr++) 51 { 52 if( itr->type >= AbstractClassRep::ARCFirstCustomField ) 53 { 54 numFields--; 55 continue; 56 } 57 58 const char *field = getDataField(itr->pFieldname, NULL); 59 if(field == NULL) 60 field = ""; 61 62 stream->writeString(itr->pFieldname); 63 stream->writeString(field); 64 } 65 66 // Dynamic Fields 67 if(mCanSaveFieldDictionary) 68 { 69 SimFieldDictionary * fieldDictionary = getFieldDictionary(); 70 for(SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr) 71 { 72 SimFieldDictionary::Entry * entry = (*ditr); 73 74 stream->writeString(entry->slotName); 75 stream->writeString(entry->value); 76 numFields++; 77 } 78 } 79 80 // Overwrite the number of fields with the correct value 81 U32 savePos2 = stream->getPosition(); 82 stream->setPosition(savePos); 83 stream->write(numFields); 84 stream->setPosition(savePos2); 85 86 return true; 87} 88 89bool SimObject::readObject(Stream *stream) 90{ 91 const char *name = stream->readSTString(true); 92 if(name && *name) 93 assignName(name); 94 95 U32 numFields; 96 stream->read(&numFields); 97 98 for(S32 i = 0;i < numFields;i++) 99 { 100 const char *fieldName = stream->readSTString(); 101 const char *data = stream->readSTString(); 102 103 setDataField(fieldName, NULL, data); 104 } 105 return true; 106} 107 108//----------------------------------------------------------------------------- 109// SimSet Methods 110//----------------------------------------------------------------------------- 111 112bool SimSet::writeObject( Stream *stream ) 113{ 114 if(! Parent::writeObject(stream)) 115 return false; 116 117 stream->write(size()); 118 for(SimSet::iterator i = begin();i < end();++i) 119 { 120 if(! Sim::saveObject((*i), stream)) 121 return false; 122 } 123 return true; 124} 125 126bool SimSet::readObject( Stream *stream ) 127{ 128 if(! Parent::readObject(stream)) 129 return false; 130 131 U32 numObj; 132 stream->read(&numObj); 133 134 for(U32 i = 0;i < numObj;i++) 135 { 136 SimObject *obj = Sim::loadObjectStream(stream); 137 if(obj == NULL) 138 return false; 139 140 addObject(obj); 141 } 142 143 return true; 144} 145 146//----------------------------------------------------------------------------- 147// Sim Functions 148//----------------------------------------------------------------------------- 149 150namespace Sim 151{ 152 153bool saveObject(SimObject *obj, const char *filename) 154{ 155 FileStream *stream; 156 if((stream = FileStream::createAndOpen( filename, Torque::FS::File::Write )) == NULL) 157 return false; 158 159 bool ret = saveObject(obj, stream); 160 delete stream; 161 162 return ret; 163} 164 165bool saveObject(SimObject *obj, Stream *stream) 166{ 167 stream->writeString(obj->getClassName()); 168 return obj->writeObject(stream); 169} 170 171//----------------------------------------------------------------------------- 172 173SimObject *loadObjectStream(const char *filename) 174{ 175 FileStream * stream; 176 if((stream = FileStream::createAndOpen( filename, Torque::FS::File::Read )) == NULL) 177 return NULL; 178 179 SimObject * ret = loadObjectStream(stream); 180 delete stream; 181 return ret; 182} 183 184SimObject *loadObjectStream(Stream *stream) 185{ 186 const char *className = stream->readSTString(true); 187 ConsoleObject *conObj = ConsoleObject::create(className); 188 if(conObj == NULL) 189 { 190 Con::errorf("Sim::restoreObjectStream - Could not create object of class \"%s\"", className); 191 return NULL; 192 } 193 194 SimObject *simObj = dynamic_cast<SimObject *>(conObj); 195 if(simObj == NULL) 196 { 197 Con::errorf("Sim::restoreObjectStream - Object of class \"%s\" is not a SimObject", className); 198 delete simObj; 199 return NULL; 200 } 201 202 if( simObj->readObject(stream) 203 && simObj->registerObject() ) 204 return simObj; 205 206 delete simObj; 207 return NULL; 208} 209 210} // end namespace Sim 211 212//----------------------------------------------------------------------------- 213// Console Functions 214//----------------------------------------------------------------------------- 215 216DefineEngineFunction(saveObject, bool, ( SimObject *object, const char *filename ),, 217 "@brief Serialize the object to a file.\n\n" 218 "@param object The object to serialize.\n" 219 "@param filename The file name and path.\n" 220 "@ingroup Console\n") 221{ 222 return object && Sim::saveObject(object, filename); 223} 224 225DefineEngineFunction(loadObject, SimObject*, ( const char *filename ),, 226 "@brief Loads a serialized object from a file.\n\n" 227 "@param Name and path to text file containing the object\n" 228 "@ingroup Console\n") 229{ 230 return Sim::loadObjectStream(filename); 231} 232