change resolve.h api to use int, not variant.

This commit is contained in:
Enno Rehling 2017-09-19 11:42:02 +02:00
parent 5e204083b8
commit 89e162c12a
22 changed files with 104 additions and 126 deletions

View File

@ -322,12 +322,12 @@ void write_building_reference(const struct building *b, struct storage *store)
WRITE_INT(store, (b && b->region) ? b->no : 0); WRITE_INT(store, (b && b->region) ? b->no : 0);
} }
int resolve_building(variant id, void *address) int resolve_building(int id, void *address)
{ {
int result = 0; int result = 0;
building *b = NULL; building *b = NULL;
if (id.i != 0) { if (id != 0) {
b = findbuilding(id.i); b = findbuilding(id);
if (b == NULL) { if (b == NULL) {
result = -1; result = -1;
} }
@ -336,10 +336,10 @@ int resolve_building(variant id, void *address)
return result; return result;
} }
variant read_building_reference(gamedata * data) int read_building_reference(gamedata * data)
{ {
variant result; int result;
READ_INT(data->store, &result.i); READ_INT(data->store, &result);
return result; return result;
} }

View File

@ -152,10 +152,10 @@ extern "C" {
#include "build.h" #include "build.h"
#define NOBUILDING NULL #define NOBUILDING NULL
int resolve_building(variant data, void *address); int resolve_building(int id, void *address);
void write_building_reference(const struct building *b, void write_building_reference(const struct building *b,
struct storage *store); struct storage *store);
variant read_building_reference(struct gamedata *data); int read_building_reference(struct gamedata *data);
struct building *findbuilding(int n); struct building *findbuilding(int n);

View File

@ -191,12 +191,7 @@ int curse_read(attrib * a, void *owner, gamedata *data)
READ_INT(store, &c->duration); READ_INT(store, &c->duration);
READ_FLT(store, &flt); READ_FLT(store, &flt);
c->vigour = flt; c->vigour = flt;
if (data->version < INTPAK_VERSION) { ur = read_reference(&c->magician, data, read_unit_reference, resolve_unit);
ur = resolve_unit(read_int(data->store), &c->magician);
}
else {
ur = read_reference(&c->magician, data, read_unit_reference, resolve_unit);
}
if (data->version < CURSEFLOAT_VERSION) { if (data->version < CURSEFLOAT_VERSION) {
READ_INT(store, &n); READ_INT(store, &n);
c->effect = (float)n; c->effect = (float)n;

View File

@ -203,12 +203,12 @@ const char *factionname(const faction * f)
return ibuf; return ibuf;
} }
int resolve_faction(variant id, void *address) int resolve_faction(int id, void *address)
{ {
int result = 0; int result = 0;
faction *f = NULL; faction *f = NULL;
if (id.i != 0) { if (id != 0) {
f = findfaction(id.i); f = findfaction(id);
if (f == NULL) { if (f == NULL) {
result = -1; result = -1;
} }
@ -332,10 +332,10 @@ bool checkpasswd(const faction * f, const char *passwd)
return true; return true;
} }
variant read_faction_reference(gamedata * data) int read_faction_reference(gamedata * data)
{ {
variant id; int id;
READ_INT(data->store, &id.i); READ_INT(data->store, &id);
return id; return id;
} }

View File

@ -126,8 +126,8 @@ extern "C" {
void write_faction_reference(const struct faction *f, void write_faction_reference(const struct faction *f,
struct storage *store); struct storage *store);
variant read_faction_reference(struct gamedata *data); int read_faction_reference(struct gamedata *data);
int resolve_faction(variant data, void *addr); int resolve_faction(int id, void *addr);
void renumber_faction(faction * f, int no); void renumber_faction(faction * f, int no);
void free_factions(void); void free_factions(void);

View File

@ -239,12 +239,12 @@ void read_groups(gamedata *data, faction * f)
pa = &g->allies; pa = &g->allies;
for (;;) { for (;;) {
ally *a; ally *a;
variant fid; int fid;
fid = read_faction_reference(data); fid = read_faction_reference(data);
if (fid.i <= 0) if (fid <= 0)
break; break;
a = ally_add(pa, findfaction(fid.i)); a = ally_add(pa, findfaction(fid));
READ_INT(store, &a->status); READ_INT(store, &a->status);
if (!a->faction) if (!a->faction)
ur_add(fid, &a->faction, resolve_faction); ur_add(fid, &a->faction, resolve_faction);

View File

@ -258,12 +258,12 @@ unsigned char index)
return (rel + ursprung_y(f, pl, NULL) + plane_center_y(pl)); return (rel + ursprung_y(f, pl, NULL) + plane_center_y(pl));
} }
static int resolve_plane(variant id, void *addr) static int resolve_plane(int id, void *addr)
{ {
int result = 0; int result = 0;
plane *pl = NULL; plane *pl = NULL;
if (id.i != 0) { if (id != 0) {
pl = getplanebyid(id.i); pl = getplanebyid(id);
if (pl == NULL) { if (pl == NULL) {
result = -1; result = -1;
} }
@ -279,15 +279,16 @@ void write_plane_reference(const plane * u, struct storage *store)
int read_plane_reference(plane ** pp, struct storage *store) int read_plane_reference(plane ** pp, struct storage *store)
{ {
variant id; int id;
READ_INT(store, &id.i); READ_INT(store, &id);
if (id.i == 0) { if (id == 0) {
*pp = NULL; *pp = NULL;
return AT_READ_FAIL; return AT_READ_FAIL;
} }
*pp = getplanebyid(id.i); *pp = getplanebyid(id);
if (*pp == NULL) if (*pp == NULL) {
ur_add(id, pp, resolve_plane); ur_add(id, pp, resolve_plane);
}
return AT_READ_OK; return AT_READ_OK;
} }

View File

@ -561,21 +561,15 @@ void write_race_reference(const race * rc, struct storage *store)
WRITE_TOK(store, rc ? rc->_name : "none"); WRITE_TOK(store, rc ? rc->_name : "none");
} }
variant read_race_reference(struct storage *store) struct race * read_race_reference(struct storage *store)
{ {
variant result;
char zName[20]; char zName[20];
READ_TOK(store, zName, sizeof(zName)); READ_TOK(store, zName, sizeof(zName));
if (strcmp(zName, "none") == 0) { if (strcmp(zName, "none") == 0) {
result.v = NULL; return NULL;
return result;
} }
else { return rc_find_i(zName);
result.v = rc_find_i(zName);
}
assert(result.v != NULL);
return result;
} }
void register_race_function(race_func func, const char *name) { void register_race_function(race_func func, const char *name) {

View File

@ -263,7 +263,7 @@ extern "C" {
void write_race_reference(const struct race *rc, void write_race_reference(const struct race *rc,
struct storage *store); struct storage *store);
variant read_race_reference(struct storage *store); struct race *read_race_reference(struct storage *store);
const char *raceprefix(const struct unit *u); const char *raceprefix(const struct unit *u);
void register_race_function(race_func, const char *); void register_race_function(race_func, const char *);

View File

@ -1255,9 +1255,11 @@ int production(const region * r)
return p; return p;
} }
int resolve_region_coor(variant id, void *address) int resolve_region_coor(int id, void *address)
{ {
region *r = findregion(id.sa[0], id.sa[1]); int x = (id >> 16);
int y = id & 0xFFFF;
region *r = findregion(x, y);
if (r) { if (r) {
*(region **)address = r; *(region **)address = r;
return 0; return 0;
@ -1266,11 +1268,11 @@ int resolve_region_coor(variant id, void *address)
return -1; return -1;
} }
int resolve_region_id(variant id, void *address) int resolve_region_id(int id, void *address)
{ {
region *r = NULL; region *r = NULL;
if (id.i != 0) { if (id != 0) {
r = findregionbyid(id.i); r = findregionbyid(id);
if (r == NULL) { if (r == NULL) {
*(region **)address = NULL; *(region **)address = NULL;
return -1; return -1;
@ -1280,19 +1282,21 @@ int resolve_region_id(variant id, void *address)
return 0; return 0;
} }
variant read_region_reference(gamedata *data) int read_region_reference(gamedata *data)
{ {
struct storage * store = data->store; struct storage * store = data->store;
variant result; int result;
if (data->version < UIDHASH_VERSION) { if (data->version < UIDHASH_VERSION) {
int n; int n;
short x, y;
READ_INT(store, &n); READ_INT(store, &n);
result.sa[0] = (short)n; x = (short)n;
READ_INT(store, &n); READ_INT(store, &n);
result.sa[1] = (short)n; y = (short)n;
result = x << 16 | (y & 0xFFFF);
} }
else { else {
READ_INT(store, &result.i); READ_INT(store, &result);
} }
return result; return result;
} }

View File

@ -257,9 +257,9 @@ extern "C" {
void region_set_morale(region * r, int morale, int turn); void region_set_morale(region * r, int morale, int turn);
void write_region_reference(const struct region *r, struct storage *store); void write_region_reference(const struct region *r, struct storage *store);
variant read_region_reference(struct gamedata *data); int read_region_reference(struct gamedata *data);
int resolve_region_coor(variant id, void *address); int resolve_region_coor(int id, void *address);
int resolve_region_id(variant id, void *address); int resolve_region_id(int id, void *address);
#define RESOLVE_REGION(version) ((version<UIDHASH_VERSION)?resolve_region_coor:resolve_region_id) #define RESOLVE_REGION(version) ((version<UIDHASH_VERSION)?resolve_region_coor:resolve_region_id)
const char *regionname(const struct region *r, const struct faction *f); const char *regionname(const struct region *r, const struct faction *f);

View File

@ -420,7 +420,6 @@ void read_planes(gamedata *data) {
READ_INT(store, &nread); READ_INT(store, &nread);
while (--nread >= 0) { while (--nread >= 0) {
int id; int id;
variant fno;
plane *pl; plane *pl;
READ_INT(store, &id); READ_INT(store, &id);
@ -459,8 +458,8 @@ void read_planes(gamedata *data) {
else { else {
/* WATCHERS - eliminated in February 2016, ca. turn 966 */ /* WATCHERS - eliminated in February 2016, ca. turn 966 */
if (data->version < NOWATCH_VERSION) { if (data->version < NOWATCH_VERSION) {
fno = read_faction_reference(data); int fno = read_faction_reference(data);
while (fno.i) { while (fno) {
fno = read_faction_reference(data); fno = read_faction_reference(data);
} }
} }
@ -509,15 +508,15 @@ void write_alliances(gamedata *data)
WRITE_SECTION(data->store); WRITE_SECTION(data->store);
} }
static int resolve_owner(variant id, void *address) static int resolve_owner(int id, void *address)
{ {
region_owner *owner = (region_owner *)address; region_owner *owner = (region_owner *)address;
int result = 0; int result = 0;
faction *f = NULL; faction *f = NULL;
if (id.i != 0) { if (id != 0) {
f = findfaction(id.i); f = findfaction(id);
if (f == NULL) { if (f == NULL) {
log_error("region has an invalid owner (%s)", itoa36(id.i)); log_error("region has an invalid owner (%s)", itoa36(id));
} }
} }
owner->owner = f; owner->owner = f;
@ -1171,9 +1170,7 @@ static ally **addally(const faction * f, ally ** sfp, int aid, int state)
sf = ally_add(sfp, af); sf = ally_add(sfp, af);
if (!sf->faction) { if (!sf->faction) {
variant id; ur_add(aid, &sf->faction, resolve_faction);
id.i = aid;
ur_add(id, &sf->faction, resolve_faction);
} }
sf->status = state & HELP_ALL; sf->status = state & HELP_ALL;
@ -1489,7 +1486,7 @@ static int cb_sb_maxlevel(spellbook_entry *sbe, void *cbdata) {
int readgame(const char *filename) int readgame(const char *filename)
{ {
int n; int n, stream_version;
char path[MAX_PATH]; char path[MAX_PATH];
gamedata gdata = { 0 }; gamedata gdata = { 0 };
storage store; storage store;
@ -1506,11 +1503,8 @@ int readgame(const char *filename)
return -1; return -1;
} }
sz = fread(&gdata.version, sizeof(int), 1, F); sz = fread(&gdata.version, sizeof(int), 1, F);
if (sz != sizeof(int) || gdata.version >= INTPAK_VERSION) { sz = fread(&stream_version, sizeof(int), 1, F);
int stream_version; assert((sz == 1 && stream_version == STREAM_VERSION) || !"unsupported data format");
size_t sz = fread(&stream_version, sizeof(int), 1, F);
assert((sz == 1 && stream_version == STREAM_VERSION) || !"unsupported data format");
}
assert(gdata.version >= MIN_VERSION || !"unsupported data format"); assert(gdata.version >= MIN_VERSION || !"unsupported data format");
assert(gdata.version <= MAX_VERSION || !"unsupported data format"); assert(gdata.version <= MAX_VERSION || !"unsupported data format");

View File

@ -751,11 +751,11 @@ void write_unit_reference(const unit * u, struct storage *store)
WRITE_INT(store, (u && u->region) ? u->no : 0); WRITE_INT(store, (u && u->region) ? u->no : 0);
} }
int resolve_unit(variant id, void *address) int resolve_unit(int id, void *address)
{ {
unit *u = NULL; unit *u = NULL;
if (id.i != 0) { if (id != 0) {
u = findunit(id.i); u = findunit(id);
if (u == NULL) { if (u == NULL) {
*(unit **)address = NULL; *(unit **)address = NULL;
return -1; return -1;
@ -765,11 +765,11 @@ int resolve_unit(variant id, void *address)
return 0; return 0;
} }
variant read_unit_reference(gamedata *data) int read_unit_reference(gamedata *data)
{ {
variant var; int id;
READ_INT(data->store, &var.i); READ_INT(data->store, &id);
return var; return id;
} }
int get_level(const unit * u, skill_t id) int get_level(const unit * u, skill_t id)

View File

@ -185,9 +185,9 @@ extern "C" {
void make_zombie(unit * u); void make_zombie(unit * u);
/* see resolve.h */ /* see resolve.h */
int resolve_unit(variant data, void *address); int resolve_unit(int id, void *address);
void write_unit_reference(const struct unit *u, struct storage *store); void write_unit_reference(const struct unit *u, struct storage *store);
variant read_unit_reference(struct gamedata *data); int read_unit_reference(struct gamedata *data);
bool leave(struct unit *u, bool force); bool leave(struct unit *u, bool force);
bool can_leave(struct unit *u); bool can_leave(struct unit *u);

View File

@ -2262,10 +2262,10 @@ bool create_newfamiliar(unit * mage, unit * familiar)
return true; return true;
} }
static int resolve_familiar(variant data, void *addr) static int resolve_familiar(int id, void *addr)
{ {
unit *familiar; unit *familiar;
int result = resolve_unit(data, &familiar); int result = resolve_unit(id, &familiar);
if (result == 0 && familiar) { if (result == 0 && familiar) {
attrib *a = a_find(familiar->attribs, &at_familiarmage); attrib *a = a_find(familiar->attribs, &at_familiarmage);
if (a != NULL && a->data.v) { if (a != NULL && a->data.v) {
@ -2346,10 +2346,10 @@ unit *has_clone(unit * mage)
return NULL; return NULL;
} }
static int resolve_clone(variant data, void *addr) static int resolve_clone(int id, void *addr)
{ {
unit *clone; unit *clone;
int result = resolve_unit(data, &clone); int result = resolve_unit(id, &clone);
if (result == 0 && clone) { if (result == 0 && clone) {
attrib *a = a_find(clone->attribs, &at_clonemage); attrib *a = a_find(clone->attribs, &at_clonemage);
if (a != NULL && a->data.v) { if (a != NULL && a->data.v) {
@ -2372,10 +2372,10 @@ static int read_clone(attrib * a, void *owner, struct gamedata *data)
} }
/* mages */ /* mages */
static int resolve_mage(variant data, void *addr) static int resolve_mage(int id, void *addr)
{ {
unit *mage; unit *mage;
int result = resolve_unit(data, &mage); int result = resolve_unit(id, &mage);
if (result == 0 && mage) { if (result == 0 && mage) {
attrib *a = a_find(mage->attribs, &at_familiar); attrib *a = a_find(mage->attribs, &at_familiar);
if (a != NULL && a->data.v) { if (a != NULL && a->data.v) {

View File

@ -2900,7 +2900,7 @@ static int dc_read_compat(struct attrib *a, void *target, gamedata *data)
struct storage *store = data->store; struct storage *store = data->store;
region *r = NULL; region *r = NULL;
unit *u; unit *u;
variant var; int id;
int duration; int duration;
float strength; float strength;
int rx, ry; int rx, ry;
@ -2909,8 +2909,8 @@ static int dc_read_compat(struct attrib *a, void *target, gamedata *data)
UNUSED_ARG(target); UNUSED_ARG(target);
READ_INT(store, &duration); READ_INT(store, &duration);
READ_FLT(store, &strength); READ_FLT(store, &strength);
READ_INT(store, &var.i); READ_INT(store, &id);
u = findunit(var.i); u = findunit(id);
/* this only affects really old data. no need to change: */ /* this only affects really old data. no need to change: */
READ_INT(store, &rx); READ_INT(store, &rx);
@ -2927,7 +2927,7 @@ static int dc_read_compat(struct attrib *a, void *target, gamedata *data)
effect, 0); effect, 0);
c->data.v = r; c->data.v = r;
if (u == NULL) { if (u == NULL) {
ur_add(var, &c->magician, resolve_unit); ur_add(id, &c->magician, resolve_unit);
} }
} }
return AT_READ_FAIL; /* we don't care for the attribute. */ return AT_READ_FAIL; /* we don't care for the attribute. */

View File

@ -83,15 +83,15 @@ static void changefaction_write(const trigger * t, struct storage *store)
static int changefaction_read(trigger * t, gamedata *data) static int changefaction_read(trigger * t, gamedata *data)
{ {
variant var; int id;
changefaction_data *td = (changefaction_data *)t->data.v; changefaction_data *td = (changefaction_data *)t->data.v;
read_reference(&td->unit, data, read_unit_reference, resolve_unit); read_reference(&td->unit, data, read_unit_reference, resolve_unit);
var = read_faction_reference(data); id = read_faction_reference(data);
if (var.i == 0) { if (id == 0) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }
ur_add(var, &td->faction, resolve_faction); ur_add(id, &td->faction, resolve_faction);
return AT_READ_OK; return AT_READ_OK;
} }

View File

@ -90,8 +90,8 @@ static int changerace_read(trigger * t, gamedata *data)
{ {
changerace_data *td = (changerace_data *)t->data.v; changerace_data *td = (changerace_data *)t->data.v;
read_reference(&td->u, data, read_unit_reference, resolve_unit); read_reference(&td->u, data, read_unit_reference, resolve_unit);
td->race = (const struct race *)read_race_reference(data->store).v; td->race = read_race_reference(data->store);
td->irace = (const struct race *)read_race_reference(data->store).v; td->irace = read_race_reference(data->store);
return AT_READ_OK; return AT_READ_OK;
} }

View File

@ -90,13 +90,13 @@ static void createunit_write(const trigger * t, struct storage *store)
static int createunit_read(trigger * t, gamedata *data) static int createunit_read(trigger * t, gamedata *data)
{ {
createunit_data *td = (createunit_data *)t->data.v; createunit_data *td = (createunit_data *)t->data.v;
variant var; int id;
int result = AT_READ_OK; int result = AT_READ_OK;
var = read_faction_reference(data); id = read_faction_reference(data);
if (var.i > 0) { if (id > 0) {
td->f = findfaction(var.i); td->f = findfaction(id);
if (!td->f) { if (!td->f) {
ur_add(var, &td->f, resolve_faction); ur_add(id, &td->f, resolve_faction);
} }
} }
else { else {
@ -105,7 +105,7 @@ static int createunit_read(trigger * t, gamedata *data)
read_reference(&td->r, data, read_region_reference, read_reference(&td->r, data, read_region_reference,
RESOLVE_REGION(data->version)); RESOLVE_REGION(data->version));
td->race = (const struct race *)read_race_reference(data->store).v; td->race = read_race_reference(data->store);
if (!td->race) { if (!td->race) {
result = AT_READ_FAIL; result = AT_READ_FAIL;
} }

View File

@ -37,26 +37,19 @@ static unresolved *ur_list;
static unresolved *ur_begin; static unresolved *ur_begin;
static unresolved *ur_current; static unresolved *ur_current;
variant read_int(struct storage *store)
{
variant var;
READ_INT(store, &var.i);
return var;
}
int int
read_reference(void *address, struct gamedata * data, read_fun reader, read_reference(void *address, struct gamedata * data, read_fun reader,
resolve_fun resolver) resolve_fun resolver)
{ {
variant var = reader(data); int id = reader(data);
int result = resolver(var, address); int result = resolver(id, address);
if (result != 0) { if (result != 0) {
ur_add(var, address, resolver); ur_add(id, address, resolver);
} }
return result; return result;
} }
void ur_add(variant data, void *ptrptr, resolve_fun fun) void ur_add(int id, void *ptrptr, resolve_fun fun)
{ {
assert(ptrptr); assert(ptrptr);
if (ur_list == NULL) { if (ur_list == NULL) {
@ -68,7 +61,7 @@ void ur_add(variant data, void *ptrptr, resolve_fun fun)
ur_current->data.v = ur_begin; ur_current->data.v = ur_begin;
ur_current = ur_begin; ur_current = ur_begin;
} }
ur_current->data = data; ur_current->data.i = id;
ur_current->resolve = fun; ur_current->resolve = fun;
ur_current->ptrptr = ptrptr; ur_current->ptrptr = ptrptr;
@ -88,7 +81,7 @@ void resolve(void)
continue; continue;
} }
assert(ur->ptrptr); assert(ur->ptrptr);
ur->resolve(ur->data, ur->ptrptr); ur->resolve(ur->data.i, ur->ptrptr);
++ur; ++ur;
} }
free(ur_list); free(ur_list);

View File

@ -19,7 +19,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef RESOLVE_H #ifndef RESOLVE_H
#define RESOLVE_H #define RESOLVE_H
#include "variant.h"
struct storage; struct storage;
struct gamedata; struct gamedata;
@ -27,15 +26,13 @@ struct gamedata;
extern "C" { extern "C" {
#endif #endif
typedef int(*resolve_fun) (variant data, void *address); typedef int(*resolve_fun) (int id, void *address);
typedef variant(*read_fun) (struct gamedata * data); typedef int(*read_fun) (struct gamedata * data);
extern int read_reference(void *address, struct gamedata *data, int read_reference(void *address, struct gamedata *data,
read_fun reader, resolve_fun resolver); read_fun reader, resolve_fun resolver);
extern void ur_add(variant data, void *address, resolve_fun fun); void ur_add(int id, void *address, resolve_fun fun);
extern void resolve(void); void resolve(void);
extern variant read_int(struct storage *store);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -97,9 +97,9 @@ static void wormhole_write(const struct attrib *a, const void *owner, struct sto
} }
/** conversion code, turn 573, 2008-05-23 */ /** conversion code, turn 573, 2008-05-23 */
static int resolve_exit(variant id, void *address) static int resolve_exit(int id, void *address)
{ {
building *b = findbuilding(id.i); building *b = findbuilding(id);
region **rp = address; region **rp = address;
if (b) { if (b) {
*rp = b->region; *rp = b->region;