[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); |
---|
[8193] | 25 | if (root != NULL) |
---|
| 26 | this->loadParams(root); |
---|
[8075] | 27 | } |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | Script::~Script() |
---|
| 31 | { |
---|
| 32 | lua_setgcthreshold(luaState, 0); // collected garbage |
---|
| 33 | lua_close(luaState); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | |
---|
[8193] | 37 | void Script::loadParams(const TiXmlElement* root) |
---|
| 38 | { |
---|
[8408] | 39 | //printf("Loading params for %p \n",this); |
---|
[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 | { |
---|
[8408] | 79 | printf("ERROR while loading file %s: \n",filename.c_str()); |
---|
| 80 | reportError(error); |
---|
[8075] | 81 | } |
---|
| 82 | |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
[8408] | 86 | printf("ERROR while loading file %s: \n",filename.c_str()); |
---|
[8075] | 87 | reportError(error); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | return false; |
---|
| 91 | } |
---|
| 92 | |
---|
[8231] | 93 | |
---|
[8193] | 94 | void Script::addObject(const std::string& className, const std::string& objectName) |
---|
| 95 | { |
---|
[8408] | 96 | //printf("Script %p: I am about to add %s of class %s\n",this,objectName.c_str(),className.c_str()); |
---|
| 97 | |
---|
[8202] | 98 | BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS); |
---|
[8408] | 99 | //printf("The script class for %s is at %p \n",className.c_str(),scriptClass); |
---|
[8207] | 100 | WorldObject tmpObj; |
---|
[8197] | 101 | if (scriptClass != NULL) |
---|
| 102 | { |
---|
[8231] | 103 | tmpObj.type = className; |
---|
[8206] | 104 | if( !classIsRegistered(className) ) |
---|
[8207] | 105 | { |
---|
[8202] | 106 | static_cast<ScriptClass*>(scriptClass)->registerClass(this); |
---|
[8207] | 107 | } |
---|
[8231] | 108 | |
---|
[8197] | 109 | BaseObject* object = ClassList::getObject(objectName, className); |
---|
[8408] | 110 | //printf("%s is at %p \n",objectName.c_str(),object); |
---|
[8206] | 111 | if (object != NULL && !objectIsAdded(objectName)) |
---|
[8197] | 112 | { |
---|
[8202] | 113 | static_cast<ScriptClass*>(scriptClass)->insertObject(this, object, false); |
---|
[8207] | 114 | tmpObj.name = objectName; |
---|
| 115 | registeredObjects.push_back(tmpObj); |
---|
[8197] | 116 | } |
---|
| 117 | } |
---|
[8193] | 118 | } |
---|
| 119 | |
---|
[8231] | 120 | |
---|
| 121 | |
---|
| 122 | |
---|
[8075] | 123 | bool Script::executeFile() |
---|
| 124 | { |
---|
[8231] | 125 | printf("WARNING: script.executeFile is not implemented yet"); |
---|
| 126 | /*if(currentFile.length() != 0) |
---|
[8075] | 127 | { |
---|
| 128 | int error = lua_pcall(luaState, 0, 0, 0); |
---|
| 129 | if( error == 0) |
---|
| 130 | return true; |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | reportError(error); |
---|
| 134 | return false; |
---|
| 135 | } |
---|
[8231] | 136 | }*/ |
---|
[8075] | 137 | return false; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | bool Script::selectFunction(std::string& functionName, int retCount) |
---|
| 141 | { |
---|
[8085] | 142 | if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected |
---|
[8075] | 143 | { |
---|
| 144 | lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); |
---|
| 145 | lua_gettable(luaState, LUA_GLOBALSINDEX); |
---|
| 146 | |
---|
| 147 | if(lua_isfunction( luaState , -1)) |
---|
| 148 | { |
---|
| 149 | returnCount = retCount; |
---|
| 150 | argumentCount = 0; |
---|
| 151 | currentFunction = functionName; |
---|
| 152 | return true; |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | return false; |
---|
| 156 | } |
---|
| 157 | else |
---|
[8085] | 158 | 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()); |
---|
| 159 | return false; |
---|
[8075] | 160 | } |
---|
| 161 | |
---|
| 162 | //return number of returned values |
---|
| 163 | bool Script::executeFunction() |
---|
| 164 | { |
---|
| 165 | if(currentFunction.length() != 0 ) |
---|
| 166 | { |
---|
| 167 | int error = lua_pcall(luaState, argumentCount, returnCount,0); |
---|
| 168 | if(error != 0) |
---|
| 169 | { |
---|
[8408] | 170 | printf("ERROR while executing function %s: \n",currentFunction.c_str()); |
---|
[8075] | 171 | reportError(error); |
---|
[8408] | 172 | //clean up |
---|
| 173 | currentFunction.assign(""); |
---|
| 174 | argumentCount = returnCount = 0; |
---|
[8075] | 175 | return false; |
---|
| 176 | } |
---|
| 177 | else |
---|
| 178 | { |
---|
| 179 | currentFunction.assign("");//a function gets unusable after beeing called for the first time |
---|
| 180 | argumentCount = 0; |
---|
| 181 | return true; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | else |
---|
| 185 | printf("Error: no function selected.\n"); |
---|
[8408] | 186 | |
---|
| 187 | return false; |
---|
[8075] | 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | //overload this function to add different types |
---|
| 192 | bool Script::pushParam(int param, std::string& toFunction) |
---|
| 193 | { |
---|
| 194 | if(currentFunction.compare(toFunction) == 0) |
---|
| 195 | { |
---|
| 196 | lua_pushnumber(luaState, (lua_Number) param); |
---|
| 197 | argumentCount++; |
---|
| 198 | return true; |
---|
| 199 | } |
---|
| 200 | else |
---|
| 201 | { |
---|
| 202 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 203 | return false; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | bool Script::pushParam(float param, std::string& toFunction) |
---|
| 210 | { |
---|
| 211 | if(currentFunction.compare(toFunction) == 0) |
---|
| 212 | { |
---|
| 213 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 214 | argumentCount++; |
---|
| 215 | return true; |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | { |
---|
| 219 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 220 | return false; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | bool Script::pushParam(double param, std::string& toFunction) |
---|
| 226 | { |
---|
| 227 | if(currentFunction.compare(toFunction) == 0) |
---|
| 228 | { |
---|
| 229 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 230 | argumentCount++; |
---|
| 231 | return true; |
---|
| 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
| 235 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
| 236 | return false; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | int Script::getReturnedInt() |
---|
| 242 | { |
---|
[8408] | 243 | int returnValue = 0; |
---|
[8075] | 244 | if(returnCount > 0) |
---|
| 245 | { |
---|
[8408] | 246 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
[8075] | 247 | { |
---|
[8408] | 248 | returnValue = (int)lua_tonumber(luaState, -1*returnCount); |
---|
| 249 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 250 | returnCount--; |
---|
[8408] | 251 | |
---|
[8075] | 252 | } |
---|
| 253 | } |
---|
| 254 | return returnValue; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | |
---|
| 258 | bool Script::getReturnedBool() |
---|
| 259 | { |
---|
[8408] | 260 | bool returnValue = false; |
---|
[8075] | 261 | if(returnCount > 0) |
---|
| 262 | { |
---|
[8408] | 263 | if(lua_isboolean(luaState, -1*returnCount)) |
---|
[8075] | 264 | { |
---|
[8408] | 265 | returnValue = (bool)lua_toboolean(luaState, -1*returnCount); |
---|
| 266 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 267 | returnCount--; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | return returnValue; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | float Script::getReturnedFloat() |
---|
| 274 | { |
---|
[8408] | 275 | float returnValue = 0.0f; |
---|
[8075] | 276 | if(returnCount > 0) |
---|
| 277 | { |
---|
[8408] | 278 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
[8075] | 279 | { |
---|
[8408] | 280 | returnValue = (float)lua_tonumber(luaState, -1*returnCount); |
---|
| 281 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 282 | returnCount--; |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | return returnValue; |
---|
| 286 | } |
---|
| 287 | |
---|
[8131] | 288 | void Script::getReturnedString(std::string& string) |
---|
| 289 | { |
---|
[8408] | 290 | const char* returnValue = ""; |
---|
[8131] | 291 | if(returnCount > 0) |
---|
| 292 | { |
---|
[8408] | 293 | if(lua_isstring(luaState, -1*returnCount)) |
---|
[8131] | 294 | { |
---|
[8408] | 295 | returnValue = lua_tostring(luaState, -1*returnCount); |
---|
| 296 | lua_remove(luaState,-1*returnCount); |
---|
[8131] | 297 | returnCount--; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | string.assign(returnValue); |
---|
| 301 | } |
---|
| 302 | |
---|
[8075] | 303 | int Script::reportError(int error) |
---|
| 304 | { |
---|
[8085] | 305 | if(error != 0) |
---|
| 306 | { |
---|
| 307 | const char *msg = lua_tostring(luaState, -1); |
---|
| 308 | if (msg == NULL) msg = "(error with no message)"; |
---|
| 309 | fprintf(stderr, "ERROR: %s\n", msg); |
---|
| 310 | lua_pop(luaState, 1); |
---|
[8408] | 311 | } |
---|
[8085] | 312 | return error; |
---|
[8075] | 313 | } |
---|
| 314 | |
---|
[8231] | 315 | |
---|
[8206] | 316 | bool Script::classIsRegistered(const std::string& type) |
---|
| 317 | { |
---|
[8207] | 318 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 319 | { |
---|
| 320 | if( (*it).type == type) |
---|
| 321 | { |
---|
| 322 | return true; |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | return false; |
---|
| 326 | } |
---|
[8231] | 327 | |
---|
| 328 | |
---|
| 329 | |
---|
[8206] | 330 | bool Script::objectIsAdded(const std::string& name) |
---|
| 331 | { |
---|
[8207] | 332 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 333 | { |
---|
| 334 | if( (*it).name == name) |
---|
| 335 | { |
---|
| 336 | return true; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | return false; |
---|
[8231] | 340 | |
---|
| 341 | |
---|
[8206] | 342 | } |
---|