String SaveBody <T>(String bodyFile, T body) where T : FreeBody { XmlSerializer serializer = new XmlSerializer(typeof(FreeBodyFile)); var file = new FreeBodyFile { Body = body, Default = _WorkingDefaults[body] }; using (var xmlStream = new MemoryStream()) using (TextWriter fileStream = new StreamWriter(xmlStream)) { serializer.Serialize(fileStream, body); fileStream.Flush(); xmlStream.Seek(0, SeekOrigin.Begin); String text = new StreamReader(xmlStream).ReadToEnd(); File.WriteAllText(bodyFile, text); return(text); } }
static FreeBody LoadBody(String bodyFileName) { var genericParser = new XmlSerializer(typeof(FreeBody)); String typeName = null; FreeBodyFile asBodyFile = null; using (var f = new FileStream(bodyFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { XmlDocument doc = new XmlDocument(); doc.Load(f); typeName = doc.DocumentElement.Name; f.Seek(0, SeekOrigin.Begin); XmlSerializer fileParser; switch (Path.GetExtension(bodyFileName)) { default: throw new NotImplementedException(); case ".fb": // FreeBody fileParser = new XmlSerializer(typeof(FreeBody)); break; case ".fbd": // FreeBody Defaults/...Definition? Needs new model throw new NotImplementedException(); fileParser = new XmlSerializer(typeof(FreeBodyFile)); break; } //var file = (FreeBodyFile)fileParser.Deserialize(f); //Debugger.Break(); } var typeMap = new Dictionary <String, Type>() { { "Asteroid", typeof(BodyTypes.Asteroid) }, { "Belt", typeof(BodyTypes.Belt) }, { "BlackHole", typeof(BodyTypes.BlackHole) }, { "Comet", typeof(BodyTypes.Comet) }, { "MegaStructure", typeof(BodyTypes.MegaStructure) }, { "Moon", typeof(BodyTypes.Moon) }, { "Planet", typeof(BodyTypes.Planet) }, { "Star", typeof(BodyTypes.Star) }, { "Settlement", typeof(BodyTypes.Settlement) }, { "WarpGate", typeof(BodyTypes.WarpGate) }, }; var type = typeMap[typeName]; var specificParser = new XmlSerializer(type); using (var f = new FileStream(bodyFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { var fb = (FreeBody)specificParser.Deserialize(f); fb.CelestialContexts.RemoveAll(c => c.FrameId == fb.Id); // Remove cases where the body is parented to itself return(fb); } }