[7975] | 1 | #include "Script.h" |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | Script::Script() |
---|
| 5 | { |
---|
[8044] | 6 | returnCount = argumentCount = 0; |
---|
| 7 | |
---|
[7975] | 8 | luaState = lua_open(); |
---|
| 9 | |
---|
| 10 | luaopen_base(luaState); |
---|
| 11 | luaopen_table(luaState); |
---|
| 12 | luaopen_io(luaState); |
---|
| 13 | luaopen_string(luaState); |
---|
| 14 | luaopen_math(luaState); |
---|
| 15 | luaopen_debug(luaState); |
---|
| 16 | |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | Script::~Script() |
---|
| 21 | { |
---|
| 22 | lua_setgcthreshold(luaState, 0); // collected garbage |
---|
| 23 | lua_close(luaState); |
---|
| 24 | } |
---|
[8032] | 25 | |
---|
| 26 | |
---|
| 27 | bool Script::loadFile(std::string& filename) |
---|
| 28 | { |
---|
[8044] | 29 | |
---|
[8045] | 30 | if(currentFile.length() != 0) |
---|
| 31 | printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str()); |
---|
| 32 | |
---|
[8032] | 33 | int error = luaL_loadfile (luaState, filename.c_str()); |
---|
| 34 | |
---|
| 35 | if(error == 0) |
---|
| 36 | { |
---|
[8044] | 37 | error = lua_pcall(luaState, 0, 0, 0); |
---|
| 38 | |
---|
| 39 | if(error == 0) |
---|
[8045] | 40 | { |
---|
| 41 | currentFile = filename; |
---|
[8044] | 42 | return true; |
---|
[8045] | 43 | } |
---|
[8044] | 44 | else |
---|
| 45 | { |
---|
| 46 | reportError(error); |
---|
| 47 | } |
---|
[8045] | 48 | |
---|
[8032] | 49 | } |
---|
| 50 | else |
---|
| 51 | { |
---|
[8044] | 52 | reportError(error); |
---|
[8032] | 53 | } |
---|
| 54 | |
---|
[8044] | 55 | return false; |
---|
[8032] | 56 | } |
---|
| 57 | |
---|
[8044] | 58 | bool Script::executeFile() |
---|
| 59 | { |
---|
[8045] | 60 | if(currentFile.length() != 0) |
---|
[8044] | 61 | { |
---|
[8045] | 62 | int error = lua_pcall(luaState, 0, 0, 0); |
---|
| 63 | if( error == 0) |
---|
| 64 | return true; |
---|
| 65 | else |
---|
| 66 | { |
---|
| 67 | reportError(error); |
---|
| 68 | return false; |
---|
| 69 | } |
---|
[8044] | 70 | } |
---|
[8045] | 71 | return false; |
---|
[8044] | 72 | } |
---|
[8032] | 73 | |
---|
[8045] | 74 | bool Script::selectFunction(std::string& functionName, int retCount) |
---|
[8032] | 75 | { |
---|
[8058] | 76 | lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); |
---|
[8032] | 77 | lua_gettable(luaState, LUA_GLOBALSINDEX); |
---|
[8045] | 78 | |
---|
[8058] | 79 | if(lua_isfunction( luaState , -1)) |
---|
[8045] | 80 | { |
---|
[8058] | 81 | returnCount = retCount; |
---|
| 82 | argumentCount = 0; |
---|
| 83 | currentFunction = functionName; |
---|
[8045] | 84 | return true; |
---|
| 85 | } |
---|
| 86 | else |
---|
[8058] | 87 | return false; |
---|
| 88 | |
---|
[8032] | 89 | } |
---|
| 90 | |
---|
| 91 | //return number of returned values |
---|
[8045] | 92 | bool Script::executeFunction() |
---|
[8032] | 93 | { |
---|
[8045] | 94 | if(currentFunction.length() != 0 ) |
---|
| 95 | { |
---|
| 96 | int error = lua_pcall(luaState, argumentCount, returnCount,0); |
---|
| 97 | if(error != 0) |
---|
| 98 | { |
---|
| 99 | reportError(error); |
---|
| 100 | return false; |
---|
| 101 | } |
---|
| 102 | else |
---|
| 103 | { |
---|
| 104 | currentFunction.assign("");//a function gets unusable after beeing called for the first time |
---|
| 105 | returnCount = argumentCount = 0; |
---|
| 106 | return true; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | printf("Error: no function selected.\n"); |
---|
[8032] | 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | //overload this function to add different types |
---|
| 115 | bool Script::pushParam(int param, std::string& toFunction) |
---|
| 116 | { |
---|
[8044] | 117 | if(currentFunction.compare(toFunction) == 0) |
---|
[8032] | 118 | { |
---|
[8044] | 119 | lua_pushnumber(luaState, (lua_Number) param); |
---|
| 120 | argumentCount++; |
---|
[8032] | 121 | return true; |
---|
| 122 | } |
---|
| 123 | else |
---|
| 124 | { |
---|
[8044] | 125 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 126 | return false; |
---|
[8032] | 127 | } |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | bool Script::pushParam(float param, std::string& toFunction) |
---|
| 133 | { |
---|
[8044] | 134 | if(currentFunction.compare(toFunction) == 0) |
---|
[8032] | 135 | { |
---|
[8044] | 136 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 137 | argumentCount++; |
---|
[8032] | 138 | return true; |
---|
| 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
[8044] | 142 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
[8032] | 143 | return false; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | } |
---|
[8044] | 147 | |
---|
[8045] | 148 | bool Script::pushParam(double param, std::string& toFunction) |
---|
| 149 | { |
---|
| 150 | if(currentFunction.compare(toFunction) == 0) |
---|
| 151 | { |
---|
| 152 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 153 | argumentCount++; |
---|
| 154 | return true; |
---|
| 155 | } |
---|
| 156 | else |
---|
| 157 | { |
---|
| 158 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 159 | return false; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } |
---|
| 163 | |
---|
[8058] | 164 | int Script::getReturnedInt() |
---|
| 165 | { |
---|
| 166 | if(returnCount > 0) |
---|
| 167 | { |
---|
[8060] | 168 | printf("int found"); |
---|
[8058] | 169 | if(lua_isnumber(luaState, 1)) |
---|
| 170 | { |
---|
| 171 | printf("int found"); |
---|
| 172 | int returnValue = (int)lua_tonumber(luaState, 1); |
---|
| 173 | returnCount--; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
[8044] | 179 | int Script::reportError(int error) |
---|
| 180 | { |
---|
| 181 | const char *msg = lua_tostring(luaState, -1); |
---|
| 182 | if (msg == NULL) msg = "(error with no message)"; |
---|
| 183 | fprintf(stderr, "ERROR: %s\n", msg); |
---|
| 184 | lua_pop(luaState, 1); |
---|
| 185 | return error; |
---|
| 186 | } |
---|
| 187 | |
---|