Example #1
0
void Game::Serialize(Serializer::Writer &wr)
{
	// leading signature
	for (Uint32 i = 0; i < strlen(s_saveStart)+1; i++)
		wr.Byte(s_saveStart[i]);

	// version
	wr.Int32(s_saveVersion);

	Serializer::Writer section;

	// space, all the bodies and things
	m_space->Serialize(section);
	wr.WrSection("Space", section.GetData());


	// game state and space transition state
	section = Serializer::Writer();

	section.Int32(m_space->GetIndexForBody(m_player.Get()));

	// hyperspace clouds being brought over from the previous system
	section.Int32(m_hyperspaceClouds.size());
	for (std::list<HyperspaceCloud*>::const_iterator i = m_hyperspaceClouds.begin(); i != m_hyperspaceClouds.end(); ++i)
		(*i)->Serialize(section, m_space.Get());

	section.Double(m_time);
	section.Int32(Uint32(m_state));

	section.Bool(m_wantHyperspace);
	section.Double(m_hyperspaceProgress);
	section.Double(m_hyperspaceDuration);
	section.Double(m_hyperspaceEndTime);

	wr.WrSection("Game", section.GetData());


	// system political data (crime etc)
	section = Serializer::Writer();
	Polit::Serialize(section);
	wr.WrSection("Polit", section.GetData());


	// views. must be saved in init order
	section = Serializer::Writer();
	Pi::cpan->Save(section);
	wr.WrSection("ShipCpanel", section.GetData());

	section = Serializer::Writer();
	Pi::sectorView->Save(section);
	wr.WrSection("SectorView", section.GetData());

	section = Serializer::Writer();
	Pi::worldView->Save(section);
	wr.WrSection("WorldView", section.GetData());


	// lua
	section = Serializer::Writer();
	Pi::luaSerializer->Serialize(section);
	wr.WrSection("LuaModules", section.GetData());


	// trailing signature
	for (Uint32 i = 0; i < strlen(s_saveEnd)+1; i++)
		wr.Byte(s_saveEnd[i]);
}
void BinaryConverter::Save(const std::string& filename, const std::string& savepath, Model* m, const bool bInPlace)
{
	printf("Saving file (%s)\n", filename.c_str());
	FILE *f = nullptr;
	FileSystem::FileSourceFS newFS(FileSystem::GetDataDir());
	if (!bInPlace) {
		if (!FileSystem::userFiles.MakeDirectory(SAVE_TARGET_DIR))
			throw CouldNotOpenFileException();

		std::string newpath = savepath.substr(0, savepath.size()-filename.size());
		size_t pos = newpath.find_first_of("/", 0);
		while(pos<savepath.size()-filename.size()) {
			newpath = savepath.substr(0, pos);
			pos = savepath.find_first_of("/", pos+1);
			if (!FileSystem::userFiles.MakeDirectory(FileSystem::JoinPathBelow(SAVE_TARGET_DIR,newpath)))
				throw CouldNotOpenFileException();
			printf("Made directory (%s)\n", FileSystem::JoinPathBelow(SAVE_TARGET_DIR,newpath).c_str());
		}

		f = FileSystem::userFiles.OpenWriteStream(
			FileSystem::JoinPathBelow(SAVE_TARGET_DIR, savepath + SGM_EXTENSION));
		printf("Save file (%s)\n", FileSystem::JoinPathBelow(SAVE_TARGET_DIR, savepath + SGM_EXTENSION).c_str());
		if (!f) throw CouldNotOpenFileException();
	} else {
		f = newFS.OpenWriteStream(savepath + SGM_EXTENSION);
		if (!f) throw CouldNotOpenFileException();
	}

	Serializer::Writer wr;

	wr.Int32(SGM_STRING_ID.value);

	wr.Int32(SGM_VERSION);

	wr.String(m->GetName().c_str());

	SaveMaterials(wr, m);

	SaveHelperVisitor sv(&wr, m);
	m->GetRoot()->Accept(sv);

	m->GetCollisionMesh()->Save(wr);
	wr.Float(m->GetDrawClipRadius());

	SaveAnimations(wr, m);

	//save tags
	wr.Int32(m->GetNumTags());
	for (unsigned int i = 0; i < m->GetNumTags(); i++)
		wr.String(m->GetTagByIndex(i)->GetName().c_str());

	// compress in memory, write to open file 
	size_t outSize = 0;
	size_t nwritten = 0;
	const std::string& data = wr.GetData();
	void *pCompressedData = tdefl_compress_mem_to_heap(data.data(), data.length(), &outSize, 128);
	if (pCompressedData) {
		nwritten = fwrite(pCompressedData, outSize, 1, f);
		mz_free(pCompressedData);
	}
	fclose(f);

	if (nwritten != 1) throw CouldNotWriteToFileException();
}