Ejemplo n.º 1
0
size_t GameDatabase::SaveGame(const Game &game, 
							  const StringPairArray &rulesets, 
							  const StringPairArray& options,
							  const ISerialize& stats, 
							  const bool reload)
{
	wxString file = FilePath::Data(DataFileGames);
	wxString keyfile = FilePath::Data(DataFileGameKeys);

	// Save the game.
	wxFile gamedb;
	gamedb.Open(file, wxFile::write_append);
	wxFileOutputStream x(gamedb);
	wxDataOutputStream games(x);

	// Move to the end.
	gamedb.SeekEnd();

	// Get the starting position
	wxUint32 pos = gamedb.Tell();

	// Write out the rulesets loaded.
	games << (wxUint32) rulesets.size();
	GameDatabase::StringPairArray::const_iterator it, itEnd = rulesets.end();
	for(it = rulesets.begin(); it != itEnd; ++it)
	{
		games << it->first;
		games << it->second;
	}

	// options data
	games << (wxUint32) options.size();

	itEnd = options.end();
	for(it = options.begin(); it != itEnd; ++it)
	{
		games << it->first;
		games << it->second;
	}

	// Write out the game.
	game.save(games);

	// Stats must always go at the end so we can ignore them while loading if
	// desired.
	stats.save(games);
		
	// Close the file.
	gamedb.Close();

	// Now update the key file.
	wxFile keydb;
	keydb.Open(keyfile, wxFile::write_append);
	wxFileOutputStream y(keydb);
	wxDataOutputStream keys(y);

	// Move to the end
	keydb.SeekEnd();

	// Write the offset
	keys << pos;

	wxUint32 keyLength;
	keyLength = keydb.Length();

	// Flush it.
	keydb.Close();

	if(true == reload)
	{
		InitializeDatabase();
	}

	// Calculate the number of games.
	size_t index = (keyLength / sizeof(size_t)) - 1;
	return index;
}