2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2016-07-10 03:10:27 +02:00
|
|
|
* Copyright (c) 2010-2016 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
#include "luavaluecasts.h"
|
|
|
|
#include "luainterface.h"
|
|
|
|
#include <framework/otml/otmlnode.h>
|
|
|
|
|
|
|
|
// bool
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(bool b)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
|
|
|
g_lua.pushBoolean(b);
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, bool& b)
|
|
|
|
{
|
|
|
|
b = g_lua.toBoolean(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// int
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(int i)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
|
|
|
g_lua.pushInteger(i);
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, int& i)
|
|
|
|
{
|
|
|
|
i = g_lua.toInteger(index);
|
2012-01-07 01:52:59 +01:00
|
|
|
if(i == 0 && !g_lua.isNumber(index) && !g_lua.isNil())
|
|
|
|
return false;
|
2011-08-19 20:53:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// double
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(double d)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
|
|
|
g_lua.pushNumber(d);
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, double& d)
|
|
|
|
{
|
|
|
|
d = g_lua.toNumber(index);
|
2012-01-07 01:52:59 +01:00
|
|
|
if(d == 0 && !g_lua.isNumber(index) && !g_lua.isNil())
|
|
|
|
return false;
|
2011-08-19 20:53:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// string
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const char* cstr)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
|
|
|
g_lua.pushCString(cstr);
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const std::string& str)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
|
|
|
g_lua.pushString(str);
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, std::string& str)
|
|
|
|
{
|
|
|
|
str = g_lua.toString(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lua cpp function
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const LuaCppFunction& func)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
|
|
|
g_lua.pushCppFunction(func);
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// color
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const Color& color)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
lua: now we have a function to static allocate an array
lua_newtable() definition:
#define lua_newtable(L) lua_createtable(L, 0, 0)
This simply allocates space for 0 fields, meaning, if we push any
values into this array, it will re-allocate the array, which is bad.
This function statically allocates an array, so it's ready to have
X fields into it.
Performance tests:
lua_newtable
1428161
1426992
1413513
lua_createtable
2004544
1974117
1957533
These tests were done on an AMD 8350fx CPU, single thread used.
narr: This is for fields that just have an index, e.g. arr[0] etc.
nrec: For fields which needs like arr.a, arr.b etc.
This is how many times each of the functions can run per second, as
you can see about 1.7x the calls to lua_newtable.
All credits goes to @dalkon, he was too lazy to do it by himself, and
asked me to do it for him.
2013-12-21 15:13:24 +01:00
|
|
|
g_lua.createTable(0, 4);
|
2011-08-19 20:53:23 +02:00
|
|
|
g_lua.pushInteger(color.r());
|
|
|
|
g_lua.setField("r");
|
|
|
|
g_lua.pushInteger(color.g());
|
|
|
|
g_lua.setField("g");
|
|
|
|
g_lua.pushInteger(color.b());
|
|
|
|
g_lua.setField("b");
|
|
|
|
g_lua.pushInteger(color.a());
|
|
|
|
g_lua.setField("a");
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, Color& color)
|
|
|
|
{
|
|
|
|
if(g_lua.isTable(index)) {
|
|
|
|
g_lua.getField("r", index);
|
2012-07-23 06:33:08 +02:00
|
|
|
color.setRed((int)g_lua.popInteger());
|
2011-08-19 20:53:23 +02:00
|
|
|
g_lua.getField("g", index);
|
2012-07-23 06:33:08 +02:00
|
|
|
color.setGreen((int)g_lua.popInteger());
|
2011-08-19 20:53:23 +02:00
|
|
|
g_lua.getField("b", index);
|
2012-07-23 06:33:08 +02:00
|
|
|
color.setBlue((int)g_lua.popInteger());
|
2011-08-19 20:53:23 +02:00
|
|
|
g_lua.getField("a", index);
|
2012-07-23 06:33:08 +02:00
|
|
|
color.setAlpha((int)g_lua.popInteger());
|
2011-08-19 20:53:23 +02:00
|
|
|
return true;
|
|
|
|
} else if(g_lua.isString()) {
|
2012-05-28 15:06:26 +02:00
|
|
|
return stdext::cast(g_lua.toString(index), color);
|
2011-08-19 20:53:23 +02:00
|
|
|
} else if(g_lua.isNil()) {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::white;
|
2011-08-19 20:53:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rect
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const Rect& rect)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
lua: now we have a function to static allocate an array
lua_newtable() definition:
#define lua_newtable(L) lua_createtable(L, 0, 0)
This simply allocates space for 0 fields, meaning, if we push any
values into this array, it will re-allocate the array, which is bad.
This function statically allocates an array, so it's ready to have
X fields into it.
Performance tests:
lua_newtable
1428161
1426992
1413513
lua_createtable
2004544
1974117
1957533
These tests were done on an AMD 8350fx CPU, single thread used.
narr: This is for fields that just have an index, e.g. arr[0] etc.
nrec: For fields which needs like arr.a, arr.b etc.
This is how many times each of the functions can run per second, as
you can see about 1.7x the calls to lua_newtable.
All credits goes to @dalkon, he was too lazy to do it by himself, and
asked me to do it for him.
2013-12-21 15:13:24 +01:00
|
|
|
g_lua.createTable(0, 4);
|
2011-08-19 20:53:23 +02:00
|
|
|
g_lua.pushInteger(rect.x());
|
|
|
|
g_lua.setField("x");
|
|
|
|
g_lua.pushInteger(rect.y());
|
|
|
|
g_lua.setField("y");
|
|
|
|
g_lua.pushInteger(rect.width());
|
|
|
|
g_lua.setField("width");
|
|
|
|
g_lua.pushInteger(rect.height());
|
|
|
|
g_lua.setField("height");
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, Rect& rect)
|
|
|
|
{
|
|
|
|
if(g_lua.isTable(index)) {
|
|
|
|
g_lua.getField("x", index);
|
|
|
|
rect.setX(g_lua.popInteger());
|
|
|
|
g_lua.getField("y", index);
|
|
|
|
rect.setY(g_lua.popInteger());
|
|
|
|
g_lua.getField("width", index);
|
|
|
|
rect.setWidth(g_lua.popInteger());
|
|
|
|
g_lua.getField("height", index);
|
|
|
|
rect.setHeight(g_lua.popInteger());
|
2011-11-16 18:03:11 +01:00
|
|
|
return true;
|
2011-08-19 20:53:23 +02:00
|
|
|
} else if(g_lua.isString()) {
|
2012-05-28 15:06:26 +02:00
|
|
|
return stdext::cast(g_lua.toString(index), rect);
|
2011-08-19 20:53:23 +02:00
|
|
|
} else if(g_lua.isNil()) {
|
|
|
|
rect = Rect();
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-16 18:03:11 +01:00
|
|
|
return false;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// point
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const Point& point)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
lua: now we have a function to static allocate an array
lua_newtable() definition:
#define lua_newtable(L) lua_createtable(L, 0, 0)
This simply allocates space for 0 fields, meaning, if we push any
values into this array, it will re-allocate the array, which is bad.
This function statically allocates an array, so it's ready to have
X fields into it.
Performance tests:
lua_newtable
1428161
1426992
1413513
lua_createtable
2004544
1974117
1957533
These tests were done on an AMD 8350fx CPU, single thread used.
narr: This is for fields that just have an index, e.g. arr[0] etc.
nrec: For fields which needs like arr.a, arr.b etc.
This is how many times each of the functions can run per second, as
you can see about 1.7x the calls to lua_newtable.
All credits goes to @dalkon, he was too lazy to do it by himself, and
asked me to do it for him.
2013-12-21 15:13:24 +01:00
|
|
|
g_lua.createTable(0, 2);
|
2011-08-19 20:53:23 +02:00
|
|
|
g_lua.pushInteger(point.x);
|
|
|
|
g_lua.setField("x");
|
|
|
|
g_lua.pushInteger(point.y);
|
|
|
|
g_lua.setField("y");
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, Point& point)
|
|
|
|
{
|
|
|
|
if(g_lua.isTable(index)) {
|
|
|
|
g_lua.getField("x", index);
|
|
|
|
point.x = g_lua.popInteger();
|
|
|
|
g_lua.getField("y", index);
|
|
|
|
point.y = g_lua.popInteger();
|
|
|
|
return true;
|
|
|
|
} else if(g_lua.isString()) {
|
2012-05-28 15:06:26 +02:00
|
|
|
return stdext::cast(g_lua.toString(index), point);
|
2011-08-19 20:53:23 +02:00
|
|
|
} else if(g_lua.isNil()) {
|
|
|
|
point = Point();
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-16 18:03:11 +01:00
|
|
|
return false;
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
2011-11-03 20:07:07 +01:00
|
|
|
// size
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const Size& size)
|
2011-11-03 20:07:07 +01:00
|
|
|
{
|
lua: now we have a function to static allocate an array
lua_newtable() definition:
#define lua_newtable(L) lua_createtable(L, 0, 0)
This simply allocates space for 0 fields, meaning, if we push any
values into this array, it will re-allocate the array, which is bad.
This function statically allocates an array, so it's ready to have
X fields into it.
Performance tests:
lua_newtable
1428161
1426992
1413513
lua_createtable
2004544
1974117
1957533
These tests were done on an AMD 8350fx CPU, single thread used.
narr: This is for fields that just have an index, e.g. arr[0] etc.
nrec: For fields which needs like arr.a, arr.b etc.
This is how many times each of the functions can run per second, as
you can see about 1.7x the calls to lua_newtable.
All credits goes to @dalkon, he was too lazy to do it by himself, and
asked me to do it for him.
2013-12-21 15:13:24 +01:00
|
|
|
g_lua.createTable(0, 2);
|
2011-11-03 20:07:07 +01:00
|
|
|
g_lua.pushInteger(size.width());
|
|
|
|
g_lua.setField("width");
|
|
|
|
g_lua.pushInteger(size.height());
|
|
|
|
g_lua.setField("height");
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2011-11-03 20:07:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool luavalue_cast(int index, Size& size)
|
|
|
|
{
|
|
|
|
if(g_lua.isTable(index)) {
|
|
|
|
g_lua.getField("width", index);
|
|
|
|
size.setWidth(g_lua.popInteger());
|
|
|
|
g_lua.getField("height", index);
|
|
|
|
size.setHeight(g_lua.popInteger());
|
|
|
|
return true;
|
|
|
|
} else if(g_lua.isString()) {
|
2012-05-28 15:06:26 +02:00
|
|
|
return stdext::cast(g_lua.toString(index), size);
|
2011-11-03 20:07:07 +01:00
|
|
|
} else if(g_lua.isNil()) {
|
|
|
|
size = Size();
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-16 18:03:11 +01:00
|
|
|
return false;
|
2011-11-03 20:07:07 +01:00
|
|
|
}
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
// otml nodes
|
2012-01-03 01:42:53 +01:00
|
|
|
void push_otml_subnode_luavalue(const OTMLNodePtr& node)
|
2011-08-19 20:53:23 +02:00
|
|
|
{
|
|
|
|
if(node->hasValue()) {
|
2012-07-23 06:33:08 +02:00
|
|
|
union {
|
|
|
|
bool b;
|
|
|
|
double d;
|
|
|
|
long l;
|
|
|
|
};
|
|
|
|
std::string value = node->rawValue();
|
|
|
|
if(stdext::cast(value, b))
|
|
|
|
g_lua.pushBoolean(b);
|
|
|
|
else if(stdext::cast(value, l))
|
|
|
|
g_lua.pushInteger(l);
|
|
|
|
else if(stdext::cast(value, d))
|
|
|
|
g_lua.pushNumber(d);
|
2012-02-02 01:38:42 +01:00
|
|
|
else
|
2012-07-23 06:33:08 +02:00
|
|
|
g_lua.pushString(value);
|
2011-08-19 20:53:23 +02:00
|
|
|
} else if(node->hasChildren()) {
|
|
|
|
g_lua.newTable();
|
|
|
|
bool pushedChild = false;
|
2012-01-16 09:26:57 +01:00
|
|
|
int currentIndex = 1;
|
2011-08-19 20:53:23 +02:00
|
|
|
for(const OTMLNodePtr& cnode : node->children()) {
|
2012-01-16 09:26:57 +01:00
|
|
|
push_otml_subnode_luavalue(cnode);
|
|
|
|
if(!g_lua.isNil()) {
|
|
|
|
if(cnode->isUnique()) {
|
|
|
|
g_lua.pushString(cnode->tag());
|
2012-02-02 01:38:42 +01:00
|
|
|
g_lua.insert(-2);
|
2012-01-16 09:26:57 +01:00
|
|
|
g_lua.rawSet();
|
2011-08-19 20:53:23 +02:00
|
|
|
} else
|
2012-01-16 09:26:57 +01:00
|
|
|
g_lua.rawSeti(currentIndex++);
|
|
|
|
pushedChild = true;
|
|
|
|
} else
|
|
|
|
g_lua.pop();
|
2011-08-19 20:53:23 +02:00
|
|
|
}
|
|
|
|
if(!pushedChild) {
|
|
|
|
g_lua.pop();
|
|
|
|
g_lua.pushNil();
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
g_lua.pushNil();
|
|
|
|
}
|
|
|
|
|
2012-07-05 14:38:48 +02:00
|
|
|
int push_luavalue(const OTMLNodePtr& node)
|
2012-01-03 01:42:53 +01:00
|
|
|
{
|
2012-02-02 01:38:42 +01:00
|
|
|
if(node) {
|
|
|
|
g_lua.newTable();
|
|
|
|
int currentIndex = 1;
|
|
|
|
for(const OTMLNodePtr& cnode : node->children()) {
|
|
|
|
push_otml_subnode_luavalue(cnode);
|
2012-06-04 02:28:19 +02:00
|
|
|
if(cnode->isUnique() && !cnode->tag().empty()) {
|
2012-02-02 01:38:42 +01:00
|
|
|
g_lua.setField(cnode->tag());
|
|
|
|
} else
|
|
|
|
g_lua.rawSeti(currentIndex++);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
g_lua.pushNil();
|
2012-07-05 14:38:48 +02:00
|
|
|
return 1;
|
2012-01-03 01:42:53 +01:00
|
|
|
}
|
|
|
|
|
2011-11-13 05:13:07 +01:00
|
|
|
bool luavalue_cast(int index, OTMLNodePtr& node)
|
|
|
|
{
|
|
|
|
node = OTMLNode::create();
|
|
|
|
node->setUnique(true);
|
|
|
|
if(g_lua.isTable(index)) {
|
|
|
|
g_lua.pushNil();
|
|
|
|
while(g_lua.next(index < 0 ? index-1 : index)) {
|
2012-02-02 01:10:55 +01:00
|
|
|
std::string cnodeName;
|
2013-02-18 17:16:22 +01:00
|
|
|
if(g_lua.isString(-2)) {
|
|
|
|
g_lua.pushValue(-2);
|
|
|
|
cnodeName = g_lua.toString();
|
|
|
|
g_lua.pop();
|
|
|
|
} else
|
|
|
|
assert(g_lua.isNumber());
|
2011-11-13 05:13:07 +01:00
|
|
|
if(g_lua.isTable()) {
|
|
|
|
OTMLNodePtr cnode;
|
2012-02-02 00:37:40 +01:00
|
|
|
if(luavalue_cast(-1, cnode)) {
|
2012-02-02 01:38:42 +01:00
|
|
|
if(cnodeName.empty())
|
2013-01-17 11:38:44 +01:00
|
|
|
cnode->setUnique(false);
|
2012-02-02 01:10:55 +01:00
|
|
|
else
|
|
|
|
cnode->setTag(cnodeName);
|
2011-11-13 05:13:07 +01:00
|
|
|
node->addChild(cnode);
|
|
|
|
}
|
2012-02-02 01:10:55 +01:00
|
|
|
} else {
|
2012-02-02 01:38:42 +01:00
|
|
|
std::string value;
|
|
|
|
if(g_lua.isBoolean())
|
2012-05-28 15:06:26 +02:00
|
|
|
value = stdext::unsafe_cast<std::string>(g_lua.toBoolean());
|
2012-02-02 01:38:42 +01:00
|
|
|
else
|
|
|
|
value = g_lua.toString();
|
|
|
|
if(cnodeName.empty())
|
2012-02-02 01:10:55 +01:00
|
|
|
node->writeIn(value);
|
|
|
|
else
|
|
|
|
node->writeAt(cnodeName, value);
|
|
|
|
}
|
2011-11-13 05:13:07 +01:00
|
|
|
g_lua.pop();
|
|
|
|
}
|
2011-11-16 18:03:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-11-13 05:13:07 +01:00
|
|
|
}
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
// object ptr
|
|
|
|
bool luavalue_cast(int index, LuaObjectPtr& obj) {
|
|
|
|
if(g_lua.isUserdata(index)) {
|
|
|
|
obj = g_lua.toObject(index);
|
|
|
|
return true;
|
|
|
|
} else if(g_lua.isNil(index)) {
|
|
|
|
obj = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|