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);
}
int resolve_building(variant id, void *address)
int resolve_building(int id, void *address)
{
int result = 0;
building *b = NULL;
if (id.i != 0) {
b = findbuilding(id.i);
if (id != 0) {
b = findbuilding(id);
if (b == NULL) {
result = -1;
}
@ -336,10 +336,10 @@ int resolve_building(variant id, void *address)
return result;
}
variant read_building_reference(gamedata * data)
int read_building_reference(gamedata * data)
{
variant result;
READ_INT(data->store, &result.i);
int result;
READ_INT(data->store, &result);
return result;
}

View File

@ -152,10 +152,10 @@ extern "C" {
#include "build.h"
#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,
struct storage *store);
variant read_building_reference(struct gamedata *data);
int read_building_reference(struct gamedata *data);
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_FLT(store, &flt);
c->vigour = flt;
if (data->version < INTPAK_VERSION) {
ur = resolve_unit(read_int(data->store), &c->magician);
}
else {
ur = read_reference(&c->magician, data, read_unit_reference, resolve_unit);
}
ur = read_reference(&c->magician, data, read_unit_reference, resolve_unit);
if (data->version < CURSEFLOAT_VERSION) {
READ_INT(store, &n);
c->effect = (float)n;

View File

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

View File

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

View File

@ -239,12 +239,12 @@ void read_groups(gamedata *data, faction * f)
pa = &g->allies;
for (;;) {
ally *a;
variant fid;
int fid;
fid = read_faction_reference(data);
if (fid.i <= 0)
if (fid <= 0)
break;
a = ally_add(pa, findfaction(fid.i));
a = ally_add(pa, findfaction(fid));
READ_INT(store, &a->status);
if (!a->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));
}
static int resolve_plane(variant id, void *addr)
static int resolve_plane(int id, void *addr)
{
int result = 0;
plane *pl = NULL;
if (id.i != 0) {
pl = getplanebyid(id.i);
if (id != 0) {
pl = getplanebyid(id);
if (pl == NULL) {
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)
{
variant id;
READ_INT(store, &id.i);
if (id.i == 0) {
int id;
READ_INT(store, &id);
if (id == 0) {
*pp = NULL;
return AT_READ_FAIL;
}
*pp = getplanebyid(id.i);
if (*pp == NULL)
*pp = getplanebyid(id);
if (*pp == NULL) {
ur_add(id, pp, resolve_plane);
}
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");
}
variant read_race_reference(struct storage *store)
struct race * read_race_reference(struct storage *store)
{
variant result;
char zName[20];
READ_TOK(store, zName, sizeof(zName));
if (strcmp(zName, "none") == 0) {
result.v = NULL;
return result;
return NULL;
}
else {
result.v = rc_find_i(zName);
}
assert(result.v != NULL);
return result;
return rc_find_i(zName);
}
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,
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);
void register_race_function(race_func, const char *);

View File

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

View File

@ -257,9 +257,9 @@ extern "C" {
void region_set_morale(region * r, int morale, int turn);
void write_region_reference(const struct region *r, struct storage *store);
variant read_region_reference(struct gamedata *data);
int resolve_region_coor(variant id, void *address);
int resolve_region_id(variant id, void *address);
int read_region_reference(struct gamedata *data);
int resolve_region_coor(int id, void *address);
int resolve_region_id(int id, void *address);
#define RESOLVE_REGION(version) ((version<UIDHASH_VERSION)?resolve_region_coor:resolve_region_id)
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);
while (--nread >= 0) {
int id;
variant fno;
plane *pl;
READ_INT(store, &id);
@ -459,8 +458,8 @@ void read_planes(gamedata *data) {
else {
/* WATCHERS - eliminated in February 2016, ca. turn 966 */
if (data->version < NOWATCH_VERSION) {
fno = read_faction_reference(data);
while (fno.i) {
int fno = read_faction_reference(data);
while (fno) {
fno = read_faction_reference(data);
}
}
@ -509,15 +508,15 @@ void write_alliances(gamedata *data)
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;
int result = 0;
faction *f = NULL;
if (id.i != 0) {
f = findfaction(id.i);
if (id != 0) {
f = findfaction(id);
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;
@ -1171,9 +1170,7 @@ static ally **addally(const faction * f, ally ** sfp, int aid, int state)
sf = ally_add(sfp, af);
if (!sf->faction) {
variant id;
id.i = aid;
ur_add(id, &sf->faction, resolve_faction);
ur_add(aid, &sf->faction, resolve_faction);
}
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 n;
int n, stream_version;
char path[MAX_PATH];
gamedata gdata = { 0 };
storage store;
@ -1506,11 +1503,8 @@ int readgame(const char *filename)
return -1;
}
sz = fread(&gdata.version, sizeof(int), 1, F);
if (sz != sizeof(int) || gdata.version >= INTPAK_VERSION) {
int stream_version;
size_t sz = fread(&stream_version, sizeof(int), 1, F);
assert((sz == 1 && stream_version == STREAM_VERSION) || !"unsupported data format");
}
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 <= 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);
}
int resolve_unit(variant id, void *address)
int resolve_unit(int id, void *address)
{
unit *u = NULL;
if (id.i != 0) {
u = findunit(id.i);
if (id != 0) {
u = findunit(id);
if (u == NULL) {
*(unit **)address = NULL;
return -1;
@ -765,11 +765,11 @@ int resolve_unit(variant id, void *address)
return 0;
}
variant read_unit_reference(gamedata *data)
int read_unit_reference(gamedata *data)
{
variant var;
READ_INT(data->store, &var.i);
return var;
int id;
READ_INT(data->store, &id);
return id;
}
int get_level(const unit * u, skill_t id)

View File

@ -185,9 +185,9 @@ extern "C" {
void make_zombie(unit * u);
/* 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);
variant read_unit_reference(struct gamedata *data);
int read_unit_reference(struct gamedata *data);
bool leave(struct unit *u, bool force);
bool can_leave(struct unit *u);

View File

@ -2262,10 +2262,10 @@ bool create_newfamiliar(unit * mage, unit * familiar)
return true;
}
static int resolve_familiar(variant data, void *addr)
static int resolve_familiar(int id, void *addr)
{
unit *familiar;
int result = resolve_unit(data, &familiar);
int result = resolve_unit(id, &familiar);
if (result == 0 && familiar) {
attrib *a = a_find(familiar->attribs, &at_familiarmage);
if (a != NULL && a->data.v) {
@ -2346,10 +2346,10 @@ unit *has_clone(unit * mage)
return NULL;
}
static int resolve_clone(variant data, void *addr)
static int resolve_clone(int id, void *addr)
{
unit *clone;
int result = resolve_unit(data, &clone);
int result = resolve_unit(id, &clone);
if (result == 0 && clone) {
attrib *a = a_find(clone->attribs, &at_clonemage);
if (a != NULL && a->data.v) {
@ -2372,10 +2372,10 @@ static int read_clone(attrib * a, void *owner, struct gamedata *data)
}
/* mages */
static int resolve_mage(variant data, void *addr)
static int resolve_mage(int id, void *addr)
{
unit *mage;
int result = resolve_unit(data, &mage);
int result = resolve_unit(id, &mage);
if (result == 0 && mage) {
attrib *a = a_find(mage->attribs, &at_familiar);
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;
region *r = NULL;
unit *u;
variant var;
int id;
int duration;
float strength;
int rx, ry;
@ -2909,8 +2909,8 @@ static int dc_read_compat(struct attrib *a, void *target, gamedata *data)
UNUSED_ARG(target);
READ_INT(store, &duration);
READ_FLT(store, &strength);
READ_INT(store, &var.i);
u = findunit(var.i);
READ_INT(store, &id);
u = findunit(id);
/* this only affects really old data. no need to change: */
READ_INT(store, &rx);
@ -2927,7 +2927,7 @@ static int dc_read_compat(struct attrib *a, void *target, gamedata *data)
effect, 0);
c->data.v = r;
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. */

View File

@ -83,15 +83,15 @@ static void changefaction_write(const trigger * t, struct storage *store)
static int changefaction_read(trigger * t, gamedata *data)
{
variant var;
int id;
changefaction_data *td = (changefaction_data *)t->data.v;
read_reference(&td->unit, data, read_unit_reference, resolve_unit);
var = read_faction_reference(data);
if (var.i == 0) {
id = read_faction_reference(data);
if (id == 0) {
return AT_READ_FAIL;
}
ur_add(var, &td->faction, resolve_faction);
ur_add(id, &td->faction, resolve_faction);
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;
read_reference(&td->u, data, read_unit_reference, resolve_unit);
td->race = (const struct race *)read_race_reference(data->store).v;
td->irace = (const struct race *)read_race_reference(data->store).v;
td->race = read_race_reference(data->store);
td->irace = read_race_reference(data->store);
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)
{
createunit_data *td = (createunit_data *)t->data.v;
variant var;
int id;
int result = AT_READ_OK;
var = read_faction_reference(data);
if (var.i > 0) {
td->f = findfaction(var.i);
id = read_faction_reference(data);
if (id > 0) {
td->f = findfaction(id);
if (!td->f) {
ur_add(var, &td->f, resolve_faction);
ur_add(id, &td->f, resolve_faction);
}
}
else {
@ -105,7 +105,7 @@ static int createunit_read(trigger * t, gamedata *data)
read_reference(&td->r, data, read_region_reference,
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) {
result = AT_READ_FAIL;
}

View File

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

View File

@ -19,7 +19,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef RESOLVE_H
#define RESOLVE_H
#include "variant.h"
struct storage;
struct gamedata;
@ -27,15 +26,13 @@ struct gamedata;
extern "C" {
#endif
typedef int(*resolve_fun) (variant data, void *address);
typedef variant(*read_fun) (struct gamedata * data);
extern int read_reference(void *address, struct gamedata *data,
typedef int(*resolve_fun) (int id, void *address);
typedef int(*read_fun) (struct gamedata * data);
int read_reference(void *address, struct gamedata *data,
read_fun reader, resolve_fun resolver);
extern void ur_add(variant data, void *address, resolve_fun fun);
extern void resolve(void);
extern variant read_int(struct storage *store);
void ur_add(int id, void *address, resolve_fun fun);
void resolve(void);
#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 */
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;
if (b) {
*rp = b->region;