extract read_region and test that it fixes bad names.

This commit is contained in:
Enno Rehling 2016-11-12 01:03:07 +01:00
parent ec787743f0
commit 663ad17b5a
3 changed files with 60 additions and 17 deletions

View File

@ -1125,6 +1125,17 @@ static region *readregion(struct gamedata *data, int x, int y)
return r; return r;
} }
region *read_region(gamedata *data)
{
storage *store = data->store;
region *r;
int x, y;
READ_INT(store, &x);
READ_INT(store, &y);
r = readregion(data, x, y);
return r;
}
void writeregion(struct gamedata *data, const region * r) void writeregion(struct gamedata *data, const region * r)
{ {
assert(r); assert(r);
@ -1189,6 +1200,14 @@ void writeregion(struct gamedata *data, const region * r)
WRITE_SECTION(data->store); WRITE_SECTION(data->store);
} }
void write_region(gamedata *data, const region *r)
{
storage *store = data->store;
WRITE_INT(store, r->x);
WRITE_INT(store, r->y);
writeregion(data, r);
}
static ally **addally(const faction * f, ally ** sfp, int aid, int state) static ally **addally(const faction * f, ally ** sfp, int aid, int state)
{ {
struct faction *af = findfaction(aid); struct faction *af = findfaction(aid);
@ -1344,10 +1363,6 @@ void _test_write_password(gamedata *data, const faction *f) {
write_password(data, f); write_password(data, f);
} }
/** Reads a faction from a file.
* This function requires no context, can be called in any state. The
* faction may not already exist, however.
*/
faction *read_faction(struct gamedata * data) faction *read_faction(struct gamedata * data)
{ {
ally **sfp; ally **sfp;
@ -1803,18 +1818,11 @@ int read_game(gamedata *data) {
rmax = nread; rmax = nread;
} }
log_debug(" - Einzulesende Regionen: %d/%d\r", rmax, nread); log_debug(" - Einzulesende Regionen: %d/%d\r", rmax, nread);
while (--nread >= 0) { while (--nread >= 0) {
unit **up; unit **up;
int x, y;
READ_INT(store, &x);
READ_INT(store, &y);
if ((nread & 0x3FF) == 0) { /* das spart extrem Zeit */ r = read_region(data);
log_debug(" - Einzulesende Regionen: %d/%d * %d,%d \r", rmax, nread, x, y);
}
--rmax;
r = readregion(data, x, y);
/* Burgen */ /* Burgen */
READ_INT(store, &p); READ_INT(store, &p);
@ -1861,8 +1869,13 @@ int read_game(gamedata *data) {
*up = u; *up = u;
up = &u->next; up = &u->next;
update_interval(u->faction, u->region); update_interval(u->faction, r);
} }
if ((nread & 0x3FF) == 0) { /* das spart extrem Zeit */
log_debug(" - Einzulesende Regionen: %d/%d * %d,%d \r", rmax, nread, r->x, r->y);
}
--rmax;
} }
read_borders(data); read_borders(data);
@ -2032,9 +2045,7 @@ int write_game(gamedata *data) {
log_debug(" - Schreibe Regionen: %d", n); log_debug(" - Schreibe Regionen: %d", n);
} }
WRITE_SECTION(store); WRITE_SECTION(store);
WRITE_INT(store, r->x); write_region(data, r);
WRITE_INT(store, r->y);
writeregion(data, r);
WRITE_INT(store, listlen(r->buildings)); WRITE_INT(store, listlen(r->buildings));
WRITE_SECTION(store); WRITE_SECTION(store);

View File

@ -63,6 +63,9 @@ extern "C" {
void write_faction(struct gamedata *data, const struct faction *f); void write_faction(struct gamedata *data, const struct faction *f);
struct faction *read_faction(struct gamedata *data); struct faction *read_faction(struct gamedata *data);
void write_region(struct gamedata *data, const struct region *r);
struct region *read_region(struct gamedata *data);
void write_building(struct gamedata *data, const struct building *b); void write_building(struct gamedata *data, const struct building *b);
struct building *read_building(struct gamedata *data); struct building *read_building(struct gamedata *data);

View File

@ -111,6 +111,34 @@ static void test_readwrite_faction(CuTest * tc)
test_cleanup(); test_cleanup();
} }
static void test_readwrite_region(CuTest * tc)
{
gamedata data;
storage store;
region *r;
test_setup();
r = test_create_region(0, 0, 0);
free(r->land->name);
r->land->name = _strdup(" Hodor ");
CuAssertStrEquals(tc, " Hodor ", r->land->name);
mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION);
write_region(&data, r);
data.strm.api->rewind(data.strm.handle);
free_gamedata();
gamedata_init(&data, &store, RELEASE_VERSION);
r = read_region(&data);
CuAssertPtrNotNull(tc, r);
CuAssertStrEquals(tc, "Hodor", r->land->name);
regions = r;
mstream_done(&data.strm);
gamedata_done(&data);
test_cleanup();
}
static void test_readwrite_building(CuTest * tc) static void test_readwrite_building(CuTest * tc)
{ {
gamedata data; gamedata data;
@ -427,6 +455,7 @@ CuSuite *get_save_suite(void)
SUITE_ADD_TEST(suite, test_readwrite_data); SUITE_ADD_TEST(suite, test_readwrite_data);
SUITE_ADD_TEST(suite, test_readwrite_unit); SUITE_ADD_TEST(suite, test_readwrite_unit);
SUITE_ADD_TEST(suite, test_readwrite_faction); SUITE_ADD_TEST(suite, test_readwrite_faction);
SUITE_ADD_TEST(suite, test_readwrite_region);
SUITE_ADD_TEST(suite, test_readwrite_building); SUITE_ADD_TEST(suite, test_readwrite_building);
SUITE_ADD_TEST(suite, test_readwrite_ship); SUITE_ADD_TEST(suite, test_readwrite_ship);
SUITE_ADD_TEST(suite, test_readwrite_dead_faction_createunit); SUITE_ADD_TEST(suite, test_readwrite_dead_faction_createunit);