[8711] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Silvan Nellen |
---|
| 13 | co-programmer: Benjamin Grauer |
---|
| 14 | */ |
---|
| 15 | |
---|
[8075] | 16 | #include "script.h" |
---|
[8202] | 17 | #include "script_class.h" |
---|
| 18 | #include "luaincl.h" |
---|
[9298] | 19 | #include "debug.h" |
---|
[8202] | 20 | |
---|
[9869] | 21 | #include "loading/resource_manager.h" |
---|
[8193] | 22 | #include "loading/load_param.h" |
---|
[8197] | 23 | #include "parser/tinyxml/tinyxml.h" |
---|
[8075] | 24 | |
---|
[9869] | 25 | ObjectListDefinition(Script); |
---|
| 26 | |
---|
[8075] | 27 | |
---|
[8193] | 28 | Script::Script(const TiXmlElement* root) |
---|
[8075] | 29 | { |
---|
[9869] | 30 | this->registerObject(this, Script::_objectList); |
---|
[8231] | 31 | |
---|
[8075] | 32 | returnCount = argumentCount = 0; |
---|
| 33 | |
---|
| 34 | luaState = lua_open(); |
---|
| 35 | |
---|
| 36 | luaopen_base(luaState); |
---|
| 37 | luaopen_table(luaState); |
---|
| 38 | luaopen_io(luaState); |
---|
| 39 | luaopen_string(luaState); |
---|
| 40 | luaopen_math(luaState); |
---|
| 41 | luaopen_debug(luaState); |
---|
[8193] | 42 | if (root != NULL) |
---|
| 43 | this->loadParams(root); |
---|
[8075] | 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
[9298] | 47 | Script::Script(const std::string& filename) |
---|
| 48 | { |
---|
[9869] | 49 | this->registerObject(this, Script::_objectList); |
---|
[9298] | 50 | |
---|
| 51 | returnCount = argumentCount = 0; |
---|
| 52 | |
---|
| 53 | luaState = lua_open(); |
---|
| 54 | |
---|
| 55 | luaopen_base(luaState); |
---|
| 56 | luaopen_table(luaState); |
---|
| 57 | luaopen_io(luaState); |
---|
| 58 | luaopen_string(luaState); |
---|
| 59 | luaopen_math(luaState); |
---|
| 60 | luaopen_debug(luaState); |
---|
[9869] | 61 | |
---|
[9298] | 62 | this->loadFile(filename); |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
[8075] | 66 | Script::~Script() |
---|
| 67 | { |
---|
| 68 | lua_setgcthreshold(luaState, 0); // collected garbage |
---|
| 69 | lua_close(luaState); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
[8193] | 73 | void Script::loadParams(const TiXmlElement* root) |
---|
| 74 | { |
---|
[9298] | 75 | //printf(("Loading params for %p \n",this); |
---|
[8199] | 76 | BaseObject::loadParams(root); |
---|
[8231] | 77 | |
---|
[9298] | 78 | LOAD_PARAM_START_CYCLE(root, object); |
---|
[8193] | 79 | { |
---|
[8197] | 80 | LoadParam_CYCLE(object, "object", this, Script, addObject) |
---|
[9298] | 81 | .describe("The name of an object that is needed by a script"); |
---|
[8193] | 82 | } |
---|
[9298] | 83 | LOAD_PARAM_END_CYCLE(object); |
---|
[8193] | 84 | |
---|
| 85 | |
---|
| 86 | LoadParam(root, "file", this, Script, loadFileNoRet) |
---|
| 87 | .describe("the fileName of the script, that should be loaded into this world") |
---|
| 88 | .defaultValues(""); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | bool Script::loadFile(const std::string& filename) |
---|
[8075] | 94 | { |
---|
[9869] | 95 | std::string filedest(Resources::ResourceManager::getInstance()->mainGlobalPath().name()); |
---|
| 96 | |
---|
[10321] | 97 | std::string directory = ""; |
---|
| 98 | std::string file = filename; |
---|
| 99 | |
---|
| 100 | unsigned int seperation = filename.find_last_of('/'); |
---|
| 101 | if (seperation != std::string::npos) |
---|
| 102 | { |
---|
| 103 | directory = filename.substr( 0, seperation+1 ); |
---|
| 104 | file = filename.substr( seperation+1 ); |
---|
| 105 | } |
---|
| 106 | filedest += "scripts/" + directory + file; |
---|
| 107 | |
---|
[9061] | 108 | this->addThisScript(); |
---|
| 109 | this->registerStandartClasses(); |
---|
[9869] | 110 | |
---|
[8075] | 111 | if(currentFile.length() != 0) |
---|
[8131] | 112 | { |
---|
[9298] | 113 | printf("SCRIPT %s : ERROR: Could not load %s because an other file is already loaded: %s\n",currentFile.c_str(),filename.c_str(), currentFile.c_str()); |
---|
[8085] | 114 | return false; |
---|
| 115 | } |
---|
[8075] | 116 | |
---|
[8711] | 117 | int error = luaL_loadfile (luaState, filedest.c_str()); |
---|
[8075] | 118 | |
---|
| 119 | if(error == 0) |
---|
| 120 | { |
---|
[9235] | 121 | currentFile = filename; |
---|
[8075] | 122 | error = lua_pcall(luaState, 0, 0, 0); |
---|
| 123 | |
---|
| 124 | if(error == 0) |
---|
| 125 | { |
---|
| 126 | return true; |
---|
| 127 | } |
---|
| 128 | else |
---|
| 129 | { |
---|
[9298] | 130 | printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str()); |
---|
[8408] | 131 | reportError(error); |
---|
[8075] | 132 | } |
---|
| 133 | |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | { |
---|
[9298] | 137 | printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str()); |
---|
[8075] | 138 | reportError(error); |
---|
| 139 | } |
---|
| 140 | |
---|
[10319] | 141 | printf("SCRIPT : ERROR: while loading file %s \n",filename.c_str()); |
---|
[8075] | 142 | return false; |
---|
| 143 | } |
---|
| 144 | |
---|
[8231] | 145 | |
---|
[8193] | 146 | void Script::addObject(const std::string& className, const std::string& objectName) |
---|
| 147 | { |
---|
[9298] | 148 | //printf(("Script %s: I am about to add %s of class %s\n",this->getName(),objectName.c_str(),className.c_str()); |
---|
[8408] | 149 | |
---|
[9869] | 150 | ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); |
---|
[9298] | 151 | // printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); |
---|
[8207] | 152 | WorldObject tmpObj; |
---|
[8197] | 153 | if (scriptClass != NULL) |
---|
| 154 | { |
---|
[8231] | 155 | tmpObj.type = className; |
---|
[8206] | 156 | if( !classIsRegistered(className) ) |
---|
[8207] | 157 | { |
---|
[9869] | 158 | scriptClass->registerClass(this); |
---|
[8207] | 159 | } |
---|
[8231] | 160 | |
---|
[9869] | 161 | BaseObject* object = ObjectListBase::getBaseObject(className, objectName); |
---|
[9298] | 162 | // printf(("%s is at %p \n",objectName.c_str(),object); |
---|
[8206] | 163 | if (object != NULL && !objectIsAdded(objectName)) |
---|
[8197] | 164 | { |
---|
[9869] | 165 | scriptClass->insertObject(this, object, false); |
---|
[8207] | 166 | tmpObj.name = objectName; |
---|
| 167 | registeredObjects.push_back(tmpObj); |
---|
[8197] | 168 | } |
---|
| 169 | } |
---|
[8193] | 170 | } |
---|
| 171 | |
---|
[8231] | 172 | |
---|
| 173 | |
---|
| 174 | |
---|
[8075] | 175 | bool Script::executeFile() |
---|
| 176 | { |
---|
[9869] | 177 | PRINTF(2)("script.executeFile is not implemented yet\n"); |
---|
[8231] | 178 | /*if(currentFile.length() != 0) |
---|
[8075] | 179 | { |
---|
| 180 | int error = lua_pcall(luaState, 0, 0, 0); |
---|
| 181 | if( error == 0) |
---|
| 182 | return true; |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | reportError(error); |
---|
| 186 | return false; |
---|
| 187 | } |
---|
[8231] | 188 | }*/ |
---|
[8075] | 189 | return false; |
---|
| 190 | } |
---|
| 191 | |
---|
[9061] | 192 | bool Script::selectFunction(const std::string& functionName, int retCount) |
---|
[8075] | 193 | { |
---|
[8085] | 194 | if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected |
---|
[8075] | 195 | { |
---|
| 196 | lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); |
---|
| 197 | lua_gettable(luaState, LUA_GLOBALSINDEX); |
---|
| 198 | |
---|
| 199 | if(lua_isfunction( luaState , -1)) |
---|
| 200 | { |
---|
| 201 | returnCount = retCount; |
---|
| 202 | argumentCount = 0; |
---|
| 203 | currentFunction = functionName; |
---|
| 204 | return true; |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | return false; |
---|
| 208 | } |
---|
| 209 | else |
---|
[9298] | 210 | printf("SCRIPT %s : ERROR: There is an other function active ( %s ) or there are unremoved return values on the stack. Please remove them first.\n",currentFile.c_str(),currentFunction.c_str()); |
---|
[8085] | 211 | return false; |
---|
[8075] | 212 | } |
---|
| 213 | |
---|
| 214 | //return number of returned values |
---|
| 215 | bool Script::executeFunction() |
---|
| 216 | { |
---|
| 217 | if(currentFunction.length() != 0 ) |
---|
| 218 | { |
---|
| 219 | int error = lua_pcall(luaState, argumentCount, returnCount,0); |
---|
| 220 | if(error != 0) |
---|
| 221 | { |
---|
[9869] | 222 | PRINTF(1)("Script '%s' : Failed to execute function '%s': \n",currentFile.c_str(),currentFunction.c_str()); |
---|
[8075] | 223 | reportError(error); |
---|
[8408] | 224 | //clean up |
---|
| 225 | currentFunction.assign(""); |
---|
| 226 | argumentCount = returnCount = 0; |
---|
[8075] | 227 | return false; |
---|
| 228 | } |
---|
| 229 | else |
---|
| 230 | { |
---|
| 231 | currentFunction.assign("");//a function gets unusable after beeing called for the first time |
---|
| 232 | argumentCount = 0; |
---|
| 233 | return true; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | else |
---|
[9869] | 237 | PRINTF(1)("SCRIPT '%s' : no function selected.\n",currentFile.c_str()); |
---|
[8408] | 238 | |
---|
| 239 | return false; |
---|
[8075] | 240 | } |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | //overload this function to add different types |
---|
| 244 | bool Script::pushParam(int param, std::string& toFunction) |
---|
| 245 | { |
---|
[8711] | 246 | if(currentFunction == toFunction) |
---|
[8075] | 247 | { |
---|
| 248 | lua_pushnumber(luaState, (lua_Number) param); |
---|
| 249 | argumentCount++; |
---|
[8711] | 250 | return true; |
---|
[8075] | 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
[9298] | 254 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
[8075] | 255 | return false; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | bool Script::pushParam(float param, std::string& toFunction) |
---|
| 262 | { |
---|
| 263 | if(currentFunction.compare(toFunction) == 0) |
---|
| 264 | { |
---|
| 265 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 266 | argumentCount++; |
---|
| 267 | return true; |
---|
| 268 | } |
---|
| 269 | else |
---|
| 270 | { |
---|
[9298] | 271 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
[8075] | 272 | return false; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | bool Script::pushParam(double param, std::string& toFunction) |
---|
| 278 | { |
---|
| 279 | if(currentFunction.compare(toFunction) == 0) |
---|
| 280 | { |
---|
| 281 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 282 | argumentCount++; |
---|
| 283 | return true; |
---|
| 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
[9298] | 287 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
[8075] | 288 | return false; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | int Script::getReturnedInt() |
---|
| 294 | { |
---|
[8408] | 295 | int returnValue = 0; |
---|
[8075] | 296 | if(returnCount > 0) |
---|
| 297 | { |
---|
[8408] | 298 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
[8075] | 299 | { |
---|
[8408] | 300 | returnValue = (int)lua_tonumber(luaState, -1*returnCount); |
---|
| 301 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 302 | returnCount--; |
---|
[9869] | 303 | |
---|
[8075] | 304 | } |
---|
| 305 | } |
---|
| 306 | return returnValue; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | bool Script::getReturnedBool() |
---|
| 311 | { |
---|
[8408] | 312 | bool returnValue = false; |
---|
[8075] | 313 | if(returnCount > 0) |
---|
| 314 | { |
---|
[8408] | 315 | if(lua_isboolean(luaState, -1*returnCount)) |
---|
[8075] | 316 | { |
---|
[8408] | 317 | returnValue = (bool)lua_toboolean(luaState, -1*returnCount); |
---|
| 318 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 319 | returnCount--; |
---|
| 320 | } |
---|
[8711] | 321 | else |
---|
[9298] | 322 | printf("SCRIPT %s : ERROR: Trying to retreive non bolean value\n",this->currentFile.c_str()); |
---|
[8075] | 323 | } |
---|
| 324 | return returnValue; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | float Script::getReturnedFloat() |
---|
| 328 | { |
---|
[8408] | 329 | float returnValue = 0.0f; |
---|
[8075] | 330 | if(returnCount > 0) |
---|
| 331 | { |
---|
[8408] | 332 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
[8075] | 333 | { |
---|
[8408] | 334 | returnValue = (float)lua_tonumber(luaState, -1*returnCount); |
---|
| 335 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 336 | returnCount--; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | return returnValue; |
---|
| 340 | } |
---|
| 341 | |
---|
[8131] | 342 | void Script::getReturnedString(std::string& string) |
---|
| 343 | { |
---|
[8408] | 344 | const char* returnValue = ""; |
---|
[8131] | 345 | if(returnCount > 0) |
---|
| 346 | { |
---|
[8408] | 347 | if(lua_isstring(luaState, -1*returnCount)) |
---|
[8131] | 348 | { |
---|
[8408] | 349 | returnValue = lua_tostring(luaState, -1*returnCount); |
---|
| 350 | lua_remove(luaState,-1*returnCount); |
---|
[8131] | 351 | returnCount--; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | string.assign(returnValue); |
---|
| 355 | } |
---|
| 356 | |
---|
[9003] | 357 | |
---|
| 358 | void Script::addThisScript() |
---|
| 359 | { |
---|
[9869] | 360 | ScriptClass* scriptClass = ScriptClass::objectList().getObject("Script"); |
---|
| 361 | |
---|
| 362 | if (scriptClass != NULL) |
---|
[9003] | 363 | { |
---|
[9869] | 364 | scriptClass->registerClass(this); |
---|
| 365 | scriptClass->insertObject(this, this,"thisscript", false); |
---|
[9003] | 366 | } |
---|
| 367 | } |
---|
| 368 | |
---|
[8075] | 369 | int Script::reportError(int error) |
---|
| 370 | { |
---|
[8085] | 371 | if(error != 0) |
---|
| 372 | { |
---|
| 373 | const char *msg = lua_tostring(luaState, -1); |
---|
[9298] | 374 | if (msg == NULL) msg = "(error with no message)\n"; |
---|
| 375 | printf("ERROR: %s\n", msg); |
---|
[8085] | 376 | lua_pop(luaState, 1); |
---|
[8408] | 377 | } |
---|
[8085] | 378 | return error; |
---|
[8075] | 379 | } |
---|
| 380 | |
---|
[8711] | 381 | bool Script::registerStandartClasses() |
---|
| 382 | { |
---|
| 383 | bool success = false; |
---|
[9869] | 384 | |
---|
[9235] | 385 | //this->registerClass(std::string("Vector")); |
---|
| 386 | this->registerClass("ScriptTrigger"); |
---|
| 387 | // this->registerClass("AttractorMine"); |
---|
[9061] | 388 | |
---|
[8711] | 389 | return success; |
---|
| 390 | } |
---|
[9869] | 391 | |
---|
| 392 | |
---|
[9235] | 393 | void Script::registerClass( const std::string& className) |
---|
[8711] | 394 | { |
---|
[9869] | 395 | ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); |
---|
[9298] | 396 | //printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); |
---|
[9869] | 397 | |
---|
[8711] | 398 | WorldObject tmpObj; |
---|
| 399 | if (scriptClass != NULL) |
---|
| 400 | { |
---|
| 401 | tmpObj.type = className; |
---|
| 402 | if( !classIsRegistered(className) ) |
---|
| 403 | { |
---|
| 404 | static_cast<ScriptClass*>(scriptClass)->registerClass(this); |
---|
| 405 | tmpObj.name = ""; |
---|
| 406 | registeredObjects.push_back(tmpObj); |
---|
[9235] | 407 | return; |
---|
[8711] | 408 | } |
---|
| 409 | } |
---|
[9869] | 410 | |
---|
[8711] | 411 | } |
---|
[8231] | 412 | |
---|
[8206] | 413 | bool Script::classIsRegistered(const std::string& type) |
---|
| 414 | { |
---|
[8207] | 415 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 416 | { |
---|
| 417 | if( (*it).type == type) |
---|
| 418 | { |
---|
| 419 | return true; |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | return false; |
---|
| 423 | } |
---|
[8231] | 424 | |
---|
| 425 | |
---|
| 426 | |
---|
[8206] | 427 | bool Script::objectIsAdded(const std::string& name) |
---|
| 428 | { |
---|
[8207] | 429 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 430 | { |
---|
| 431 | if( (*it).name == name) |
---|
| 432 | { |
---|
| 433 | return true; |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | return false; |
---|
[8231] | 437 | |
---|
| 438 | |
---|
[8206] | 439 | } |
---|