diff options
author | jurkan <jurkan@gmx.de> | 2011-10-22 22:41:33 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2011-10-23 00:10:35 +0200 |
commit | 328f9dbb01b63c1a0651980fbdec39b34930f379 (patch) | |
tree | a303a7e1e2001da20f9efca38a025ad5fd2aa84e /src | |
parent | 8d20dc6b593f4d3b1c28b274a8f126b4299b73e4 (diff) | |
download | manaserv-328f9dbb01b63c1a0651980fbdec39b34930f379.tar.gz manaserv-328f9dbb01b63c1a0651980fbdec39b34930f379.tar.bz2 manaserv-328f9dbb01b63c1a0651980fbdec39b34930f379.tar.xz manaserv-328f9dbb01b63c1a0651980fbdec39b34930f379.zip |
Added function to determine the script engine by the file name extension
Reviewed-by: Bjorn, Bertram.
Diffstat (limited to 'src')
-rw-r--r-- | src/game-server/itemmanager.cpp | 5 | ||||
-rw-r--r-- | src/game-server/mapcomposite.cpp | 39 | ||||
-rw-r--r-- | src/game-server/monster.cpp | 4 | ||||
-rw-r--r-- | src/game-server/statusmanager.cpp | 4 | ||||
-rw-r--r-- | src/scripting/script.cpp | 18 | ||||
-rw-r--r-- | src/scripting/script.h | 1 |
6 files changed, 60 insertions, 11 deletions
diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp index 0a428e51..32f48762 100644 --- a/src/game-server/itemmanager.cpp +++ b/src/game-server/itemmanager.cpp @@ -416,7 +416,10 @@ void ItemManager::readEffectNode(xmlNodePtr effectNode, ItemClass *item) } LOG_INFO("Loading item script: " << filename.str()); - Script *script = Script::create("lua"); + + std::string engineName = + Script::determineEngineByFilename(filename.str()); + Script *script = Script::create(engineName); if (!script->loadFile(filename.str())) { // Delete the script as it's invalid. diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp index ecd39569..4d0581ad 100644 --- a/src/game-server/mapcomposite.cpp +++ b/src/game-server/mapcomposite.cpp @@ -727,14 +727,23 @@ void MapComposite::initializeContent() } else if (utils::compareStrI(type, "NPC") == 0) { + int npcId = utils::stringToInt(object->getProperty("NPC_ID")); + std::string scriptText = object->getProperty("SCRIPT"); + if (!mScript) { - mScript = Script::create("lua"); + // Determine script engine by xml property + std::string scriptEngineName = object->getProperty("ENGINE"); + if (scriptEngineName.empty()) + { + // Set engine to default value and print warning + scriptEngineName = Configuration::getValue("defaultScriptEngine", "lua"); + LOG_WARN("No script engine specified for map script \"" + + mName + "\", falling back to default"); + } + mScript = Script::create(scriptEngineName); } - int npcId = utils::stringToInt(object->getProperty("NPC_ID")); - std::string scriptText = object->getProperty("SCRIPT"); - if (npcId && !scriptText.empty()) { mScript->loadNPC(object->getName(), npcId, @@ -748,14 +757,28 @@ void MapComposite::initializeContent() } else if (utils::compareStrI(type, "SCRIPT") == 0) { + std::string scriptFilename = object->getProperty("FILENAME"); + std::string scriptText = object->getProperty("TEXT"); + if (!mScript) { - mScript = Script::create("lua"); + // Determine script engine by xml property + std::string scriptEngineName = object->getProperty("ENGINE"); + if (!scriptFilename.empty() && scriptEngineName.empty()) + { + // Engine property is empty - determine by filename + scriptEngineName = Script::determineEngineByFilename(scriptFilename); + } + else if (scriptEngineName.empty()) + { + // Set engine to default value and print warning + scriptEngineName = Configuration::getValue("defaultScriptEngine", "lua"); + LOG_WARN("No script engine specified for map script \"" + + mName + "\", falling back to default"); + } + mScript = Script::create(scriptEngineName); } - std::string scriptFilename = object->getProperty("FILENAME"); - std::string scriptText = object->getProperty("TEXT"); - if (!scriptFilename.empty()) { mScript->loadFile(scriptFilename); diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp index d0c55073..1eb72aef 100644 --- a/src/game-server/monster.cpp +++ b/src/game-server/monster.cpp @@ -334,7 +334,9 @@ void Monster::loadScript(const std::string &scriptName) if (ResourceManager::exists(filename.str())) { LOG_INFO("Loading monster script: " << filename.str()); - mScript = Script::create("lua"); + std::string engineName = + Script::determineEngineByFilename(filename.str()); + mScript = Script::create(engineName); mScript->loadFile(filename.str()); } else diff --git a/src/game-server/statusmanager.cpp b/src/game-server/statusmanager.cpp index 77519754..66c2642a 100644 --- a/src/game-server/statusmanager.cpp +++ b/src/game-server/statusmanager.cpp @@ -89,7 +89,9 @@ void StatusManager::reload() if (ResourceManager::exists(filename.str())) // file exists! { LOG_INFO("Loading status script: " << filename.str()); - Script *s = Script::create("lua"); + std::string engineName = + Script::determineEngineByFilename(filename.str()); + Script *s = Script::create(engineName); s->loadFile(filename.str()); statusEffect->setScript(s); } else { diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index 490abf09..b6121bb8 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -20,6 +20,7 @@ #include "scripting/script.h" +#include "common/configuration.h" #include "common/resourcemanager.h" #include "game-server/being.h" #include "utils/logger.h" @@ -172,3 +173,20 @@ bool Script::performCraft(Being* crafter, std::list<InventoryItem> recipe) } return true; } + +std::string Script::determineEngineByFilename(const std::string &filename) +{ + std::string ext = filename.substr(filename.find_last_of(".") + 1); + + if (ext == "lua") + { + return "lua"; + } + else + { + // Set to default engine and print warning + LOG_WARN("Unknown file extension for script \"" + + filename + "\", falling back to default script engine"); + return Configuration::getValue("defaultScriptEngine", "lua"); + } +} diff --git a/src/scripting/script.h b/src/scripting/script.h index 44a8b7ac..e2ab3afa 100644 --- a/src/scripting/script.h +++ b/src/scripting/script.h @@ -143,6 +143,7 @@ class Script static bool performSpecialAction(int specialId, Being *caster); static bool performCraft(Being* crafter, std::list<InventoryItem> recipe); + static std::string determineEngineByFilename(const std::string &filename); protected: static Script *globalEventScript; |