using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes { public static class NewElementPackage { private const int PathLength = 128; public sealed class NewElementData { public List AttributeTypes { get; } = new(); public List NatureTypes { get; } = new(); } public sealed class AttributeTypeUiEntry { public uint AttributeId { get; set; } public string Path { get; set; } = string.Empty; public int X { get; set; } public int Y { get; set; } public int BarX { get; set; } public int BarY { get; set; } } public sealed class NatureTypeUiEntry { public uint NatureId { get; set; } public string Path { get; set; } = string.Empty; public int X { get; set; } public int Y { get; set; } public int GaugeX { get; set; } public int GaugeY { get; set; } public float Angle { get; set; } } public static NewElementData ReadFromBinary(string filePath) { using BinaryReader reader = new(File.OpenRead(filePath)); NewElementData data = new(); int attributeCount = reader.ReadInt32(); for (int i = 0; i < attributeCount; i++) { data.AttributeTypes.Add(new AttributeTypeUiEntry { AttributeId = reader.ReadUInt32(), Path = ReadFixedAsciiString(reader, PathLength), X = reader.ReadInt32(), Y = reader.ReadInt32(), BarX = reader.ReadInt32(), BarY = reader.ReadInt32() }); } int natureCount = reader.ReadInt32(); for (int i = 0; i < natureCount; i++) { data.NatureTypes.Add(new NatureTypeUiEntry { NatureId = reader.ReadUInt32(), Path = ReadFixedAsciiString(reader, PathLength), X = reader.ReadInt32(), Y = reader.ReadInt32(), GaugeX = reader.ReadInt32(), GaugeY = reader.ReadInt32(), Angle = reader.ReadSingle() }); } return data; } public static void WriteToBinary(NewElementData data, string filePath) { using BinaryWriter writer = new(File.Open(filePath, FileMode.Create, FileAccess.Write)); writer.Write(data.AttributeTypes.Count); foreach (AttributeTypeUiEntry entry in data.AttributeTypes) { writer.Write(entry.AttributeId); WriteFixedAsciiString(writer, entry.Path, PathLength); writer.Write(entry.X); writer.Write(entry.Y); writer.Write(entry.BarX); writer.Write(entry.BarY); } writer.Write(data.NatureTypes.Count); foreach (NatureTypeUiEntry entry in data.NatureTypes) { writer.Write(entry.NatureId); WriteFixedAsciiString(writer, entry.Path, PathLength); writer.Write(entry.X); writer.Write(entry.Y); writer.Write(entry.GaugeX); writer.Write(entry.GaugeY); writer.Write(entry.Angle); } } public static void ExportToXml(NewElementData data, string attributeXmlPath, string natureXmlPath) { Directory.CreateDirectory(Path.GetDirectoryName(attributeXmlPath)!); XElement attributeRoot = new("AttributeTypeUITable", data.AttributeTypes.Select(entry => new XElement("AttributeTypeUI", new XElement("AttributeId", entry.AttributeId), new XElement("Path", XmlSafe(entry.Path)), new XElement("X", entry.X), new XElement("Y", entry.Y), new XElement("BarX", entry.BarX), new XElement("BarY", entry.BarY)))); SaveXml(attributeXmlPath, attributeRoot); XElement natureRoot = new("NatureTypeUITable", data.NatureTypes.Select(entry => new XElement("NatureTypeUI", new XElement("NatureId", entry.NatureId), new XElement("Path", XmlSafe(entry.Path)), new XElement("X", entry.X), new XElement("Y", entry.Y), new XElement("GaugeX", entry.GaugeX), new XElement("GaugeY", entry.GaugeY), new XElement("Angle", entry.Angle.ToString("R", System.Globalization.CultureInfo.InvariantCulture))))); SaveXml(natureXmlPath, natureRoot); } public static NewElementData ImportFromXml(string attributeXmlPath, string natureXmlPath) { NewElementData data = new(); XElement attributeRoot = XElement.Load(attributeXmlPath); foreach (XElement entryElement in attributeRoot.Elements("AttributeTypeUI")) { data.AttributeTypes.Add(new AttributeTypeUiEntry { AttributeId = uint.Parse(entryElement.Element("AttributeId")!.Value), Path = CleanString(entryElement.Element("Path")?.Value), X = int.Parse(entryElement.Element("X")!.Value), Y = int.Parse(entryElement.Element("Y")!.Value), BarX = int.Parse(entryElement.Element("BarX")!.Value), BarY = int.Parse(entryElement.Element("BarY")!.Value) }); } XElement natureRoot = XElement.Load(natureXmlPath); foreach (XElement entryElement in natureRoot.Elements("NatureTypeUI")) { data.NatureTypes.Add(new NatureTypeUiEntry { NatureId = uint.Parse(entryElement.Element("NatureId")!.Value), Path = CleanString(entryElement.Element("Path")?.Value), X = int.Parse(entryElement.Element("X")!.Value), Y = int.Parse(entryElement.Element("Y")!.Value), GaugeX = int.Parse(entryElement.Element("GaugeX")!.Value), GaugeY = int.Parse(entryElement.Element("GaugeY")!.Value), Angle = float.Parse(entryElement.Element("Angle")!.Value, System.Globalization.CultureInfo.InvariantCulture) }); } return data; } private static string ReadFixedAsciiString(BinaryReader reader, int length) { return CleanString(Encoding.ASCII.GetString(reader.ReadBytes(length))); } private static void WriteFixedAsciiString(BinaryWriter writer, string value, int length) { byte[] bytes = Encoding.ASCII.GetBytes(CleanString(value)); Array.Resize(ref bytes, length); writer.Write(bytes, 0, length); } private static string CleanString(string? value) { if (string.IsNullOrEmpty(value)) { return string.Empty; } int nullIndex = value.IndexOf('\0'); string trimmed = nullIndex >= 0 ? value[..nullIndex] : value; return trimmed.TrimEnd('\0'); } private static string XmlSafe(string? value) { if (string.IsNullOrEmpty(value)) { return string.Empty; } StringBuilder builder = new(value.Length); foreach (char c in value) { if (XmlConvert.IsXmlChar(c)) { builder.Append(c); } } return builder.ToString(); } private static void SaveXml(string path, XElement root) { XmlWriterSettings settings = new() { Indent = true, Encoding = new UTF8Encoding(false) }; using XmlWriter writer = XmlWriter.Create(path, settings); root.Save(writer); } } }