[8075] | 1 | #include "script.h" |
---|
[8202] | 2 | #include "script_class.h" |
---|
| 3 | #include "luaincl.h" |
---|
[8197] | 4 | |
---|
[8202] | 5 | |
---|
[8193] | 6 | #include "loading/load_param.h" |
---|
[8197] | 7 | #include "parser/tinyxml/tinyxml.h" |
---|
[8075] | 8 | |
---|
[8197] | 9 | #include "class_list.h" |
---|
[8075] | 10 | |
---|
[8193] | 11 | Script::Script(const TiXmlElement* root) |
---|
[8075] | 12 | { |
---|
[8193] | 13 | this->setClassID(CL_SCRIPT, "Script"); |
---|
[8231] | 14 | |
---|
[8075] | 15 | returnCount = argumentCount = 0; |
---|
| 16 | |
---|
| 17 | luaState = lua_open(); |
---|
| 18 | |
---|
| 19 | luaopen_base(luaState); |
---|
| 20 | luaopen_table(luaState); |
---|
| 21 | luaopen_io(luaState); |
---|
| 22 | luaopen_string(luaState); |
---|
| 23 | luaopen_math(luaState); |
---|
| 24 | luaopen_debug(luaState); |
---|
[8231] | 25 | |
---|
[8193] | 26 | if (root != NULL) |
---|
| 27 | this->loadParams(root); |
---|
[8075] | 28 | } |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | Script::~Script() |
---|
| 32 | { |
---|
| 33 | lua_setgcthreshold(luaState, 0); // collected garbage |
---|
| 34 | lua_close(luaState); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
[8193] | 38 | void Script::loadParams(const TiXmlElement* root) |
---|
| 39 | { |
---|
[8199] | 40 | BaseObject::loadParams(root); |
---|
[8231] | 41 | |
---|
[8197] | 42 | LOAD_PARAM_START_CYCLE(root, object); |
---|
[8193] | 43 | { |
---|
[8197] | 44 | LoadParam_CYCLE(object, "object", this, Script, addObject) |
---|
[8193] | 45 | .describe("The name of an object that is needed by a script"); |
---|
| 46 | } |
---|
[8197] | 47 | LOAD_PARAM_END_CYCLE(object); |
---|
[8193] | 48 | |
---|
| 49 | |
---|
| 50 | LoadParam(root, "file", this, Script, loadFileNoRet) |
---|
| 51 | .describe("the fileName of the script, that should be loaded into this world") |
---|
| 52 | .defaultValues(""); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | bool Script::loadFile(const std::string& filename) |
---|
[8075] | 58 | { |
---|
| 59 | |
---|
| 60 | if(currentFile.length() != 0) |
---|
[8131] | 61 | { |
---|
[8075] | 62 | printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str()); |
---|
[8085] | 63 | return false; |
---|
| 64 | } |
---|
[8075] | 65 | |
---|
| 66 | int error = luaL_loadfile (luaState, filename.c_str()); |
---|
| 67 | |
---|
| 68 | if(error == 0) |
---|
| 69 | { |
---|
| 70 | error = lua_pcall(luaState, 0, 0, 0); |
---|
| 71 | |
---|
| 72 | if(error == 0) |
---|
| 73 | { |
---|
| 74 | currentFile = filename; |
---|
| 75 | return true; |
---|
| 76 | } |
---|
| 77 | else |
---|
| 78 | { |
---|
| 79 | reportError(error); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | } |
---|
| 83 | else |
---|
| 84 | { |
---|
| 85 | reportError(error); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | return false; |
---|
| 89 | } |
---|
| 90 | |
---|
[8231] | 91 | |
---|
[8193] | 92 | void Script::addObject(const std::string& className, const std::string& objectName) |
---|
| 93 | { |
---|
[8202] | 94 | BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS); |
---|
[8207] | 95 | WorldObject tmpObj; |
---|
[8197] | 96 | if (scriptClass != NULL) |
---|
| 97 | { |
---|
[8231] | 98 | tmpObj.type = className; |
---|
[8206] | 99 | if( !classIsRegistered(className) ) |
---|
[8207] | 100 | { |
---|
[8202] | 101 | static_cast<ScriptClass*>(scriptClass)->registerClass(this); |
---|
[8207] | 102 | } |
---|
[8231] | 103 | |
---|
[8197] | 104 | BaseObject* object = ClassList::getObject(objectName, className); |
---|
[8206] | 105 | if (object != NULL && !objectIsAdded(objectName)) |
---|
[8197] | 106 | { |
---|
[8202] | 107 | static_cast<ScriptClass*>(scriptClass)->insertObject(this, object, false); |
---|
[8207] | 108 | tmpObj.name = objectName; |
---|
| 109 | registeredObjects.push_back(tmpObj); |
---|
[8197] | 110 | } |
---|
| 111 | } |
---|
[8193] | 112 | } |
---|
| 113 | |
---|
[8231] | 114 | |
---|
| 115 | |
---|
| 116 | |
---|
[8075] | 117 | bool Script::executeFile() |
---|
| 118 | { |
---|
[8231] | 119 | printf("WARNING: script.executeFile is not implemented yet"); |
---|
| 120 | /*if(currentFile.length() != 0) |
---|
[8075] | 121 | { |
---|
| 122 | int error = lua_pcall(luaState, 0, 0, 0); |
---|
| 123 | if( error == 0) |
---|
| 124 | return true; |
---|
| 125 | else |
---|
| 126 | { |
---|
| 127 | reportError(error); |
---|
| 128 | return false; |
---|
| 129 | } |
---|
[8231] | 130 | }*/ |
---|
[8075] | 131 | return false; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | bool Script::selectFunction(std::string& functionName, int retCount) |
---|
| 135 | { |
---|
[8085] | 136 | if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected |
---|
[8075] | 137 | { |
---|
| 138 | lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); |
---|
| 139 | lua_gettable(luaState, LUA_GLOBALSINDEX); |
---|
| 140 | |
---|
| 141 | if(lua_isfunction( luaState , -1)) |
---|
| 142 | { |
---|
| 143 | returnCount = retCount; |
---|
| 144 | argumentCount = 0; |
---|
| 145 | currentFunction = functionName; |
---|
| 146 | return true; |
---|
| 147 | } |
---|
| 148 | else |
---|
| 149 | return false; |
---|
| 150 | } |
---|
| 151 | else |
---|
[8085] | 152 | printf("There is an other function active ( %s ) or there are unremoved return values on the stack. Please remove them first.\n",currentFunction.c_str()); |
---|
| 153 | return false; |
---|
[8075] | 154 | } |
---|
| 155 | |
---|
| 156 | //return number of returned values |
---|
| 157 | bool Script::executeFunction() |
---|
| 158 | { |
---|
| 159 | if(currentFunction.length() != 0 ) |
---|
| 160 | { |
---|
| 161 | int error = lua_pcall(luaState, argumentCount, returnCount,0); |
---|
| 162 | if(error != 0) |
---|
| 163 | { |
---|
| 164 | reportError(error); |
---|
| 165 | return false; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | currentFunction.assign("");//a function gets unusable after beeing called for the first time |
---|
| 170 | argumentCount = 0; |
---|
| 171 | return true; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | else |
---|
| 175 | printf("Error: no function selected.\n"); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | //overload this function to add different types |
---|
| 180 | bool Script::pushParam(int param, std::string& toFunction) |
---|
| 181 | { |
---|
| 182 | if(currentFunction.compare(toFunction) == 0) |
---|
| 183 | { |
---|
| 184 | lua_pushnumber(luaState, (lua_Number) param); |
---|
| 185 | argumentCount++; |
---|
| 186 | return true; |
---|
| 187 | } |
---|
| 188 | else |
---|
| 189 | { |
---|
| 190 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 191 | return false; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | bool Script::pushParam(float param, std::string& toFunction) |
---|
| 198 | { |
---|
| 199 | if(currentFunction.compare(toFunction) == 0) |
---|
| 200 | { |
---|
| 201 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 202 | argumentCount++; |
---|
| 203 | return true; |
---|
| 204 | } |
---|
| 205 | else |
---|
| 206 | { |
---|
| 207 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 208 | return false; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | bool Script::pushParam(double param, std::string& toFunction) |
---|
| 214 | { |
---|
| 215 | if(currentFunction.compare(toFunction) == 0) |
---|
| 216 | { |
---|
| 217 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 218 | argumentCount++; |
---|
| 219 | return true; |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
| 223 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 224 | return false; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | int Script::getReturnedInt() |
---|
| 230 | { |
---|
| 231 | int returnValue; |
---|
| 232 | if(returnCount > 0) |
---|
| 233 | { |
---|
| 234 | if(lua_isnumber(luaState, -1)) |
---|
| 235 | { |
---|
| 236 | returnValue = (int)lua_tonumber(luaState, -1); |
---|
| 237 | returnCount--; |
---|
| 238 | lua_pop(luaState,1); |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | return returnValue; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | bool Script::getReturnedBool() |
---|
| 246 | { |
---|
| 247 | bool returnValue; |
---|
| 248 | if(returnCount > 0) |
---|
| 249 | { |
---|
| 250 | if(lua_isboolean(luaState, -1)) |
---|
| 251 | { |
---|
| 252 | returnValue = (bool)lua_toboolean(luaState, -1); |
---|
| 253 | returnCount--; |
---|
| 254 | lua_pop(luaState,1); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | return returnValue; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | float Script::getReturnedFloat() |
---|
| 261 | { |
---|
| 262 | float returnValue; |
---|
| 263 | if(returnCount > 0) |
---|
| 264 | { |
---|
| 265 | if(lua_isnumber(luaState, -1)) |
---|
| 266 | { |
---|
| 267 | returnValue = (float)lua_tonumber(luaState, -1); |
---|
| 268 | returnCount--; |
---|
| 269 | lua_pop(luaState,1); |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | return returnValue; |
---|
| 273 | } |
---|
| 274 | |
---|
[8131] | 275 | void Script::getReturnedString(std::string& string) |
---|
| 276 | { |
---|
[8134] | 277 | const char* returnValue; |
---|
[8131] | 278 | if(returnCount > 0) |
---|
| 279 | { |
---|
[8133] | 280 | if(lua_isstring(luaState, -1)) |
---|
[8131] | 281 | { |
---|
| 282 | returnValue = lua_tostring(luaState, -1); |
---|
| 283 | returnCount--; |
---|
| 284 | lua_pop(luaState,1); |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | string.assign(returnValue); |
---|
| 288 | } |
---|
| 289 | |
---|
[8075] | 290 | int Script::reportError(int error) |
---|
| 291 | { |
---|
[8085] | 292 | if(error != 0) |
---|
| 293 | { |
---|
| 294 | const char *msg = lua_tostring(luaState, -1); |
---|
| 295 | if (msg == NULL) msg = "(error with no message)"; |
---|
| 296 | fprintf(stderr, "ERROR: %s\n", msg); |
---|
| 297 | lua_pop(luaState, 1); |
---|
| 298 | return error; |
---|
[8075] | 299 | } |
---|
[8085] | 300 | } |
---|
[8075] | 301 | |
---|
[8231] | 302 | |
---|
[8206] | 303 | bool Script::classIsRegistered(const std::string& type) |
---|
| 304 | { |
---|
[8207] | 305 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 306 | { |
---|
| 307 | if( (*it).type == type) |
---|
| 308 | { |
---|
| 309 | return true; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | return false; |
---|
| 313 | } |
---|
[8231] | 314 | |
---|
| 315 | |
---|
| 316 | |
---|
[8206] | 317 | bool Script::objectIsAdded(const std::string& name) |
---|
| 318 | { |
---|
[8207] | 319 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 320 | { |
---|
| 321 | if( (*it).name == name) |
---|
| 322 | { |
---|
| 323 | return true; |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | return false; |
---|
[8231] | 327 | |
---|
| 328 | |
---|
[8206] | 329 | } |
---|