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 "bind_hashtable.h"
|
|
|
|
|
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/ship.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
|
|
|
|
#include <attributes/object.h>
|
|
|
|
|
|
|
|
#include <util/variant.h>
|
|
|
|
#include <util/attrib.h>
|
|
|
|
|
|
|
|
#include <tolua.h>
|
2013-12-29 14:38:58 +01:00
|
|
|
#include <lua.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <string.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_hashtable_get(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
hashtable self = (hashtable) tolua_tousertype(L, 1, 0);
|
2011-03-07 08:02:35 +01:00
|
|
|
const char *name = tolua_tostring(L, 2, 0);
|
|
|
|
attrib *a = a_find(*self, &at_object);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
for (; a && a->type == &at_object; a = a->next) {
|
2011-03-07 08:02:35 +01:00
|
|
|
const char *obj_name = object_name(a);
|
2010-08-08 10:06:34 +02:00
|
|
|
if (obj_name && name && strcmp(obj_name, name) == 0) {
|
|
|
|
variant val;
|
|
|
|
object_type type;
|
|
|
|
|
|
|
|
object_get(a, &type, &val);
|
|
|
|
switch (type) {
|
|
|
|
case TNONE:
|
|
|
|
lua_pushnil(L);
|
|
|
|
break;
|
|
|
|
case TINTEGER:
|
2011-03-07 08:02:35 +01:00
|
|
|
lua_pushnumber(L, (lua_Number) val.i);
|
2010-08-08 10:06:34 +02:00
|
|
|
break;
|
|
|
|
case TREAL:
|
2011-03-07 08:02:35 +01:00
|
|
|
lua_pushnumber(L, (lua_Number) val.f);
|
2010-08-08 10:06:34 +02:00
|
|
|
break;
|
|
|
|
case TREGION:
|
|
|
|
tolua_pushusertype(L, val.v, TOLUA_CAST "region");
|
|
|
|
break;
|
|
|
|
case TBUILDING:
|
|
|
|
tolua_pushusertype(L, val.v, TOLUA_CAST "building");
|
|
|
|
break;
|
|
|
|
case TUNIT:
|
|
|
|
tolua_pushusertype(L, val.v, TOLUA_CAST "unit");
|
|
|
|
break;
|
|
|
|
case TSHIP:
|
|
|
|
tolua_pushusertype(L, val.v, TOLUA_CAST "ship");
|
|
|
|
break;
|
|
|
|
case TSTRING:
|
2011-03-07 08:02:35 +01:00
|
|
|
tolua_pushstring(L, (const char *)val.v);
|
2010-08-08 10:06:34 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(!"not implemented");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_hashtable_set_number(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
hashtable self = (hashtable) tolua_tousertype(L, 1, 0);
|
2011-03-07 08:02:35 +01:00
|
|
|
const char *name = tolua_tostring(L, 2, 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
lua_Number value = tolua_tonumber(L, 3, 0);
|
2011-03-07 08:02:35 +01:00
|
|
|
attrib *a = a_find(*self, &at_object);
|
2010-08-08 10:06:34 +02:00
|
|
|
variant val;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
val.f = (float)value;
|
|
|
|
|
|
|
|
for (; a && a->type == &at_object; a = a->next) {
|
|
|
|
if (strcmp(object_name(a), name) == 0) {
|
|
|
|
object_set(a, TREAL, val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a = a_add(self, object_create(name, TREAL, val));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_hashtable_set_string(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
hashtable self = (hashtable) tolua_tousertype(L, 1, 0);
|
2011-03-07 08:02:35 +01:00
|
|
|
const char *name = tolua_tostring(L, 2, 0);
|
|
|
|
const char *value = tolua_tostring(L, 3, 0);
|
|
|
|
attrib *a = a_find(*self, &at_object);
|
2010-08-08 10:06:34 +02:00
|
|
|
variant val;
|
|
|
|
|
2013-12-29 14:38:58 +01:00
|
|
|
val.v = _strdup(value);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
for (; a && a->type == &at_object; a = a->next) {
|
|
|
|
if (strcmp(object_name(a), name) == 0) {
|
|
|
|
object_set(a, TSTRING, val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a = a_add(self, object_create(name, TSTRING, val));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_hashtable_set_usertype(lua_State * L, int type)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
hashtable self = (hashtable) tolua_tousertype(L, 1, 0);
|
2011-03-07 08:02:35 +01:00
|
|
|
const char *name = tolua_tostring(L, 2, 0);
|
|
|
|
unit *value = tolua_tousertype(L, 3, 0);
|
|
|
|
attrib *a = a_find(*self, &at_object);
|
2010-08-08 10:06:34 +02:00
|
|
|
variant val;
|
|
|
|
|
|
|
|
val.v = value;
|
|
|
|
|
|
|
|
for (; a && a->type == &at_object; a = a->next) {
|
|
|
|
if (strcmp(object_name(a), name) == 0) {
|
|
|
|
object_set(a, type, val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a = a_add(self, object_create(name, type, val));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_hashtable_set(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
if (tolua_isnumber(L, 3, 0, &tolua_err)) {
|
|
|
|
return tolua_hashtable_set_number(L);
|
|
|
|
} else if (tolua_isusertype(L, 3, TOLUA_CAST "unit", 0, &tolua_err)) {
|
|
|
|
return tolua_hashtable_set_usertype(L, TUNIT);
|
|
|
|
} else if (tolua_isusertype(L, 3, TOLUA_CAST "faction", 0, &tolua_err)) {
|
|
|
|
return tolua_hashtable_set_usertype(L, TFACTION);
|
|
|
|
} else if (tolua_isusertype(L, 3, TOLUA_CAST "ship", 0, &tolua_err)) {
|
|
|
|
return tolua_hashtable_set_usertype(L, TSHIP);
|
|
|
|
} else if (tolua_isusertype(L, 3, TOLUA_CAST "building", 0, &tolua_err)) {
|
|
|
|
return tolua_hashtable_set_usertype(L, TBUILDING);
|
|
|
|
} else if (tolua_isusertype(L, 3, TOLUA_CAST "region", 0, &tolua_err)) {
|
|
|
|
return tolua_hashtable_set_usertype(L, TREGION);
|
|
|
|
}
|
|
|
|
return tolua_hashtable_set_string(L);
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tolua_hashtable_open(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
/* register user types */
|
|
|
|
tolua_usertype(L, TOLUA_CAST "hashtable");
|
|
|
|
|
|
|
|
tolua_module(L, NULL, 0);
|
|
|
|
tolua_beginmodule(L, NULL);
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
tolua_cclass(L, TOLUA_CAST "hashtable", TOLUA_CAST "hashtable",
|
|
|
|
TOLUA_CAST "", NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
tolua_beginmodule(L, TOLUA_CAST "hashtable");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "get", tolua_hashtable_get);
|
|
|
|
tolua_function(L, TOLUA_CAST "set", tolua_hashtable_set);
|
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
}
|