Changeset 904
- Timestamp:
- 03/09/10 23:15:31 (5 months ago)
- Location:
- trunk
- Files:
-
- 6 modified
-
client/Game2/GameApp.cpp (modified) (4 diffs)
-
client/Game2/GameApp.h (modified) (2 diffs)
-
client/Game2/SysConsole.cpp (modified) (3 diffs)
-
client/Game2/SysConsole.h (modified) (2 diffs)
-
engine/Script/Env.cpp (modified) (2 diffs)
-
engine/Script/Env.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/client/Game2/GameApp.cpp
r890 r904 43 43 #include "../../engine/VideoServices/VideoBuffer.h" 44 44 #include "../../engine/MainCharacter/MainCharacter.h" 45 #include "../../engine/Script/Core.h" 45 46 #include "../../engine/Util/FuzzyLogic.h" 46 47 #include "../../engine/Util/Profiler.h" … … 569 570 570 571 MR_GameApp::MR_GameApp(HINSTANCE pInstance, bool safeMode) : 571 introMovie(NULL), s ysConsole(NULL)572 introMovie(NULL), scripting(NULL), sysConsole(NULL) 572 573 { 573 574 This = this; … … 612 613 delete sysConsole; 613 614 sysConsole = NULL; 615 } 616 if (scripting != NULL) { 617 delete scripting; 618 scripting = NULL; 614 619 } 615 620 … … 1136 1141 // Create the system console and execute the init script. 1137 1142 // This allows the script to modify the configuration (e.g. for unit tests). 1138 s ysConsole = new SysConsole();1139 sysConsole ->Init();1143 scripting = new Script::Core(); 1144 sysConsole = new SysConsole(scripting); 1140 1145 if (!initScript.empty()) { 1141 1146 sysConsole->RunScript(initScript); -
trunk/client/Game2/GameApp.h
r896 r904 37 37 typedef boost::shared_ptr<Rulebook> RulebookPtr; 38 38 class SysConsole; 39 } 40 namespace Script { 41 class Core; 39 42 } 40 43 } … … 83 86 HoverRace::Client::IntroMovie *introMovie; 84 87 HoverRace::Client::FullscreenTest *fullscreenTest; 88 HoverRace::Script::Core *scripting; 85 89 HoverRace::Client::SysConsole *sysConsole; 86 90 MR_ClientSession *mCurrentSession; -
trunk/client/Game2/SysConsole.cpp
r895 r904 29 29 #include <boost/format.hpp> 30 30 31 #include "../../engine/Script/Core.h"32 33 31 #include "SysConsole.h" 34 32 35 33 namespace fs = boost::filesystem; 36 34 37 using namespace HoverRace; 38 using namespace HoverRace::Client; 39 using namespace HoverRace::Script; 40 41 SysConsole::SysConsole() : 42 SUPER(), onInitRef(-1) 43 { 35 namespace { 36 class LogStreamBuf : public std::stringbuf 37 { 38 typedef std::stringbuf SUPER; 39 public: 40 LogStreamBuf() { } 41 virtual ~LogStreamBuf() { sync(); } 42 43 protected: 44 virtual int sync() 45 { 46 std::string s = str(); 47 48 # ifdef _WIN32 49 OutputDebugString(s.c_str()); 50 # endif 51 std::cout << s << std::flush; 52 53 str(std::string()); 54 return 0; 55 } 56 }; 57 58 class LogStream : public std::ostream 59 { 60 typedef std::ostream SUPER; 61 public: 62 LogStream() : SUPER(new LogStreamBuf()) { } 63 virtual ~LogStream() { delete rdbuf(); } 64 }; 65 } 66 67 namespace HoverRace { 68 namespace Client { 69 70 SysConsole::SysConsole(Script::Core *scripting) : 71 SUPER(scripting), outHandle(scripting->AddOutput(boost::make_shared<LogStream>())) 72 { 73 lua_State *state = scripting->GetState(); 74 75 scripting->PrintStack(); 76 77 // Initial table for callbacks. 78 lua_newtable(state); 79 onInitRef = luaL_ref(scripting->GetState(), LUA_REGISTRYINDEX); 44 80 } 45 81 46 82 SysConsole::~SysConsole() 47 83 { 48 if (onInitRef >= 0) { 49 lua_State *state = GetScripting()->GetState(); 50 luaL_unref(state, LUA_REGISTRYINDEX, onInitRef); 51 } 52 } 53 54 void SysConsole::InitEnv(Script::Core *scripting) 55 { 56 SUPER::InitEnv(scripting); 57 onInitRef = luaL_ref(scripting->GetState(), LUA_REGISTRYINDEX); 58 } 59 60 void SysConsole::InitGlobals(Script::Core *scripting) 61 { 62 SUPER::InitGlobals(scripting); 63 84 Script::Core *scripting = GetScripting(); 85 luaL_unref(scripting->GetState(), LUA_REGISTRYINDEX, onInitRef); 86 scripting->RemoveOutput(outHandle); 87 } 88 89 void SysConsole::InitEnv() 90 { 91 Script::Core *scripting = GetScripting(); 64 92 lua_State *state = scripting->GetState(); 65 93 66 lua_pushlightuserdata(state, this); 67 lua_pushcclosure(state, SysConsole::LOnInit, 1); 68 lua_setglobal(state, "on_init"); 69 70 lua_pushlightuserdata(state, this); 71 lua_pushcclosure(state, SysConsole::LGetOnInit, 1); 72 lua_setglobal(state, "get_on_init"); 73 74 // Initial arrays for callbacks. 75 lua_newtable(state); 76 lua_rawseti(state, LUA_REGISTRYINDEX, onInitRef); 94 // Start with the standard global environment. 95 CopyGlobals(); 96 97 lua_pushlightuserdata(state, this); // table this 98 lua_pushcclosure(state, SysConsole::LOnInit, 1); // table fn 99 lua_pushstring(state, "on_init"); // table fn str 100 lua_insert(state, -2); // table str fn 101 lua_rawset(state, -3); // table 102 103 lua_pushlightuserdata(state, this); // table this 104 lua_pushcclosure(state, SysConsole::LGetOnInit, 1); // table fn 105 lua_pushstring(state, "get_on_init"); // table fn str 106 lua_insert(state, -2); // table str fn 107 lua_rawset(state, -3); // table 77 108 } 78 109 … … 113 144 fs::ifstream ifs(scriptPath, std::ios_base::in); 114 145 std::string ris((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); 115 SubmitChunk(ris); 146 try { 147 Execute(ris); 148 } 149 catch (Script::ScriptExn &ex) { 150 LogError(ex.what()); 151 } 116 152 } 117 153 … … 166 202 return 1; 167 203 } 204 205 } // namespace Client 206 } // namespace HoverRace -
trunk/client/Game2/SysConsole.h
r895 r904 23 23 #pragma once 24 24 25 #include "Console.h" 25 #include "../../engine/Script/Core.h" 26 #include "../../engine/Script/Env.h" 27 28 namespace HoverRace { 29 namespace Script { 30 class Core; 31 } 32 } 26 33 27 34 namespace HoverRace { 28 35 namespace Client { 29 36 30 class SysConsole : p ublic Console37 class SysConsole : private Script::Env 31 38 { 32 typedef ConsoleSUPER;39 typedef Script::Env SUPER; 33 40 34 41 public: 35 SysConsole( );42 SysConsole(Script::Core *scripting); 36 43 virtual ~SysConsole(); 37 44 38 45 protected: 39 virtual void InitEnv(Script::Core *scripting); 40 virtual void InitGlobals(Script::Core *scripting); 46 virtual void InitEnv(); 41 47 42 public:43 virtual void Advance(Util::OS::timestamp_t tick) { };44 virtual void Clear() { };45 46 48 protected: 47 49 virtual void LogInfo(const std::string &s); … … 59 61 60 62 private: 63 Script::Core::OutHandle outHandle; 61 64 int onInitRef; 62 65 }; -
trunk/engine/Script/Env.cpp
r898 r904 23 23 #include "StdAfx.h" 24 24 25 #include "Core.h" 26 25 27 #include "Env.h" 26 28 … … 28 30 namespace Script { 29 31 32 /** 33 * Constructor. 34 * @param scripting The scripting engine (may not be @c NULL). 35 */ 30 36 Env::Env(Core *scripting) : 31 scripting(scripting) 37 scripting(scripting), initialized(false) 32 38 { 39 lua_State *state = scripting->GetState(); 40 41 // Store a placeholder at the registry location. 42 lua_pushinteger(state, 1); 43 envRef = luaL_ref(state, LUA_REGISTRYINDEX); 33 44 } 34 45 35 46 Env::~Env() 36 47 { 48 luaL_unref(scripting->GetState(), LUA_REGISTRYINDEX, envRef); 49 } 50 51 /** 52 * Initialize the environment in which scripts will run in. 53 * Upon entry, the Lua stack will have at least one entry, the table which 54 * represents the environment. Implementing functions will fill this table 55 * with the globals which will be available to the functions which are run 56 * in this environment. Upon return, this same table must be at the top of 57 * the stack. 58 */ 59 void Env::InitEnv() 60 { 61 } 62 63 /** 64 * Copy the global environment into the current table at the top of the stack. 65 * This is meant to be called from InitEnv() as a convenience. 66 */ 67 void Env::CopyGlobals() 68 { 69 lua_State *state = scripting->GetState(); 70 71 lua_pushnil(state); // table nil 72 while (lua_next(state, LUA_GLOBALSINDEX) != 0) { 73 // table key value 74 lua_pushvalue(state, -2); // table key value key 75 lua_insert(state, -2); // table key key value 76 lua_rawset(state, -4); // table key 77 } 78 // table 79 } 80 81 /** 82 * Execute a chunk of code in the current environment. 83 * @param chunk The code to execute. 84 * @throw IncompleteExn If the code does not complete a statement; i.e., 85 * expecting more tokens. Callers can catch this 86 * to keep reading more data to finish the statement. 87 * @throw ScriptExn The code either failed to compile or signaled an error 88 * while executing. 89 */ 90 void Env::Execute(const std::string &chunk) 91 { 92 lua_State *state = scripting->GetState(); 93 94 // May throw ScriptExn or IncompleteExn, in which case stack will be unchanged. 95 scripting->Compile(chunk); 96 97 if (initialized) { 98 lua_rawgeti(state, LUA_REGISTRYINDEX, envRef); 99 } 100 else { 101 lua_newtable(state); 102 InitEnv(); 103 initialized = true; 104 lua_pushvalue(state, -1); 105 lua_rawseti(state, LUA_REGISTRYINDEX, envRef); 106 } 107 lua_setfenv(state, -2); 108 109 // May throw ScriptExn, but the function on the stack will be consumed anyway. 110 scripting->CallAndPrint(); 37 111 } 38 112 -
trunk/engine/Script/Env.h
r898 r904 23 23 #pragma once 24 24 25 #include "Core.h"26 27 25 #ifdef _WIN32 28 26 # ifdef MR_ENGINE … … 46 44 virtual ~Env(); 47 45 46 protected: 47 Core *GetScripting() const { return scripting; } 48 49 virtual void InitEnv() = 0; 50 void CopyGlobals(); 51 52 void Execute(const std::string &chunk); 53 48 54 private: 49 55 Core *scripting; 56 bool initialized; 57 int envRef; 50 58 }; 51 59
