public static void Serialize(Type type, string[] ignoreFields) { DiffSerializer serializer = new DiffSerializer(type, ignoreFields); FileStream fs = new FileStream(type.Name + "s.xml", FileMode.OpenOrCreate); XmlWriterSettings ws = new XmlWriterSettings(); ws.Indent = true; XmlDictionaryWriter.Create(fs, ws); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); writer.WriteStartElement("ArrayOf" + type.Name); PropertyInfo name = type.GetProperty("Name"); int count = 0; for (int i = 0; i < 1000; i++) { object obj = Activator.CreateInstance(type); ITEM_SET_DEFAULTS.Invoke(obj, new object[] { i, false }); string value = (String)name.GetValue(obj, null); if (!String.IsNullOrWhiteSpace(value)) { serializer.WriteObject(writer, obj); count++; } } writer.WriteString("\n"); writer.WriteEndElement(); writer.Close(); fs.Close(); Console.WriteLine("Found " + count.ToString() + " items."); }
private void Process(XmlDictionaryWriter writer, object nodeValue, object baseValue, string name, Type type) { if (nodeValue != null && !nodeValue.Equals(baseValue)) { if (!name.Equals("value__")) { writer.WriteString("\n\t\t"); writer.WriteStartElement(name); } if (type.Equals(typeof(bool))) { if ((bool)nodeValue) { writer.WriteString("true"); } else { writer.WriteString("false"); } } else if (type.IsPrimitive || type.Equals(typeof(String))) { writer.WriteString(nodeValue.ToString()); } else if (!type.IsArray) { DiffSerializer innerSerializer = new DiffSerializer(type, null, true); innerSerializer.WriteObject(writer, nodeValue); } if (!name.Equals("value__")) { writer.WriteEndElement(); } } }
/// <summary> /// Supports Serialization of NPC, Items & Projectiles. /// It also supports multiple formats by specifing the Method. /// </summary> /// <param name="type"></param> /// <param name="ignoreFields"></param> /// <param name="SetDefaults"></param> /// <param name="invokeType"></param> /// <param name="input"></param> public static Dictionary<Int32, String> Serialize( Type type, string[] ignoreFields, MethodInfo SetDefaults, InvokeType invokeType = InvokeType.ITEM_NPC, string[] inputs = null, int MaxObjects = 1000, bool NPCOverride = false) { var FilePath = (invokeType == InvokeType.ITEM_NPC_BY_NAME) ? type.Name + "sByName.xml" : type.Name + "s.xml"; if (File.Exists(FilePath)) File.Delete(FilePath); DiffSerializer serializer = new DiffSerializer(type, ignoreFields); FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate); XmlWriterSettings ws = new XmlWriterSettings(); ws.Indent = true; XmlDictionaryWriter.Create(fs, ws); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); writer.WriteStartElement("ArrayOf" + type.Name); var returnData = new Dictionary<Int32, String>(); int count = 0; for (int i = 0; i < MaxObjects; i++) { object obj = Activator.CreateInstance(type); try { if(invokeType == InvokeType.ITEM_NPC) SetDefaults.Invoke(obj, new object[] { i, null }); else if (invokeType == InvokeType.ITEM_NPC_BY_NAME) SetDefaults.Invoke(obj, new object[] { inputs[i] }); else SetDefaults.Invoke(obj, new object[] { i }); } catch //(Exception e) //Usually catches player data which is not set { //Console.WriteLine("[Error] {0}", e.Message); } FieldInfo info = type.GetField("Name"); string value = String.Empty; try { value = (String)info.GetValue(obj); } catch (NullReferenceException) { //Close writer.WriteString("\n"); writer.WriteEndElement(); writer.Close(); fs.Close(); Console.WriteLine("Please restart this application, The Assemblies need refreshing."); Console.ReadKey(true); Environment.Exit(0); } if (!String.IsNullOrWhiteSpace(value)) { Console.WriteLine("Processing `{0}`...", value); serializer.WriteObject(writer, obj); count++; if (obj is Terraria.NPC) { var npc = obj as Terraria.NPC; if(!returnData.ContainsKey(npc.type)) returnData.Add(npc.type, value); } else returnData.Add(count, value); } Thread.Sleep(5); } writer.WriteString("\n"); writer.WriteEndElement(); writer.Close(); fs.Close(); Console.WriteLine("Found {0} {1}s.", count, type.Name); return returnData; }
private void Process(XmlDictionaryWriter writer, object nodeValue, object baseValue, string name, Type type) { if (nodeValue != null && !nodeValue.Equals(baseValue)) { if (!name.Equals("value__")) { writer.WriteString("\n\t\t"); writer.WriteStartElement(name); } if (type.Equals(typeof(bool))) { if ((bool)nodeValue) { writer.WriteString("true"); } else { writer.WriteString("false"); } } else if (type.IsPrimitive || type.Equals(typeof(String))) { writer.WriteString(nodeValue.ToString()); } else if(!type.IsArray) { DiffSerializer innerSerializer = new DiffSerializer(type, null, true); innerSerializer.WriteObject(writer, nodeValue); } if (!name.Equals("value__")) { writer.WriteEndElement(); } } }
/// <summary> /// Supports Serialization of NPC, Items & Projectiles. /// It also supports multiple formats by specifing the Method. /// </summary> /// <param name="type"></param> /// <param name="ignoreFields"></param> /// <param name="SetDefaults"></param> /// <param name="invokeType"></param> /// <param name="input"></param> public static Dictionary <Int32, String> Serialize(Type type, string[] ignoreFields, MethodInfo SetDefaults, InvokeType invokeType = InvokeType.ITEM_NPC, string[] inputs = null, int MaxObjects = 1000) { var FilePath = (invokeType == InvokeType.ITEM_NPC_BY_NAME) ? type.Name + "sByName.xml" : type.Name + "s.xml"; if (File.Exists(FilePath)) { File.Delete(FilePath); } DiffSerializer serializer = new DiffSerializer(type, ignoreFields); FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate); XmlWriterSettings ws = new XmlWriterSettings(); ws.Indent = true; XmlDictionaryWriter.Create(fs, ws); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); writer.WriteStartElement("ArrayOf" + type.Name); var returnData = new Dictionary <Int32, String>(); int count = 0; for (int i = 0; i < MaxObjects; i++) { object obj = Activator.CreateInstance(type); try { if (invokeType == InvokeType.ITEM_NPC) { SetDefaults.Invoke(obj, new object[] { i, null }); } else if (invokeType == InvokeType.ITEM_NPC_BY_NAME) { SetDefaults.Invoke(obj, new object[] { inputs[i] }); } else { SetDefaults.Invoke(obj, new object[] { i }); } } catch //(Exception e) //Usually catches player data which is not set { //Console.WriteLine("[Error] {0}", e.Message); } FieldInfo info = type.GetField("Name"); string value = String.Empty; try { value = (String)info.GetValue(obj); } catch (NullReferenceException) { //Close writer.WriteString("\n"); writer.WriteEndElement(); writer.Close(); fs.Close(); Console.WriteLine("Please restart this application, The Assemblies need refreshing."); Console.ReadKey(true); Environment.Exit(0); } if (!String.IsNullOrWhiteSpace(value)) { Console.WriteLine("Processing `{0}`...", value); serializer.WriteObject(writer, obj); count++; returnData.Add(count, value); } Thread.Sleep(5); } writer.WriteString("\n"); writer.WriteEndElement(); writer.Close(); fs.Close(); Console.WriteLine("Found {0} {1}s.", count, type.Name); return(returnData); }