server/src/bind_attrib.c

415 lines
11 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/* 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 <platform.h>
#include "bind_attrib.h"
#include <kernel/config.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
2010-08-08 10:06:34 +02:00
#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 <storage.h>
2010-08-08 10:06:34 +02:00
/* external libraries */
#include <bson.h>
2010-08-08 10:06:34 +02:00
#include <lua.h>
#include <tolua.h>
#include <errno.h>
2011-03-07 08:02:35 +01:00
static void init_ext(attrib * a)
{
lua_State *L = (lua_State *) global.vm_state;
2010-08-08 10:06:34 +02:00
lua_pushstring(L, "callbacks");
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_istable(L, -1)) {
lua_pushstring(L, "attrib_init");
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) {
lua_rawgeti(L, LUA_REGISTRYINDEX, a->data.i);
2011-03-07 08:02:35 +01:00
if (lua_pcall(L, 1, 0, 0) != 0) {
const char *error = lua_tostring(L, -1);
log_error("attrib_init '%d': %s.\n", a->data.i, error);
2010-08-08 10:06:34 +02:00
}
}
}
}
2011-03-07 08:02:35 +01:00
static void free_ext(attrib * a)
{
lua_State *L = (lua_State *) global.vm_state;
if (a->data.i > 0) {
2010-08-08 10:06:34 +02:00
luaL_unref(L, LUA_REGISTRYINDEX, a->data.i);
}
}
2011-03-07 08:02:35 +01:00
static int age_ext(attrib * a)
{
2010-08-08 10:06:34 +02:00
return AT_AGE_KEEP;
}
2011-03-07 08:02:35 +01:00
static void write_ext_i(lua_State * L, const char *name, bson_buffer * bb)
2010-08-08 10:06:34 +02:00
{
int type = lua_type(L, -1);
switch (type) {
case LUA_TNUMBER:
2011-03-07 08:02:35 +01:00
{
double value = tolua_tonumber(L, -1, 0);
bson_append_double(bb, name, value);
}
2010-08-08 10:06:34 +02:00
break;
case LUA_TSTRING:
2011-03-07 08:02:35 +01:00
{
const char *value = tolua_tostring(L, -1, 0);
bson_append_string(bb, name, value);
}
2010-08-08 10:06:34 +02:00
break;
case LUA_TTABLE:
2011-03-07 08:02:35 +01:00
{
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);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 3);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
bson_append_finish_object(sub);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
break;
case LUA_TUSERDATA:
2011-03-07 08:02:35 +01:00
{
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");
2011-03-07 08:02:35 +01:00
bson_append_null(bb, name);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
break;
default:
bson_append_null(bb, name);
break;
}
}
static void
2011-03-07 08:02:35 +01:00
write_ext(const attrib * a, const void *owner, struct storage *store)
{
lua_State *L = (lua_State *) global.vm_state;
if (a->data.i > 0) {
2010-08-08 10:06:34 +02:00
int handle = a->data.i;
bson_buffer bb;
bson b;
2011-03-07 08:02:35 +01:00
bson_buffer_init(&bb);
2010-08-08 10:06:34 +02:00
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);
}
}
2011-03-07 08:02:35 +01:00
static int read_ext_i(lua_State * L, bson_iterator * it, bson_type type)
2010-08-08 10:06:34 +02:00
{
switch (type) {
case bson_double:
2011-03-07 08:02:35 +01:00
{
lua_pushnumber(L, bson_iterator_double(it));
}
2010-08-08 10:06:34 +02:00
break;
case bson_string:
2011-03-07 08:02:35 +01:00
{
lua_pushstring(L, bson_iterator_string(it));
}
2010-08-08 10:06:34 +02:00
break;
case bson_array:
2011-03-07 08:02:35 +01:00
{
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;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
lua_rawseti(L, -2, i + 1);
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
break;
case bson_object:
2011-03-07 08:02:35 +01:00
{
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;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
lua_rawset(L, -3);
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
break;
case bson_oid:
2011-03-07 08:02:35 +01:00
{
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
2010-08-08 10:06:34 +02:00
lua_pushnil(L);
2011-03-07 08:02:35 +01:00
} 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[1], oid->ints[2]);
2011-03-07 08:02:35 +01:00
lua_pushnil(L);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
break;
case bson_null:
lua_pushnil(L);
break;
case bson_eoo:
return EFAULT;
default:
return EINVAL;
}
return 0;
}
2011-03-07 08:02:35 +01:00
static int resolve_bson(variant data, void *address)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_State *L = (lua_State *) global.vm_state;
2010-08-08 10:06:34 +02:00
bson b;
int err;
bson_iterator it;
2011-03-07 08:02:35 +01:00
attrib *a = (attrib *) address;
char *buffer = data.v;
2010-08-08 10:06:34 +02:00
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);
2011-03-07 08:02:35 +01:00
return err ? AT_READ_FAIL : AT_READ_OK;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static int read_ext(attrib * a, void *owner, struct storage *store)
{
2010-08-08 10:06:34 +02:00
variant data;
int len = store->r_int(store);
data.v = bson_malloc(len);
2011-03-07 08:02:35 +01:00
store->r_bin(store, data.v, (size_t) len);
2010-08-08 10:06:34 +02:00
a->data.v = 0;
ur_add(data, a, resolve_bson);
return AT_READ_OK;
2011-03-07 17:26:50 +01:00
}
2010-08-08 10:06:34 +02:00
attrib_type at_lua_ext = {
"lua", init_ext, free_ext, age_ext, write_ext, read_ext
};
2011-03-07 08:02:35 +01:00
static attrib **get_attribs(lua_State * L, int idx)
{
attrib **ap = NULL;
2010-08-08 10:06:34 +02:00
tolua_Error tolua_err;
2011-02-06 08:01:51 +01:00
if (tolua_isusertype(L, idx, TOLUA_CAST "unit", 0, &tolua_err)) {
2011-03-07 08:02:35 +01:00
unit *u = (unit *) tolua_tousertype(L, idx, 0);
if (u)
ap = &u->attribs;
2011-02-06 08:01:51 +01:00
} else if (tolua_isusertype(L, idx, TOLUA_CAST "region", 0, &tolua_err)) {
2011-03-07 08:02:35 +01:00
region *r = (region *) tolua_tousertype(L, idx, 0);
if (r)
ap = &r->attribs;
2011-02-06 08:01:51 +01:00
} else if (tolua_isusertype(L, idx, TOLUA_CAST "faction", 0, &tolua_err)) {
2011-03-07 08:02:35 +01:00
faction *f = (faction *) tolua_tousertype(L, idx, 0);
if (f)
ap = &f->attribs;
2011-02-06 08:01:51 +01:00
} else if (lua_isstring(L, idx)) {
2011-03-07 08:02:35 +01:00
const char *str = tolua_tostring(L, idx, NULL);
if (str && strcmp(str, "global") == 0) {
2011-02-06 08:01:51 +01:00
ap = &global.attribs;
}
2010-08-08 10:06:34 +02:00
}
2011-02-06 08:01:51 +01:00
return ap;
}
2011-03-07 08:02:35 +01:00
static int tolua_attrib_create(lua_State * L)
2011-02-06 08:01:51 +01:00
{
2011-03-07 08:02:35 +01:00
attrib **ap = get_attribs(L, 1);
2010-08-08 10:06:34 +02:00
if (ap) {
2011-03-07 08:02:35 +01:00
attrib *a = a_new(&at_lua_ext);
2010-08-08 10:06:34 +02:00
int handle;
lua_pushvalue(L, 2);
handle = luaL_ref(L, LUA_REGISTRYINDEX);
a->data.i = handle;
a_add(ap, a);
2011-03-07 08:02:35 +01:00
tolua_pushusertype(L, (void *)a, TOLUA_CAST "attrib");
2010-08-08 10:06:34 +02:00
return 1;
}
return 0;
}
2011-03-07 08:02:35 +01:00
int tolua_attrib_data(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
attrib *a = (attrib *) tolua_tousertype(L, 1, 0);
2010-08-08 10:06:34 +02:00
if (a && a->data.i) {
lua_rawgeti(L, LUA_REGISTRYINDEX, a->data.i);
return 1;
}
return 0;
}
2011-03-07 08:02:35 +01:00
attrib *tolua_get_lua_ext(struct attrib * alist)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
while (alist && alist->type != &at_lua_ext)
alist = alist->next;
2010-08-08 10:06:34 +02:00
return alist;
}
2011-03-07 08:02:35 +01:00
int tolua_attriblist_next(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
attrib **attrib_ptr = (attrib **) lua_touserdata(L, lua_upvalueindex(1));
attrib *a = *attrib_ptr;
2010-08-08 10:06:34 +02:00
if (a != NULL) {
2011-03-07 08:02:35 +01:00
tolua_pushusertype(L, (void *)a, TOLUA_CAST "attrib");
2010-08-08 10:06:34 +02:00
*attrib_ptr = tolua_get_lua_ext(a->next);
return 1;
2011-03-07 08:02:35 +01:00
} else
return 0; /* no more values to return */
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int tolua_attrib_get(lua_State * L)
{
attrib **ap = get_attribs(L, 1);
2011-02-06 08:11:40 +01:00
if (ap) {
2011-03-07 08:02:35 +01:00
attrib *a = tolua_get_lua_ext(*ap);
attrib **attrib_ptr = (attrib **) lua_newuserdata(L, sizeof(attrib *));
2011-02-06 08:11:40 +01:00
luaL_getmetatable(L, "attrib");
lua_setmetatable(L, -2);
*attrib_ptr = a;
lua_pushcclosure(L, tolua_attriblist_next, 1);
return 1;
2011-03-07 08:02:35 +01:00
} else
return 0;
2011-02-06 08:11:40 +01:00
}
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
void tolua_attrib_open(lua_State * L)
2010-08-08 10:06:34 +02:00
{
at_register(&at_lua_ext);
tolua_usertype(L, TOLUA_CAST "attrib");
tolua_module(L, NULL, 0);
tolua_beginmodule(L, NULL);
{
2011-03-07 08:02:35 +01:00
tolua_cclass(L, TOLUA_CAST "attrib", TOLUA_CAST "attrib", TOLUA_CAST "",
NULL);
2010-08-08 10:06:34 +02:00
tolua_beginmodule(L, TOLUA_CAST "attrib");
{
tolua_function(L, TOLUA_CAST "create", &tolua_attrib_create);
2011-02-06 08:11:40 +01:00
tolua_function(L, TOLUA_CAST "get", &tolua_attrib_get);
2010-08-08 10:06:34 +02:00
tolua_variable(L, TOLUA_CAST "data", &tolua_attrib_data, NULL);
}
tolua_endmodule(L);
}
tolua_endmodule(L);
}