/// <summary> /// Save an InterstellarSystem object to a new save file. /// </summary> static public bool SaveNewSystemFile(InterstellaSystem systemToSave) { // Get the InterstellarSystem object as a list of strings in a text format. List <string> SaveLines = parseSystemToSaveFile(systemToSave); try { // Attempt to open a new save file in the executing directory and write to the system to it. FileWriter = new StreamWriter(ExecutingDomainFilePath + systemToSave.SystemSaveName + "OSSaveFile.txt"); foreach (var Line in SaveLines) { FileWriter.WriteLine(Line); } } catch { // return false if the save operation is unsucesfull return(false); } finally { // Close the file writer regardless of if the save operation is successfull FileWriter.Close(); } // return true if the save operation is sucessfull return(true); }
/// <summary> /// Parse an InterstellarSystem Object into a text format. /// </summary> static private List <string> parseSystemToSaveFile(InterstellaSystem system) { List <string> TextSaveFormat = new List <string>(); // add file infomation header TextSaveFormat.Add($"#{system.SystemSaveName} Orbital Simulator Save File"); TextSaveFormat.Add($"SaveTime: { DateTime.Now },"); // Add System TextSaveFormat.Add($"SystemName: {system.SystemSaveName},"); TextSaveFormat.Add($"CollissionMassRetentionPercentage: {system.CollissionMassRetentionPercentage},"); TextSaveFormat.Add($"Objects:"); // Add Objects TextSaveFormat.Add($"["); foreach (InterstellaObject interstellaObject in system.InterstellaObjects) { TextSaveFormat.Add(" {"); TextSaveFormat.Add($" Type: {interstellaObject.Type.ToString()},"); TextSaveFormat.Add($" Position: {{{interstellaObject.Position.X}, {interstellaObject.Position.Y} }},"); TextSaveFormat.Add($" Velocity: {{{interstellaObject.Velocity.X}, {interstellaObject.Velocity.Y} }},"); TextSaveFormat.Add($" Acceleration: {{{interstellaObject.Acceleration.X}, {interstellaObject.Acceleration.Y} }},"); TextSaveFormat.Add($" Mass: {interstellaObject.Mass},"); TextSaveFormat.Add($" Radius: {interstellaObject.Radius},"); TextSaveFormat.Add(" },"); } TextSaveFormat.Add($"]"); return(TextSaveFormat); }
/// <summary> /// Over-write an existing save file with /// </summary> static public bool OverWriteSystemFile(InterstellaSystem systemToSave, string fileStringToOverWrite) { // Get the InterstellarSystem object as a list of strings in a text format. List <string> SaveLines = parseSystemToSaveFile(systemToSave); // Attempt to overwrite the file of the given path. try { // Open the stream writer to the file path to over write, set not to append (to overwrite) FileWriter = new StreamWriter(fileStringToOverWrite, false); foreach (var Line in SaveLines) { FileWriter.WriteLine(Line); } } catch { // Return false if the save operation fails return(false); } finally { // Close the file writer regardless of if the save operation is successfull FileWriter.Close(); } // return true if the save operation is sucessfull return(true); }
/// <summary> /// Converts a complete List of Data Objects and returns an InterstellarSystem Object /// </summary> private static InterstellaSystem ParseIntermediateFormToSystem(List <Data> intermediateDataFormatSystem) { InterstellaSystem DataAsSystemType = new InterstellaSystem(); // loop through each data object foreach (var DataItem in intermediateDataFormatSystem) { // Check For System Name if (DataItem.Tag == _DataReadTypes[0].Tag) { DataAsSystemType.SystemSaveName = DataItem.DataString; } // Check For System Mass Retention else if (DataItem.Tag == _DataReadTypes[1].Tag) { DataAsSystemType.CollissionMassRetentionPercentage = (double)Convert.ChangeType(DataItem.DataString, DataItem.DesiredType); } // Check for objects collections else if (DataItem.Tag == _DataReadTypes[2].Tag) { List <InterstellaObject> Objects = new List <InterstellaObject>(); // Cast each data string too a interstella Object foreach (var DataObject in DataItem.DataStrings) { InterstellaObjectParams ObjectParams = new InterstellaObjectParams(); // Get Properties foreach (var ObjectProperty in DataObject.DataStrings) { // Get Type if (ObjectProperty.Tag == _DataReadTypes[3].Tag) { ObjectParams.Type = ConvertDataStringToTypeEnum(ObjectProperty.DataString); } // Get Position else if (ObjectProperty.Tag == _DataReadTypes[4].Tag) { ObjectParams.Position = CastDataToVector(ObjectProperty.DataStrings); } // Get Velocity else if (ObjectProperty.Tag == _DataReadTypes[5].Tag) { ObjectParams.Velocity = CastDataToVector(ObjectProperty.DataStrings); } // Get Acceleration else if (ObjectProperty.Tag == _DataReadTypes[6].Tag) { ObjectParams.Acceleration = CastDataToVector(ObjectProperty.DataStrings); } // Get Mass else if (ObjectProperty.Tag == _DataReadTypes[7].Tag) { ObjectParams.Mass = (double)Convert.ChangeType(ObjectProperty.DataString, ObjectProperty.DesiredType); } // Get Radius else if (ObjectProperty.Tag == _DataReadTypes[8].Tag) { ObjectParams.Radius = (double)Convert.ChangeType(ObjectProperty.DataString, ObjectProperty.DesiredType); } } // Use the retrieved data to construct an Interstellar Object Objects.Add(new InterstellaObject(ObjectParams)); } // Add these objects to the system DataAsSystemType.InterstellaObjects = Objects; } } return(DataAsSystemType); }