| @@ -0,0 +1,1420 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "luainterface.h" | |
| 24 | + | #include "luaobject.h" | |
| 25 | + | ||
| 26 | + | #include <framework/core/resourcemanager.h> | |
| 27 | + | #include <framework/util/stats.h> | |
| 28 | + | extern "C" { | |
| 29 | + | #if defined(_MSC_VER) || defined(ANDROID) | |
| 30 | + | #include <luajit/lua.h> | |
| 31 | + | #include <luajit/lualib.h> | |
| 32 | + | #include <luajit/lauxlib.h> | |
| 33 | + | #else | |
| 34 | + | #include <lua.h> | |
| 35 | + | #include <lualib.h> | |
| 36 | + | #include <lauxlib.h> | |
| 37 | + | #endif | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | #include "lbitlib.h" | |
| 41 | + | ||
| 42 | + | LuaInterface g_lua; | |
| 43 | + | ||
| 44 | + | LuaInterface::LuaInterface() | |
| 45 | + | { | |
| 46 | + | L = nullptr; | |
| 47 | + | m_cppCallbackDepth = 0; | |
| 48 | + | m_weakTableRef = 0; | |
| 49 | + | m_totalObjRefs = 0; | |
| 50 | + | m_totalFuncRefs = 0; | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | LuaInterface::~LuaInterface() | |
| 54 | + | { | |
| 55 | + | } | |
| 56 | + | ||
| 57 | + | void LuaInterface::init() | |
| 58 | + | { | |
| 59 | + | createLuaState(); | |
| 60 | + | ||
| 61 | + | // store global environment reference | |
| 62 | + | pushThread(); | |
| 63 | + | getEnv(); | |
| 64 | + | m_globalEnv = ref(); | |
| 65 | + | pop(); | |
| 66 | + | ||
| 67 | + | // check if demangle_class is working as expected | |
| 68 | + | VALIDATE(stdext::demangle_class<LuaObject>() == "LuaObject"); | |
| 69 | + | ||
| 70 | + | // register LuaObject, the base of all other objects | |
| 71 | + | registerClass<LuaObject>(); | |
| 72 | + | bindClassMemberFunction<LuaObject>("getUseCount", &LuaObject::getUseCount); | |
| 73 | + | bindClassMemberFunction<LuaObject>("getClassName", &LuaObject::getClassName); | |
| 74 | + | ||
| 75 | + | registerClassMemberFunction<LuaObject>("getFieldsTable", (LuaCppFunction) ([](LuaInterface* lua) -> int { | |
| 76 | + | LuaObjectPtr obj = g_lua.popObject(); | |
| 77 | + | obj->luaGetFieldsTable(); | |
| 78 | + | return 1; | |
| 79 | + | })); | |
| 80 | + | } | |
| 81 | + | ||
| 82 | + | void LuaInterface::terminate() | |
| 83 | + | { | |
| 84 | + | // close lua state, it will release all objects | |
| 85 | + | closeLuaState(); | |
| 86 | + | VALIDATE(m_totalFuncRefs == 0); | |
| 87 | + | VALIDATE(m_totalObjRefs == 0); | |
| 88 | + | } | |
| 89 | + | ||
| 90 | + | void LuaInterface::registerFunctions() | |
| 91 | + | { | |
| 92 | + | } | |
| 93 | + | ||
| 94 | + | void LuaInterface::registerSingletonClass(const std::string& className) | |
| 95 | + | { | |
| 96 | + | newTable(); | |
| 97 | + | pushValue(); | |
| 98 | + | setGlobal(className); | |
| 99 | + | pop(); | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | void LuaInterface::registerClass(const std::string& className, const std::string& baseClass) | |
| 103 | + | { | |
| 104 | + | // creates the class table (that it's also the class methods table) | |
| 105 | + | newTable(); | |
| 106 | + | pushValue(); | |
| 107 | + | setGlobal(className); | |
| 108 | + | const int klass = getTop(); | |
| 109 | + | ||
| 110 | + | // creates the class fieldmethods table | |
| 111 | + | newTable(); | |
| 112 | + | pushValue(); | |
| 113 | + | setGlobal(className + "_fieldmethods"); | |
| 114 | + | int klass_fieldmethods = getTop(); | |
| 115 | + | ||
| 116 | + | // creates the class metatable | |
| 117 | + | newTable(); | |
| 118 | + | pushValue(); | |
| 119 | + | setGlobal(className + "_mt"); | |
| 120 | + | int klass_mt = getTop(); | |
| 121 | + | ||
| 122 | + | // set metatable metamethods | |
| 123 | + | pushCppFunction(&LuaInterface::luaObjectGetEvent); | |
| 124 | + | setField("__index", klass_mt); | |
| 125 | + | pushCppFunction(&LuaInterface::luaObjectSetEvent); | |
| 126 | + | setField("__newindex", klass_mt); | |
| 127 | + | pushCppFunction(&LuaInterface::luaObjectEqualEvent); | |
| 128 | + | setField("__eq", klass_mt); | |
| 129 | + | pushCppFunction(&LuaInterface::luaObjectCollectEvent); | |
| 130 | + | setField("__gc", klass_mt); | |
| 131 | + | ||
| 132 | + | // set some fields that will be used later in metatable | |
| 133 | + | pushValue(klass); | |
| 134 | + | setField("methods", klass_mt); | |
| 135 | + | pushValue(klass_fieldmethods); | |
| 136 | + | setField("fieldmethods", klass_mt); | |
| 137 | + | ||
| 138 | + | // redirect methods and fieldmethods to the base class ones | |
| 139 | + | if(!className.empty() && className != "LuaObject") { | |
| 140 | + | // the following code is what create classes hierarchy for lua, by reproducing: | |
| 141 | + | // DerivedClass = { __index = BaseClass } | |
| 142 | + | // DerivedClass_fieldmethods = { __index = BaseClass_methods } | |
| 143 | + | ||
| 144 | + | // redirect the class methods to the base methods | |
| 145 | + | pushValue(klass); | |
| 146 | + | newTable(); | |
| 147 | + | getGlobal(baseClass); | |
| 148 | + | setField("__index"); | |
| 149 | + | setMetatable(); | |
| 150 | + | pop(); | |
| 151 | + | ||
| 152 | + | // redirect the class fieldmethods to the base fieldmethods | |
| 153 | + | pushValue(klass_fieldmethods); | |
| 154 | + | newTable(); | |
| 155 | + | getGlobal(baseClass + "_fieldmethods"); | |
| 156 | + | setField("__index"); | |
| 157 | + | setMetatable(); | |
| 158 | + | pop(); | |
| 159 | + | } | |
| 160 | + | ||
| 161 | + | // pops klass, klass_mt, klass_fieldmethods | |
| 162 | + | pop(3); | |
| 163 | + | } | |
| 164 | + | ||
| 165 | + | void LuaInterface::registerClassStaticFunction(const std::string& className, | |
| 166 | + | const std::string& functionName, | |
| 167 | + | const LuaCppFunction& function) | |
| 168 | + | { | |
| 169 | + | registerClassMemberFunction(className, functionName, function); | |
| 170 | + | } | |
| 171 | + | ||
| 172 | + | void LuaInterface::registerClassMemberFunction(const std::string& className, | |
| 173 | + | const std::string& functionName, | |
| 174 | + | const LuaCppFunction& function) | |
| 175 | + | { | |
| 176 | + | getGlobal(className); | |
| 177 | + | pushCppFunction(function); | |
| 178 | + | setField(functionName); | |
| 179 | + | pop(); | |
| 180 | + | } | |
| 181 | + | ||
| 182 | + | void LuaInterface::registerClassMemberField(const std::string& className, | |
| 183 | + | const std::string& field, | |
| 184 | + | const LuaCppFunction& getFunction, | |
| 185 | + | const LuaCppFunction& setFunction) | |
| 186 | + | { | |
| 187 | + | getGlobal(className + "_fieldmethods"); | |
| 188 | + | ||
| 189 | + | if(getFunction) { | |
| 190 | + | pushCppFunction(getFunction); | |
| 191 | + | setField(stdext::format("get_%s", field)); | |
| 192 | + | } | |
| 193 | + | ||
| 194 | + | if(setFunction) { | |
| 195 | + | pushCppFunction(setFunction); | |
| 196 | + | setField(stdext::format("set_%s", field)); | |
| 197 | + | } | |
| 198 | + | ||
| 199 | + | pop(); | |
| 200 | + | } | |
| 201 | + | ||
| 202 | + | void LuaInterface::registerGlobalFunction(const std::string& functionName, const LuaCppFunction& function) | |
| 203 | + | { | |
| 204 | + | pushCppFunction(function); | |
| 205 | + | setGlobal(functionName); | |
| 206 | + | } | |
| 207 | + | ||
| 208 | + | int LuaInterface::luaObjectGetEvent(LuaInterface* lua) | |
| 209 | + | { | |
| 210 | + | // stack: obj, key | |
| 211 | + | LuaObjectPtr obj = lua->toObject(-2); | |
| 212 | + | std::string key = lua->toString(-1); | |
| 213 | + | VALIDATE(obj); | |
| 214 | + | ||
| 215 | + | lua->remove(-1); // removes key | |
| 216 | + | ||
| 217 | + | // if a get method for this key exists, calls it | |
| 218 | + | lua->getMetatable(); // pushes obj metatable | |
| 219 | + | lua->getField("fieldmethods"); // push obj fieldmethods | |
| 220 | + | lua->remove(-2); // removes obj metatable | |
| 221 | + | lua->getField(std::string("get_") + key); // pushes get method | |
| 222 | + | lua->remove(-2); // remove obj fieldmethods | |
| 223 | + | if(!lua->isNil()) { // is the get method not nil? | |
| 224 | + | lua->insert(-2); // moves obj to the top | |
| 225 | + | lua->signalCall(1, 1); // calls get method, arguments: obj | |
| 226 | + | return 1; | |
| 227 | + | } | |
| 228 | + | lua->pop(); // pops the nil get method | |
| 229 | + | ||
| 230 | + | // if the field for this key exists, returns it | |
| 231 | + | obj->luaGetField(key); | |
| 232 | + | if(!lua->isNil()) { | |
| 233 | + | lua->remove(-2); // removes the obj | |
| 234 | + | // field value is on the stack | |
| 235 | + | return 1; | |
| 236 | + | } | |
| 237 | + | lua->pop(); // pops the nil field | |
| 238 | + | ||
| 239 | + | // pushes the method assigned by this key | |
| 240 | + | lua->getMetatable(); // pushes obj metatable | |
| 241 | + | lua->getField("methods"); // push obj methods | |
| 242 | + | lua->remove(-2); // removes obj metatable | |
| 243 | + | lua->getField(key); // pushes obj method | |
| 244 | + | lua->remove(-2); // remove obj methods | |
| 245 | + | lua->remove(-2); // removes obj | |
| 246 | + | ||
| 247 | + | // the result value is on the stack | |
| 248 | + | return 1; | |
| 249 | + | } | |
| 250 | + | ||
| 251 | + | int LuaInterface::luaObjectSetEvent(LuaInterface* lua) | |
| 252 | + | { | |
| 253 | + | // stack: obj, key, value | |
| 254 | + | LuaObjectPtr obj = lua->toObject(-3); | |
| 255 | + | std::string key = lua->toString(-2); | |
| 256 | + | VALIDATE(obj); | |
| 257 | + | ||
| 258 | + | lua->remove(-2); // removes key | |
| 259 | + | lua->insert(-2); // moves obj to the top | |
| 260 | + | ||
| 261 | + | // check if a set method for this field exists and call it | |
| 262 | + | lua->getMetatable(); // pushes obj metatable | |
| 263 | + | lua->getField("fieldmethods"); // push obj fieldmethods | |
| 264 | + | lua->remove(-2); // removes obj metatable | |
| 265 | + | lua->getField(std::string("set_") + key); // pushes set method | |
| 266 | + | lua->remove(-2); // remove obj fieldmethods | |
| 267 | + | if(!lua->isNil()) { // is the set method not nil? | |
| 268 | + | lua->insert(-3); // moves func to -3 | |
| 269 | + | lua->insert(-2); // moves obj to -2, and value to -1 | |
| 270 | + | lua->signalCall(2, 0); // calls set method, arguments: obj, value | |
| 271 | + | return 0; | |
| 272 | + | } | |
| 273 | + | lua->pop(); // pops the nil set method | |
| 274 | + | ||
| 275 | + | // no set method exists, then treats as an field and set it | |
| 276 | + | lua->pop(); // pops the object | |
| 277 | + | obj->luaSetField(key); // sets the obj field | |
| 278 | + | return 0; | |
| 279 | + | } | |
| 280 | + | ||
| 281 | + | int LuaInterface::luaObjectEqualEvent(LuaInterface* lua) | |
| 282 | + | { | |
| 283 | + | // stack: obj1, obj2 | |
| 284 | + | bool ret = false; | |
| 285 | + | ||
| 286 | + | // check if obj1 == obj2 | |
| 287 | + | if(lua->isUserdata(-1) && lua->isUserdata(-2)) { | |
| 288 | + | LuaObjectPtr* objPtr2 = static_cast<LuaObjectPtr*>(lua->popUserdata()); | |
| 289 | + | LuaObjectPtr* objPtr1 = static_cast<LuaObjectPtr*>(lua->popUserdata()); | |
| 290 | + | VALIDATE(objPtr1 && objPtr2); | |
| 291 | + | if(*objPtr1 == *objPtr2) | |
| 292 | + | ret = true; | |
| 293 | + | } else | |
| 294 | + | lua->pop(2); | |
| 295 | + | ||
| 296 | + | lua->pushBoolean(ret); | |
| 297 | + | return 1; | |
| 298 | + | } | |
| 299 | + | ||
| 300 | + | int LuaInterface::luaObjectCollectEvent(LuaInterface* lua) | |
| 301 | + | { | |
| 302 | + | // gets object pointer | |
| 303 | + | auto objPtr = static_cast<LuaObjectPtr*>(lua->popUserdata()); | |
| 304 | + | VALIDATE(objPtr); | |
| 305 | + | ||
| 306 | + | // resets pointer to decrease object use count | |
| 307 | + | objPtr->reset(); | |
| 308 | + | g_lua.m_totalObjRefs--; | |
| 309 | + | return 0; | |
| 310 | + | } | |
| 311 | + | ||
| 312 | + | ||
| 313 | + | /////////////////////////////////////////////////////////////////////////////// | |
| 314 | + | ||
| 315 | + | ||
| 316 | + | bool LuaInterface::safeRunScript(const std::string& fileName) | |
| 317 | + | { | |
| 318 | + | try { | |
| 319 | + | runScript(fileName); | |
| 320 | + | return true; | |
| 321 | + | } catch(stdext::exception& e) { | |
| 322 | + | g_logger.error(stdext::format("Failed to load script '%s': %s", fileName, e.what())); | |
| 323 | + | return false; | |
| 324 | + | } | |
| 325 | + | } | |
| 326 | + | ||
| 327 | + | void LuaInterface::runScript(const std::string& fileName) | |
| 328 | + | { | |
| 329 | + | loadScript(fileName); | |
| 330 | + | safeCall(0, 0); | |
| 331 | + | } | |
| 332 | + | ||
| 333 | + | void LuaInterface::runBuffer(const std::string& buffer, const std::string& source) | |
| 334 | + | { | |
| 335 | + | loadBuffer(buffer, source); | |
| 336 | + | safeCall(0, 0); | |
| 337 | + | } | |
| 338 | + | ||
| 339 | + | void LuaInterface::loadScript(const std::string& fileName) | |
| 340 | + | { | |
| 341 | + | // resolve file full path | |
| 342 | + | std::string filePath = fileName; | |
| 343 | + | if(!stdext::starts_with(fileName, "/")) | |
| 344 | + | filePath = getCurrentSourcePath() + "/" + filePath; | |
| 345 | + | ||
| 346 | + | filePath = g_resources.guessFilePath(filePath, "lua"); | |
| 347 | + | ||
| 348 | + | std::string buffer = g_resources.readFileContents(filePath); | |
| 349 | + | std::string source = std::string("@") + filePath; | |
| 350 | + | loadBuffer(buffer, source); | |
| 351 | + | } | |
| 352 | + | ||
| 353 | + | void LuaInterface::loadFunction(const std::string& buffer, const std::string& source) | |
| 354 | + | { | |
| 355 | + | if(buffer.empty()) { | |
| 356 | + | pushNil(); | |
| 357 | + | return; | |
| 358 | + | } | |
| 359 | + | ||
| 360 | + | std::string buf; | |
| 361 | + | if(stdext::starts_with(buffer, "function")) | |
| 362 | + | buf = stdext::format("__func = %s", buffer); | |
| 363 | + | else | |
| 364 | + | buf = stdext::format("__func = function(self)\n%s\nend", buffer); | |
| 365 | + | ||
| 366 | + | loadBuffer(buf, source); | |
| 367 | + | safeCall(); | |
| 368 | + | ||
| 369 | + | // get the function | |
| 370 | + | getGlobal("__func"); | |
| 371 | + | ||
| 372 | + | // reset the global __func | |
| 373 | + | pushNil(); | |
| 374 | + | setGlobal("__func"); | |
| 375 | + | } | |
| 376 | + | ||
| 377 | + | void LuaInterface::evaluateExpression(const std::string& expression, const std::string& source) | |
| 378 | + | { | |
| 379 | + | // evaluates the expression | |
| 380 | + | if(!expression.empty()) { | |
| 381 | + | std::string buffer = stdext::format("__exp = (%s)", expression); | |
| 382 | + | loadBuffer(buffer, source); | |
| 383 | + | safeCall(); | |
| 384 | + | ||
| 385 | + | // gets the expression result | |
| 386 | + | getGlobal("__exp"); | |
| 387 | + | ||
| 388 | + | // resets global __exp | |
| 389 | + | pushNil(); | |
| 390 | + | setGlobal("__exp"); | |
| 391 | + | } else | |
| 392 | + | pushNil(); | |
| 393 | + | } | |
| 394 | + | ||
| 395 | + | std::string LuaInterface::traceback(const std::string& errorMessage, int level) | |
| 396 | + | { | |
| 397 | + | // gets debug.traceback | |
| 398 | + | getGlobal("debug"); | |
| 399 | + | getField("traceback"); | |
| 400 | + | remove(-2); // remove debug | |
| 401 | + | ||
| 402 | + | // calls debug.traceback(errorMessage, level) | |
| 403 | + | pushString(errorMessage); | |
| 404 | + | pushInteger(level); | |
| 405 | + | call(2,1); | |
| 406 | + | ||
| 407 | + | // returns the traceback message | |
| 408 | + | return popString(); | |
| 409 | + | } | |
| 410 | + | ||
| 411 | + | void LuaInterface::throwError(const std::string& message) | |
| 412 | + | { | |
| 413 | + | if(isInCppCallback()) { | |
| 414 | + | pushString(message); | |
| 415 | + | error(); | |
| 416 | + | } else | |
| 417 | + | throw stdext::exception(message); | |
| 418 | + | } | |
| 419 | + | ||
| 420 | + | std::string LuaInterface::getCurrentSourcePath(int level) | |
| 421 | + | { | |
| 422 | + | std::string path; | |
| 423 | + | if(!L) | |
| 424 | + | return path; | |
| 425 | + | ||
| 426 | + | // check all stack functions for script source path | |
| 427 | + | while(true) { | |
| 428 | + | getStackFunction(level); // pushes stack function | |
| 429 | + | ||
| 430 | + | // only lua functions is wanted, because only them have a working directory | |
| 431 | + | if(isLuaFunction()) { | |
| 432 | + | path = functionSourcePath(); | |
| 433 | + | break; | |
| 434 | + | } else if(isNil()) { | |
| 435 | + | pop(); | |
| 436 | + | break; | |
| 437 | + | } else | |
| 438 | + | pop(); | |
| 439 | + | ||
| 440 | + | // next level | |
| 441 | + | level++; | |
| 442 | + | } | |
| 443 | + | ||
| 444 | + | return path; | |
| 445 | + | } | |
| 446 | + | ||
| 447 | + | std::string LuaInterface::getCurrentFunction(int level) | |
| 448 | + | { | |
| 449 | + | std::string path = "unknown"; | |
| 450 | + | if(!L) | |
| 451 | + | return path; | |
| 452 | + | ||
| 453 | + | // check all stack functions for script source path | |
| 454 | + | while(true) { | |
| 455 | + | getStackFunction(level); // pushes stack function | |
| 456 | + | ||
| 457 | + | // only lua functions is wanted, because only them have a working directory | |
| 458 | + | if(isLuaFunction()) { | |
| 459 | + | path = functionSource(); | |
| 460 | + | break; | |
| 461 | + | } else if(isNil()) { | |
| 462 | + | pop(); | |
| 463 | + | break; | |
| 464 | + | } else | |
| 465 | + | pop(); | |
| 466 | + | ||
| 467 | + | // next level | |
| 468 | + | level++; | |
| 469 | + | } | |
| 470 | + | ||
| 471 | + | return path; | |
| 472 | + | } | |
| 473 | + | ||
| 474 | + | int LuaInterface::safeCall(int numArgs, int numRets, const std::shared_ptr<std::string>& error) | |
| 475 | + | { | |
| 476 | + | VALIDATE(hasIndex(-numArgs-1)); | |
| 477 | + | ||
| 478 | + | // saves the current stack size for calculating the number of results later | |
| 479 | + | int previousStackSize = stackSize(); | |
| 480 | + | ||
| 481 | + | // pushes error function | |
| 482 | + | int errorFuncIndex = previousStackSize - numArgs; | |
| 483 | + | pushCFunction(&LuaInterface::luaErrorHandler); | |
| 484 | + | insert(errorFuncIndex); | |
| 485 | + | ||
| 486 | + | // calls the function in protected mode (means errors will be caught) | |
| 487 | + | int ret = pcall(numArgs, LUA_MULTRET, errorFuncIndex); | |
| 488 | + | ||
| 489 | + | remove(errorFuncIndex); // remove error func | |
| 490 | + | ||
| 491 | + | // if there was an error throw an exception | |
| 492 | + | if (ret != 0) { | |
| 493 | + | if (error) { | |
| 494 | + | error->assign(popString()); | |
| 495 | + | clearStack(); | |
| 496 | + | return 0; | |
| 497 | + | } else { | |
| 498 | + | std::string errorMsg = popString(); | |
| 499 | + | g_logger.error(stdext::format("Lua error: %s", errorMsg)); | |
| 500 | + | throw LuaException(errorMsg); | |
| 501 | + | } | |
| 502 | + | } | |
| 503 | + | int rets = (stackSize() + numArgs + 1) - previousStackSize; | |
| 504 | + | while(numRets != -1 && rets != numRets) { | |
| 505 | + | if(rets < numRets) { | |
| 506 | + | pushNil(); | |
| 507 | + | rets++; | |
| 508 | + | } else { | |
| 509 | + | pop(); | |
| 510 | + | rets--; | |
| 511 | + | } | |
| 512 | + | } | |
| 513 | + | ||
| 514 | + | // returns the number of results | |
| 515 | + | return rets; | |
| 516 | + | } | |
| 517 | + | ||
| 518 | + | int LuaInterface::signalCall(int numArgs, int numRets) | |
| 519 | + | { | |
| 520 | + | int rets = 0; | |
| 521 | + | int funcIndex = -numArgs-1; | |
| 522 | + | ||
| 523 | + | try { | |
| 524 | + | // must be a function | |
| 525 | + | if(isFunction(funcIndex)) { | |
| 526 | + | static std::shared_ptr<std::string> error = std::make_shared<std::string>(); | |
| 527 | + | rets = safeCall(numArgs, -1, error); | |
| 528 | + | if (!error->empty()) { | |
| 529 | + | g_logger.error(stdext::format("protected lua call failed: %s", *error)); | |
| 530 | + | error->clear(); | |
| 531 | + | return rets; | |
| 532 | + | } | |
| 533 | + | ||
| 534 | + | if(numRets != -1) { | |
| 535 | + | if(rets != numRets) | |
| 536 | + | throw LuaException("function call didn't return the expected number of results", 0); | |
| 537 | + | } | |
| 538 | + | } | |
| 539 | + | // can also calls table of functions | |
| 540 | + | else if(isTable(funcIndex)) { | |
| 541 | + | // loop through table values | |
| 542 | + | pushNil(); | |
| 543 | + | bool done = false; | |
| 544 | + | while(next(funcIndex-1)) { | |
| 545 | + | if(isFunction()) { | |
| 546 | + | // repush arguments | |
| 547 | + | for(int i=0;i<numArgs;++i) | |
| 548 | + | pushValue(-numArgs-2); | |
| 549 | + | ||
| 550 | + | static std::shared_ptr<std::string> error = std::make_shared<std::string>(); | |
| 551 | + | int rets = safeCall(numArgs, -1, error); | |
| 552 | + | if (!error->empty()) { | |
| 553 | + | g_logger.error(stdext::format("protected lua call failed: %s", *error)); | |
| 554 | + | error->clear(); | |
| 555 | + | return rets; | |
| 556 | + | } | |
| 557 | + | if(rets == 1) { | |
| 558 | + | done = popBoolean(); | |
| 559 | + | if(done) { | |
| 560 | + | pop(); | |
| 561 | + | break; | |
| 562 | + | } | |
| 563 | + | } else if(rets != 0) | |
| 564 | + | throw LuaException("function call didn't return the expected number of results", 0); | |
| 565 | + | } else { | |
| 566 | + | throw LuaException("attempt to call a non function", 0); | |
| 567 | + | } | |
| 568 | + | } | |
| 569 | + | pop(numArgs + 1); // pops the table of function and arguments | |
| 570 | + | ||
| 571 | + | if(numRets == 1 || numRets == -1) { | |
| 572 | + | rets = 1; | |
| 573 | + | pushBoolean(done); | |
| 574 | + | } | |
| 575 | + | } | |
| 576 | + | // nil values are ignored | |
| 577 | + | else if(isNil(funcIndex)) { | |
| 578 | + | pop(numArgs + 1); // pops the function and arguments | |
| 579 | + | } | |
| 580 | + | // if not nil, warn | |
| 581 | + | else { | |
| 582 | + | throw LuaException("attempt to call a non function value", 0); | |
| 583 | + | } | |
| 584 | + | } catch(stdext::exception& e) { | |
| 585 | + | g_logger.error(stdext::format("protected lua call failed: %s", e.what())); | |
| 586 | + | } | |
| 587 | + | ||
| 588 | + | // pushes nil values if needed | |
| 589 | + | while(numRets != -1 && rets < numRets) { | |
| 590 | + | pushNil(); | |
| 591 | + | rets++; | |
| 592 | + | } | |
| 593 | + | ||
| 594 | + | // returns the number of results on the stack | |
| 595 | + | return rets; | |
| 596 | + | } | |
| 597 | + | ||
| 598 | + | int LuaInterface::newSandboxEnv() | |
| 599 | + | { | |
| 600 | + | newTable(); // pushes the new environment table | |
| 601 | + | newTable(); // pushes the new environment metatable | |
| 602 | + | getRef(getGlobalEnvironment()); // pushes the global environment | |
| 603 | + | setField("__index"); // sets metatable __index to the global environment | |
| 604 | + | setMetatable(); // assigns environment metatable | |
| 605 | + | return ref(); // return a reference to the environment table | |
| 606 | + | } | |
| 607 | + | ||
| 608 | + | /////////////////////////////////////////////////////////////////////////////// | |
| 609 | + | // lua C functions | |
| 610 | + | ||
| 611 | + | int LuaInterface::luaScriptLoader(lua_State* L) | |
| 612 | + | { | |
| 613 | + | // loads the script as a function | |
| 614 | + | std::string fileName = g_lua.popString(); | |
| 615 | + | ||
| 616 | + | try { | |
| 617 | + | g_lua.loadScript(fileName); | |
| 618 | + | return 1; | |
| 619 | + | } catch(stdext::exception& e) { | |
| 620 | + | g_lua.pushString(std::string("\n\t") + e.what()); | |
| 621 | + | return 1; | |
| 622 | + | } | |
| 623 | + | } | |
| 624 | + | ||
| 625 | + | int LuaInterface::lua_dofile(lua_State* L) | |
| 626 | + | { | |
| 627 | + | std::string file = g_lua.popString(); | |
| 628 | + | ||
| 629 | + | try { | |
| 630 | + | g_lua.loadScript(file); | |
| 631 | + | g_lua.call(0, LUA_MULTRET); | |
| 632 | + | return g_lua.stackSize(); | |
| 633 | + | } catch(stdext::exception& e) { | |
| 634 | + | g_lua.pushString(e.what()); | |
| 635 | + | g_lua.error(); | |
| 636 | + | return 0; | |
| 637 | + | } | |
| 638 | + | } | |
| 639 | + | ||
| 640 | + | int LuaInterface::lua_dofiles(lua_State* L) | |
| 641 | + | { | |
| 642 | + | std::string contains = ""; | |
| 643 | + | if(g_lua.getTop() > 2) { | |
| 644 | + | contains = g_lua.popString(); | |
| 645 | + | } | |
| 646 | + | ||
| 647 | + | bool recursive = false; | |
| 648 | + | if(g_lua.getTop() > 1) { | |
| 649 | + | recursive = g_lua.popBoolean(); | |
| 650 | + | } | |
| 651 | + | ||
| 652 | + | std::string directory = g_lua.popString(); | |
| 653 | + | g_lua.loadFiles(directory, recursive, contains); | |
| 654 | + | ||
| 655 | + | return 0; | |
| 656 | + | } | |
| 657 | + | ||
| 658 | + | int LuaInterface::lua_loadfile(lua_State* L) | |
| 659 | + | { | |
| 660 | + | std::string fileName = g_lua.popString(); | |
| 661 | + | ||
| 662 | + | try { | |
| 663 | + | g_lua.loadScript(fileName); | |
| 664 | + | return 1; | |
| 665 | + | } catch(stdext::exception& e) { | |
| 666 | + | g_lua.pushNil(); | |
| 667 | + | g_lua.pushString(e.what()); | |
| 668 | + | g_lua.error(); | |
| 669 | + | return 2; | |
| 670 | + | } | |
| 671 | + | } | |
| 672 | + | ||
| 673 | + | int LuaInterface::luaErrorHandler(lua_State* L) | |
| 674 | + | { | |
| 675 | + | // pops the error message | |
| 676 | + | auto error = g_lua.popString(); | |
| 677 | + | ||
| 678 | + | // prevents repeated tracebacks | |
| 679 | + | if(error.find("stack traceback:") == std::string::npos) | |
| 680 | + | error = g_lua.traceback(error, 1); | |
| 681 | + | ||
| 682 | + | // pushes the new error message with traceback information | |
| 683 | + | g_lua.pushString(error); | |
| 684 | + | return 1; | |
| 685 | + | } | |
| 686 | + | ||
| 687 | + | int LuaInterface::luaCppFunctionCallback(lua_State* L) | |
| 688 | + | { | |
| 689 | + | // retrieves function pointer from userdata | |
| 690 | + | auto funcPtr = static_cast<LuaCppFunctionPtr*>(g_lua.popUpvalueUserdata()); | |
| 691 | + | VALIDATE(funcPtr); | |
| 692 | + | ||
| 693 | + | int numRets = 0; | |
| 694 | + | ||
| 695 | + | // enable only for tests, it has high cpu usage | |
| 696 | + | // AutoStat s(STATS_LUACALLBACK, g_lua.getSource(1)); | |
| 697 | + | ||
| 698 | + | // do the call | |
| 699 | + | try { | |
| 700 | + | g_lua.m_cppCallbackDepth++; | |
| 701 | + | numRets = (*(funcPtr->get()))(&g_lua); | |
| 702 | + | g_lua.m_cppCallbackDepth--; | |
| 703 | + | #ifndef NDEBUG | |
| 704 | + | if (numRets != g_lua.stackSize()) { | |
| 705 | + | throw stdext::exception(stdext::format("LuaInterface::luaCppFunctionCallback, numRets != g_lua.stackSize() (%i != %i)", numRets, g_lua.stackSize())); | |
| 706 | + | } | |
| 707 | + | #endif | |
| 708 | + | } catch(stdext::exception& e) { | |
| 709 | + | // cleanup stack | |
| 710 | + | while(g_lua.stackSize() > 0) | |
| 711 | + | g_lua.pop(); | |
| 712 | + | numRets = 0; | |
| 713 | + | g_lua.pushString(stdext::format("C++ call failed: %s", g_lua.traceback(e.what()))); | |
| 714 | + | g_lua.error(); | |
| 715 | + | } | |
| 716 | + | catch (...) { | |
| 717 | + | g_logger.fatal(stdext::format("Critical lua error!\nC++ call failed:\n%s|%s", g_lua.getCurrentFunction(), g_lua.traceback("fatal error"))); | |
| 718 | + | } | |
| 719 | + | ||
| 720 | + | return numRets; | |
| 721 | + | } | |
| 722 | + | ||
| 723 | + | int LuaInterface::luaCollectCppFunction(lua_State* L) | |
| 724 | + | { | |
| 725 | + | auto funcPtr = static_cast<LuaCppFunctionPtr*>(g_lua.popUserdata()); | |
| 726 | + | VALIDATE(funcPtr); | |
| 727 | + | funcPtr->reset(); | |
| 728 | + | g_lua.m_totalFuncRefs--; | |
| 729 | + | return 0; | |
| 730 | + | } | |
| 731 | + | ||
| 732 | + | ||
| 733 | + | /////////////////////////////////////////////////////////////////////////////// | |
| 734 | + | // from here all next functions are interfaces for the Lua API | |
| 735 | + | ||
| 736 | + | void LuaInterface::createLuaState() | |
| 737 | + | { | |
| 738 | + | // creates lua state | |
| 739 | + | L = luaL_newstate(); | |
| 740 | + | if(!L) | |
| 741 | + | g_logger.fatal("Unable to create lua state"); | |
| 742 | + | ||
| 743 | + | // load lua standard libraries | |
| 744 | + | luaL_openlibs(L); | |
| 745 | + | ||
| 746 | + | // load bit32 lib for bitwise operations | |
| 747 | + | luaopen_bit32(L); | |
| 748 | + | ||
| 749 | + | // creates weak table | |
| 750 | + | newTable(); | |
| 751 | + | newTable(); | |
| 752 | + | pushString("v"); | |
| 753 | + | setField("__mode"); | |
| 754 | + | setMetatable(); | |
| 755 | + | m_weakTableRef = ref(); | |
| 756 | + | ||
| 757 | + | // installs script loader | |
| 758 | + | getGlobal("package"); | |
| 759 | + | getField("loaders"); | |
| 760 | + | pushCFunction(&LuaInterface::luaScriptLoader); | |
| 761 | + | rawSeti(5); | |
| 762 | + | pop(2); | |
| 763 | + | ||
| 764 | + | // replace dofile | |
| 765 | + | pushCFunction(&LuaInterface::lua_dofile); | |
| 766 | + | setGlobal("dofile"); | |
| 767 | + | ||
| 768 | + | // dofiles | |
| 769 | + | pushCFunction(&LuaInterface::lua_dofiles); | |
| 770 | + | setGlobal("dofiles"); | |
| 771 | + | ||
| 772 | + | // replace loadfile | |
| 773 | + | pushCFunction(&LuaInterface::lua_loadfile); | |
| 774 | + | setGlobal("loadfile"); | |
| 775 | + | ||
| 776 | + | #ifdef FREE_VERSION | |
| 777 | + | pushCFunction(nullptr); | |
| 778 | + | setGlobal("io"); | |
| 779 | + | #endif | |
| 780 | + | } | |
| 781 | + | ||
| 782 | + | void LuaInterface::closeLuaState() | |
| 783 | + | { | |
| 784 | + | if(L) { | |
| 785 | + | // close lua, it also collects | |
| 786 | + | lua_close(L); | |
| 787 | + | L = NULL; | |
| 788 | + | } | |
| 789 | + | } | |
| 790 | + | ||
| 791 | + | void LuaInterface::collectGarbage() | |
| 792 | + | { | |
| 793 | + | // prevents recursive collects | |
| 794 | + | static bool collecting = false; | |
| 795 | + | if(!collecting) { | |
| 796 | + | collecting = true; | |
| 797 | + | ||
| 798 | + | // we must collect two times because __gc metamethod | |
| 799 | + | // is called on uservalues only the second time | |
| 800 | + | for(int i=0;i<2;++i) | |
| 801 | + | lua_gc(L, LUA_GCCOLLECT, 0); | |
| 802 | + | ||
| 803 | + | collecting = false; | |
| 804 | + | } | |
| 805 | + | } | |
| 806 | + | ||
| 807 | + | void LuaInterface::loadBuffer(const std::string& buffer, const std::string& source) | |
| 808 | + | { | |
| 809 | + | #ifdef FREE_VERSION | |
| 810 | + | for (auto& c : buffer) { | |
| 811 | + | if ((uint8_t)c < 8) { | |
| 812 | + | throw LuaException("Compiled lua files are not allowed in free version", 0); | |
| 813 | + | } | |
| 814 | + | } | |
| 815 | + | #endif | |
| 816 | + | // loads lua buffer | |
| 817 | + | int ret = luaL_loadbuffer(L, buffer.c_str(), buffer.length(), source.c_str()); | |
| 818 | + | if(ret != 0) | |
| 819 | + | throw LuaException(popString(), 0); | |
| 820 | + | } | |
| 821 | + | ||
| 822 | + | int dumpWriter(lua_State *L, const void* p, size_t sz, void* ud) { | |
| 823 | + | if (!ud) | |
| 824 | + | return -1; | |
| 825 | + | ||
| 826 | + | std::string* ret = (std::string*)ud; | |
| 827 | + | ret->append((const char*)p, sz); | |
| 828 | + | return 0; | |
| 829 | + | } | |
| 830 | + | ||
| 831 | + | std::string LuaInterface::generateByteCode(const std::string& buffer, std::string source) | |
| 832 | + | { | |
| 833 | + | stdext::replace_all(source, "\\", "/"); | |
| 834 | + | source = std::string("@/") + source; | |
| 835 | + | ||
| 836 | + | std::string ret; | |
| 837 | + | int status = luaL_loadbuffer(L, buffer.c_str(), buffer.length(), source.c_str()); | |
| 838 | + | if (status != 0) | |
| 839 | + | return ret; | |
| 840 | + | ||
| 841 | + | lua_dump(L, dumpWriter, &ret); | |
| 842 | + | clearStack(); | |
| 843 | + | return ret; | |
| 844 | + | } | |
| 845 | + | ||
| 846 | + | int LuaInterface::pcall(int numArgs, int numRets, int errorFuncIndex) | |
| 847 | + | { | |
| 848 | + | VALIDATE(hasIndex(-numArgs - 1)); | |
| 849 | + | return lua_pcall(L, numArgs, numRets, errorFuncIndex); | |
| 850 | + | } | |
| 851 | + | ||
| 852 | + | void LuaInterface::call(int numArgs, int numRets) | |
| 853 | + | { | |
| 854 | + | VALIDATE(hasIndex(-numArgs - 1)); | |
| 855 | + | lua_call(L, numArgs, numRets); | |
| 856 | + | } | |
| 857 | + | ||
| 858 | + | void LuaInterface::error() | |
| 859 | + | { | |
| 860 | + | VALIDATE(hasIndex(-1)); | |
| 861 | + | lua_error(L); | |
| 862 | + | } | |
| 863 | + | ||
| 864 | + | int LuaInterface::ref() | |
| 865 | + | { | |
| 866 | + | int ref = luaL_ref(L, LUA_REGISTRYINDEX); | |
| 867 | + | VALIDATE(ref != LUA_NOREF); | |
| 868 | + | VALIDATE(ref < 2147483647); | |
| 869 | + | return ref; | |
| 870 | + | } | |
| 871 | + | ||
| 872 | + | int LuaInterface::weakRef() | |
| 873 | + | { | |
| 874 | + | static int id = 0; | |
| 875 | + | ||
| 876 | + | // generates a new id | |
| 877 | + | ++id; | |
| 878 | + | if(id == 2147483647) | |
| 879 | + | id = 0; | |
| 880 | + | ||
| 881 | + | // gets weak table | |
| 882 | + | getRef(m_weakTableRef); | |
| 883 | + | insert(-2); | |
| 884 | + | ||
| 885 | + | // sets weak_table[id] = v | |
| 886 | + | rawSeti(id); | |
| 887 | + | ||
| 888 | + | // pops weak table | |
| 889 | + | pop(); | |
| 890 | + | ||
| 891 | + | return id; | |
| 892 | + | } | |
| 893 | + | ||
| 894 | + | void LuaInterface::unref(int ref) | |
| 895 | + | { | |
| 896 | + | if(ref >= 0 && L != NULL) | |
| 897 | + | luaL_unref(L, LUA_REGISTRYINDEX, ref); | |
| 898 | + | } | |
| 899 | + | ||
| 900 | + | const char* LuaInterface::typeName(int index) | |
| 901 | + | { | |
| 902 | + | VALIDATE(hasIndex(index)); | |
| 903 | + | int type = lua_type(L, index); | |
| 904 | + | return lua_typename(L, type); | |
| 905 | + | } | |
| 906 | + | ||
| 907 | + | std::string LuaInterface::functionSourcePath() | |
| 908 | + | { | |
| 909 | + | std::string path; | |
| 910 | + | ||
| 911 | + | // gets function source path | |
| 912 | + | lua_Debug ar; | |
| 913 | + | memset(&ar, 0, sizeof(ar)); | |
| 914 | + | lua_getinfo(L, ">Sn", &ar); | |
| 915 | + | if(ar.source) { | |
| 916 | + | // scripts coming from files has source beginning with '@' | |
| 917 | + | if(ar.source[0] == '@') { | |
| 918 | + | path = ar.source; | |
| 919 | + | path = path.substr(1, path.find_last_of("/") - 1); | |
| 920 | + | path = path.substr(0, path.find_last_of(":")); | |
| 921 | + | } | |
| 922 | + | } | |
| 923 | + | ||
| 924 | + | return path; | |
| 925 | + | } | |
| 926 | + | ||
| 927 | + | std::string LuaInterface::functionSource() | |
| 928 | + | { | |
| 929 | + | std::string path; | |
| 930 | + | ||
| 931 | + | // gets function source path | |
| 932 | + | lua_Debug ar; | |
| 933 | + | memset(&ar, 0, sizeof(ar)); | |
| 934 | + | lua_getinfo(L, ">Sn", &ar); | |
| 935 | + | if(ar.source) { | |
| 936 | + | // scripts coming from files has source beginning with '@' | |
| 937 | + | //if(ar.source[0] == '@') { | |
| 938 | + | path = ar.source; | |
| 939 | + | //path = path.substr(1, path.find_last_of("/") - 1); | |
| 940 | + | //path = path.substr(0, path.find_last_of(":")); | |
| 941 | + | //} | |
| 942 | + | } | |
| 943 | + | ||
| 944 | + | return path; | |
| 945 | + | } | |
| 946 | + | ||
| 947 | + | void LuaInterface::insert(int index) | |
| 948 | + | { | |
| 949 | + | VALIDATE(hasIndex(index)); | |
| 950 | + | lua_insert(L, index); | |
| 951 | + | } | |
| 952 | + | ||
| 953 | + | void LuaInterface::remove(int index) | |
| 954 | + | { | |
| 955 | + | VALIDATE(hasIndex(index)); | |
| 956 | + | lua_remove(L, index); | |
| 957 | + | } | |
| 958 | + | ||
| 959 | + | bool LuaInterface::next(int index) | |
| 960 | + | { | |
| 961 | + | VALIDATE(hasIndex(index)); | |
| 962 | + | return lua_next(L, index); | |
| 963 | + | } | |
| 964 | + | ||
| 965 | + | void LuaInterface::getStackFunction(int level) | |
| 966 | + | { | |
| 967 | + | lua_Debug ar; | |
| 968 | + | if(lua_getstack(L, level, &ar) == 1) | |
| 969 | + | lua_getinfo(L, "f", &ar); | |
| 970 | + | else | |
| 971 | + | pushNil(); | |
| 972 | + | } | |
| 973 | + | ||
| 974 | + | void LuaInterface::getRef(int ref) | |
| 975 | + | { | |
| 976 | + | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); | |
| 977 | + | } | |
| 978 | + | ||
| 979 | + | void LuaInterface::getWeakRef(int weakRef) | |
| 980 | + | { | |
| 981 | + | // pushes weak_table[weakRef] | |
| 982 | + | getRef(m_weakTableRef); | |
| 983 | + | rawGeti(weakRef); | |
| 984 | + | remove(-2); | |
| 985 | + | } | |
| 986 | + | ||
| 987 | + | void LuaInterface::setGlobalEnvironment(int env) | |
| 988 | + | { | |
| 989 | + | pushThread(); | |
| 990 | + | getRef(env); | |
| 991 | + | VALIDATE(isTable()); | |
| 992 | + | setEnv(); | |
| 993 | + | pop(); | |
| 994 | + | } | |
| 995 | + | ||
| 996 | + | void LuaInterface::setMetatable(int index) | |
| 997 | + | { | |
| 998 | + | VALIDATE(hasIndex(index)); | |
| 999 | + | lua_setmetatable(L, index); | |
| 1000 | + | } | |
| 1001 | + | ||
| 1002 | + | void LuaInterface::getMetatable(int index) | |
| 1003 | + | { | |
| 1004 | + | VALIDATE(hasIndex(index)); | |
| 1005 | + | lua_getmetatable(L, index); | |
| 1006 | + | } | |
| 1007 | + | ||
| 1008 | + | void LuaInterface::getField(const char* key, int index) | |
| 1009 | + | { | |
| 1010 | + | VALIDATE(hasIndex(index)); | |
| 1011 | + | VALIDATE(isUserdata(index) || isTable(index)); | |
| 1012 | + | lua_getfield(L, index, key); | |
| 1013 | + | } | |
| 1014 | + | ||
| 1015 | + | void LuaInterface::setField(const char* key, int index) | |
| 1016 | + | { | |
| 1017 | + | VALIDATE(hasIndex(index)); | |
| 1018 | + | VALIDATE(isUserdata(index) || isTable(index)); | |
| 1019 | + | lua_setfield(L, index, key); | |
| 1020 | + | } | |
| 1021 | + | ||
| 1022 | + | void LuaInterface::getEnv(int index) | |
| 1023 | + | { | |
| 1024 | + | VALIDATE(hasIndex(index)); | |
| 1025 | + | lua_getfenv(L, index); | |
| 1026 | + | } | |
| 1027 | + | ||
| 1028 | + | void LuaInterface::setEnv(int index) | |
| 1029 | + | { | |
| 1030 | + | VALIDATE(hasIndex(index)); | |
| 1031 | + | lua_setfenv(L, index); | |
| 1032 | + | } | |
| 1033 | + | ||
| 1034 | + | void LuaInterface::getTable(int index) | |
| 1035 | + | { | |
| 1036 | + | VALIDATE(hasIndex(index)); | |
| 1037 | + | lua_gettable(L, index); | |
| 1038 | + | } | |
| 1039 | + | ||
| 1040 | + | void LuaInterface::setTable(int index) | |
| 1041 | + | { | |
| 1042 | + | VALIDATE(hasIndex(index)); | |
| 1043 | + | lua_settable(L, index); | |
| 1044 | + | } | |
| 1045 | + | ||
| 1046 | + | void LuaInterface::clearTable(int index) | |
| 1047 | + | { | |
| 1048 | + | VALIDATE(hasIndex(index) && isTable(index)); | |
| 1049 | + | pushNil(); // table, nil | |
| 1050 | + | while(next(index-1)) { // table, key, value | |
| 1051 | + | pop(); // table, key | |
| 1052 | + | pushValue(); // table, key, key | |
| 1053 | + | if(next(index-2)) { // table, key, nextkey, value | |
| 1054 | + | pop(); // table, key, nextkey | |
| 1055 | + | insert(-2); // table, nextkey, key | |
| 1056 | + | pushNil(); // table, nextkey, key, nil | |
| 1057 | + | rawSet(index-3); // table, nextkey | |
| 1058 | + | } else { // table, key | |
| 1059 | + | pushNil(); // table, key, nil | |
| 1060 | + | rawSet(index-2); // table | |
| 1061 | + | break; | |
| 1062 | + | } | |
| 1063 | + | } | |
| 1064 | + | } | |
| 1065 | + | ||
| 1066 | + | void LuaInterface::getGlobal(const std::string& key) | |
| 1067 | + | { | |
| 1068 | + | lua_getglobal(L, key.c_str()); | |
| 1069 | + | } | |
| 1070 | + | ||
| 1071 | + | void LuaInterface::getGlobalField(const std::string& globalKey, const std::string& fieldKey) | |
| 1072 | + | { | |
| 1073 | + | getGlobal(globalKey); | |
| 1074 | + | if(!isNil()) { | |
| 1075 | + | VALIDATE(isTable() || isUserdata()); | |
| 1076 | + | getField(fieldKey); | |
| 1077 | + | remove(-2); | |
| 1078 | + | } | |
| 1079 | + | } | |
| 1080 | + | ||
| 1081 | + | void LuaInterface::setGlobal(const std::string& key) | |
| 1082 | + | { | |
| 1083 | + | VALIDATE(hasIndex(-1)); | |
| 1084 | + | lua_setglobal(L, key.c_str()); | |
| 1085 | + | } | |
| 1086 | + | ||
| 1087 | + | void LuaInterface::rawGet(int index) | |
| 1088 | + | { | |
| 1089 | + | VALIDATE(hasIndex(index)); | |
| 1090 | + | lua_rawget(L, index); | |
| 1091 | + | } | |
| 1092 | + | ||
| 1093 | + | void LuaInterface::rawGeti(int n, int index) | |
| 1094 | + | { | |
| 1095 | + | VALIDATE(hasIndex(index)); | |
| 1096 | + | lua_rawgeti(L, index, n); | |
| 1097 | + | } | |
| 1098 | + | ||
| 1099 | + | void LuaInterface::rawSet(int index) | |
| 1100 | + | { | |
| 1101 | + | VALIDATE(hasIndex(index)); | |
| 1102 | + | lua_rawset(L, index); | |
| 1103 | + | } | |
| 1104 | + | ||
| 1105 | + | void LuaInterface::rawSeti(int n, int index) | |
| 1106 | + | { | |
| 1107 | + | VALIDATE(hasIndex(index)); | |
| 1108 | + | lua_rawseti(L, index, n); | |
| 1109 | + | } | |
| 1110 | + | ||
| 1111 | + | void LuaInterface::newTable() | |
| 1112 | + | { | |
| 1113 | + | lua_newtable(L); | |
| 1114 | + | } | |
| 1115 | + | ||
| 1116 | + | void LuaInterface::createTable(int narr, int nrec) | |
| 1117 | + | { | |
| 1118 | + | lua_createtable(L, narr, nrec); | |
| 1119 | + | } | |
| 1120 | + | ||
| 1121 | + | void* LuaInterface::newUserdata(int size) | |
| 1122 | + | { | |
| 1123 | + | return lua_newuserdata(L, size); | |
| 1124 | + | } | |
| 1125 | + | ||
| 1126 | + | void LuaInterface::pop(int n) | |
| 1127 | + | { | |
| 1128 | + | if(n > 0) { | |
| 1129 | + | VALIDATE(hasIndex(-n)); | |
| 1130 | + | lua_pop(L, n); | |
| 1131 | + | } | |
| 1132 | + | } | |
| 1133 | + | ||
| 1134 | + | long LuaInterface::popInteger() | |
| 1135 | + | { | |
| 1136 | + | VALIDATE(hasIndex(-1)); | |
| 1137 | + | long v = toInteger(-1); | |
| 1138 | + | pop(); | |
| 1139 | + | return v; | |
| 1140 | + | } | |
| 1141 | + | ||
| 1142 | + | double LuaInterface::popNumber() | |
| 1143 | + | { | |
| 1144 | + | VALIDATE(hasIndex(-1)); | |
| 1145 | + | double v = toNumber(-1); | |
| 1146 | + | pop(); | |
| 1147 | + | return v; | |
| 1148 | + | } | |
| 1149 | + | ||
| 1150 | + | bool LuaInterface::popBoolean() | |
| 1151 | + | { | |
| 1152 | + | VALIDATE(hasIndex(-1)); | |
| 1153 | + | bool v = toBoolean(-1); | |
| 1154 | + | pop(); | |
| 1155 | + | return v; | |
| 1156 | + | } | |
| 1157 | + | ||
| 1158 | + | std::string LuaInterface::popString() | |
| 1159 | + | { | |
| 1160 | + | VALIDATE(hasIndex(-1)); | |
| 1161 | + | std::string v = toString(-1); | |
| 1162 | + | pop(); | |
| 1163 | + | return v; | |
| 1164 | + | } | |
| 1165 | + | ||
| 1166 | + | void* LuaInterface::popUserdata() | |
| 1167 | + | { | |
| 1168 | + | VALIDATE(hasIndex(-1)); | |
| 1169 | + | void* v = toUserdata(-1); | |
| 1170 | + | pop(); | |
| 1171 | + | return v; | |
| 1172 | + | } | |
| 1173 | + | ||
| 1174 | + | LuaObjectPtr LuaInterface::popObject() | |
| 1175 | + | { | |
| 1176 | + | VALIDATE(hasIndex(-1)); | |
| 1177 | + | LuaObjectPtr v = toObject(-1); | |
| 1178 | + | pop(); | |
| 1179 | + | return v; | |
| 1180 | + | } | |
| 1181 | + | ||
| 1182 | + | void* LuaInterface::popUpvalueUserdata() | |
| 1183 | + | { | |
| 1184 | + | return lua_touserdata(L, lua_upvalueindex(1)); | |
| 1185 | + | } | |
| 1186 | + | ||
| 1187 | + | void LuaInterface::pushNil() | |
| 1188 | + | { | |
| 1189 | + | lua_pushnil(L); | |
| 1190 | + | checkStack(); | |
| 1191 | + | } | |
| 1192 | + | ||
| 1193 | + | void LuaInterface::pushInteger(long v) | |
| 1194 | + | { | |
| 1195 | + | lua_pushinteger(L, v); | |
| 1196 | + | checkStack(); | |
| 1197 | + | } | |
| 1198 | + | ||
| 1199 | + | void LuaInterface::pushNumber(double v) | |
| 1200 | + | { | |
| 1201 | + | lua_pushnumber(L, v); | |
| 1202 | + | checkStack(); | |
| 1203 | + | } | |
| 1204 | + | ||
| 1205 | + | void LuaInterface::pushBoolean(bool v) | |
| 1206 | + | { | |
| 1207 | + | lua_pushboolean(L, v); | |
| 1208 | + | checkStack(); | |
| 1209 | + | } | |
| 1210 | + | ||
| 1211 | + | void LuaInterface::pushCString(const char* v) | |
| 1212 | + | { | |
| 1213 | + | if (!v) { | |
| 1214 | + | lua_pushnil(L); | |
| 1215 | + | } else { | |
| 1216 | + | lua_pushstring(L, v); | |
| 1217 | + | } | |
| 1218 | + | checkStack(); | |
| 1219 | + | } | |
| 1220 | + | ||
| 1221 | + | void LuaInterface::pushString(const std::string& v) | |
| 1222 | + | { | |
| 1223 | + | lua_pushlstring(L, v.c_str(), v.length()); | |
| 1224 | + | checkStack(); | |
| 1225 | + | } | |
| 1226 | + | ||
| 1227 | + | void LuaInterface::pushLightUserdata(void* p) | |
| 1228 | + | { | |
| 1229 | + | lua_pushlightuserdata(L, p); | |
| 1230 | + | checkStack(); | |
| 1231 | + | } | |
| 1232 | + | ||
| 1233 | + | void LuaInterface::pushThread() | |
| 1234 | + | { | |
| 1235 | + | lua_pushthread(L); | |
| 1236 | + | checkStack(); | |
| 1237 | + | } | |
| 1238 | + | ||
| 1239 | + | void LuaInterface::pushObject(const LuaObjectPtr& obj) | |
| 1240 | + | { | |
| 1241 | + | // fills a new userdata with a new LuaObjectPtr pointer | |
| 1242 | + | new(newUserdata(sizeof(LuaObjectPtr))) LuaObjectPtr(obj); | |
| 1243 | + | m_totalObjRefs++; | |
| 1244 | + | ||
| 1245 | + | obj->luaGetMetatable(); | |
| 1246 | + | if(isNil()) | |
| 1247 | + | g_logger.fatal(stdext::format("metatable for class '%s' not found, did you bind the C++ class?", obj->getClassName())); | |
| 1248 | + | setMetatable(); | |
| 1249 | + | } | |
| 1250 | + | ||
| 1251 | + | void LuaInterface::pushCFunction(LuaCFunction func, int n) | |
| 1252 | + | { | |
| 1253 | + | lua_pushcclosure(L, func, n); | |
| 1254 | + | checkStack(); | |
| 1255 | + | } | |
| 1256 | + | ||
| 1257 | + | void LuaInterface::pushCppFunction(const LuaCppFunction& func) | |
| 1258 | + | { | |
| 1259 | + | // create a pointer to func (this pointer will hold the function existence) | |
| 1260 | + | new(newUserdata(sizeof(LuaCppFunctionPtr))) LuaCppFunctionPtr(new LuaCppFunction(func)); | |
| 1261 | + | m_totalFuncRefs++; | |
| 1262 | + | ||
| 1263 | + | // sets the userdata __gc metamethod, needed to free the function pointer when it gets collected | |
| 1264 | + | newTable(); | |
| 1265 | + | pushCFunction(&LuaInterface::luaCollectCppFunction); | |
| 1266 | + | setField("__gc"); | |
| 1267 | + | setMetatable(); | |
| 1268 | + | ||
| 1269 | + | // actually pushes a C function callback that will call the cpp function | |
| 1270 | + | pushCFunction(&LuaInterface::luaCppFunctionCallback, 1); | |
| 1271 | + | } | |
| 1272 | + | ||
| 1273 | + | void LuaInterface::pushValue(int index) | |
| 1274 | + | { | |
| 1275 | + | VALIDATE(hasIndex(index)); | |
| 1276 | + | lua_pushvalue(L, index); | |
| 1277 | + | checkStack(); | |
| 1278 | + | } | |
| 1279 | + | ||
| 1280 | + | bool LuaInterface::isNil(int index) | |
| 1281 | + | { | |
| 1282 | + | VALIDATE(hasIndex(index)); | |
| 1283 | + | return lua_isnil(L, index); | |
| 1284 | + | } | |
| 1285 | + | ||
| 1286 | + | bool LuaInterface::isBoolean(int index) | |
| 1287 | + | { | |
| 1288 | + | VALIDATE(hasIndex(index)); | |
| 1289 | + | return lua_isboolean(L, index); | |
| 1290 | + | } | |
| 1291 | + | ||
| 1292 | + | bool LuaInterface::isNumber(int index) | |
| 1293 | + | { | |
| 1294 | + | VALIDATE(hasIndex(index)); | |
| 1295 | + | return lua_isnumber(L, index); | |
| 1296 | + | } | |
| 1297 | + | ||
| 1298 | + | bool LuaInterface::isString(int index) | |
| 1299 | + | { | |
| 1300 | + | VALIDATE(hasIndex(index)); | |
| 1301 | + | return lua_isstring(L, index); | |
| 1302 | + | } | |
| 1303 | + | ||
| 1304 | + | bool LuaInterface::isTable(int index) | |
| 1305 | + | { | |
| 1306 | + | VALIDATE(hasIndex(index)); | |
| 1307 | + | return lua_istable(L, index); | |
| 1308 | + | } | |
| 1309 | + | ||
| 1310 | + | bool LuaInterface::isFunction(int index) | |
| 1311 | + | { | |
| 1312 | + | VALIDATE(hasIndex(index)); | |
| 1313 | + | return lua_isfunction(L, index); | |
| 1314 | + | } | |
| 1315 | + | ||
| 1316 | + | bool LuaInterface::isCFunction(int index) | |
| 1317 | + | { | |
| 1318 | + | VALIDATE(hasIndex(index)); | |
| 1319 | + | return lua_iscfunction(L, index); | |
| 1320 | + | } | |
| 1321 | + | ||
| 1322 | + | bool LuaInterface::isUserdata(int index) | |
| 1323 | + | { | |
| 1324 | + | VALIDATE(hasIndex(index)); | |
| 1325 | + | return lua_isuserdata(L, index); | |
| 1326 | + | } | |
| 1327 | + | ||
| 1328 | + | bool LuaInterface::toBoolean(int index) | |
| 1329 | + | { | |
| 1330 | + | VALIDATE(hasIndex(index)); | |
| 1331 | + | return (bool)lua_toboolean(L, index); | |
| 1332 | + | } | |
| 1333 | + | ||
| 1334 | + | int LuaInterface::toInteger(int index) | |
| 1335 | + | { | |
| 1336 | + | VALIDATE(hasIndex(index)); | |
| 1337 | + | return lua_tointeger(L, index); | |
| 1338 | + | } | |
| 1339 | + | ||
| 1340 | + | double LuaInterface::toNumber(int index) | |
| 1341 | + | { | |
| 1342 | + | VALIDATE(hasIndex(index)); | |
| 1343 | + | return lua_tonumber(L, index); | |
| 1344 | + | } | |
| 1345 | + | ||
| 1346 | + | const char* LuaInterface::toCString(int index) | |
| 1347 | + | { | |
| 1348 | + | VALIDATE(hasIndex(index)); | |
| 1349 | + | return lua_tostring(L, index); | |
| 1350 | + | } | |
| 1351 | + | ||
| 1352 | + | std::string LuaInterface::toString(int index) | |
| 1353 | + | { | |
| 1354 | + | VALIDATE(hasIndex(index)); | |
| 1355 | + | std::string str; | |
| 1356 | + | size_t len; | |
| 1357 | + | const char *c_str = lua_tolstring(L, index, &len); | |
| 1358 | + | if(c_str && len > 0) | |
| 1359 | + | str.assign(c_str, len); | |
| 1360 | + | return str; | |
| 1361 | + | } | |
| 1362 | + | ||
| 1363 | + | void* LuaInterface::toUserdata(int index) | |
| 1364 | + | { | |
| 1365 | + | VALIDATE(hasIndex(index)); | |
| 1366 | + | return lua_touserdata(L, index); | |
| 1367 | + | } | |
| 1368 | + | ||
| 1369 | + | LuaObjectPtr LuaInterface::toObject(int index) | |
| 1370 | + | { | |
| 1371 | + | VALIDATE(hasIndex(index)); | |
| 1372 | + | if(isUserdata(index)) { | |
| 1373 | + | LuaObjectPtr* objRef = static_cast<LuaObjectPtr*>(toUserdata(index)); | |
| 1374 | + | if(objRef && *objRef) | |
| 1375 | + | return *objRef; | |
| 1376 | + | } | |
| 1377 | + | return nullptr; | |
| 1378 | + | } | |
| 1379 | + | ||
| 1380 | + | int LuaInterface::getTop() | |
| 1381 | + | { | |
| 1382 | + | return lua_gettop(L); | |
| 1383 | + | } | |
| 1384 | + | ||
| 1385 | + | std::string LuaInterface::getSource(int level) | |
| 1386 | + | { | |
| 1387 | + | lua_Debug ar; | |
| 1388 | + | ar.short_src[0] = 0; | |
| 1389 | + | ar.currentline = 0; | |
| 1390 | + | if (lua_getstack(L, level, &ar) == 1) { | |
| 1391 | + | lua_getinfo(L, "Sl", &ar); | |
| 1392 | + | } | |
| 1393 | + | return std::string(ar.short_src) + ":" + std::to_string(ar.currentline); | |
| 1394 | + | } | |
| 1395 | + | ||
| 1396 | + | void LuaInterface::loadFiles(std::string directory, bool recursive, std::string contains) | |
| 1397 | + | { | |
| 1398 | + | for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) { | |
| 1399 | + | std::string fullPath = directory + "/" + fileName; | |
| 1400 | + | ||
| 1401 | + | if(recursive && g_resources.directoryExists(fullPath)) { | |
| 1402 | + | loadFiles(fullPath, true, contains); | |
| 1403 | + | continue; | |
| 1404 | + | } | |
| 1405 | + | ||
| 1406 | + | if(!g_resources.isFileType(fileName, "lua")) | |
| 1407 | + | continue; | |
| 1408 | + | ||
| 1409 | + | if(!contains.empty() && fileName.find(contains) == std::string::npos) | |
| 1410 | + | continue; | |
| 1411 | + | ||
| 1412 | + | try { | |
| 1413 | + | g_lua.loadScript(fullPath); | |
| 1414 | + | g_lua.call(0, 0); | |
| 1415 | + | } catch(stdext::exception& e) { | |
| 1416 | + | g_lua.pushString(e.what()); | |
| 1417 | + | g_lua.error(); | |
| 1418 | + | } | |
| 1419 | + | } | |
| 1420 | + | } |
| @@ -0,0 +1,495 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef LUAINTERFACE_H | |
| 24 | + | #define LUAINTERFACE_H | |
| 25 | + | ||
| 26 | + | #include "declarations.h" | |
| 27 | + | ||
| 28 | + | #include <framework/util/stats.h> | |
| 29 | + | ||
| 30 | + | ||
| 31 | + | struct lua_State; | |
| 32 | + | typedef int (*LuaCFunction) (lua_State *L); | |
| 33 | + | ||
| 34 | + | /// Class that manages LUA stuff | |
| 35 | + | class LuaInterface | |
| 36 | + | { | |
| 37 | + | public: | |
| 38 | + | LuaInterface(); | |
| 39 | + | ~LuaInterface(); | |
| 40 | + | ||
| 41 | + | void init(); | |
| 42 | + | void terminate(); | |
| 43 | + | ||
| 44 | + | /// Register core script functions | |
| 45 | + | void registerFunctions(); | |
| 46 | + | ||
| 47 | + | // functions that will register all script stuff in lua global environment | |
| 48 | + | void registerSingletonClass(const std::string& className); | |
| 49 | + | void registerClass(const std::string& className, const std::string& baseClass = "LuaObject"); | |
| 50 | + | ||
| 51 | + | void registerClassStaticFunction(const std::string& className, | |
| 52 | + | const std::string& functionName, | |
| 53 | + | const LuaCppFunction& function); | |
| 54 | + | ||
| 55 | + | void registerClassMemberFunction(const std::string& className, | |
| 56 | + | const std::string& functionName, | |
| 57 | + | const LuaCppFunction& function); | |
| 58 | + | ||
| 59 | + | void registerClassMemberField(const std::string& className, | |
| 60 | + | const std::string& field, | |
| 61 | + | const LuaCppFunction& getFunction, | |
| 62 | + | const LuaCppFunction& setFunction); | |
| 63 | + | ||
| 64 | + | void registerGlobalFunction(const std::string& functionName, | |
| 65 | + | const LuaCppFunction& function); | |
| 66 | + | ||
| 67 | + | // register shortcuts using templates | |
| 68 | + | template<class C, class B = LuaObject> | |
| 69 | + | void registerClass() { | |
| 70 | + | registerClass(stdext::demangle_class<C>(), stdext::demangle_class<B>()); | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | template<class C> | |
| 74 | + | void registerClassStaticFunction(const std::string& functionName, const LuaCppFunction& function) { | |
| 75 | + | registerClassStaticFunction(stdext::demangle_class<C>(), functionName, function); | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | template<class C> | |
| 79 | + | void registerClassMemberFunction(const std::string& functionName, const LuaCppFunction& function) { | |
| 80 | + | registerClassMemberFunction(stdext::demangle_class<C>(), functionName, function); | |
| 81 | + | } | |
| 82 | + | ||
| 83 | + | template<class C> | |
| 84 | + | void registerClassMemberField(const std::string& field, | |
| 85 | + | const LuaCppFunction& getFunction, | |
| 86 | + | const LuaCppFunction& setFunction) { | |
| 87 | + | registerClassMemberField(stdext::demangle_class<C>(), field, getFunction, setFunction); | |
| 88 | + | } | |
| 89 | + | ||
| 90 | + | // methods for binding functions | |
| 91 | + | template<class C, typename F> | |
| 92 | + | void bindSingletonFunction(const std::string& functionName, F C::*function, C *instance); | |
| 93 | + | template<class C, typename F> | |
| 94 | + | void bindSingletonFunction(const std::string& className, const std::string& functionName, F C::* function, C* instance); | |
| 95 | + | template<typename F> | |
| 96 | + | void bindSingletonFunction(const std::string& className, const std::string& functionName, const F& function); | |
| 97 | + | ||
| 98 | + | template<class C, typename F> | |
| 99 | + | void bindClassStaticFunction(const std::string& functionName, const F& function); | |
| 100 | + | template<typename F> | |
| 101 | + | void bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function); | |
| 102 | + | ||
| 103 | + | template<class C, typename F, class FC> | |
| 104 | + | void bindClassMemberFunction(const std::string& functionName, F FC::*function); | |
| 105 | + | template<class C, typename F, class FC> | |
| 106 | + | void bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function); | |
| 107 | + | ||
| 108 | + | template<class C, typename F1, typename F2, class FC> | |
| 109 | + | void bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction); | |
| 110 | + | template<class C, typename F1, typename F2, class FC> | |
| 111 | + | void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction); | |
| 112 | + | ||
| 113 | + | template<class C, typename F, class FC> | |
| 114 | + | void bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction); | |
| 115 | + | template<class C, typename F, class FC> | |
| 116 | + | void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction); | |
| 117 | + | ||
| 118 | + | template<class C, typename F, class FC> | |
| 119 | + | void bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction); | |
| 120 | + | template<class C, typename F, class FC> | |
| 121 | + | void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction); | |
| 122 | + | ||
| 123 | + | template<typename F> | |
| 124 | + | void bindGlobalFunction(const std::string& functionName, const F& function); | |
| 125 | + | ||
| 126 | + | private: | |
| 127 | + | /// Metamethod that will retrieve fields values (that include functions) from the object when using '.' or ':' | |
| 128 | + | static int luaObjectGetEvent(LuaInterface* lua); | |
| 129 | + | /// Metamethod that is called when setting a field of the object by using the keyword '=' | |
| 130 | + | static int luaObjectSetEvent(LuaInterface* lua); | |
| 131 | + | /// Metamethod that will check equality of objects by using the keyword '==' | |
| 132 | + | static int luaObjectEqualEvent(LuaInterface* lua); | |
| 133 | + | /// Metamethod that is called every two lua garbage collections | |
| 134 | + | /// for any LuaObject that have no references left in lua environment | |
| 135 | + | /// anymore, thus this creates the possibility of holding an object | |
| 136 | + | /// existence by lua until it got no references left | |
| 137 | + | static int luaObjectCollectEvent(LuaInterface* lua); | |
| 138 | + | ||
| 139 | + | public: | |
| 140 | + | /// Loads and runs a script, any errors are printed to stdout and returns false | |
| 141 | + | bool safeRunScript(const std::string& fileName); | |
| 142 | + | ||
| 143 | + | /// Loads and runs a script | |
| 144 | + | /// @exception LuaException is thrown on any lua error | |
| 145 | + | void runScript(const std::string& fileName); | |
| 146 | + | ||
| 147 | + | /// Loads and runs the script from buffer | |
| 148 | + | /// @exception LuaException is thrown on any lua error | |
| 149 | + | void runBuffer(const std::string& buffer, const std::string& source); | |
| 150 | + | ||
| 151 | + | /// Loads a script file and pushes it's main function onto stack, | |
| 152 | + | /// @exception LuaException is thrown on any lua error | |
| 153 | + | void loadScript(const std::string& fileName); | |
| 154 | + | ||
| 155 | + | /// Loads a function from buffer and pushes it onto stack, | |
| 156 | + | /// @exception LuaException is thrown on any lua error | |
| 157 | + | void loadFunction(const std::string& buffer, const std::string& source = "lua function buffer"); | |
| 158 | + | ||
| 159 | + | /// Evaluates a lua expression and pushes the result value onto the stack | |
| 160 | + | /// @exception LuaException is thrown on any lua error | |
| 161 | + | void evaluateExpression(const std::string& expression, const std::string& source = "lua expression"); | |
| 162 | + | ||
| 163 | + | /// Generates a traceback message for the current call stack | |
| 164 | + | /// @param errorMessage is an additional error message | |
| 165 | + | /// @param level is the level of the traceback, 0 means trace from calling function | |
| 166 | + | /// @return the generated traceback message | |
| 167 | + | std::string traceback(const std::string& errorMessage = "", int level = 0); | |
| 168 | + | ||
| 169 | + | /// Throw a lua error if inside a lua call or generates an C++ stdext::exception | |
| 170 | + | /// @param message is the error message wich will be displayed before the error traceback | |
| 171 | + | /// @exception stdext::exception is thrown with the error message if the error is not captured by lua | |
| 172 | + | void throwError(const std::string& message); | |
| 173 | + | ||
| 174 | + | /// Searches for the source of the current running function | |
| 175 | + | std::string getCurrentSourcePath(int level = 0); | |
| 176 | + | ||
| 177 | + | /// gets current function name | |
| 178 | + | std::string getCurrentFunction(int level = 0); | |
| 179 | + | ||
| 180 | + | /// @brief Calls a function | |
| 181 | + | /// The function and arguments must be on top of the stack in order, | |
| 182 | + | /// results are pushed onto the stack. | |
| 183 | + | /// @exception LuaException is thrown on any lua error | |
| 184 | + | /// @return number of results | |
| 185 | + | int safeCall(int numArgs = 0, int numRets = -1, const std::shared_ptr<std::string>& error = nullptr); | |
| 186 | + | ||
| 187 | + | /// Same as safeCall but catches exceptions and can also calls a table of functions, | |
| 188 | + | /// if any error occurs it will be reported to stdout and returns 0 results | |
| 189 | + | /// @param requestedResults is the number of results requested to pushes onto the stack, | |
| 190 | + | /// if supplied, the call will always pushes that number of results, even if it fails | |
| 191 | + | int signalCall(int numArgs = 0, int numRets = -1); | |
| 192 | + | ||
| 193 | + | /// @brief Creates a new environment table | |
| 194 | + | /// The new environment table is redirected to the global environment (aka _G), | |
| 195 | + | /// this allows to access global variables from _G in the new environment and | |
| 196 | + | /// prevents new variables in this new environment to be set on the global environment | |
| 197 | + | int newSandboxEnv(); | |
| 198 | + | ||
| 199 | + | template<typename... T> | |
| 200 | + | int luaCallGlobalField(const std::string& global, const std::string& field, const T&... args); | |
| 201 | + | ||
| 202 | + | template<typename... T> | |
| 203 | + | void callGlobalField(const std::string& global, const std::string& field, const T&... args); | |
| 204 | + | ||
| 205 | + | template<typename R, typename... T> | |
| 206 | + | R callGlobalField(const std::string& global, const std::string& field, const T&... args); | |
| 207 | + | ||
| 208 | + | bool isInCppCallback() { return m_cppCallbackDepth != 0; } | |
| 209 | + | ||
| 210 | + | private: | |
| 211 | + | /// Load scripts requested by lua 'require' | |
| 212 | + | static int luaScriptLoader(lua_State* L); | |
| 213 | + | /// Run scripts requested by lua 'dofile' | |
| 214 | + | static int lua_dofile(lua_State* L); | |
| 215 | + | /// Run scripts requested by lua 'dofiles' | |
| 216 | + | static int lua_dofiles(lua_State* L); | |
| 217 | + | /// Run scripts requested by lua 'dofiles' | |
| 218 | + | static int lua_loadfile(lua_State* L); | |
| 219 | + | /// Handle lua errors from safeCall | |
| 220 | + | static int luaErrorHandler(lua_State* L); | |
| 221 | + | /// Handle bound cpp functions callbacks | |
| 222 | + | static int luaCppFunctionCallback(lua_State* L); | |
| 223 | + | /// Collect bound cpp function pointers | |
| 224 | + | static int luaCollectCppFunction(lua_State* L); | |
| 225 | + | ||
| 226 | + | public: | |
| 227 | + | void createLuaState(); | |
| 228 | + | void closeLuaState(); | |
| 229 | + | ||
| 230 | + | void collectGarbage(); | |
| 231 | + | ||
| 232 | + | void loadBuffer(const std::string& buffer, const std::string& source); | |
| 233 | + | ||
| 234 | + | std::string generateByteCode(const std::string & buffer, std::string source); | |
| 235 | + | ||
| 236 | + | int pcall(int numArgs = 0, int numRets = 0, int errorFuncIndex = 0); | |
| 237 | + | void call(int numArgs = 0, int numRets = 0); | |
| 238 | + | void error(); | |
| 239 | + | ||
| 240 | + | int ref(); | |
| 241 | + | int weakRef(); | |
| 242 | + | void unref(int ref); | |
| 243 | + | void useValue() { pushValue(); ref(); } | |
| 244 | + | ||
| 245 | + | const char* typeName(int index = -1); | |
| 246 | + | std::string functionSourcePath(); | |
| 247 | + | std::string functionSource(); | |
| 248 | + | ||
| 249 | + | void insert(int index); | |
| 250 | + | void remove(int index); | |
| 251 | + | bool next(int index = -2); | |
| 252 | + | ||
| 253 | + | void checkStack() { VALIDATE(getTop() <= 20); } | |
| 254 | + | void getStackFunction(int level = 0); | |
| 255 | + | ||
| 256 | + | void getRef(int ref); | |
| 257 | + | void getWeakRef(int weakRef); | |
| 258 | + | ||
| 259 | + | int getGlobalEnvironment() { return m_globalEnv; } | |
| 260 | + | void setGlobalEnvironment(int env); | |
| 261 | + | void resetGlobalEnvironment() { setGlobalEnvironment(m_globalEnv); } | |
| 262 | + | ||
| 263 | + | void setMetatable(int index = -2); | |
| 264 | + | void getMetatable(int index = -1); | |
| 265 | + | ||
| 266 | + | void getField(const char* key, int index = -1); | |
| 267 | + | void getField(const std::string& key, int index = -1) { return getField(key.c_str(), index); } | |
| 268 | + | void setField(const char* key, int index = -2); | |
| 269 | + | void setField(const std::string& key, int index = -2) { return setField(key.c_str(), index); } | |
| 270 | + | ||
| 271 | + | void getTable(int index = -2); | |
| 272 | + | void setTable(int index = -3); | |
| 273 | + | void clearTable(int index = -1); | |
| 274 | + | ||
| 275 | + | void getEnv(int index = -1); | |
| 276 | + | void setEnv(int index = -2); | |
| 277 | + | ||
| 278 | + | void getGlobal(const std::string& key); | |
| 279 | + | void getGlobalField(const std::string& globalKey, const std::string& fieldKey); | |
| 280 | + | void setGlobal(const std::string& key); | |
| 281 | + | ||
| 282 | + | void rawGet(int index = -1); | |
| 283 | + | void rawGeti(int n, int index = -1); | |
| 284 | + | void rawSet(int index = -3); | |
| 285 | + | void rawSeti(int n, int index = -2); | |
| 286 | + | ||
| 287 | + | void newTable(); | |
| 288 | + | void createTable(int narr, int nrec); | |
| 289 | + | void* newUserdata(int size); | |
| 290 | + | ||
| 291 | + | void pop(int n = 1); | |
| 292 | + | long popInteger(); | |
| 293 | + | double popNumber(); | |
| 294 | + | bool popBoolean(); | |
| 295 | + | std::string popString(); | |
| 296 | + | void* popUserdata(); | |
| 297 | + | void* popUpvalueUserdata(); | |
| 298 | + | LuaObjectPtr popObject(); | |
| 299 | + | ||
| 300 | + | void pushNil(); | |
| 301 | + | void pushInteger(long v); | |
| 302 | + | void pushNumber(double v); | |
| 303 | + | void pushBoolean(bool v); | |
| 304 | + | void pushCString(const char* v); | |
| 305 | + | void pushString(const std::string& v); | |
| 306 | + | void pushLightUserdata(void* p); | |
| 307 | + | void pushThread(); | |
| 308 | + | void pushValue(int index = -1); | |
| 309 | + | void pushObject(const LuaObjectPtr& obj); | |
| 310 | + | void pushCFunction(LuaCFunction func, int n = 0); | |
| 311 | + | void pushCppFunction(const LuaCppFunction& func); | |
| 312 | + | ||
| 313 | + | bool isNil(int index = -1); | |
| 314 | + | bool isBoolean(int index = -1); | |
| 315 | + | bool isNumber(int index = -1); | |
| 316 | + | bool isString(int index = -1); | |
| 317 | + | bool isTable(int index = -1); | |
| 318 | + | bool isFunction(int index = -1); | |
| 319 | + | bool isCFunction(int index = -1); | |
| 320 | + | bool isLuaFunction(int index = -1) { return (isFunction() && !isCFunction()); } | |
| 321 | + | bool isUserdata(int index = -1); | |
| 322 | + | ||
| 323 | + | bool toBoolean(int index = -1); | |
| 324 | + | int toInteger(int index = -1); | |
| 325 | + | double toNumber(int index = -1); | |
| 326 | + | const char* toCString(int index = -1); | |
| 327 | + | std::string toString(int index = -1); | |
| 328 | + | void* toUserdata(int index = -1); | |
| 329 | + | LuaObjectPtr toObject(int index = -1); | |
| 330 | + | ||
| 331 | + | int getTop(); | |
| 332 | + | int stackSize() { return getTop(); } | |
| 333 | + | void clearStack() { pop(stackSize()); } | |
| 334 | + | bool hasIndex(int index) { return (stackSize() >= (index < 0 ? -index : index) && index != 0); } | |
| 335 | + | ||
| 336 | + | std::string getSource(int level = 2); | |
| 337 | + | ||
| 338 | + | void loadFiles(std::string directory, bool recursive = false, std::string contains = ""); | |
| 339 | + | ||
| 340 | + | /// Pushes any type onto the stack | |
| 341 | + | template<typename T, typename... Args> | |
| 342 | + | int polymorphicPush(const T& v, const Args&... args); | |
| 343 | + | int polymorphicPush() { return 0; } | |
| 344 | + | ||
| 345 | + | /// Casts a value from stack to any type | |
| 346 | + | /// @exception LuaBadValueCastException thrown if the cast fails | |
| 347 | + | template<class T> | |
| 348 | + | T castValue(int index = -1); | |
| 349 | + | ||
| 350 | + | /// Same as castValue but also pops | |
| 351 | + | template<class T> | |
| 352 | + | T polymorphicPop() { T v = castValue<T>(); pop(1); return v; } | |
| 353 | + | ||
| 354 | + | private: | |
| 355 | + | lua_State* L; | |
| 356 | + | int m_weakTableRef; | |
| 357 | + | int m_cppCallbackDepth; | |
| 358 | + | int m_totalObjRefs; | |
| 359 | + | int m_totalFuncRefs; | |
| 360 | + | int m_globalEnv; | |
| 361 | + | }; | |
| 362 | + | ||
| 363 | + | extern LuaInterface g_lua; | |
| 364 | + | ||
| 365 | + | // must be included after, because they need LuaInterface fully declared | |
| 366 | + | #include "luaexception.h" | |
| 367 | + | #include "luabinder.h" | |
| 368 | + | #include "luavaluecasts.h" | |
| 369 | + | ||
| 370 | + | template<typename T, typename... Args> | |
| 371 | + | int LuaInterface::polymorphicPush(const T& v, const Args&... args) { | |
| 372 | + | int r = push_luavalue(v); | |
| 373 | + | return r + polymorphicPush(args...); | |
| 374 | + | } | |
| 375 | + | ||
| 376 | + | // next templates must be defined after above includes | |
| 377 | + | ||
| 378 | + | template<class C, typename F> | |
| 379 | + | void LuaInterface::bindSingletonFunction(const std::string& functionName, F C::*function, C *instance) { | |
| 380 | + | registerClassStaticFunction<C>(functionName, luabinder::bind_singleton_mem_fun(function, instance)); | |
| 381 | + | } | |
| 382 | + | ||
| 383 | + | template<class C, typename F> | |
| 384 | + | void LuaInterface::bindSingletonFunction(const std::string& className, const std::string& functionName, F C::*function, C *instance) { | |
| 385 | + | registerClassStaticFunction(className, functionName, luabinder::bind_singleton_mem_fun(function, instance)); | |
| 386 | + | } | |
| 387 | + | ||
| 388 | + | template<typename F> | |
| 389 | + | void LuaInterface::bindSingletonFunction(const std::string& className, const std::string& functionName, const F& function) | |
| 390 | + | { | |
| 391 | + | registerClassStaticFunction(className, functionName, luabinder::bind_fun(function)); | |
| 392 | + | } | |
| 393 | + | ||
| 394 | + | template<class C, typename F> | |
| 395 | + | void LuaInterface::bindClassStaticFunction(const std::string& functionName, const F& function) { | |
| 396 | + | registerClassStaticFunction<C>(functionName, luabinder::bind_fun(function)); | |
| 397 | + | } | |
| 398 | + | template<typename F> | |
| 399 | + | void LuaInterface::bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function) { | |
| 400 | + | registerClassStaticFunction(className, functionName, luabinder::bind_fun(function)); | |
| 401 | + | } | |
| 402 | + | ||
| 403 | + | template<class C, typename F, class FC> | |
| 404 | + | void LuaInterface::bindClassMemberFunction(const std::string& functionName, F FC::*function) { | |
| 405 | + | registerClassMemberFunction<C>(functionName, luabinder::bind_mem_fun<C>(function)); | |
| 406 | + | } | |
| 407 | + | template<class C, typename F, class FC> | |
| 408 | + | void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function) { | |
| 409 | + | registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun<C>(function)); | |
| 410 | + | } | |
| 411 | + | ||
| 412 | + | template<class C, typename F1, typename F2, class FC> | |
| 413 | + | void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) { | |
| 414 | + | registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction)); | |
| 415 | + | } | |
| 416 | + | template<class C, typename F1, typename F2, class FC> | |
| 417 | + | void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) { | |
| 418 | + | registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction)); | |
| 419 | + | } | |
| 420 | + | ||
| 421 | + | template<class C, typename F, class FC> | |
| 422 | + | void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction) { | |
| 423 | + | registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction()); | |
| 424 | + | } | |
| 425 | + | template<class C, typename F, class FC> | |
| 426 | + | void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction) { | |
| 427 | + | registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction()); | |
| 428 | + | } | |
| 429 | + | ||
| 430 | + | template<class C, typename F, class FC> | |
| 431 | + | void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction) { | |
| 432 | + | registerClassMemberField<C>(fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction)); | |
| 433 | + | } | |
| 434 | + | template<class C, typename F, class FC> | |
| 435 | + | void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction) { | |
| 436 | + | registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction)); | |
| 437 | + | } | |
| 438 | + | ||
| 439 | + | template<typename F> | |
| 440 | + | void LuaInterface::bindGlobalFunction(const std::string& functionName, const F& function) { | |
| 441 | + | registerGlobalFunction(functionName, luabinder::bind_fun(function)); | |
| 442 | + | } | |
| 443 | + | ||
| 444 | + | template<class T> | |
| 445 | + | T LuaInterface::castValue(int index) { | |
| 446 | + | T o; | |
| 447 | + | if (!luavalue_cast(index, o)) | |
| 448 | + | throw LuaBadValueCastException(typeName(index), stdext::demangle_type<T>()); | |
| 449 | + | return o; | |
| 450 | + | } | |
| 451 | + | ||
| 452 | + | bool luavalue_cast(int index, Size &value); | |
| 453 | + | bool luavalue_cast(int index, Point &value); | |
| 454 | + | bool luavalue_cast(int index, Rect &value); | |
| 455 | + | bool luavalue_cast(int index, Color &value); | |
| 456 | + | bool luavalue_cast(int index, std::string &value); | |
| 457 | + | bool luavalue_cast(int index, uint64 &value); | |
| 458 | + | ||
| 459 | + | // Otras sobrecargas de luavalue_cast... | |
| 460 | + | ||
| 461 | + | template<typename... T> | |
| 462 | + | int LuaInterface::luaCallGlobalField(const std::string& global, const std::string& field, const T&... args) { | |
| 463 | + | AutoStat s(STATS_LUA, std::string(global) + ":" + field); | |
| 464 | + | ||
| 465 | + | g_lua.getGlobalField(global, field); | |
| 466 | + | int ret = 0; | |
| 467 | + | ||
| 468 | + | if(!g_lua.isNil()) { | |
| 469 | + | int numArgs = g_lua.polymorphicPush(args...); | |
| 470 | + | ret = g_lua.signalCall(numArgs); | |
| 471 | + | } else | |
| 472 | + | g_lua.pop(1); | |
| 473 | + | return ret; | |
| 474 | + | } | |
| 475 | + | ||
| 476 | + | template<typename... T> | |
| 477 | + | void LuaInterface::callGlobalField(const std::string& global, const std::string& field, const T&... args) { | |
| 478 | + | int rets = luaCallGlobalField(global, field, args...); | |
| 479 | + | if(rets > 0) | |
| 480 | + | pop(rets); | |
| 481 | + | } | |
| 482 | + | ||
| 483 | + | template<typename R, typename... T> | |
| 484 | + | R LuaInterface::callGlobalField(const std::string& global, const std::string& field, const T&... args) { | |
| 485 | + | R result; | |
| 486 | + | int rets = luaCallGlobalField(global, field, args...); | |
| 487 | + | if(rets > 0) { | |
| 488 | + | VALIDATE(rets == 1); | |
| 489 | + | result = g_lua.polymorphicPop<R>(); | |
| 490 | + | } else | |
| 491 | + | result = R(); | |
| 492 | + | return result; | |
| 493 | + | } | |
| 494 | + | ||
| 495 | + | #endif |
| @@ -0,0 +1,123 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "luaobject.h" | |
| 24 | + | #include "luainterface.h" | |
| 25 | + | ||
| 26 | + | #include <typeinfo> | |
| 27 | + | #include <framework/core/application.h> | |
| 28 | + | ||
| 29 | + | LuaObject::LuaObject() : | |
| 30 | + | m_fieldsTableRef(-1) | |
| 31 | + | { | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | LuaObject::~LuaObject() | |
| 35 | + | { | |
| 36 | + | VALIDATE(!g_app.isTerminated()); | |
| 37 | + | releaseLuaFieldsTable(); | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | bool LuaObject::hasLuaField(const std::string& field) | |
| 41 | + | { | |
| 42 | + | bool ret = false; | |
| 43 | + | if(m_fieldsTableRef != -1) { | |
| 44 | + | g_lua.getRef(m_fieldsTableRef); | |
| 45 | + | g_lua.getField(field); // push the field value | |
| 46 | + | ret = !g_lua.isNil(); | |
| 47 | + | g_lua.pop(2); | |
| 48 | + | } | |
| 49 | + | return ret; | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | void LuaObject::releaseLuaFieldsTable() | |
| 53 | + | { | |
| 54 | + | if(m_fieldsTableRef != -1) { | |
| 55 | + | g_lua.unref(m_fieldsTableRef); | |
| 56 | + | m_fieldsTableRef = -1; | |
| 57 | + | } | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | void LuaObject::luaSetField(const std::string& key) | |
| 61 | + | { | |
| 62 | + | // create fields table on the fly | |
| 63 | + | if(m_fieldsTableRef == -1) { | |
| 64 | + | g_lua.newTable(); // create fields table | |
| 65 | + | m_fieldsTableRef = g_lua.ref(); // save a reference for it | |
| 66 | + | } | |
| 67 | + | ||
| 68 | + | g_lua.getRef(m_fieldsTableRef); // push the table | |
| 69 | + | g_lua.insert(-2); // move the value to the top | |
| 70 | + | g_lua.setField(key); // set the field | |
| 71 | + | g_lua.pop(); // pop the fields table | |
| 72 | + | } | |
| 73 | + | ||
| 74 | + | void LuaObject::luaGetField(const std::string& key) | |
| 75 | + | { | |
| 76 | + | if(m_fieldsTableRef != -1) { | |
| 77 | + | g_lua.getRef(m_fieldsTableRef); // push the obj's fields table | |
| 78 | + | g_lua.getField(key); // push the field value | |
| 79 | + | g_lua.remove(-2); // remove the table | |
| 80 | + | } else { | |
| 81 | + | g_lua.pushNil(); | |
| 82 | + | } | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | void LuaObject::luaGetMetatable() | |
| 86 | + | { | |
| 87 | + | static std::unordered_map<const std::type_info*, int> metatableMap; | |
| 88 | + | const std::type_info& tinfo = typeid(*this); | |
| 89 | + | auto it = metatableMap.find(&tinfo); | |
| 90 | + | ||
| 91 | + | int metatableRef; | |
| 92 | + | if(it == metatableMap.end()) { | |
| 93 | + | g_lua.getGlobal(getClassName() + "_mt"); | |
| 94 | + | metatableRef = g_lua.ref(); | |
| 95 | + | metatableMap[&tinfo] = metatableRef; | |
| 96 | + | } else | |
| 97 | + | metatableRef = it->second; | |
| 98 | + | ||
| 99 | + | g_lua.getRef(metatableRef); | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | void LuaObject::luaGetFieldsTable() | |
| 103 | + | { | |
| 104 | + | if(m_fieldsTableRef != -1) | |
| 105 | + | g_lua.getRef(m_fieldsTableRef); | |
| 106 | + | else | |
| 107 | + | g_lua.pushNil(); | |
| 108 | + | } | |
| 109 | + | ||
| 110 | + | int LuaObject::getUseCount() | |
| 111 | + | { | |
| 112 | + | return ref_count(); | |
| 113 | + | } | |
| 114 | + | ||
| 115 | + | std::string LuaObject::getClassName() | |
| 116 | + | { | |
| 117 | + | // TODO: this could be cached for more performance | |
| 118 | + | #ifdef _MSC_VER | |
| 119 | + | return stdext::demangle_name(typeid(*this).name()) + 6; | |
| 120 | + | #else | |
| 121 | + | return stdext::demangle_name(typeid(*this).name()); | |
| 122 | + | #endif | |
| 123 | + | } |
| @@ -0,0 +1,208 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef LUAOBJECT_H | |
| 24 | + | #define LUAOBJECT_H | |
| 25 | + | ||
| 26 | + | #include <framework/util/stats.h> | |
| 27 | + | #include "declarations.h" | |
| 28 | + | ||
| 29 | + | /// LuaObject, all script-able classes have it as base | |
| 30 | + | // @bindclass | |
| 31 | + | class LuaObject : public stdext::shared_object | |
| 32 | + | { | |
| 33 | + | public: | |
| 34 | + | LuaObject(); | |
| 35 | + | virtual ~LuaObject(); | |
| 36 | + | ||
| 37 | + | template<typename T> | |
| 38 | + | void connectLuaField(const std::string& field, const std::function<T>& f, bool pushFront = false); | |
| 39 | + | ||
| 40 | + | /// Calls a function or table of functions stored in a lua field, results are pushed onto the stack, | |
| 41 | + | /// if any lua error occurs, it will be reported to stdout and return 0 results | |
| 42 | + | /// @return the number of results | |
| 43 | + | template<typename... T> | |
| 44 | + | int luaCallLuaField(const std::string& field, const T&... args); | |
| 45 | + | ||
| 46 | + | template<typename R, typename... T> | |
| 47 | + | R callLuaField(const std::string& field, const T&... args); | |
| 48 | + | template<typename... T> | |
| 49 | + | void callLuaField(const std::string& field, const T&... args); | |
| 50 | + | ||
| 51 | + | /// Returns true if the lua field exists | |
| 52 | + | bool hasLuaField(const std::string& field); | |
| 53 | + | ||
| 54 | + | /// Sets a field in this lua object | |
| 55 | + | template<typename T> | |
| 56 | + | void setLuaField(const std::string& key, const T& value); | |
| 57 | + | ||
| 58 | + | /// Gets a field from this lua object | |
| 59 | + | template<typename T> | |
| 60 | + | T getLuaField(const std::string& key); | |
| 61 | + | ||
| 62 | + | /// Release fields table reference | |
| 63 | + | void releaseLuaFieldsTable(); | |
| 64 | + | ||
| 65 | + | /// Sets a field from this lua object, the value must be on the stack | |
| 66 | + | void luaSetField(const std::string& key); | |
| 67 | + | ||
| 68 | + | /// Gets a field from this lua object, the result is pushed onto the stack | |
| 69 | + | void luaGetField(const std::string& key); | |
| 70 | + | ||
| 71 | + | /// Get object's metatable | |
| 72 | + | void luaGetMetatable(); | |
| 73 | + | ||
| 74 | + | /// Gets the table containing all stored fields of this lua object, the result is pushed onto the stack | |
| 75 | + | void luaGetFieldsTable(); | |
| 76 | + | ||
| 77 | + | /// Returns the number of references of this object | |
| 78 | + | /// @note each userdata of this object on lua counts as a reference | |
| 79 | + | int getUseCount(); | |
| 80 | + | ||
| 81 | + | /// Returns the derived class name, its the same name used in Lua | |
| 82 | + | std::string getClassName(); | |
| 83 | + | ||
| 84 | + | LuaObjectPtr asLuaObject() { return static_self_cast<LuaObject>(); } | |
| 85 | + | ||
| 86 | + | void operator=(const LuaObject& other) { } | |
| 87 | + | ||
| 88 | + | private: | |
| 89 | + | int m_fieldsTableRef; | |
| 90 | + | }; | |
| 91 | + | ||
| 92 | + | template<typename F> | |
| 93 | + | void connect(const LuaObjectPtr& obj, const std::string& field, const std::function<F>& f, bool pushFront = false); | |
| 94 | + | ||
| 95 | + | template<typename Lambda> | |
| 96 | + | typename std::enable_if<std::is_constructible<decltype(&Lambda::operator())>::value, void>::type | |
| 97 | + | connect(const LuaObjectPtr& obj, const std::string& field, const Lambda& f, bool pushFront = false); | |
| 98 | + | ||
| 99 | + | #include "luainterface.h" | |
| 100 | + | ||
| 101 | + | template<typename T> | |
| 102 | + | void LuaObject::connectLuaField(const std::string& field, const std::function<T>& f, bool pushFront) | |
| 103 | + | { | |
| 104 | + | luaGetField(field); | |
| 105 | + | if(g_lua.isTable()) { | |
| 106 | + | if(pushFront) | |
| 107 | + | g_lua.pushInteger(1); | |
| 108 | + | push_luavalue(f); | |
| 109 | + | g_lua.callGlobalField("table","insert"); | |
| 110 | + | } else { | |
| 111 | + | if(g_lua.isNil()) { | |
| 112 | + | push_luavalue(f); | |
| 113 | + | luaSetField(field); | |
| 114 | + | g_lua.pop(); | |
| 115 | + | } else if(g_lua.isFunction()) { | |
| 116 | + | g_lua.newTable(); | |
| 117 | + | g_lua.insert(-2); | |
| 118 | + | g_lua.rawSeti(1); | |
| 119 | + | push_luavalue(f); | |
| 120 | + | g_lua.rawSeti(2); | |
| 121 | + | luaSetField(field); | |
| 122 | + | } | |
| 123 | + | } | |
| 124 | + | } | |
| 125 | + | ||
| 126 | + | // connect for std::function | |
| 127 | + | template<typename F> | |
| 128 | + | void connect(const LuaObjectPtr& obj, const std::string& field, const std::function<F>& f, bool pushFront) { | |
| 129 | + | obj->connectLuaField<F>(field, f, pushFront); | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | namespace luabinder { | |
| 133 | + | template<typename F> | |
| 134 | + | struct connect_lambda; | |
| 135 | + | ||
| 136 | + | template<typename Lambda, typename Ret, typename... Args> | |
| 137 | + | struct connect_lambda<Ret(Lambda::*)(Args...) const> { | |
| 138 | + | static void call(const LuaObjectPtr& obj, const std::string& field, const Lambda& f, bool pushFront) { | |
| 139 | + | connect(obj, field, std::function<Ret(Args...)>(f), pushFront); | |
| 140 | + | } | |
| 141 | + | }; | |
| 142 | + | }; | |
| 143 | + | ||
| 144 | + | // connect for lambdas | |
| 145 | + | template<typename Lambda> | |
| 146 | + | typename std::enable_if<std::is_constructible<decltype(&Lambda::operator())>::value, void>::type | |
| 147 | + | connect(const LuaObjectPtr& obj, const std::string& field, const Lambda& f, bool pushFront) { | |
| 148 | + | typedef decltype(&Lambda::operator()) F; | |
| 149 | + | luabinder::connect_lambda<F>::call(obj, field, f, pushFront); | |
| 150 | + | } | |
| 151 | + | ||
| 152 | + | template<typename... T> | |
| 153 | + | int LuaObject::luaCallLuaField(const std::string& field, const T&... args) { | |
| 154 | + | AutoStat s(STATS_LUA, getClassName() + ":" + field); | |
| 155 | + | ||
| 156 | + | // note that the field must be retrieved from this object lua value | |
| 157 | + | // to force using the __index metamethod of it's metatable | |
| 158 | + | // so cannot use LuaObject::getField here | |
| 159 | + | // push field | |
| 160 | + | //AutoStat s(STATS_LUA, field); | |
| 161 | + | g_lua.pushObject(asLuaObject()); | |
| 162 | + | g_lua.getField(field); | |
| 163 | + | ||
| 164 | + | int ret = 0; | |
| 165 | + | if(!g_lua.isNil()) { | |
| 166 | + | // the first argument is always this object (self) | |
| 167 | + | g_lua.insert(-2); | |
| 168 | + | int numArgs = g_lua.polymorphicPush(args...); | |
| 169 | + | ret = g_lua.signalCall(1 + numArgs); | |
| 170 | + | } else { | |
| 171 | + | g_lua.pop(2); | |
| 172 | + | } | |
| 173 | + | ||
| 174 | + | return ret; | |
| 175 | + | } | |
| 176 | + | ||
| 177 | + | template<typename R, typename... T> | |
| 178 | + | R LuaObject::callLuaField(const std::string& field, const T&... args) { | |
| 179 | + | R result; | |
| 180 | + | int rets = luaCallLuaField(field, args...); | |
| 181 | + | if(rets > 0) { | |
| 182 | + | VALIDATE(rets == 1); | |
| 183 | + | result = g_lua.polymorphicPop<R>(); | |
| 184 | + | } else | |
| 185 | + | result = R(); | |
| 186 | + | return result; | |
| 187 | + | } | |
| 188 | + | ||
| 189 | + | template<typename... T> | |
| 190 | + | void LuaObject::callLuaField(const std::string& field, const T&... args) { | |
| 191 | + | int rets = luaCallLuaField(field, args...); | |
| 192 | + | if(rets > 0) | |
| 193 | + | g_lua.pop(rets); | |
| 194 | + | } | |
| 195 | + | ||
| 196 | + | template<typename T> | |
| 197 | + | void LuaObject::setLuaField(const std::string& key, const T& value) { | |
| 198 | + | g_lua.polymorphicPush(value); | |
| 199 | + | luaSetField(key); | |
| 200 | + | } | |
| 201 | + | ||
| 202 | + | template<typename T> | |
| 203 | + | T LuaObject::getLuaField(const std::string& key) { | |
| 204 | + | luaGetField(key); | |
| 205 | + | return g_lua.polymorphicPop<T>(); | |
| 206 | + | } | |
| 207 | + | ||
| 208 | + | #endif |
| @@ -0,0 +1,335 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "luavaluecasts.h" | |
| 24 | + | #include "luainterface.h" | |
| 25 | + | #include <framework/otml/otmlnode.h> | |
| 26 | + | ||
| 27 | + | // bool | |
| 28 | + | int push_luavalue(bool b) | |
| 29 | + | { | |
| 30 | + | g_lua.pushBoolean(b); | |
| 31 | + | return 1; | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | bool luavalue_cast(int index, bool& b) | |
| 35 | + | { | |
| 36 | + | b = g_lua.toBoolean(index); | |
| 37 | + | return true; | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | // int | |
| 41 | + | int push_luavalue(int i) | |
| 42 | + | { | |
| 43 | + | g_lua.pushInteger(i); | |
| 44 | + | return 1; | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | bool luavalue_cast(int index, int& i) | |
| 48 | + | { | |
| 49 | + | i = g_lua.toInteger(index); | |
| 50 | + | if(i == 0 && !g_lua.isNumber(index) && !g_lua.isNil()) | |
| 51 | + | return false; | |
| 52 | + | return true; | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | // double | |
| 56 | + | int push_luavalue(double d) | |
| 57 | + | { | |
| 58 | + | g_lua.pushNumber(d); | |
| 59 | + | return 1; | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | bool luavalue_cast(int index, double& d) | |
| 63 | + | { | |
| 64 | + | d = g_lua.toNumber(index); | |
| 65 | + | if(d == 0 && !g_lua.isNumber(index) && !g_lua.isNil()) | |
| 66 | + | return false; | |
| 67 | + | return true; | |
| 68 | + | } | |
| 69 | + | ||
| 70 | + | // string | |
| 71 | + | int push_luavalue(const char* cstr) | |
| 72 | + | { | |
| 73 | + | g_lua.pushCString(cstr); | |
| 74 | + | return 1; | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | int push_luavalue(const std::string& str) | |
| 78 | + | { | |
| 79 | + | g_lua.pushString(str); | |
| 80 | + | return 1; | |
| 81 | + | } | |
| 82 | + | ||
| 83 | + | bool luavalue_cast(int index, std::string& str) | |
| 84 | + | { | |
| 85 | + | str = g_lua.toString(index); | |
| 86 | + | return true; | |
| 87 | + | } | |
| 88 | + | ||
| 89 | + | // lua cpp function | |
| 90 | + | int push_luavalue(const LuaCppFunction& func) | |
| 91 | + | { | |
| 92 | + | g_lua.pushCppFunction(func); | |
| 93 | + | return 1; | |
| 94 | + | } | |
| 95 | + | ||
| 96 | + | // color | |
| 97 | + | int push_luavalue(const Color& color) | |
| 98 | + | { | |
| 99 | + | g_lua.createTable(0, 4); | |
| 100 | + | g_lua.pushInteger(color.r()); | |
| 101 | + | g_lua.setField("r"); | |
| 102 | + | g_lua.pushInteger(color.g()); | |
| 103 | + | g_lua.setField("g"); | |
| 104 | + | g_lua.pushInteger(color.b()); | |
| 105 | + | g_lua.setField("b"); | |
| 106 | + | g_lua.pushInteger(color.a()); | |
| 107 | + | g_lua.setField("a"); | |
| 108 | + | return 1; | |
| 109 | + | } | |
| 110 | + | ||
| 111 | + | bool luavalue_cast(int index, Color& color) | |
| 112 | + | { | |
| 113 | + | if(g_lua.isTable(index)) { | |
| 114 | + | g_lua.getField("r", index); | |
| 115 | + | color.setRed((int)g_lua.popInteger()); | |
| 116 | + | g_lua.getField("g", index); | |
| 117 | + | color.setGreen((int)g_lua.popInteger()); | |
| 118 | + | g_lua.getField("b", index); | |
| 119 | + | color.setBlue((int)g_lua.popInteger()); | |
| 120 | + | g_lua.getField("a", index); | |
| 121 | + | color.setAlpha((int)g_lua.popInteger()); | |
| 122 | + | return true; | |
| 123 | + | } else if(g_lua.isString()) { | |
| 124 | + | return stdext::cast(g_lua.toString(index), color); | |
| 125 | + | } else if(g_lua.isNil()) { | |
| 126 | + | color = Color::white; | |
| 127 | + | return true; | |
| 128 | + | } | |
| 129 | + | return false; | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | // rect | |
| 133 | + | int push_luavalue(const Rect& rect) | |
| 134 | + | { | |
| 135 | + | g_lua.createTable(0, 4); | |
| 136 | + | g_lua.pushInteger(rect.x()); | |
| 137 | + | g_lua.setField("x"); | |
| 138 | + | g_lua.pushInteger(rect.y()); | |
| 139 | + | g_lua.setField("y"); | |
| 140 | + | g_lua.pushInteger(rect.width()); | |
| 141 | + | g_lua.setField("width"); | |
| 142 | + | g_lua.pushInteger(rect.height()); | |
| 143 | + | g_lua.setField("height"); | |
| 144 | + | return 1; | |
| 145 | + | } | |
| 146 | + | ||
| 147 | + | bool luavalue_cast(int index, Rect& rect) | |
| 148 | + | { | |
| 149 | + | if(g_lua.isTable(index)) { | |
| 150 | + | g_lua.getField("x", index); | |
| 151 | + | rect.setX(g_lua.popInteger()); | |
| 152 | + | g_lua.getField("y", index); | |
| 153 | + | rect.setY(g_lua.popInteger()); | |
| 154 | + | g_lua.getField("width", index); | |
| 155 | + | rect.setWidth(g_lua.popInteger()); | |
| 156 | + | g_lua.getField("height", index); | |
| 157 | + | rect.setHeight(g_lua.popInteger()); | |
| 158 | + | return true; | |
| 159 | + | } else if(g_lua.isString()) { | |
| 160 | + | return stdext::cast(g_lua.toString(index), rect); | |
| 161 | + | } else if(g_lua.isNil()) { | |
| 162 | + | rect = Rect(); | |
| 163 | + | return true; | |
| 164 | + | } | |
| 165 | + | return false; | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | // point | |
| 169 | + | int push_luavalue(const Point& point) | |
| 170 | + | { | |
| 171 | + | g_lua.createTable(0, 2); | |
| 172 | + | g_lua.pushInteger(point.x); | |
| 173 | + | g_lua.setField("x"); | |
| 174 | + | g_lua.pushInteger(point.y); | |
| 175 | + | g_lua.setField("y"); | |
| 176 | + | return 1; | |
| 177 | + | } | |
| 178 | + | ||
| 179 | + | bool luavalue_cast(int index, Point& point) | |
| 180 | + | { | |
| 181 | + | if(g_lua.isTable(index)) { | |
| 182 | + | g_lua.getField("x", index); | |
| 183 | + | point.x = g_lua.popInteger(); | |
| 184 | + | g_lua.getField("y", index); | |
| 185 | + | point.y = g_lua.popInteger(); | |
| 186 | + | return true; | |
| 187 | + | } else if(g_lua.isString()) { | |
| 188 | + | return stdext::cast(g_lua.toString(index), point); | |
| 189 | + | } else if(g_lua.isNil()) { | |
| 190 | + | point = Point(); | |
| 191 | + | return true; | |
| 192 | + | } | |
| 193 | + | return false; | |
| 194 | + | } | |
| 195 | + | ||
| 196 | + | // size | |
| 197 | + | int push_luavalue(const Size& size) | |
| 198 | + | { | |
| 199 | + | g_lua.createTable(0, 2); | |
| 200 | + | g_lua.pushInteger(size.width()); | |
| 201 | + | g_lua.setField("width"); | |
| 202 | + | g_lua.pushInteger(size.height()); | |
| 203 | + | g_lua.setField("height"); | |
| 204 | + | return 1; | |
| 205 | + | } | |
| 206 | + | ||
| 207 | + | bool luavalue_cast(int index, Size& size) | |
| 208 | + | { | |
| 209 | + | if(g_lua.isTable(index)) { | |
| 210 | + | g_lua.getField("width", index); | |
| 211 | + | size.setWidth(g_lua.popInteger()); | |
| 212 | + | g_lua.getField("height", index); | |
| 213 | + | size.setHeight(g_lua.popInteger()); | |
| 214 | + | return true; | |
| 215 | + | } else if(g_lua.isString()) { | |
| 216 | + | return stdext::cast(g_lua.toString(index), size); | |
| 217 | + | } else if(g_lua.isNil()) { | |
| 218 | + | size = Size(); | |
| 219 | + | return true; | |
| 220 | + | } | |
| 221 | + | return false; | |
| 222 | + | } | |
| 223 | + | ||
| 224 | + | // otml nodes | |
| 225 | + | void push_otml_subnode_luavalue(const OTMLNodePtr& node) | |
| 226 | + | { | |
| 227 | + | if(node->hasValue()) { | |
| 228 | + | union { | |
| 229 | + | bool b; | |
| 230 | + | double d; | |
| 231 | + | long l; | |
| 232 | + | }; | |
| 233 | + | std::string value = node->rawValue(); | |
| 234 | + | if(stdext::cast(value, b)) | |
| 235 | + | g_lua.pushBoolean(b); | |
| 236 | + | else if(stdext::cast(value, l)) | |
| 237 | + | g_lua.pushInteger(l); | |
| 238 | + | else if(stdext::cast(value, d)) | |
| 239 | + | g_lua.pushNumber(d); | |
| 240 | + | else | |
| 241 | + | g_lua.pushString(value); | |
| 242 | + | } else if(node->hasChildren()) { | |
| 243 | + | g_lua.newTable(); | |
| 244 | + | bool pushedChild = false; | |
| 245 | + | int currentIndex = 1; | |
| 246 | + | for(const OTMLNodePtr& cnode : node->children()) { | |
| 247 | + | push_otml_subnode_luavalue(cnode); | |
| 248 | + | if(!g_lua.isNil()) { | |
| 249 | + | if(cnode->isUnique()) { | |
| 250 | + | g_lua.pushString(cnode->tag()); | |
| 251 | + | g_lua.insert(-2); | |
| 252 | + | g_lua.rawSet(); | |
| 253 | + | } else | |
| 254 | + | g_lua.rawSeti(currentIndex++); | |
| 255 | + | pushedChild = true; | |
| 256 | + | } else | |
| 257 | + | g_lua.pop(); | |
| 258 | + | } | |
| 259 | + | if(!pushedChild) { | |
| 260 | + | g_lua.pop(); | |
| 261 | + | g_lua.pushNil(); | |
| 262 | + | } | |
| 263 | + | } else | |
| 264 | + | g_lua.pushNil(); | |
| 265 | + | } | |
| 266 | + | ||
| 267 | + | int push_luavalue(const OTMLNodePtr& node) | |
| 268 | + | { | |
| 269 | + | if(node) { | |
| 270 | + | g_lua.newTable(); | |
| 271 | + | int currentIndex = 1; | |
| 272 | + | for(const OTMLNodePtr& cnode : node->children()) { | |
| 273 | + | push_otml_subnode_luavalue(cnode); | |
| 274 | + | if(cnode->isUnique() && !cnode->tag().empty()) { | |
| 275 | + | g_lua.setField(cnode->tag()); | |
| 276 | + | } else | |
| 277 | + | g_lua.rawSeti(currentIndex++); | |
| 278 | + | } | |
| 279 | + | } else | |
| 280 | + | g_lua.pushNil(); | |
| 281 | + | return 1; | |
| 282 | + | } | |
| 283 | + | ||
| 284 | + | bool luavalue_cast(int index, OTMLNodePtr& node) | |
| 285 | + | { | |
| 286 | + | node = OTMLNode::create(); | |
| 287 | + | node->setUnique(true); | |
| 288 | + | if(g_lua.isTable(index)) { | |
| 289 | + | g_lua.pushNil(); | |
| 290 | + | while(g_lua.next(index < 0 ? index-1 : index)) { | |
| 291 | + | std::string cnodeName; | |
| 292 | + | if(g_lua.isString(-2)) { | |
| 293 | + | g_lua.pushValue(-2); | |
| 294 | + | cnodeName = g_lua.toString(); | |
| 295 | + | g_lua.pop(); | |
| 296 | + | } else | |
| 297 | + | VALIDATE(g_lua.isNumber()); | |
| 298 | + | if(g_lua.isTable()) { | |
| 299 | + | OTMLNodePtr cnode; | |
| 300 | + | if(luavalue_cast(-1, cnode)) { | |
| 301 | + | if(cnodeName.empty()) | |
| 302 | + | cnode->setUnique(false); | |
| 303 | + | else | |
| 304 | + | cnode->setTag(cnodeName); | |
| 305 | + | node->addChild(cnode); | |
| 306 | + | } | |
| 307 | + | } else { | |
| 308 | + | std::string value; | |
| 309 | + | if(g_lua.isBoolean()) | |
| 310 | + | value = stdext::unsafe_cast<std::string>(g_lua.toBoolean()); | |
| 311 | + | else | |
| 312 | + | value = g_lua.toString(); | |
| 313 | + | if(cnodeName.empty()) | |
| 314 | + | node->writeIn(value); | |
| 315 | + | else | |
| 316 | + | node->writeAt(cnodeName, value); | |
| 317 | + | } | |
| 318 | + | g_lua.pop(); | |
| 319 | + | } | |
| 320 | + | return true; | |
| 321 | + | } | |
| 322 | + | return false; | |
| 323 | + | } | |
| 324 | + | ||
| 325 | + | // object ptr | |
| 326 | + | bool luavalue_cast(int index, LuaObjectPtr& obj) { | |
| 327 | + | if(g_lua.isUserdata(index)) { | |
| 328 | + | obj = g_lua.toObject(index); | |
| 329 | + | return true; | |
| 330 | + | } else if(g_lua.isNil(index)) { | |
| 331 | + | obj = nullptr; | |
| 332 | + | return true; | |
| 333 | + | } | |
| 334 | + | return false; | |
| 335 | + | } |