Beispiel #1
0
bool C4Console::SaveGame(const char * path)
{
	// Network hosts only
	if (::Network.isEnabled() && !::Network.isHost())
		{ Message(LoadResStr("IDS_GAME_NOCLIENTSAVE")); return false; }

	// Save game to open scenario file
	bool fOkay=true;
	SetCursor(C4ConsoleGUI::CURSOR_Wait);

	C4GameSave *pGameSave = new C4GameSaveSavegame();
	if (!pGameSave->Save(path))
		{ Out("Save failed"); fOkay=false; }
	delete pGameSave;

	SetCursor(C4ConsoleGUI::CURSOR_Normal);

	// Status report
	if (!fOkay) Message(LoadResStr("IDS_CNS_SAVERROR"));
	else Out(LoadResStr("IDS_CNS_GAMESAVED"));

	return fOkay;
}
Beispiel #2
0
bool C4Console::SaveScenario(const char * path)
{
	// Network hosts only
	if (::Network.isEnabled() && !::Network.isHost())
		{ Message(LoadResStr("IDS_GAME_NOCLIENTSAVE")); return false; }

	// Open new scenario file
	if (path)
	{
		// Close current scenario file
		Game.ScenarioFile.Close();
		// Copy current scenario file to target
		if (!C4Group_CopyItem(Game.ScenarioFilename,path))
		{
			Message(FormatString(LoadResStr("IDS_CNS_SAVEASERROR"),path).getData());
			return false;
		}
		SCopy(path, Game.ScenarioFilename);
		SetCaptionToFilename(Game.ScenarioFilename);
		if (!Game.ScenarioFile.Open(Game.ScenarioFilename))
		{
			Message(FormatString(LoadResStr("IDS_CNS_SAVEASERROR"),Game.ScenarioFilename).getData());
			return false;
		}
	}

	// Can't save to child groups
	if (Game.ScenarioFile.GetMother() && Game.ScenarioFile.GetMother()->IsPacked())
	{
		StdStrBuf str;
		str.Format(LoadResStr("IDS_CNS_NOCHILDSAVE"),
		           GetFilename(Game.ScenarioFile.GetName()));
		Message(str.getData());
		return false;
	}

	// Save game to open scenario file
	SetCursor(C4ConsoleGUI::CURSOR_Wait);

	bool fOkay=true;
	C4GameSave *pGameSave = new C4GameSaveScenario(!Console.Active || ::Landscape.Mode==C4LSC_Exact, false);
	if (!pGameSave->Save(Game.ScenarioFile, false))
		{ Out("Game::Save failed"); fOkay=false; }
	delete pGameSave;

	// Close and reopen scenario file to fix file changes
	if (!Game.ScenarioFile.Close())
		{ Out("ScenarioFile::Close failed"); fOkay=false; }
	if (!Game.ScenarioFile.Open(Game.ScenarioFilename))
		{ Out("ScenarioFile::Open failed"); fOkay=false; }

	SetCursor(C4ConsoleGUI::CURSOR_Normal);

	// Initialize/script notification
	if (Game.fScriptCreatedObjects)
	{
		StdStrBuf str(LoadResStr("IDS_CNS_SCRIPTCREATEDOBJECTS"));
		str += LoadResStr("IDS_CNS_WARNDOUBLE");
		Message(str.getData());
		Game.fScriptCreatedObjects=false;
	}

	// Status report
	if (!fOkay) Message(LoadResStr("IDS_CNS_SAVERROR"));
	else Out(LoadResStr("IDS_CNS_SCENARIOSAVED"));

	return fOkay;
}
Beispiel #3
0
bool C4Console::SaveScenario(const char * path, bool export_packed)
{
	C4Group *save_target_group = &Game.ScenarioFile;
	C4Group export_group;
	if (export_packed)
	{
		// Export to packed file: Delete existing
		if (FileExists(path))
		{
			if (ItemIdentical(Game.ScenarioFilename, path) || !EraseItem(path))
			{
				Message(FormatString(LoadResStr("IDS_CNS_SAVEASERROR"), path).getData());
				return false;
			}
		}
		// Write into new, packed copy
		if (!C4Group_PackDirectoryTo(Game.ScenarioFilename, path) || !export_group.Open(path))
		{
			Message(FormatString(LoadResStr("IDS_CNS_SAVEASERROR"), path).getData());
			return false;
		}
		save_target_group = &export_group;
	}
	else if (path)
	{
		// When trying to save into a subfolder of the existing scenario, the copy function
		// below will try to recursively copy everything until it blows the stack. There is
		// really no good reason to do this, so we just disallow it here.
		if (SEqual2(path, Game.ScenarioFilename))
		{
			Message(LoadResStr("IDS_CNS_RECURSIVESAVEASERROR"));
			return false;
		}
		// Open new scenario file
		// Close current scenario file to allow re-opening at new path
		Game.ScenarioFile.Close();
		// Copy current scenario file to target
		if (!C4Group_CopyItem(Game.ScenarioFilename,path))
		{
			Message(FormatString(LoadResStr("IDS_CNS_SAVEASERROR"),path).getData());
			return false;
		}
		// Re-open at new path (unless exporting, in which case the export is just a copy)
		SCopy(path, Game.ScenarioFilename, _MAX_PATH);
		SetCaptionToFilename(Game.ScenarioFilename);
		if (!Game.ScenarioFile.Open(Game.ScenarioFilename))
		{
			Message(FormatString(LoadResStr("IDS_CNS_SAVEASERROR"), Game.ScenarioFilename).getData());
			return false;
		}
	}
	else
	{
		// Do not save to temp network file
		if (Game.TempScenarioFile && ItemIdentical(Game.TempScenarioFile.getData(), Game.ScenarioFilename))
		{
			Message(LoadResStr("IDS_CNS_NONETREFSAVE"));
			return false;
		}
	}

	// Can't save to child groups
	if (save_target_group->GetMother() && save_target_group->GetMother()->IsPacked())
	{
		StdStrBuf str;
		str.Format(LoadResStr("IDS_CNS_NOCHILDSAVE"),
		           GetFilename(save_target_group->GetName()));
		Message(str.getData());
		return false;
	}

	// Save game to open scenario file
	SetCursor(C4ConsoleGUI::CURSOR_Wait);

	bool fOkay=true;
	C4GameSave *pGameSave = new C4GameSaveScenario(!Console.Active || ::Landscape.GetMode() == LandscapeMode::Exact, false);
	if (!pGameSave->Save(*save_target_group, false))
		{ Out("Game::Save failed"); fOkay=false; }
	delete pGameSave;

	// Close and reopen scenario file to fix file changes
	if (!export_packed)
	{
		if (!Game.ScenarioFile.Close())
		{
			Out("ScenarioFile::Close failed"); fOkay = false;
		}
		if (!Game.ScenarioFile.Open(Game.ScenarioFilename))
		{
			Out("ScenarioFile::Open failed"); fOkay = false;
		}
	}

	SetCursor(C4ConsoleGUI::CURSOR_Normal);

	// Initialize/script notification
	if (Game.fScriptCreatedObjects)
	{
		StdStrBuf str(LoadResStr("IDS_CNS_SCRIPTCREATEDOBJECTS"));
		str += LoadResStr("IDS_CNS_WARNDOUBLE");
		Message(str.getData());
		Game.fScriptCreatedObjects = false;
	}

	// Status report
	if (!fOkay) Message(LoadResStr("IDS_CNS_SAVERROR"));
	else Out(LoadResStr("IDS_CNS_SCENARIOSAVED"));

	return fOkay;
}