forked from github/server
update to latest version of storage library.
This commit is contained in:
parent
f623133344
commit
dbf60a7ce5
5 changed files with 99 additions and 77 deletions
|
@ -18,6 +18,8 @@ without prior permission by the authors of Eressea.
|
||||||
#include <kernel/version.h>
|
#include <kernel/version.h>
|
||||||
|
|
||||||
#include <storage.h>
|
#include <storage.h>
|
||||||
|
#include <stream.h>
|
||||||
|
#include <filestream.h>
|
||||||
#include <binarystore.h>
|
#include <binarystore.h>
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
@ -29,36 +31,37 @@ without prior permission by the authors of Eressea.
|
||||||
|
|
||||||
static int tolua_storage_create(lua_State * L)
|
static int tolua_storage_create(lua_State * L)
|
||||||
{
|
{
|
||||||
const char *filename = tolua_tostring(L, 1, 0);
|
const char *filename = tolua_tostring(L, 1, 0);
|
||||||
const char *type = tolua_tostring(L, 2, "rb");
|
const char *type = tolua_tostring(L, 2, "rb");
|
||||||
gamedata *data = (gamedata *)calloc(1, sizeof(gamedata));
|
gamedata *data = (gamedata *)calloc(1, sizeof(gamedata));
|
||||||
storage *store = (storage *)calloc(1, sizeof(storage));
|
storage *store = (storage *)calloc(1, sizeof(storage));
|
||||||
FILE * F;
|
FILE * F;
|
||||||
|
|
||||||
data->store = store;
|
data->store = store;
|
||||||
|
|
||||||
F = fopen(filename, type);
|
F = fopen(filename, type);
|
||||||
if (strchr(type, 'r')) {
|
if (strchr(type, 'r')) {
|
||||||
fread(&data->version, sizeof(int), 1, F);
|
fread(&data->version, sizeof(int), 1, F);
|
||||||
fseek(F, sizeof(int), SEEK_CUR);
|
fseek(F, sizeof(int), SEEK_CUR);
|
||||||
}
|
}
|
||||||
else if (strchr(type, 'w')) {
|
else if (strchr(type, 'w')) {
|
||||||
int n = STREAM_VERSION;
|
int n = STREAM_VERSION;
|
||||||
data->version = RELEASE_VERSION;
|
data->version = RELEASE_VERSION;
|
||||||
fwrite(&data->version, sizeof(int), 1, F);
|
fwrite(&data->version, sizeof(int), 1, F);
|
||||||
fwrite(&n, sizeof(int), 1, F);
|
fwrite(&n, sizeof(int), 1, F);
|
||||||
}
|
}
|
||||||
binstore_init(store, F);
|
fstream_init(&data->strm, F);
|
||||||
tolua_pushusertype(L, (void *)data, TOLUA_CAST "storage");
|
binstore_init(store, &data->strm);
|
||||||
return 1;
|
tolua_pushusertype(L, (void *)data, TOLUA_CAST "storage");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tolua_storage_read_unit(lua_State * L)
|
static int tolua_storage_read_unit(lua_State * L)
|
||||||
{
|
{
|
||||||
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
||||||
struct unit *u = read_unit(data);
|
struct unit *u = read_unit(data);
|
||||||
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
|
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tolua_storage_write_unit(lua_State * L)
|
static int tolua_storage_write_unit(lua_State * L)
|
||||||
|
@ -71,76 +74,78 @@ static int tolua_storage_write_unit(lua_State * L)
|
||||||
|
|
||||||
static int tolua_storage_read_float(lua_State * L)
|
static int tolua_storage_read_float(lua_State * L)
|
||||||
{
|
{
|
||||||
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
||||||
float num;
|
float num;
|
||||||
READ_FLT(data->store, &num);
|
READ_FLT(data->store, &num);
|
||||||
tolua_pushnumber(L, (lua_Number) num);
|
tolua_pushnumber(L, (lua_Number)num);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tolua_storage_read_int(lua_State * L)
|
static int tolua_storage_read_int(lua_State * L)
|
||||||
{
|
{
|
||||||
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
||||||
int num;
|
int num;
|
||||||
READ_INT(data->store, &num);
|
READ_INT(data->store, &num);
|
||||||
tolua_pushnumber(L, (lua_Number) num);
|
tolua_pushnumber(L, (lua_Number)num);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tolua_storage_write(lua_State * L)
|
static int tolua_storage_write(lua_State * L)
|
||||||
{
|
{
|
||||||
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
||||||
if (data->version && tolua_isnumber(L, 2, 0, 0)) {
|
if (data->version && tolua_isnumber(L, 2, 0, 0)) {
|
||||||
lua_Number num = tolua_tonumber(L, 2, 0);
|
lua_Number num = tolua_tonumber(L, 2, 0);
|
||||||
double n;
|
double n;
|
||||||
if (modf(num, &n) == 0.0) {
|
if (modf(num, &n) == 0.0) {
|
||||||
WRITE_INT(data->store, (int)num);
|
WRITE_INT(data->store, (int)num);
|
||||||
} else {
|
}
|
||||||
WRITE_FLT(data->store, (float)num);
|
else {
|
||||||
|
WRITE_FLT(data->store, (float)num);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tolua_storage_tostring(lua_State * L)
|
static int tolua_storage_tostring(lua_State * L)
|
||||||
{
|
{
|
||||||
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
||||||
char name[64];
|
char name[64];
|
||||||
_snprintf(name, sizeof(name), "<storage enc=%d ver=%d>", data->encoding,
|
_snprintf(name, sizeof(name), "<storage enc=%d ver=%d>", data->encoding,
|
||||||
data->version);
|
data->version);
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tolua_storage_close(lua_State * L)
|
static int tolua_storage_close(lua_State * L)
|
||||||
{
|
{
|
||||||
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
|
||||||
binstore_done(data->store);
|
binstore_done(data->store);
|
||||||
return 0;
|
fstream_done(&data->strm);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tolua_storage_open(lua_State * L)
|
void tolua_storage_open(lua_State * L)
|
||||||
{
|
{
|
||||||
/* register user types */
|
/* register user types */
|
||||||
tolua_usertype(L, TOLUA_CAST "storage");
|
tolua_usertype(L, TOLUA_CAST "storage");
|
||||||
|
|
||||||
tolua_module(L, NULL, 0);
|
tolua_module(L, NULL, 0);
|
||||||
tolua_beginmodule(L, NULL);
|
tolua_beginmodule(L, NULL);
|
||||||
{
|
|
||||||
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_cclass(L, TOLUA_CAST "storage", TOLUA_CAST "storage", TOLUA_CAST "",
|
||||||
tolua_function(L, TOLUA_CAST "write", tolua_storage_write);
|
NULL);
|
||||||
tolua_function(L, TOLUA_CAST "read_int", tolua_storage_read_int);
|
tolua_beginmodule(L, TOLUA_CAST "storage");
|
||||||
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 "__tostring", tolua_storage_tostring);
|
||||||
tolua_function(L, TOLUA_CAST "read_unit", tolua_storage_read_unit);
|
tolua_function(L, TOLUA_CAST "write", tolua_storage_write);
|
||||||
tolua_function(L, TOLUA_CAST "close", tolua_storage_close);
|
tolua_function(L, TOLUA_CAST "read_int", tolua_storage_read_int);
|
||||||
tolua_function(L, TOLUA_CAST "create", tolua_storage_create);
|
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);
|
tolua_endmodule(L);
|
||||||
}
|
|
||||||
tolua_endmodule(L);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
#include "faction.h"
|
#include "faction.h"
|
||||||
#include "unit.h"
|
#include "unit.h"
|
||||||
#include "region.h"
|
#include "region.h"
|
||||||
|
#include <stream.h>
|
||||||
|
#include <filestream.h>
|
||||||
#include <storage.h>
|
#include <storage.h>
|
||||||
#include <binarystore.h>
|
#include <binarystore.h>
|
||||||
|
|
||||||
|
@ -19,9 +21,11 @@ static void test_group_readwrite(CuTest * tc)
|
||||||
ally *al;
|
ally *al;
|
||||||
storage store;
|
storage store;
|
||||||
FILE *F;
|
FILE *F;
|
||||||
|
stream strm;
|
||||||
|
|
||||||
F = fopen("test.dat", "wb");
|
F = fopen("test.dat", "wb");
|
||||||
binstore_init(&store, F);
|
fstream_init(&strm, F);
|
||||||
|
binstore_init(&store, &strm);
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
test_create_world();
|
test_create_world();
|
||||||
f = test_create_faction(0);
|
f = test_create_faction(0);
|
||||||
|
@ -30,13 +34,16 @@ static void test_group_readwrite(CuTest * tc)
|
||||||
al->status = HELP_GIVE;
|
al->status = HELP_GIVE;
|
||||||
write_groups(&store, f);
|
write_groups(&store, f);
|
||||||
binstore_done(&store);
|
binstore_done(&store);
|
||||||
|
fstream_done(&strm);
|
||||||
|
|
||||||
F = fopen("test.dat", "rb");
|
F = fopen("test.dat", "rb");
|
||||||
binstore_init(&store, F);
|
fstream_init(&strm, F);
|
||||||
|
binstore_init(&store, &strm);
|
||||||
f->groups = 0;
|
f->groups = 0;
|
||||||
free_group(g);
|
free_group(g);
|
||||||
read_groups(&store, f);
|
read_groups(&store, f);
|
||||||
binstore_done(&store);
|
binstore_done(&store);
|
||||||
|
fstream_done(&strm);
|
||||||
|
|
||||||
CuAssertPtrNotNull(tc, f->groups);
|
CuAssertPtrNotNull(tc, f->groups);
|
||||||
CuAssertPtrNotNull(tc, f->groups->allies);
|
CuAssertPtrNotNull(tc, f->groups->allies);
|
||||||
|
|
|
@ -70,6 +70,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#include <util/umlaut.h>
|
#include <util/umlaut.h>
|
||||||
#include <util/unicode.h>
|
#include <util/unicode.h>
|
||||||
|
|
||||||
|
#include <stream.h>
|
||||||
|
#include <filestream.h>
|
||||||
#include <storage.h>
|
#include <storage.h>
|
||||||
#include <binarystore.h>
|
#include <binarystore.h>
|
||||||
|
|
||||||
|
@ -1457,6 +1459,7 @@ int readgame(const char *filename, int backup)
|
||||||
const struct building_type *bt_lighthouse = bt_find("lighthouse");
|
const struct building_type *bt_lighthouse = bt_find("lighthouse");
|
||||||
gamedata gdata = { 0 };
|
gamedata gdata = { 0 };
|
||||||
storage store;
|
storage store;
|
||||||
|
stream strm;
|
||||||
FILE *F;
|
FILE *F;
|
||||||
|
|
||||||
init_locales();
|
init_locales();
|
||||||
|
@ -1482,7 +1485,8 @@ int readgame(const char *filename, int backup)
|
||||||
assert(gdata.version <= RELEASE_VERSION || !"unsupported data format");
|
assert(gdata.version <= RELEASE_VERSION || !"unsupported data format");
|
||||||
|
|
||||||
gdata.encoding = enc_gamedata;
|
gdata.encoding = enc_gamedata;
|
||||||
binstore_init(&store, F);
|
fstream_init(&strm, F);
|
||||||
|
binstore_init(&store, &strm);
|
||||||
gdata.store = &store;
|
gdata.store = &store;
|
||||||
global.data_version = gdata.version; /* HACK: attribute::read does not have access to gamedata, only storage */
|
global.data_version = gdata.version; /* HACK: attribute::read does not have access to gamedata, only storage */
|
||||||
|
|
||||||
|
@ -1733,7 +1737,7 @@ int readgame(const char *filename, int backup)
|
||||||
read_borders(&store);
|
read_borders(&store);
|
||||||
|
|
||||||
binstore_done(&store);
|
binstore_done(&store);
|
||||||
|
fstream_done(&strm);
|
||||||
/* Unaufgeloeste Zeiger initialisieren */
|
/* Unaufgeloeste Zeiger initialisieren */
|
||||||
log_printf(stdout, "fixing unresolved references.\n");
|
log_printf(stdout, "fixing unresolved references.\n");
|
||||||
resolve();
|
resolve();
|
||||||
|
@ -1797,6 +1801,7 @@ int writegame(const char *filename)
|
||||||
char path[MAX_PATH];
|
char path[MAX_PATH];
|
||||||
gamedata gdata;
|
gamedata gdata;
|
||||||
storage store;
|
storage store;
|
||||||
|
stream strm;
|
||||||
FILE *F;
|
FILE *F;
|
||||||
|
|
||||||
clear_monster_orders();
|
clear_monster_orders();
|
||||||
|
@ -1826,7 +1831,8 @@ int writegame(const char *filename)
|
||||||
fwrite(&gdata.version, sizeof(int), 1, F);
|
fwrite(&gdata.version, sizeof(int), 1, F);
|
||||||
fwrite(&n, sizeof(int), 1, F);
|
fwrite(&n, sizeof(int), 1, F);
|
||||||
|
|
||||||
binstore_init(&store, F);
|
fstream_init(&strm, F);
|
||||||
|
binstore_init(&store, &strm);
|
||||||
|
|
||||||
/* globale Variablen */
|
/* globale Variablen */
|
||||||
|
|
||||||
|
@ -1942,6 +1948,7 @@ int writegame(const char *filename)
|
||||||
WRITE_SECTION(&store);
|
WRITE_SECTION(&store);
|
||||||
|
|
||||||
binstore_done(&store);
|
binstore_done(&store);
|
||||||
|
fstream_done(&strm);
|
||||||
|
|
||||||
log_printf(stdout, "\nOk.\n");
|
log_printf(stdout, "\nOk.\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -18,6 +18,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
#ifndef H_KRNL_SAVE
|
#ifndef H_KRNL_SAVE
|
||||||
#define H_KRNL_SAVE
|
#define H_KRNL_SAVE
|
||||||
|
|
||||||
|
#include <stream.h>
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
@ -31,6 +33,7 @@ extern "C" {
|
||||||
|
|
||||||
typedef struct gamedata {
|
typedef struct gamedata {
|
||||||
struct storage *store;
|
struct storage *store;
|
||||||
|
stream strm;
|
||||||
int version;
|
int version;
|
||||||
int encoding;
|
int encoding;
|
||||||
} gamedata;
|
} gamedata;
|
||||||
|
|
2
storage
2
storage
|
@ -1 +1 @@
|
||||||
Subproject commit c6103e59c0938b173c0e08a852ff1cbbc4e284e3
|
Subproject commit 5f0b7095d42209762c9eac73c866c614018b440d
|
Loading…
Reference in a new issue