00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "DataStack.h"
00021 #include "GameManager.h"
00022
00023
00024
00025
00026 u32 DataStack::mIDCount = 0;
00027
00028
00029 DataStack::DataStack(const std::string &name)
00030 : mRefCount(1), mName(name)
00031 {
00032 mID = mIDCount++;
00033 }
00034
00035
00036 DataStack::~DataStack()
00037 {
00038 clear();
00039 }
00040
00041
00042 void DataStack::init()
00043 {
00044 }
00045
00046
00047 void DataStack::clear()
00048 {
00049 removeAll();
00050 }
00051
00052
00053 DataStack* DataStack::refFactory(const std::string &name)
00054 {
00055 return new DataStack(name);
00056 }
00057
00058
00059 void DataStack::refAdd()
00060 {
00061 mRefCount++;
00062 }
00063
00064
00065 void DataStack::refRelease()
00066 {
00067 if(--mRefCount == 0)
00068 {
00069 if(!GameManager::Instance()->getDataStore()->removeDataStack(this))
00070 delete this;
00071 }
00072 }
00073
00074
00075 u32 DataStack::getID() const
00076 {
00077 return mID;
00078 }
00079
00080
00081 const std::string& DataStack::getName() const
00082 {
00083 return mName;
00084 }
00085
00086
00087 u32 DataStack::getSize() const
00088 {
00089 return mVars.size();
00090 }
00091
00092
00093 std::string DataStack::getVar(const std::string &name)
00094 {
00095
00096 std::map<std::string, std::string>::iterator it;
00097 it = mVars.find(name);
00098
00099
00100 if(it != mVars.end())
00101 return it->second;
00102
00103
00104 else return NULL;
00105 }
00106
00107
00108 void DataStack::removeAll()
00109 {
00110 mVars.clear();
00111 }
00112
00113
00114 bool DataStack::removeVar(const std::string &name)
00115 {
00116 std::map<std::string, std::string>::iterator it;
00117 it = mVars.find(name);
00118
00119 if(it != mVars.end())
00120 {
00121 mVars.erase(it);
00122 return true;
00123 }
00124
00125 else return false;
00126 }
00127
00128
00129 bool DataStack::saveBencode(const std::string &fileName)
00130 {
00131
00132 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00133
00134
00135 IWriteFile *file = fileSystem->createAndWriteFile(fileName.c_str());
00136
00137
00138 std::stringstream fBuffer;
00139
00140
00141 fBuffer << "d3:fidi2419e5:ftype3:bdse";
00142
00143
00144 fBuffer << "d";
00145
00146 std::map<std::string, std::string>::iterator it;
00147 for(it = mVars.begin(); it != mVars.end(); it++)
00148 {
00149 fBuffer << it->first.size() << ":" << it->first;
00150 fBuffer << it->second.size() << ":" << it->second;
00151 }
00152
00153 fBuffer << "e";
00154
00155
00156 file->write(fBuffer.str().c_str(), fBuffer.str().size());
00157
00158
00159 file->drop();
00160
00161 return true;
00162 }
00163
00164
00165 bool DataStack::loadBencode(const std::string &fileName)
00166 {
00167
00168 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00169
00170
00171 if(!fileSystem->existFile(fileName.c_str()))
00172 return false;
00173
00174
00175 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00176
00177 long fSize = file->getSize();
00178 c8 *fBuffer = new c8[fSize+1];
00179 file->read(fBuffer, fSize);
00180 file->drop();
00181
00182
00183 std::string fHeader = std::string(fBuffer).substr(0, 25);
00184
00185 if(fHeader != "d3:fidi2419e5:ftype3:bdse")
00186 return false;
00187
00188
00189 removeAll();
00190
00191
00192 std::stringstream bCountStream;
00193 unsigned long bCount = 0;
00194
00195 int state = 0;
00196 std::string key;
00197 std::string value;
00198
00199 for(unsigned int i = 25; i < fSize; i++)
00200 {
00201 if(bCount != 0)
00202 {
00203 if(state == 0)
00204 key += fBuffer[i];
00205
00206 else if(state == 1)
00207 value += fBuffer[i];
00208
00209 bCount--;
00210 }
00211
00212 else
00213 {
00214 if(!key.empty())
00215 state = 1;
00216
00217 if(!value.empty())
00218 {
00219 mVars[key] = value;
00220 key.clear();
00221 value.clear();
00222 state = 0;
00223 }
00224
00225 if(fBuffer[i] >= '0' && fBuffer[i] <= '9')
00226 bCountStream << fBuffer[i];
00227 }
00228
00229 if(fBuffer[i] == ':')
00230 {
00231 bCountStream >> bCount;
00232 bCountStream.clear();
00233 }
00234 }
00235
00236
00237 delete[] fBuffer;
00238
00239
00240 return true;
00241 }
00242
00243
00244 bool DataStack::saveXML(const std::string &fileName)
00245 {
00246
00247 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00248
00249
00250 IWriteFile *file = fileSystem->createAndWriteFile(fileName.c_str());
00251
00252
00253 std::stringstream fBuffer;
00254
00255
00256 fBuffer << "<?xml version=\"1.0\"?>\n";
00257
00258
00259 fBuffer << "<datastack>\n";
00260
00261 std::map<std::string, std::string>::iterator it;
00262 for(it = mVars.begin(); it != mVars.end(); it++)
00263 fBuffer << " <var name=\"" << it->first << "\" value=\"" << it->second << "\" />\n";
00264
00265 fBuffer << "</datastack>";
00266
00267
00268 file->write(fBuffer.str().c_str(), fBuffer.str().size());
00269
00270
00271 file->drop();
00272
00273 return true;
00274 }
00275
00276
00277 bool DataStack::loadXML(const std::string &fileName)
00278 {
00279
00280 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00281
00282
00283 if(!fileSystem->existFile(fileName.c_str()))
00284 return false;
00285
00286
00287 IXMLReader *file = fileSystem->createXMLReader(fileName.c_str());
00288
00289 if(!file)
00290 return false;
00291
00292
00293 bool isDataStack = false;
00294
00295 while(file && file->read())
00296 {
00297 switch(file->getNodeType())
00298 {
00299 case io::EXN_ELEMENT:
00300
00301
00302 if(core::stringw("datastack") == file->getNodeName())
00303 isDataStack = true;
00304
00305
00306 if(isDataStack)
00307 {
00308 if(core::stringw("var") == file->getNodeName())
00309 {
00310 core::stringc name = file->getAttributeValue(L"name");
00311 core::stringc value = file->getAttributeValue(L"value");
00312 mVars[name.c_str()] = value.c_str();
00313 }
00314 }
00315
00316 break;
00317
00318 default:
00319 break;
00320 }
00321 }
00322
00323 if(!isDataStack)
00324 return false;
00325
00326
00327 file->drop();
00328
00329 return true;
00330 }
00331
00332
00333
00334 template<typename T>
00335 void bindSetGetVar(asIScriptEngine *engine, const std::string &asType)
00336 {
00337
00338 int r;
00339 std::stringstream ss;
00340
00341
00342 ss.str("");
00343 ss << "bool set_" << asType << "(const string&, const " << asType << ")";
00344 r = engine->RegisterObjectMethod("DataStack", ss.str().c_str(),
00345 asMETHODPR(DataStack, setVar<T>, (const std::string&, const T&), bool),
00346 asCALL_THISCALL); assert(r >= 0);
00347
00348
00349 ss.str("");
00350 ss << asType << " get_" << asType << "(const string&)";
00351 r = engine->RegisterObjectMethod("DataStack", ss.str().c_str(),
00352 asMETHODPR(DataStack, getVar<T>, (const std::string&), T),
00353 asCALL_THISCALL); assert(r >= 0);
00354 }
00355
00356 void bindDataStack(asIScriptEngine *engine)
00357 {
00358
00359 int r;
00360
00361
00362 r = engine->RegisterObjectType("DataStack", sizeof(DataStack), asOBJ_REF); assert(r >= 0);
00363
00364
00365 r = engine->RegisterObjectBehaviour("DataStack", asBEHAVE_FACTORY, "DataStack@ f(const string &)",
00366 asFUNCTION(DataStack::refFactory), asCALL_CDECL); assert(r >= 0);
00367 r = engine->RegisterObjectBehaviour("DataStack", asBEHAVE_ADDREF, "void f()",
00368 asMETHOD(DataStack, refAdd), asCALL_THISCALL); assert(r >= 0);
00369 r = engine->RegisterObjectBehaviour("DataStack", asBEHAVE_RELEASE, "void f()",
00370 asMETHOD(DataStack, refRelease), asCALL_THISCALL); assert(r >= 0);
00371
00372
00373 r = engine->RegisterObjectMethod("DataStack", "void init()",
00374 asMETHOD(DataStack, init), asCALL_THISCALL); assert(r >= 0);
00375 r = engine->RegisterObjectMethod("DataStack", "void clear()",
00376 asMETHOD(DataStack, clear), asCALL_THISCALL); assert(r >= 0);
00377
00378 r = engine->RegisterObjectMethod("DataStack", "u32 getID()",
00379 asMETHOD(DataStack, getID), asCALL_THISCALL); assert(r >= 0);
00380 r = engine->RegisterObjectMethod("DataStack", "const string& getName()",
00381 asMETHOD(DataStack, getName), asCALL_THISCALL); assert(r >= 0);
00382
00383
00384 bindSetGetVar<s32>(engine, "s32");
00385 bindSetGetVar<c8>(engine, "c8");
00386 bindSetGetVar<f32>(engine, "f32");
00387 bindSetGetVar<f64>(engine, "f64");
00388 bindSetGetVar<s16>(engine, "s16");
00389 bindSetGetVar<s32>(engine, "s32");
00390 bindSetGetVar<u16>(engine, "u16");
00391 bindSetGetVar<u32>(engine, "u32");
00392 bindSetGetVar<u8>(engine, "u8");
00393 bindSetGetVar<std::string>(engine, "string");
00394
00395 r = engine->RegisterObjectMethod("DataStack", "void removeAll()",
00396 asMETHOD(DataStack, removeAll), asCALL_THISCALL); assert(r >= 0);
00397 r = engine->RegisterObjectMethod("DataStack", "bool removeVar(const string &)",
00398 asMETHOD(DataStack, removeVar), asCALL_THISCALL); assert(r >= 0);
00399
00400 r = engine->RegisterObjectMethod("DataStack", "bool saveBencode(const string &)",
00401 asMETHOD(DataStack, saveBencode), asCALL_THISCALL);assert(r >= 0);
00402 r = engine->RegisterObjectMethod("DataStack", "bool loadBencode(const string &)",
00403 asMETHOD(DataStack, loadBencode), asCALL_THISCALL);assert(r >= 0);
00404
00405 r = engine->RegisterObjectMethod("DataStack", "bool saveXML(const string &)",
00406 asMETHOD(DataStack, saveXML), asCALL_THISCALL);assert(r >= 0);
00407 r = engine->RegisterObjectMethod("DataStack", "bool loadXML(const string &)",
00408 asMETHOD(DataStack, loadXML), asCALL_THISCALL);assert(r >= 0);
00409 }
00410
00411