void SaveAsync(string newSaveName, string sessionPath, MyWorldInfo copyFrom) { // Try a simple path, then a random if it already exists var newSessionPath = MyLocalCache.GetSessionSavesPath(newSaveName, false, false); while (Directory.Exists(newSessionPath)) { newSessionPath = MyLocalCache.GetSessionSavesPath(newSaveName + MyUtils.GetRandomInt(int.MaxValue).ToString("########"), false, false); } Directory.CreateDirectory(newSessionPath); MyUtils.CopyDirectory(sessionPath, newSessionPath); ulong sizeInBytes; var checkpoint = MyLocalCache.LoadCheckpoint(newSessionPath, out sizeInBytes); Debug.Assert(checkpoint != null); checkpoint.SessionName = copyFrom.SessionName; checkpoint.WorkshopId = null; MyLocalCache.SaveCheckpoint(checkpoint, newSessionPath); MyLocalCache.SaveLastLoadedTime(newSessionPath, DateTime.Now); }
// Starts new session with campaign data public void LoadSessionFromActiveCampaign(string relativePath, Action afterLoad = null, string campaignDirectoryName = null) { var savePath = relativePath; string absolutePath; // >> WORLD FILE OPERATIONS // Find the existing file in order of modded content to vanilla if (m_activeCampaign.IsVanilla) { absolutePath = Path.Combine(MyFileSystem.ContentPath, savePath); if (!MyFileSystem.FileExists(absolutePath)) { MySandboxGame.Log.WriteLine("ERROR: Missing vanilla world file in campaign: " + m_activeCampaignName); Debug.Fail("ERROR: Missing vanilla world file in campaign: " + m_activeCampaignName); return; } } else { // Modded content absolutePath = Path.Combine(m_activeCampaign.ModFolderPath, savePath); // try finding respective vanilla file if the file does not exist if (!MyFileSystem.FileExists(absolutePath)) { absolutePath = Path.Combine(MyFileSystem.ContentPath, savePath); if (!MyFileSystem.FileExists(absolutePath)) { MySandboxGame.Log.WriteLine("ERROR: Missing world file in campaign: " + m_activeCampaignName); Debug.Fail("ERROR: Missing world file in campaign: " + m_activeCampaignName); return; } } } // Copy the save and load the session if (string.IsNullOrEmpty(campaignDirectoryName)) { campaignDirectoryName = ActiveCampaignName + " " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"); } var directory = new DirectoryInfo(Path.GetDirectoryName(absolutePath)); var targetDirectory = Path.Combine(MyFileSystem.SavesPath, campaignDirectoryName, directory.Name); while (MyFileSystem.DirectoryExists(targetDirectory)) { // Finid new unique name for the folder targetDirectory = Path.Combine(MyFileSystem.SavesPath, directory.Name + " " + MyUtils.GetRandomInt(int.MaxValue).ToString("########")); } MyUtils.CopyDirectory(directory.FullName, targetDirectory); // >> LOCALIZATION // Set localization for loaded level in the after load event if (string.IsNullOrEmpty(m_selectedLanguage)) { m_selectedLanguage = m_activeCampaign.DefaultLocalizationLanguage; if (string.IsNullOrEmpty(m_selectedLanguage) && m_activeCampaign.LocalizationLanguages.Count > 0) { m_selectedLanguage = m_activeCampaign.LocalizationLanguages[0]; } } if (!string.IsNullOrEmpty(m_selectedLanguage)) { // Initilize the sessionComponent in after load event afterLoad += () => { var comp = MySession.Static.GetComponent <MyLocalizationSessionComponent>(); comp.LoadCampaignLocalization(m_activeCampaign.LocalizationPaths, m_activeCampaign.ModFolderPath); comp.SwitchLanguage(m_selectedLanguage); }; } // ATM only single player campaigns are supported if (!m_activeCampaign.IsMultiplayer) { MySessionLoader.LoadSingleplayerSession(targetDirectory, afterLoad); } }