From 853d531d112a1d329c0ccc4c9549bae60be086ef Mon Sep 17 00:00:00 2001 From: SoftCoder Date: Mon, 30 Dec 2013 12:55:59 -0800 Subject: [PATCH] - added two new lua methods basxto: storeSaveGameData(name, value) string loadSaveGameData(name) --- source/glest_game/game/script_manager.cpp | 80 ++++++++++++++++++++++- source/glest_game/game/script_manager.h | 9 +++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index b76499a4..09c732ba 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -390,6 +390,9 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro luaScript.registerFunction(enableSpeedChange, "enableSpeedChange"); luaScript.registerFunction(getSpeedChangeEnabled, "getSpeedChangeEnabled"); + luaScript.registerFunction(storeSaveGameData, "storeSaveGameData"); + luaScript.registerFunction(loadSaveGameData, "loadSaveGameData"); + //load code for(int i= 0; igetScriptCount(); ++i){ const Script* script= scenario->getScript(i); @@ -450,6 +453,9 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro else { loadGame(this->rootNode); this->rootNode = NULL; + + luaScript.beginCall("onLoad"); + luaScript.endCall(); } } catch(const megaglest_runtime_error &ex) { @@ -1975,6 +1981,15 @@ bool ScriptManager::getSpeedChangeEnabled() { void ScriptManager::addMessageToQueue(ScriptManagerMessage msg) { messageQueue.push_back(msg); } + +void ScriptManager::storeSaveGameData(string name, string value) { + luaSavedGameData[name] = value; +} + +string ScriptManager::loadSaveGameData(string name) { + return luaSavedGameData[name]; +} + // ========================== lua callbacks =============================================== int ScriptManager::showMessage(LuaHandle* luaHandle){ @@ -4828,6 +4843,47 @@ int ScriptManager::getSpeedChangeEnabled(LuaHandle* luaHandle) { return luaArguments.getReturnCount(); } +int ScriptManager::storeSaveGameData(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + try { + thisScriptManager->storeSaveGameData(luaArguments.getString(-2),luaArguments.getString(-1)); + } + catch(const megaglest_runtime_error &ex) { + char szErrBuf[8096]=""; + snprintf(szErrBuf,8096,"In [%s::%s %d]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + string sErrBuf = string(szErrBuf) + string("\nThe game may no longer be stable!\nerror [") + string(ex.what()) + string("]\n"); + + SystemFlags::OutputDebug(SystemFlags::debugError,sErrBuf.c_str()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,sErrBuf.c_str()); + + thisScriptManager->addMessageToQueue(ScriptManagerMessage(sErrBuf.c_str(), "error",-1,-1,true)); + thisScriptManager->onMessageBoxOk(false); + } + + return luaArguments.getReturnCount(); +} + +int ScriptManager::loadSaveGameData(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager->loadSaveGameData(luaArguments.getString(-1))); + } + catch(const megaglest_runtime_error &ex) { + char szErrBuf[8096]=""; + snprintf(szErrBuf,8096,"In [%s::%s %d]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + string sErrBuf = string(szErrBuf) + string("\nThe game may no longer be stable!\nerror [") + string(ex.what()) + string("]\n"); + + SystemFlags::OutputDebug(SystemFlags::debugError,sErrBuf.c_str()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,sErrBuf.c_str()); + + thisScriptManager->addMessageToQueue(ScriptManagerMessage(sErrBuf.c_str(), "error",-1,-1,true)); + thisScriptManager->onMessageBoxOk(false); + } + + return luaArguments.getReturnCount(); +} + + void ScriptManager::saveGame(XmlNode *rootNode) { std::map mapTagReplacements; XmlNode *scriptManagerNode = rootNode->addChild("ScriptManager"); @@ -4836,7 +4892,18 @@ void ScriptManager::saveGame(XmlNode *rootNode) { // string code; scriptManagerNode->addAttribute("code",code, mapTagReplacements); // LuaScript luaScript; -// + + luaScript.beginCall("onSave"); + luaScript.endCall(); + for(std::map::iterator iterMap = luaSavedGameData.begin(); + iterMap != luaSavedGameData.end(); ++iterMap) { + + XmlNode *savedGameDataItemNode = scriptManagerNode->addChild("SavedGameDataItem"); + + savedGameDataItemNode->addAttribute("key",iterMap->first, mapTagReplacements); + savedGameDataItemNode->addAttribute("value",iterMap->second, mapTagReplacements); + } + // //world // World *world; // GameCamera *gameCamera; @@ -4952,7 +5019,16 @@ void ScriptManager::loadGame(const XmlNode *rootNode) { // string code; code = scriptManagerNode->getAttribute("code")->getValue(); // LuaScript luaScript; -// + + vector savedGameDataItemNodeList = scriptManagerNode->getChildList("SavedGameDataItem"); + for(unsigned int i = 0; i < savedGameDataItemNodeList.size(); ++i) { + XmlNode *node = savedGameDataItemNodeList[i]; + string key = node->getAttribute("key")->getValue(); + string value = node->getAttribute("value")->getValue(); + + luaSavedGameData[key] = value; + } + // //world // World *world; // GameCamera *gameCamera; diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index 9d452adb..7a5ba8a3 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -218,6 +218,8 @@ private: RandomGen random; const XmlNode *rootNode; + std::map luaSavedGameData; + private: static ScriptManager* thisScriptManager; @@ -418,6 +420,10 @@ private: void disableSpeedChange(); void enableSpeedChange(); bool getSpeedChangeEnabled(); + void storeSaveGameData(string name, string value); + string loadSaveGameData(string name); + + // ----------------------------------------------------------------------- //callbacks, commands static int networkShowMessageForFaction(LuaHandle* luaHandle); @@ -582,6 +588,9 @@ private: static int disableSpeedChange(LuaHandle* luaHandle); static int enableSpeedChange(LuaHandle* luaHandle); static int getSpeedChangeEnabled(LuaHandle* luaHandle); + + static int storeSaveGameData(LuaHandle* luaHandle); + static int loadSaveGameData(LuaHandle* luaHandle); }; }}//end namespace