Changeset 910 for trunk/client/Game2/Console.cpp
- Timestamp:
- 03/10/10 23:08:56 (6 months ago)
- Files:
-
- 1 modified
-
trunk/client/Game2/Console.cpp (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/client/Game2/Console.cpp
r903 r910 58 58 }; 59 59 60 Console::Console( ) :61 inputState(ISTATE_COMMAND)60 Console::Console(Script::Core *scripting) : 61 SUPER(scripting), inputState(ISTATE_COMMAND) 62 62 { 63 63 chunk.reserve(1024); 64 outHandle = scripting->AddOutput(boost::make_shared<LogStream>(this)); 64 65 } 65 66 66 67 Console::~Console() 67 68 { 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 72 void 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 /* 96 86 lua_pushlightuserdata(state, this); 97 87 lua_pushcclosure(state, Console::LClear, 1); … … 99 89 100 90 lua_pushlightuserdata(state, this); 101 lua_pushcclosure(state, Console::LReinit, 1);102 lua_setglobal(state, "reinit");103 104 lua_pushlightuserdata(state, this);105 91 lua_pushcclosure(state, Console::LReset, 1); 106 92 lua_setglobal(state, "reset"); … … 109 95 lua_pushcclosure(state, Console::LSandbox, 1); 110 96 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 */ 129 98 } 130 99 … … 138 107 139 108 try { 140 scripting->Execute(chunk);109 Execute(chunk); 141 110 } 142 111 catch (const IncompleteExn&) { … … 147 116 LogError(exn.GetMessage()); 148 117 } 149 150 Cleanup();151 118 152 119 chunk.clear(); … … 175 142 } 176 143 144 /* 177 145 int Console::LReset(lua_State *state) 178 146 { … … 185 153 self->LogInfo("Console script environment reset complete."); 186 154 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 very194 // messed up. reset() does not need to be called afterwards; this function195 // 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 environment205 // immediately.206 lua_pushstring(state, "Console script environment reinitialization complete.");207 return lua_error(state);208 155 } 209 156 … … 221 168 return 0; 222 169 } 170 */ 223 171 224 172 // Console::LogStreamBuf
