From 3c67a6159f6a399ebf10ad21a9d116d3fd59ea26 Mon Sep 17 00:00:00 2001 From: sachith500 Date: Thu, 22 May 2014 18:17:30 +0530 Subject: [PATCH] Moved scripts to stk-code/data/scripts and provided access to them via file_manager --- .../scripts}/haybail-activate.as | 0 {src/scriptengine => data/scripts}/haybail.as | 0 .../scripts}/tutorial_bananas.as | 0 src/io/file_manager.cpp | 1 + src/io/file_manager.hpp | 4 +- src/scriptengine/README.txt | 28 --------- src/scriptengine/script_engine.cpp | 59 +++++++++---------- 7 files changed, 32 insertions(+), 60 deletions(-) rename {src/scriptengine => data/scripts}/haybail-activate.as (100%) rename {src/scriptengine => data/scripts}/haybail.as (100%) rename {src/scriptengine => data/scripts}/tutorial_bananas.as (100%) delete mode 100644 src/scriptengine/README.txt diff --git a/src/scriptengine/haybail-activate.as b/data/scripts/haybail-activate.as similarity index 100% rename from src/scriptengine/haybail-activate.as rename to data/scripts/haybail-activate.as diff --git a/src/scriptengine/haybail.as b/data/scripts/haybail.as similarity index 100% rename from src/scriptengine/haybail.as rename to data/scripts/haybail.as diff --git a/src/scriptengine/tutorial_bananas.as b/data/scripts/tutorial_bananas.as similarity index 100% rename from src/scriptengine/tutorial_bananas.as rename to data/scripts/tutorial_bananas.as diff --git a/src/io/file_manager.cpp b/src/io/file_manager.cpp index e45b98008..aca90fcfd 100644 --- a/src/io/file_manager.cpp +++ b/src/io/file_manager.cpp @@ -118,6 +118,7 @@ FileManager::FileManager() m_subdir_name[MUSIC ] = "music"; m_subdir_name[TRANSLATION] = "po"; m_subdir_name[TEXTURE ] = "textures"; + m_subdir_name[SCRIPT ] = "scripts"; m_subdir_name[SFX ] = "sfx"; m_subdir_name[SKIN ] = "skins"; m_subdir_name[SHADER ] = "shaders"; diff --git a/src/io/file_manager.hpp b/src/io/file_manager.hpp index cd3914d14..ae378bd7f 100644 --- a/src/io/file_manager.hpp +++ b/src/io/file_manager.hpp @@ -49,8 +49,8 @@ public: enum AssetType {ASSET_MIN, CHALLENGE=ASSET_MIN, FONT, GFX, GRANDPRIX, GUI, MODEL, MUSIC, - SFX, SHADER, SKIN, TEXTURE, TRANSLATION, - ASSET_MAX = TRANSLATION, + SCRIPT, SFX, SHADER, SKIN, TEXTURE, + TRANSLATION, ASSET_MAX = TRANSLATION, ASSET_COUNT}; private: diff --git a/src/scriptengine/README.txt b/src/scriptengine/README.txt deleted file mode 100644 index 483838724..000000000 --- a/src/scriptengine/README.txt +++ /dev/null @@ -1,28 +0,0 @@ -This is currently only a tiny prototype to get an idea of what the scripting engine can do/ check it's performance. -TODO: -get file manager support for loading scripts -move scripts to stk-data, etc. - - -Steps to get this working. - -==>change stk-code/src/scriptengine/scriptengine.cpp line 254 as appropriate -(add the required directory) -Build the game. -Play the tutorial -Test by changing the strings in stk-code/src/tracks/tutorial_bananas.as - - - - -Steps for creating new scripts/ actions -Add an action trigger to the relevant track (Either add it on blender and export it / set it up in scene.xml) -Create a new script with the defined action scene.xml for the action trigger created. (For example, for tutorial_bananas => - - - -action is "tutorial_bananas" -so the script it calls is, stk/stk-code/scriptengine/tutorial_bananas.as - -Make sure the method onTrigger() is defined. Check tutorial_bananas.as for more details - diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 56ebb12f3..8f005fd41 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -27,6 +27,7 @@ #include "tracks/track_object_manager.hpp" #include "tracks/track.hpp" #include "karts/kart.hpp" +#include "io/file_manager.hpp" using namespace irr; @@ -80,6 +81,32 @@ void squashKart(asIScriptGeneric *gen){ } +std::string getScript(std::string scriptName){ + std::string script_dir = file_manager->getAsset(FileManager::SCRIPT, ""); + + script_dir += scriptName + ".as"; + FILE *f = fopen(script_dir.c_str(), "rb"); + if( f == 0 ) + { + std::cout << "Failed to open the script file " + scriptName + ".as" << std::endl; + } + + // Determine the size of the file + fseek(f, 0, SEEK_END); + int len = ftell(f); + fseek(f, 0, SEEK_SET); + + // Read the entire file + std::string script; + script.resize(len); + int c = fread(&script[0], len, 1, f); + fclose(f); + if( c == 0 ) + { + std::cout << "Failed to load script file." << std::endl; + } + return script; +} void ScriptEngine::runScript(std::string scriptName) { @@ -204,42 +231,14 @@ int ScriptEngine::compileScript(asIScriptEngine *engine, std::string scriptName) { int r; - // For now we will load the script directtly from a file on the disk. - //TODO use filemanager to do this. - //std::string load_dir = "D:\\Github\\stk\\stk-code\\src\\scriptengine\\"; - //std::string load_dir = "//media//New Volume//Github//stk//stk-code//src//scriptengine//"; - std::string load_dir = "..//..//src//scriptengine//"; - load_dir += scriptName + ".as"; - FILE *f = fopen(load_dir.c_str(), "rb"); - if( f == 0 ) - { - std::cout << "Failed to open the script file " + scriptName + ".as" << std::endl; - return -1; - } - - // Determine the size of the file - fseek(f, 0, SEEK_END); - int len = ftell(f); - fseek(f, 0, SEEK_SET); - - // Read the entire file - std::string script; - script.resize(len); - int c = fread(&script[0], len, 1, f); - fclose(f); - if( c == 0 ) - { - std::cout << "Failed to load script file." << std::endl; - return -1; - } - + std::string script = getScript(scriptName); // Add the script sections that will be compiled into executable code. // If we want to combine more than one file into the same script, then // we can call AddScriptSection() several times for the same module and // the script engine will treat them all as if they were one. The script // section name, will allow us to localize any errors in the script code. asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE); - r = mod->AddScriptSection("script", &script[0], len); + r = mod->AddScriptSection("script", &script[0], script.size()); if( r < 0 ) { std::cout << "AddScriptSection() failed" << std::endl;