2017-12-29 06:13:28 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <platform.h>
|
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include "bind_storage.h"
|
|
|
|
|
|
|
|
#include <kernel/save.h>
|
2013-12-31 10:06:28 +01:00
|
|
|
|
2016-04-09 18:22:20 +02:00
|
|
|
#include <util/gamedata.h>
|
2016-02-13 12:51:50 +01:00
|
|
|
#include <util/log.h>
|
2017-12-28 18:55:45 +01:00
|
|
|
#include <util/macros.h>
|
2016-02-13 12:51:50 +01:00
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
#include <storage.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include <math.h>
|
2011-03-08 08:44:20 +01:00
|
|
|
#include <stdio.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <string.h>
|
2016-02-13 12:51:50 +01:00
|
|
|
#include <errno.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2018-09-23 19:44:05 +02:00
|
|
|
#include <lua.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#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
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
const char *filename = tolua_tostring(L, 1, 0);
|
2016-02-05 23:10:05 +01:00
|
|
|
const char *type = tolua_tostring(L, 2, "rb");
|
2015-11-04 10:38:12 +01:00
|
|
|
gamedata *data;
|
|
|
|
|
2016-02-12 22:06:31 +01:00
|
|
|
data = gamedata_open(filename, type, RELEASE_VERSION);
|
2015-11-04 10:38:12 +01:00
|
|
|
if (data) {
|
|
|
|
tolua_pushusertype(L, (void *)data, TOLUA_CAST "storage");
|
|
|
|
return 1;
|
2014-11-03 22:29:04 +01:00
|
|
|
}
|
2016-02-13 12:51:50 +01:00
|
|
|
log_error("could not open %s, mode %s (%s).", filename, type, strerror(errno));
|
2015-10-29 16:28:59 +01:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
|
|
|
struct unit *u = read_unit(data);
|
|
|
|
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-02-15 23:08:48 +01:00
|
|
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
|
|
|
struct unit *u = (struct unit *)tolua_tousertype(L, 2, 0);
|
2013-12-31 10:06:28 +01:00
|
|
|
write_unit(data, u);
|
2014-02-15 23:08:48 +01:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
|
|
|
float num;
|
|
|
|
READ_FLT(data->store, &num);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushnumber(L, num);
|
2014-11-03 22:29:04 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
|
|
|
int num;
|
|
|
|
READ_INT(data->store, &num);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, num);
|
2014-11-03 22:29:04 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_storage_write(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
|
|
|
if (data->version && tolua_isnumber(L, 2, 0, 0)) {
|
|
|
|
lua_Number num = tolua_tonumber(L, 2, 0);
|
|
|
|
double n;
|
|
|
|
if (modf(num, &n) == 0.0) {
|
|
|
|
WRITE_INT(data->store, (int)num);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WRITE_FLT(data->store, (float)num);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2014-11-03 22:29:04 +01:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_storage_tostring(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
|
|
|
char name[64];
|
2017-02-18 21:15:14 +01:00
|
|
|
/* safe to use sprintf here, because:
|
|
|
|
* %p is at most 16 characters, %d 20, text is 16,
|
|
|
|
* comes to 53 with \0 */
|
2017-01-07 21:09:39 +01:00
|
|
|
sprintf(name, "<gamedata %p ver=%d>", (void *)data, data->version);
|
2014-11-03 22:29:04 +01:00
|
|
|
lua_pushstring(L, name);
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_storage_close(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
2015-11-04 10:38:12 +01:00
|
|
|
gamedata_close(data);
|
2014-11-03 22:29:04 +01:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tolua_storage_open(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
/* register user types */
|
|
|
|
tolua_usertype(L, TOLUA_CAST "storage");
|
|
|
|
|
|
|
|
tolua_module(L, NULL, 0);
|
|
|
|
tolua_beginmodule(L, NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-11-03 22:29:04 +01:00
|
|
|
tolua_cclass(L, TOLUA_CAST "storage", TOLUA_CAST "storage", TOLUA_CAST "",
|
|
|
|
NULL);
|
|
|
|
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);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
}
|