Exemplo n.º 1
0
        public static ModSaveFile LoadFrom(FileInfo fileName)
        {
            if (!fileName.Exists)
            {
                RuntimeModController.Log.Write("Mod save exists for this world, creating a new one", fileName.Name);
                return(new ModSaveFile());
            }

            var doc = XDocument.Load(fileName.FullName);
            var msf = new ModSaveFile();

            msf.allSavedDataAsArray = doc.Element("ModSaveFile").Elements("Mod").Select(md => new ModSaveData(md)).ToArray();
            return(msf);

            /*
             * var dcserializer = GetDataContractSerializer();
             * //var serializer = new System.Xml.Serialization.XmlSerializer(typeof(ModEnvironmentConfiguration));
             * using (var fstream = fileName.OpenRead())
             * {
             *  var msf = (ModSaveFile)dcserializer.ReadObject(fstream);
             *  return msf;
             * }
             */
        }
Exemplo n.º 2
0
            public void WriteSaveData(System.Xml.XmlWriter writer)
            {
                //We simply skip null values;
                if (value == null)
                {
                    return;
                }

                writer.WriteStartElement("Data");
                writer.WriteAttributeString("Key", key);
                // Todo: does this create a security hole? Though mods can do everything anyway, this may abuse a mod to do sth harmful. Consider it!
                writer.WriteAttributeString("Type", value.GetType().AssemblyQualifiedName);

                var dcs = new DataContractSerializer(value.GetType(), "", "", null, Int32.MaxValue, false, false, null, ModSaveFile.GetDataContractResolver());

                dcs.WriteObjectContent(writer, value);

                //var dcjs = new System.Runtime.Serialization.Json.DataContractJsonSerializer(value.GetType()

                writer.WriteEndElement();
            }
Exemplo n.º 3
0
 public DataItem(XElement el)
 {
     try
     {
         // Todo: handle invalid data properly!!
         key = el.Attribute("Key").Value;
         var type = Type.GetType(el.Attribute("Type").Value, true);
         //if( type.IsPrimitive )
         // TODO: Complete member initialization#
         var dcs       = new DataContractSerializer(type, null, int.MaxValue, false, false, null, ModSaveFile.GetDataContractResolver());
         var tmpReader = el.CreateReader();
         tmpReader.MoveToContent();
         value = dcs.ReadObject(tmpReader, false);
     }
     catch (Exception)
     {
         isInvalid = true;
     }
     return;
 }