Advanced Strategic Command
luarunner.cpp
Go to the documentation of this file.
1 
2 
3 extern "C"
4 {
5 
6  #include <lua.h>
7  #include <lualib.h>
8  #include <lauxlib.h>
9 
10 }
11 
12 
13 #include "luarunner.h"
14 #include "luastate.h"
15 #include "../ascstring.h"
16 #include "../basestrm.h"
17 
18 
19 LuaRunner::LuaRunner( LuaState& luaState ) : state ( luaState )
20 {
21 
22 }
23 
24 
25 void LuaRunner::runFile( const ASCString& filename )
26 {
27  try {
28  tnfilestream stream ( filename, tnstream::reading );
29  ASCString line = stream.readString(true);
30  while ( line.size() ) {
31 
32  int errorCode = luaL_loadbuffer(state.get(), line.c_str(), line.size(), stream.getLocation().c_str());
33  if ( !errorCode )
34  errorCode = lua_pcall(state.get(), 0, 0, 0);
35 
36  if (errorCode) {
37  errors += lua_tostring(state.get(), -1);
38  errors += "\n";
39  lua_pop(state.get(), 1); /* pop error message from the stack */
40  }
41 
42  line = stream.readString(true);
43  }
44  } catch ( treadafterend err ) {
45  }
46 }
47 
48 void LuaRunner::runCommand( const ASCString& command )
49 {
50  int errorCode = luaL_loadbuffer(state.get(), command.c_str(), command.size(), command.c_str());
51  if ( !errorCode )
52  errorCode = lua_pcall(state.get(), 0, 0, 0);
53 
54  if (errorCode) {
55  errors += lua_tostring(state.get(), -1);
56  errors += "\n";
57  lua_pop(state.get(), 1); /* pop error message from the stack */
58  }
59 }
60 
62 {
63  return errors;
64 }
65 
LuaRunner(LuaState &luaState)
Definition: luarunner.cpp:19
void runFile(const ASCString &filename)
Definition: luarunner.cpp:25
void runCommand(const ASCString &command)
Definition: luarunner.cpp:48
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString getLocation()
returns the location of the stream.
Definition: basestrm.cpp:1663
lua_State * get()
Definition: luastate.h:13
virtual ASCString readString(bool includeCR=false)
Reads and returns a string.
Definition: basestrm.cpp:535
const ASCString & getErrors()
Definition: luarunner.cpp:61
void line(int x1, int y1, int x2, int y2, Uint8 actcol)
draws a simple line on the screen. Not very fast...
Definition: basegfx.cpp:181