server/src/bindings/bind_storage.c

140 lines
3.8 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 - 2008 | 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 <kernel/types.h>
#include "bind_storage.h"
#include <util/storage.h>
#include <kernel/save.h>
#include <kernel/textstore.h>
#include <kernel/binarystore.h>
#include <math.h>
#include <lua.h>
#include <tolua.h>
2011-03-07 08:02:35 +01:00
static int tolua_storage_create(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
const char *filename = tolua_tostring(L, 1, 0);
const char *type = tolua_tostring(L, 2, "rb");
storage *store = 0;
2010-08-08 10:06:34 +02:00
int mode = IO_READ;
if (strchr(type, 't')) {
store = malloc(sizeof(text_store));
memcpy(store, &text_store, sizeof(text_store));
} else {
store = malloc(sizeof(binary_store));
memcpy(store, &binary_store, sizeof(binary_store));
}
2011-03-07 08:02:35 +01:00
if (strchr(type, 'r'))
mode = IO_READ;
if (strchr(type, 'w'))
mode = IO_WRITE;
2010-08-08 10:06:34 +02:00
store->open(store, filename, mode);
2011-03-07 08:02:35 +01:00
tolua_pushusertype(L, (void *)store, TOLUA_CAST "storage");
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_storage_read_unit(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
storage *self = (storage *) tolua_tousertype(L, 1, 0);
struct unit *u = read_unit(self);
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_storage_write_unit(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
storage *store = (storage *) tolua_tousertype(L, 1, 0);
struct unit *u = (struct unit *)tolua_tousertype(L, 2, 0);
if (store->version) {
write_unit(store, u);
}
2010-08-08 10:06:34 +02:00
return 0;
}
2011-03-07 08:02:35 +01:00
static int tolua_storage_read_float(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
storage *self = (storage *) tolua_tousertype(L, 1, 0);
2010-08-08 10:06:34 +02:00
float num = self->r_flt(self);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) num);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_storage_read_int(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
storage *self = (storage *) tolua_tousertype(L, 1, 0);
2010-08-08 10:06:34 +02:00
int num = self->r_int(self);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) num);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_storage_write(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
storage *self = (storage *) tolua_tousertype(L, 1, 0);
if (self->version && tolua_isnumber(L, 2, 0, 0)) {
2010-08-08 10:06:34 +02:00
lua_Number num = tolua_tonumber(L, 2, 0);
double n;
2011-03-07 08:02:35 +01:00
if (modf(num, &n) == 0.0) {
2010-08-08 10:06:34 +02:00
self->w_int(self, (int)num);
} else {
self->w_flt(self, (float)num);
}
}
return 0;
}
2011-03-07 08:02:35 +01:00
static int tolua_storage_tostring(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
storage *self = (storage *) tolua_tousertype(L, 1, 0);
2010-08-08 10:06:34 +02:00
char name[64];
2011-03-07 08:02:35 +01:00
snprintf(name, sizeof(name), "<storage enc=%d ver=%d>", self->encoding,
self->version);
2010-08-08 10:06:34 +02:00
lua_pushstring(L, name);
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_storage_close(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
storage *self = (storage *) tolua_tousertype(L, 1, 0);
2010-08-08 10:06:34 +02:00
self->close(self);
return 0;
}
2011-03-07 08:02:35 +01:00
void tolua_storage_open(lua_State * L)
2010-08-08 10:06:34 +02:00
{
/* register user types */
tolua_usertype(L, TOLUA_CAST "storage");
tolua_module(L, NULL, 0);
tolua_beginmodule(L, NULL);
{
2011-03-07 08:02:35 +01:00
tolua_cclass(L, TOLUA_CAST "storage", TOLUA_CAST "storage", TOLUA_CAST "",
NULL);
2010-08-08 10:06:34 +02:00
tolua_beginmodule(L, TOLUA_CAST "storage");
{
tolua_function(L, TOLUA_CAST "__tostring", tolua_storage_tostring);
tolua_function(L, TOLUA_CAST "write", tolua_storage_write);
tolua_function(L, TOLUA_CAST "read_int", tolua_storage_read_int);
tolua_function(L, TOLUA_CAST "read_float", tolua_storage_read_float);
tolua_function(L, TOLUA_CAST "write_unit", tolua_storage_write_unit);
tolua_function(L, TOLUA_CAST "read_unit", tolua_storage_read_unit);
tolua_function(L, TOLUA_CAST "close", tolua_storage_close);
tolua_function(L, TOLUA_CAST "create", tolua_storage_create);
}
tolua_endmodule(L);
}
tolua_endmodule(L);
}