lua_ext attribute with bson storage of lua objects.

This commit is contained in:
Enno Rehling 2010-02-15 03:54:13 +00:00
parent d811437804
commit 198bb82c52
12 changed files with 547 additions and 77 deletions

View File

@ -4,6 +4,7 @@
#ifdef BINDINGS_TOLUA
#include "eressea/tolua/bindings.c"
#include "eressea/tolua/bind_attrib.c"
#include "eressea/tolua/bind_sqlite.c"
#include "eressea/tolua/bind_unit.c"
#include "eressea/tolua/bind_ship.c"

View File

@ -5,3 +5,5 @@
#include <eressea/server.c>
#include <external/sqlite3.c>
#include <external/md5.c>
#include <external/bson/bson.c>
#include <external/bson/numbers.c>

View File

@ -211,6 +211,34 @@ bin_r_str_buf(struct storage * store, char * result, size_t size)
}
}
static int
bin_w_bin(struct storage * store, void * arg, size_t size)
{
int result;
int len = (int)size;
result = store->w_int(store, len);
if (len>0) {
result += (int)fwrite(arg, len, 1, file(store));
}
return result;
}
static void
bin_r_bin(struct storage * store, void * result, size_t size)
{
int len = store->r_int(store);
if (len>0) {
if ((size_t)len>size) {
log_error(("destination buffer too small %d %u.\n", len, size));
fseek(file(store), len, SEEK_CUR);
} else {
fread(result, len, 1, file(store));
}
}
}
static int
bin_open(struct storage * store, const char * filename, int mode)
{
@ -254,6 +282,7 @@ const storage binary_store = {
bin_w_int_pak, bin_r_int_pak, /* id storage */
bin_w_str, bin_r_str, bin_r_str_buf, /* token storage */
bin_w_str, bin_r_str, bin_r_str_buf, /* string storage */
bin_w_bin, bin_r_bin, /* binary storage */
bin_open, bin_close,
0, 0, NULL
};

View File

@ -172,6 +172,19 @@ txt_open(struct storage * store, const char * filename, int mode)
return (F==NULL);
}
static int
txt_w_bin(struct storage * store, void * arg, size_t size)
{
assert(!"not implemented!");
return 0;
}
static void
txt_r_bin(struct storage * store, void * result, size_t size)
{
assert(!"not implemented!");
}
static int
txt_close(struct storage * store)
{
@ -185,6 +198,7 @@ const storage text_store = {
txt_w_id, txt_r_id,
txt_w_tok, txt_r_tok, txt_r_tok_buf,
txt_w_str, txt_r_str, txt_r_str_buf,
txt_w_bin, txt_r_bin,
txt_open, txt_close,
0, 0, NULL
};

View File

@ -24,6 +24,9 @@ typedef struct storage {
int (*w_str)(struct storage *, const char * tok);
char * (*r_str)(struct storage *);
void (*r_str_buf)(struct storage *, char * result, size_t size);
/* binary data: */
int (*w_bin)(struct storage *, void * arg, size_t size);
void (*r_bin)(struct storage *, void * result, size_t size);
int (*open)(struct storage *, const char * filename, int mode);
int (*close)(struct storage *);

View File

@ -276,6 +276,14 @@
<Filter
Name="tolua"
>
<File
RelativePath=".\tolua\bind_attrib.c"
>
</File>
<File
RelativePath=".\tolua\bind_attrib.h"
>
</File>
<File
RelativePath=".\tolua\bind_building.c"
>
@ -369,6 +377,46 @@
>
</File>
</Filter>
<Filter
Name="external"
>
<File
RelativePath="..\external\bson\bson.c"
>
</File>
<File
RelativePath="..\external\bson\bson.h"
>
</File>
<File
RelativePath="..\external\md5.c"
>
</File>
<File
RelativePath="..\external\md5.h"
>
</File>
<File
RelativePath="..\external\bson\numbers.c"
>
</File>
<File
RelativePath="..\external\bson\platform_hacks.h"
>
</File>
<File
RelativePath="..\external\sqlite3.c"
>
</File>
<File
RelativePath="..\external\sqlite3.h"
>
</File>
<File
RelativePath="..\external\sqlite3ext.h"
>
</File>
</Filter>
<File
RelativePath=".\console.c"
>

View File

@ -103,15 +103,16 @@
#include <lauxlib.h>
#include "tolua/bindings.h"
#include "tolua/helpers.h"
#include "tolua/bind_unit.h"
#include "tolua/bind_faction.h"
#include "tolua/bind_message.h"
#include "tolua/bind_hashtable.h"
#include "tolua/bind_ship.h"
#include "tolua/bind_attrib.h"
#include "tolua/bind_building.h"
#include "tolua/bind_region.h"
#include "tolua/bind_faction.h"
#include "tolua/bind_gmtool.h"
#include "tolua/bind_hashtable.h"
#include "tolua/bind_message.h"
#include "tolua/bind_region.h"
#include "tolua/bind_ship.h"
#include "tolua/bind_storage.h"
#include "tolua/bind_unit.h"
#endif // BINDINGS_TOLUA
#ifdef BINDINGS_LUABIND
@ -297,6 +298,7 @@ lua_init(void)
tolua_ship_open(L);
tolua_region_open(L);
tolua_faction_open(L);
tolua_attrib_open(L);
tolua_unit_open(L);
tolua_message_open(L);
tolua_hashtable_open(L);

View File

@ -0,0 +1,357 @@
/* vi: set ts=2:
+-------------------+
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
| (c) 1998 - 2010 | Katja Zedel <katze@felidae.kn-bremen.de>
| | Henning Peters <faroul@beyond.kn-bremen.de>
+-------------------+
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <config.h>
#include "bind_attrib.h"
#include <kernel/eressea.h>
#include <kernel/unit.h>
#include <kernel/ship.h>
#include <kernel/building.h>
#include <kernel/region.h>
#include <kernel/objtypes.h>
#include <util/attrib.h>
#include <util/log.h>
#include <util/resolve.h>
#include <util/storage.h>
#include <bson/bson.h>
#include <lua.h>
#include <tolua.h>
#include <errno.h>
static void
init_ext(attrib * a) {
}
static void
free_ext(attrib * a) {
lua_State * L = (lua_State *)global.vm_state;
if (a->data.i>0) {
luaL_unref(L, LUA_REGISTRYINDEX, a->data.i);
}
}
static int
age_ext(attrib * a) {
return AT_AGE_KEEP;
}
static void
write_ext_i(lua_State * L, const char * name, bson_buffer * bb)
{
int type = lua_type(L, -1);
switch (type) {
case LUA_TNUMBER:
{
double value = tolua_tonumber(L, -1, 0);
bson_append_double(bb, name, value);
}
break;
case LUA_TSTRING:
{
const char * value = tolua_tostring(L, -1, 0);
bson_append_string(bb, name, value);
}
break;
case LUA_TTABLE:
{
int n = luaL_getn(L, -1);
if (n) {
bson_buffer * arr = bson_append_start_array(bb, name);
int i;
for (i=0;i!=n;++i) {
char num[12];
bson_numstr(num, i);
lua_rawgeti(L, -1, i+1);
write_ext_i(L, num, arr);
lua_pop(L, 1);
}
bson_append_finish_object(arr);
} else {
bson_buffer * sub = bson_append_start_object(bb, name);
lua_pushnil(L); /* first key */
while (lua_next(L, -2) != 0) {
const char * key;
/* uses 'key' (at index -2) and 'value' (at index -1) */
lua_pushvalue(L, -2);
key = lua_tolstring(L, -1, 0);
lua_pushvalue(L, -2);
if (key) {
write_ext_i(L, key, sub);
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 3);
}
bson_append_finish_object(sub);
}
}
break;
case LUA_TUSERDATA:
{
tolua_Error tolua_err;
if (tolua_isusertype(L, -1, "unit", 0, &tolua_err)) {
unit * u = (unit *)tolua_tousertype(L, -1, 0);
bson_oid_t oid;
oid.ints[0] = TYP_UNIT;
oid.ints[1] = u->no;
bson_append_oid(bb, name, &oid);
} else if (tolua_isusertype(L, -1, "region", 0, &tolua_err)) {
region * r = (region *)tolua_tousertype(L, -1, 0);
bson_oid_t oid;
oid.ints[0] = TYP_REGION;
oid.ints[1] = r->uid;
bson_append_oid(bb, name, &oid);
} else if (tolua_isusertype(L, -1, "ship", 0, &tolua_err)) {
ship * sh = (ship *)tolua_tousertype(L, -1, 0);
bson_oid_t oid;
oid.ints[0] = TYP_SHIP;
oid.ints[1] = sh->no;
bson_append_oid(bb, name, &oid);
} else if (tolua_isusertype(L, -1, "building", 0, &tolua_err)) {
building * b = (building *)tolua_tousertype(L, -1, 0);
bson_oid_t oid;
oid.ints[0] = TYP_BUILDING;
oid.ints[1] = b->no;
bson_append_oid(bb, name, &oid);
} else {
log_error(("unsuported type.\n"));
bson_append_null(bb, name);
}
}
break;
default:
bson_append_null(bb, name);
break;
}
}
static void
write_ext(const attrib * a, struct storage * store) {
lua_State * L = (lua_State *)global.vm_state;
if (a->data.i>0) {
int handle = a->data.i;
bson_buffer bb;
bson b;
bson_buffer_init( & bb );
lua_rawgeti(L, LUA_REGISTRYINDEX, handle);
write_ext_i(L, "_data", &bb);
bson_from_buffer(&b, &bb);
store->w_int(store, bson_size(&b));
store->w_bin(store, b.data, bson_size(&b));
bson_destroy(&b);
}
}
static int
read_ext_i(lua_State * L, bson_iterator * it, bson_type type)
{
switch (type) {
case bson_double:
{
lua_pushnumber(L, bson_iterator_double(it));
}
break;
case bson_string:
{
lua_pushstring(L, bson_iterator_string(it));
}
break;
case bson_array:
{
bson_iterator sub;
int err;
bson_iterator_subiterator(it, &sub);
lua_newtable(L);
if (bson_iterator_more(&sub)) {
bson_type ctype;
for (ctype = bson_iterator_next(&sub); bson_iterator_more(&sub); ctype = bson_iterator_next(&sub)) {
int i = atoi(bson_iterator_key(&sub));
err = read_ext_i(L, &sub, ctype);
if (err) {
lua_pop(L, 1);
return err;
}
lua_rawseti(L, -2, i+1);
}
}
}
break;
case bson_object:
{
bson_iterator sub;
int err;
bson_iterator_subiterator(it, &sub);
lua_newtable(L);
if (bson_iterator_more(&sub)) {
bson_type ctype;
for (ctype = bson_iterator_next(&sub); bson_iterator_more(&sub); ctype = bson_iterator_next(&sub)) {
lua_pushstring(L, bson_iterator_key(&sub));
err = read_ext_i(L, &sub, ctype);
if (err) {
lua_pop(L, 1);
return err;
}
lua_rawset(L, -3);
}
}
}
break;
case bson_oid:
{
bson_oid_t * oid = bson_iterator_oid(it);
if (oid->ints[0]==TYP_UNIT) {
unit * u = findunit(oid->ints[1]);
if (u) tolua_pushusertype(L, u, "unit");
else lua_pushnil(L);
} else if (oid->ints[0]==TYP_REGION) {
region * r = findregionbyid(oid->ints[1]);
if (r) tolua_pushusertype(L, r, "region");
else lua_pushnil(L);
} else if (oid->ints[0]==TYP_SHIP) {
ship * sh = findship(oid->ints[1]);
if (sh) tolua_pushusertype(L, sh, "ship");
else lua_pushnil(L);
} else if (oid->ints[0]==TYP_BUILDING) {
building * b = findbuilding(oid->ints[1]);
if (b) tolua_pushusertype(L, b, "building");
else lua_pushnil(L);
}
else {
log_error(("unknown oid %d %d %d\n", oid->ints[0], oid->ints[3], oid->ints[2]));
lua_pushnil(L);
}
}
break;
case bson_null:
lua_pushnil(L);
break;
case bson_eoo:
return EFAULT;
default:
return EINVAL;
}
return 0;
}
static int
resolve_bson(variant data, void * address)
{
lua_State * L = (lua_State *)global.vm_state;
bson b;
int err;
bson_iterator it;
attrib * a = (attrib*)address;
char * buffer = data.v;
bson_init(&b, buffer, 1);
bson_iterator_init(&it, b.data);
err = read_ext_i(L, &it, bson_iterator_next(&it));
a->data.i = luaL_ref(L, LUA_REGISTRYINDEX);
bson_destroy(&b);
return err?AT_READ_FAIL:AT_READ_OK;
}
static int
read_ext(attrib * a, struct storage * store) {
variant data;
int len = store->r_int(store);
data.v = bson_malloc(len);
store->r_bin(store, data.v, (size_t)len);
a->data.v = 0;
ur_add(data, a, resolve_bson);
return AT_READ_OK;
};
attrib_type at_lua_ext = {
"lua", init_ext, free_ext, age_ext, write_ext, read_ext
};
static int
tolua_attrib_create(lua_State* L)
{
attrib ** ap = NULL;
tolua_Error tolua_err;
if (tolua_isusertype(L, 1, TOLUA_CAST "unit", 0, &tolua_err)) {
unit * u = (unit *)tolua_tousertype(L, 1, 0);
ap = &u->attribs;
}
if (ap) {
attrib * a = a_new(&at_lua_ext);
int handle;
lua_pushvalue(L, 2);
handle = luaL_ref(L, LUA_REGISTRYINDEX);
a->data.i = handle;
a_add(ap, a);
tolua_pushusertype(L, (void*)a, TOLUA_CAST "attrib");
return 1;
}
return 0;
}
int
tolua_attrib_data(lua_State * L)
{
attrib * a = (attrib *)tolua_tousertype(L, 1, 0);
if (a && a->data.i) {
lua_rawgeti(L, LUA_REGISTRYINDEX, a->data.i);
return 1;
}
return 0;
}
attrib *
tolua_get_lua_ext(struct attrib * alist)
{
while (alist && alist->type!=&at_lua_ext) alist = alist->next;
return alist;
}
int
tolua_attriblist_next(lua_State *L)
{
attrib** attrib_ptr = (attrib **)lua_touserdata(L, lua_upvalueindex(1));
attrib * a = *attrib_ptr;
if (a != NULL) {
tolua_pushusertype(L, (void*)a, TOLUA_CAST "attrib");
*attrib_ptr = tolua_get_lua_ext(a->next);
return 1;
}
else return 0; /* no more values to return */
}
void
tolua_attrib_open(lua_State* L)
{
at_register(&at_lua_ext);
tolua_usertype(L, TOLUA_CAST "attrib");
tolua_module(L, NULL, 0);
tolua_beginmodule(L, NULL);
{
tolua_cclass(L, TOLUA_CAST "attrib", TOLUA_CAST "attrib", TOLUA_CAST "", NULL);
tolua_beginmodule(L, TOLUA_CAST "attrib");
{
tolua_function(L, TOLUA_CAST "create", &tolua_attrib_create);
tolua_variable(L, TOLUA_CAST "data", &tolua_attrib_data, NULL);
}
tolua_endmodule(L);
}
tolua_endmodule(L);
}

View File

@ -0,0 +1,24 @@
/* vi: set ts=2:
+-------------------+
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
| (c) 1998 - 2010 | Katja Zedel <katze@felidae.kn-bremen.de>
| | Henning Peters <faroul@beyond.kn-bremen.de>
+-------------------+
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#ifdef __cplusplus
extern "C" {
#endif
struct attrib;
void tolua_attrib_open(struct lua_State *L);
struct attrib * tolua_get_lua_ext(struct attrib * alist);
int tolua_attriblist_next(struct lua_State *L);
#ifdef __cplusplus
}
#endif

View File

@ -13,6 +13,7 @@ without prior permission by the authors of Eressea.
#include <config.h>
#include "bind_unit.h"
#include "bind_attrib.h"
#include "bindings.h"
// attributes includes
@ -57,6 +58,23 @@ tolua_unit_get_objects(lua_State* L)
return 1;
}
static int
tolua_unit_get_attribs(lua_State* L)
{
unit * self = (unit *)tolua_tousertype(L, 1, 0);
attrib ** attrib_ptr = (attrib**)lua_newuserdata(L, sizeof(attrib *));
attrib * a = tolua_get_lua_ext(self->attribs);
luaL_getmetatable(L, "attrib");
lua_setmetatable(L, -2);
*attrib_ptr = a;
lua_pushcclosure(L, tolua_attriblist_next, 1);
return 1;
}
int tolua_unitlist_nextf(lua_State *L)
{
unit** unit_ptr = (unit **)lua_touserdata(L, lua_upvalueindex(1));
@ -993,6 +1011,7 @@ tolua_unit_open(lua_State * L)
tolua_variable(L, TOLUA_CAST "hp_max", &tolua_unit_get_hpmax, 0);
tolua_variable(L, TOLUA_CAST "objects", &tolua_unit_get_objects, 0);
tolua_variable(L, TOLUA_CAST "attribs", &tolua_unit_get_attribs, 0);
}
tolua_endmodule(L);
}

View File

@ -1,71 +0,0 @@
<package name="eressea">
<type name="message"/>
<type name="unit"/>
<type name="region"/>
<type name="faction"/>
<type name="ship"/>
<type name="building"/>
<type name="event"/>
<module>
<function name="atoi36" type="int">
<call expr="atoi36(str)" />
<arg name="str" type="cstring"/>
</function>
<class name="message" ctype="struct message" base=""/>
<module name="message">
<static name="set_unit">
<arg name="u" type="unit"/>
</static>
</module>
<class name="unit" ctype="struct unit" base=""/>
<module name="unit">
<function name="create" type="unit">
<call expr="create_unit(r, f, 0, f-&gt;race, 0, 0, 0)" />
<arg name="f" type="faction"/>
<arg name="r" type="region"/>
</function>
<method name="add_order">
<call expr="unit_addorder(self, parse_order(str, self-&gt;faction-&gt;locale))" />
<arg name="str" type="cstring"/>
</method>
<variable name="name" type="cstring">
<getter name="unit_getname"/>
<setter name="unit_setname"/>
</variable>
<variable name="info" type="cstring">
<getter name="unit_getinfo"/>
<setter name="unit_setinfo"/>
</variable>
<variable name="magic" type="cstring">
<getter name="unit_getmagic"/>
<setter name="unit_setmagic"/>
</variable>
<variable name="flags" type="int">
<readwrite name="flags"/>
</variable>
</module>
<class name="faction" ctype="struct faction" base=""/>
<module name="faction">
<variable name="name" type="cstring">
<getter name="faction_getname"/>
<setter name="faction_setname"/>
</variable>
<variable name="heroes" type="int">
<getter name="countheroes"/>
</variable>
<variable name="maxheroes" type="int">
<getter name="maxheroes"/>
</variable>
<variable name="id" type="int">
<readonly name="no"/>
</variable>
<variable name="age" type="int">
<readonly name="age"/>
</variable>
<variable name="score" type="int">
<readonly name="score"/>
</variable>
</module>
</module>
</package>

View File

@ -6,6 +6,48 @@ function setup()
free_game()
end
function has_attrib(u, value)
for a in u.attribs do
if (a.data==value) then return true end
end
return false
end
function test_attrib()
local r = region.create(0,0, "plain")
local f = faction.create("noreply@eressea.de", "human", "de")
local u = unit.create(f, r, 1)
local u2 = unit.create(f, r, 1)
data = { arr = { 'a', 'b', 'c' }, name = 'familiar', events = { die = 'familiar_died' }, data = { mage = u2 } }
a = { 'a' }
b = { 'a' }
uno = u.id
u2no = u2.id
a = attrib.create(u, 12)
a = attrib.create(u, "enno")
a = attrib.create(u, u2)
a = attrib.create(u, data)
write_game("attrib.dat")
free_game()
read_game("attrib.dat")
u = get_unit(uno)
u2 = get_unit(u2no)
assert_false(has_attrib(u, 42))
assert_true(has_attrib(u, "enno"))
assert_true(has_attrib(u, 12))
for a in u.attribs do
x = a.data
if (type(x)=="table") then
assert_equal('a', x.arr[1])
assert_equal('familiar', x.name)
assert_equal('familiar_died', x.events.die)
assert_equal(u2, x.data.mage)
break
end
end
end
function test_seecast()
local r = region.create(0,0, "plain")
for i = 1,10 do