protected static void SavePrefabToFile(MyObjectBuilder_Definitions prefab, string name, bool replace = false, MyBlueprintTypeEnum type = MyBlueprintTypeEnum.LOCAL) { //if (name == null) //{ // name = MyUtils.StripInvalidChars(MyCubeBuilder.Static.Clipboard.CopiedGridsName); //} Debug.Assert(name != null, "Name cannot be null"); string file = ""; if (type == MyBlueprintTypeEnum.LOCAL) { file = Path.Combine(m_localBlueprintFolder, name); } else { file = Path.Combine(m_workshopBlueprintFolder, "temp", name); } string filePath = ""; int index = 1; try { if (!replace) { while (MyFileSystem.DirectoryExists(file)) { file = Path.Combine(m_localBlueprintFolder, name + "_" + index); index++; } if (index > 1) { name += new StringBuilder("_" + (index - 1)); } } filePath = file + "\\bp.sbc"; var success = MyObjectBuilderSerializer.SerializeXML(filePath, false, prefab); Debug.Assert(success, "falied to write blueprint to file"); if (!success) { MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox( buttonType: MyMessageBoxButtonsType.OK, styleEnum: MyMessageBoxStyleEnum.Error, messageCaption: new StringBuilder("Error"), messageText: new StringBuilder("There was a problem with saving blueprint") )); if (Directory.Exists(file)) { Directory.Delete(file, true); } } } catch (Exception e) { MySandboxGame.Log.WriteLine(String.Format("Failed to write prefab at file {0}, message: {1}, stack:{2}", filePath, e.Message, e.StackTrace)); } }
public static void LoadLastSession() { var lastSessionPath = MyLocalCache.GetLastSessionPath(); if (!MyFileSystem.DirectoryExists(lastSessionPath)) { MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox( messageText: MyTexts.Get(MyCommonTexts.MessageBoxLastSessionNotFound), messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError), styleEnum: MyMessageBoxStyleEnum.Error)); return; } LoadSingleplayerSession(lastSessionPath); }
public void CreateFromClipboard(bool withScreenshot = false, bool replace = false) { if (MyCubeBuilder.Static.Clipboard.CopiedGridsName == null) { return; } string name = MyUtils.StripInvalidChars(MyCubeBuilder.Static.Clipboard.CopiedGridsName); string newName = name; string path = Path.Combine(m_localBlueprintFolder, name); int index = 1; while (MyFileSystem.DirectoryExists(path)) { newName = name + "_" + index; path = Path.Combine(m_localBlueprintFolder, newName); index++; } string imagePath = Path.Combine(path, "thumb.png"); if (withScreenshot) { TakeScreenshot(newName); } var prefab = MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_ShipBlueprintDefinition>(); prefab.Id = new MyDefinitionId(new MyObjectBuilderType(typeof(MyObjectBuilder_ShipBlueprintDefinition)), MyUtils.StripInvalidChars(name)); prefab.CubeGrids = MyCubeBuilder.Static.Clipboard.CopiedGrids.ToArray(); prefab.RespawnShip = false; prefab.DisplayName = MySteam.UserName; prefab.OwnerSteamId = MySteam.UserId; if (MyFakes.ENABLE_BATTLE_SYSTEM) { prefab.BattlePoints = GetBattlePoints(prefab.CubeGrids); } prefab.CubeGrids[0].DisplayName = name; var definitions = MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_Definitions>(); definitions.ShipBlueprints = new MyObjectBuilder_ShipBlueprintDefinition[1]; definitions.ShipBlueprints[0] = prefab; SavePrefabToFile(definitions, replace: replace); RefreshBlueprintList(); }
// 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); } }
public override void Init(MyObjectBuilder_SessionComponent sessionComponent) { base.Init(sessionComponent); // Only servers can run this session component if (!Session.IsServer) { return; } MyObjectBuilder_VisualScriptManagerSessionComponent ob = (MyObjectBuilder_VisualScriptManagerSessionComponent)sessionComponent; m_objectBuilder = ob; m_relativePathsToAbsolute.Clear(); m_stateMachineDefinitionFilePaths.Clear(); // Runs game started event m_firstUpdate = ob.FirstRun; // load vanilla level script files if (ob.LevelScriptFiles != null) { foreach (string relativeFilePath in ob.LevelScriptFiles) { var absolutePath = Path.Combine(MyFileSystem.ContentPath, relativeFilePath); // Add only existing files if (MyFileSystem.FileExists(absolutePath)) { m_relativePathsToAbsolute.Add(relativeFilePath, absolutePath); } else { MyLog.Default.WriteLine(relativeFilePath + " Level Script was not found."); Debug.Fail(relativeFilePath + " Level Script was not found."); } } } // load vanilla mission manchines if (ob.StateMachines != null) { foreach (var relativeFilePath in ob.StateMachines) { var absolutePath = Path.Combine(MyFileSystem.ContentPath, relativeFilePath); // Add only existing files if (MyFileSystem.FileExists(absolutePath)) { if (!m_relativePathsToAbsolute.ContainsKey(relativeFilePath)) { m_stateMachineDefinitionFilePaths.Add(absolutePath); } m_relativePathsToAbsolute.Add(relativeFilePath, absolutePath); } else { MyLog.Default.WriteLine(relativeFilePath + " Mission File was not found."); Debug.Fail(relativeFilePath + " Mission File was not found."); } } } // Load mission machines and level scripts from mods // Overrides vanilla files if (Session.Mods != null) { foreach (var modItem in Session.Mods) { // First try is a mod archive var directoryPath = Path.Combine(MyFileSystem.ModsPath, modItem.PublishedFileId + ".sbm"); if (!MyFileSystem.DirectoryExists(directoryPath)) { directoryPath = Path.Combine(MyFileSystem.ModsPath, modItem.Name); if (!MyFileSystem.DirectoryExists(directoryPath)) { directoryPath = null; } } if (!string.IsNullOrEmpty(directoryPath)) { foreach (var filePath in MyFileSystem.GetFiles(directoryPath, "*", MySearchOption.AllDirectories)) { var extension = Path.GetExtension(filePath); var relativePath = MyFileSystem.MakeRelativePath(Path.Combine(directoryPath, "VisualScripts"), filePath); if (extension == ".vs" || extension == ".vsc") { if (m_relativePathsToAbsolute.ContainsKey(relativePath)) { m_relativePathsToAbsolute[relativePath] = filePath; } else { m_relativePathsToAbsolute.Add(relativePath, filePath); } } } } } } // Provider will compile all required scripts for the session. MyVSAssemblyProvider.Init(m_relativePathsToAbsolute.Values); // Retrive the Level script instances m_levelScripts = new CachingList <IMyLevelScript>(); var scriptInstances = MyVSAssemblyProvider.GetLevelScriptInstances(); if (scriptInstances != null) { scriptInstances.ForEach(script => m_levelScripts.Add(script)); } m_levelScripts.ApplyAdditions(); // Store the names of the level scripts m_runningLevelScriptNames = m_levelScripts.Select(x => x.GetType().Name).ToArray(); m_failedLevelScriptExceptionTexts = new string[m_runningLevelScriptNames.Length]; // mission manager initialization - state machine definitions m_smManager = new MyVSStateMachineManager(); foreach (var stateMachineFilePath in m_stateMachineDefinitionFilePaths) { m_smManager.AddMachine(stateMachineFilePath); } }