public static string ToXml(BymlFileData data) { var stream = new MemoryStream(); XmlTextWriter xr; xr = new XmlTextWriter(stream, UnicodeEncoding.Unicode); xr.Formatting = System.Xml.Formatting.Indented; xr.WriteStartDocument(); xr.WriteStartElement("Root"); xr.WriteStartElement("isBigEndian"); xr.WriteAttributeString("Value", (data.byteOrder == Syroot.BinaryData.ByteOrder.BigEndian).ToString()); xr.WriteEndElement(); xr.WriteStartElement("BymlFormatVersion"); xr.WriteAttributeString("Value", data.Version.ToString()); xr.WriteEndElement(); xr.WriteStartElement("SupportPaths"); xr.WriteAttributeString("Value", data.SupportPaths.ToString()); xr.WriteEndElement(); xr.WriteStartElement("BymlRoot"); WriteNode(data.RootNode, null, xr); xr.WriteEndElement(); xr.WriteEndElement(); xr.Close(); return(UnicodeEncoding.Unicode.GetString(stream.ToArray())); }
public static string ToXml(BymlFileData data) { CustomStringWriter str = new CustomStringWriter(Encoding.GetEncoding(932)); XmlTextWriter xr; xr = new XmlTextWriter(str); xr.Formatting = System.Xml.Formatting.Indented; xr.WriteStartDocument(); xr.WriteStartElement("Root"); xr.WriteStartElement("isBigEndian"); xr.WriteAttributeString("Value", (data.byteOrder == Syroot.BinaryData.ByteOrder.BigEndian).ToString()); xr.WriteEndElement(); xr.WriteStartElement("BymlFormatVersion"); xr.WriteAttributeString("Value", data.Version.ToString()); xr.WriteEndElement(); xr.WriteStartElement("SupportPaths"); xr.WriteAttributeString("Value", data.SupportPaths.ToString()); xr.WriteEndElement(); xr.WriteStartElement("BymlRoot"); WriteNode(data.RootNode, null, xr); xr.WriteEndElement(); xr.WriteEndElement(); xr.Close(); return(str.ToString()); }
/// <summary> /// Serializes the given dynamic value which requires to be an array or dictionary of BYAML compatible values /// and stores it in the given file. /// </summary> public static void SaveN(string fileName, BymlFileData file) { using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { SaveN(stream, file); } }
public static byte[] SaveN(BymlFileData file) { using (MemoryStream stream = new MemoryStream()) { SaveN(stream, file); return(stream.ToArray()); } }
public static string ToYaml(ByamlExt.Byaml.BymlFileData data) { /*var settings = new SerializerSettings() * { * EmitTags = false, * EmitAlias = false, * DefaultStyle = SharpYaml.YamlStyle.Flow, * SortKeyForMapping = false, * EmitShortTypeName = true, * EmitCapacityForList = false, * LimitPrimitiveFlowSequence = 4, * }; * * settings.RegisterTagMapping("!u", typeof(uint)); * settings.RegisterTagMapping("!l", typeof(int)); * settings.RegisterTagMapping("!d", typeof(double)); * settings.RegisterTagMapping("!ul", typeof(ulong)); * settings.RegisterTagMapping("!ll", typeof(long)); * * var serializer = new Serializer(settings); * return serializer.Serialize(data);*/ NodePaths.Clear(); refNodeId = 0; YamlNode root = SaveNode("root", data.RootNode); YamlMappingNode mapping = new YamlMappingNode(); mapping.Add("Version", data.Version.ToString()); mapping.Add("IsBigEndian", (data.byteOrder == ByteOrder.BigEndian).ToString()); mapping.Add("SupportPaths", data.SupportPaths.ToString()); mapping.Add("HasReferenceNodes", (refNodeId != 0).ToString()); mapping.Add("root", root); NodePaths.Clear(); refNodeId = 0; var doc = new YamlDocument(mapping); YamlStream stream = new YamlStream(doc); var buffer = new StringBuilder(); using (var writer = new StringWriter(buffer)) { stream.Save(writer, true); return(writer.ToString()); } }
public static ByamlExt.Byaml.BymlFileData FromYaml(string text) { NodePaths.Clear(); ReferenceNodes.Clear(); var data = new ByamlExt.Byaml.BymlFileData(); var yaml = new YamlStream(); yaml.Load(new StringReader(text)); var mapping = (YamlMappingNode)yaml.Documents[0].RootNode; foreach (var child in mapping.Children) { var key = ((YamlScalarNode)child.Key).Value; var value = child.Value.ToString(); if (key == "Version") { data.Version = ushort.Parse(value); } if (key == "IsBigEndian") { data.byteOrder = bool.Parse(value) ? ByteOrder.BigEndian : ByteOrder.LittleEndian; } if (key == "SupportPaths") { data.SupportPaths = bool.Parse(value); } if (child.Value is YamlMappingNode) { data.RootNode = ParseNode(child.Value); } if (child.Value is YamlSequenceNode) { data.RootNode = ParseNode(child.Value); } } ReferenceNodes.Clear(); NodePaths.Clear(); return(data); }
public static BymlFileData ToByml(string xmlString) { BymlFileData ret = new BymlFileData(); XmlDocument xml = new XmlDocument(); xml.LoadXml(xmlString); XmlNode n = xml.SelectSingleNode("/Root/isBigEndian"); ret.byteOrder = n.Attributes["Value"].Value.ToLower() == "true" ? ByteOrder.BigEndian : ByteOrder.LittleEndian; n = xml.SelectSingleNode("/Root/BymlFormatVersion"); ret.Version = ushort.Parse(n.Attributes["Value"].Value); n = xml.SelectSingleNode("/Root/SupportPaths"); ret.SupportPaths = n.Attributes["Value"].Value.ToLower() == "true"; n = xml.SelectSingleNode("/Root/BymlRoot"); if (n.ChildNodes.Count != 1) { throw new Exception("A byml can have only one root"); } ret.RootNode = ParseNode(n.FirstChild); return(ret); }
/// <summary> /// Serializes the given dynamic value which requires to be an array or dictionary of BYAML compatible values /// and stores it in the specified stream. /// </summary> public static void SaveN(Stream stream, BymlFileData file) { ByamlFile byamlFile = new ByamlFile(file.SupportPaths, file.byteOrder, file.Version); byamlFile.Write(stream, file.RootNode); }