Cleanup game data handling a bit: Move the structure to the C-file

and make the return value opaque (we shouldn't touch that stuff anyway)
This commit is contained in:
simon.kagstrom 2010-03-27 07:33:06 +00:00
parent b6bb39da68
commit 6631286c8a
3 changed files with 23 additions and 21 deletions

View File

@ -16,6 +16,22 @@
/* Current magic */
#define VERSION_MAGIC VERSION(1)
struct game_info
{
/* These two MUST stay the same */
uint32_t sz;
uint16_t version_magic;
uint16_t flags;
uint16_t author_off;
uint16_t name_off;
uint16_t screenshot_off; /* In PNG format */
uint16_t filename_off;
uint16_t score;
uint16_t year;
uint8_t data[]; /* 4-byte aligned */
};
struct game_info_v0
{
uint32_t sz;
@ -201,7 +217,7 @@ void GameInfo::resetDefaults()
this->screenshot = NULL;
}
struct game_info *GameInfo::dump()
void *GameInfo::dump(size_t *out_sz)
{
size_t total_sz = sizeof(struct game_info);
size_t png_sz;
@ -241,6 +257,7 @@ struct game_info *GameInfo::dump()
memcpy(out->data + out->filename_off, this->filename, strlen(this->filename) + 1);
memcpy(out->data + out->screenshot_off, png_data, png_sz);
*out_sz = out->sz;
/* Marshall it all */
out->sz = htonl(out->sz);
out->author_off = htons(out->author_off);
@ -251,7 +268,7 @@ struct game_info *GameInfo::dump()
out->score = htons(out->score);
out->year = htons(out->year);
return out;
return (void *)out;
}
bool GameInfo::fromDump(struct game_info *gi)

View File

@ -15,21 +15,7 @@ enum
GENRE_PUZZLE = 4,
};
struct game_info
{
/* These two MUST stay the same */
uint32_t sz;
uint16_t version_magic;
uint16_t flags;
uint16_t author_off;
uint16_t name_off;
uint16_t screenshot_off; /* In PNG format */
uint16_t filename_off;
uint16_t score;
uint16_t year;
uint8_t data[]; /* 4-byte aligned */
};
struct game_info;
class GameInfo
{
@ -55,7 +41,7 @@ public:
void resetDefaults();
/** Returns an allocated dump structure */
struct game_info *dump();
void *dump(size_t *out_sz);
/** Fill in this game info object from a structure */
bool fromDump(struct game_info *data);

View File

@ -541,12 +541,11 @@ void Gui::saveGameInfo(const char *base_path, const char *name)
if (strcmp(this->cur_gameInfo->filename, "unknown") == 0)
return;
struct game_info *p = this->cur_gameInfo->dump();
size_t sz;
void *p = this->cur_gameInfo->dump(&sz);
if (p)
{
size_t sz = ntohl(p->sz);
char *new_name = (char *)xmalloc(strlen(base_path) +
8 + strlen(name));
FILE *fp;