Changeset 910 – HoverRace

Changeset 910

Show
Ignore:
Timestamp:
03/10/10 23:08:56 (5 months ago)
Author:
zoogie
Message:

Client::Console now extends Script::Env.
We now only use one scripting state, with SysConsole and HighConsole running inside sandboxed environments.

Location:
trunk/client/Game2
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/client/Game2/Console.cpp

    r903 r910  
    5858}; 
    5959 
    60 Console::Console() : 
    61     inputState(ISTATE_COMMAND) 
     60Console::Console(Script::Core *scripting) : 
     61    SUPER(scripting), inputState(ISTATE_COMMAND) 
    6262{ 
    6363    chunk.reserve(1024); 
     64    outHandle = scripting->AddOutput(boost::make_shared<LogStream>(this)); 
    6465} 
    6566 
    6667Console::~Console() 
    6768{ 
    68     Cleanup(); 
    69     delete scripting; 
    70 } 
    71  
    72 void Console::Init() 
    73 { 
    74     scripting = new Core(); 
    75     InitEnv(scripting); 
    76     InitGlobals(scripting); 
    77 } 
    78  
    79 /** 
    80  * Set intial settings on the scripting environment. 
    81  * @param scripting The environment to initialize (may not be @c NULL). 
    82  */ 
    83 void Console::InitEnv(Script::Core *scripting) 
    84 { 
    85     scripting->AddOutput(boost::make_shared<LogStream>(this)); 
    86 } 
    87  
    88 /** 
    89  * Populate the globals in the environment. 
    90  * @param scripting The environment to initialize (may not be @c NULL). 
    91  */ 
    92 void Console::InitGlobals(Script::Core *scripting) 
    93 { 
    94     lua_State *state = scripting->GetState(); 
    95  
     69    GetScripting()->RemoveOutput(outHandle); 
     70} 
     71 
     72void Console::InitEnv() 
     73{ 
     74    lua_State *state = GetScripting()->GetState(); 
     75 
     76    // Start with the standard global environment. 
     77    CopyGlobals(); 
     78 
     79    lua_pushlightuserdata(state, this);  // table this 
     80    lua_pushcclosure(state, Console::LClear, 1);  // table fn 
     81    lua_pushstring(state, "clear");  // table fn str 
     82    lua_insert(state, -2);  // table str fn 
     83    lua_rawset(state, -3);  // table 
     84 
     85    /* 
    9686    lua_pushlightuserdata(state, this); 
    9787    lua_pushcclosure(state, Console::LClear, 1); 
     
    9989 
    10090    lua_pushlightuserdata(state, this); 
    101     lua_pushcclosure(state, Console::LReinit, 1); 
    102     lua_setglobal(state, "reinit"); 
    103  
    104     lua_pushlightuserdata(state, this); 
    10591    lua_pushcclosure(state, Console::LReset, 1); 
    10692    lua_setglobal(state, "reset"); 
     
    10995    lua_pushcclosure(state, Console::LSandbox, 1); 
    11096    lua_setglobal(state, "sandbox"); 
    111 } 
    112  
    113 /** 
    114  * Clean up any resources invalidated during script execution. 
    115  * Note that this is not the same as invoking the garbage collector for the 
    116  * scripting environment; this is for console-specific resources which 
    117  * have been invalidated but could not be freed while the script was executing. 
    118  */ 
    119 void Console::Cleanup() 
    120 { 
    121     if (!oldScriptings.empty()) { 
    122         for (oldScriptings_t::iterator iter = oldScriptings.begin(); 
    123             iter != oldScriptings.end(); ++iter) 
    124         { 
    125             delete *iter; 
    126         } 
    127         oldScriptings.clear(); 
    128     } 
     97    */ 
    12998} 
    13099 
     
    138107 
    139108    try { 
    140         scripting->Execute(chunk); 
     109        Execute(chunk); 
    141110    } 
    142111    catch (const IncompleteExn&) { 
     
    147116        LogError(exn.GetMessage()); 
    148117    } 
    149  
    150     Cleanup(); 
    151118 
    152119    chunk.clear(); 
     
    175142} 
    176143 
     144/* 
    177145int Console::LReset(lua_State *state) 
    178146{ 
     
    185153    self->LogInfo("Console script environment reset complete."); 
    186154    return 0; 
    187 } 
    188  
    189 int Console::LReinit(lua_State *state) 
    190 { 
    191     // function reinit() 
    192     // Completely reinitialize the environment. 
    193     // This is a last-resort call for fixing an environment that is very 
    194     // messed up.  reset() does not need to be called afterwards; this function 
    195     // is a superset of what reset() does. 
    196     Console *self = static_cast<Console*>(lua_touserdata(state, lua_upvalueindex(1))); 
    197  
    198     // Save the current environment to be cleaned up later (in Clean()). 
    199     // We can't destroy it just yet -- we were called from it! 
    200     self->oldScriptings.push_back(self->scripting); 
    201  
    202     self->Init(); 
    203  
    204     // Cause an error in order to terminate execution in the current environment 
    205     // immediately. 
    206     lua_pushstring(state, "Console script environment reinitialization complete."); 
    207     return lua_error(state); 
    208155} 
    209156 
     
    221168    return 0; 
    222169} 
     170*/ 
    223171 
    224172// Console::LogStreamBuf 
  • trunk/client/Game2/Console.h

    r903 r910  
    2525#include <lua.hpp> 
    2626 
     27#include "../../engine/Script/Core.h" 
     28#include "../../engine/Script/Env.h" 
    2729#include "../../engine/Util/OS.h" 
    28  
    29 namespace HoverRace { 
    30     namespace Script { 
    31         class Core; 
    32     } 
    33 } 
    3430 
    3531namespace HoverRace { 
     
    4036 * @author Michael Imamura 
    4137 */ 
    42 class Console 
     38class Console : public Script::Env 
    4339{ 
     40    typedef Script::Env SUPER; 
     41 
    4442    public: 
    45         Console(); 
     43        Console(Script::Core *scripting); 
    4644        virtual ~Console(); 
    4745 
    48     public: 
    49         void Init(); 
    5046    protected: 
    51         Script::Core *GetScripting() const { return scripting; } 
    52         virtual void InitEnv(Script::Core *scripting); 
    53         virtual void InitGlobals(Script::Core *scripting); 
    54  
    55         virtual void Cleanup(); 
     47        virtual void InitEnv(); 
    5648 
    5749    public: 
     
    7668    private: 
    7769        static int LClear(lua_State *state); 
     70        /* 
    7871        static int LReset(lua_State *state); 
    79         static int LReinit(lua_State *state); 
    8072        static int LSandbox(lua_State *state); 
     73        */ 
    8174 
    8275    private: 
    83         Script::Core *scripting; 
    84         typedef std::list<Script::Core*> oldScriptings_t; 
    85         oldScriptings_t oldScriptings; 
     76        inputState_t inputState; 
     77        Script::Core::OutHandle outHandle; 
    8678        std::string chunk; 
    87         inputState_t inputState; 
    8879 
    8980        class LogStreamBuf; 
  • trunk/client/Game2/GameApp.cpp

    r904 r910  
    15611561        highObserver = new HighObserver(); 
    15621562        if (Config::GetInstance()->runtime.enableConsole) { 
    1563             highConsole = new HighConsole(); 
    1564             highConsole->Init(); 
     1563            highConsole = new HighConsole(scripting); 
    15651564        } 
    15661565 
  • trunk/client/Game2/HighConsole.cpp

    r896 r910  
    7676}; 
    7777 
    78 HighConsole::HighConsole() : 
    79     SUPER(), visible(false), cursorOn(true), cursorTick(0) 
     78HighConsole::HighConsole(Script::Core *scripting) : 
     79    SUPER(scripting), visible(false), cursorOn(true), cursorTick(0) 
    8080{ 
    8181    vp = new MR_2DViewPort(); 
  • trunk/client/Game2/HighConsole.h

    r896 r910  
    4646    typedef Console SUPER; 
    4747    public: 
    48         HighConsole(); 
     48        HighConsole(Script::Core *scripting); 
    4949        virtual ~HighConsole(); 
    5050