using System.Globalization; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes; public static class UITextPackage { public sealed class UITextEntry { public long HashKey { get; set; } public string Text { get; set; } = string.Empty; } public static UITextEntry[] ReadFromBinary(string inputPath) { using BinaryReader reader = new(File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read)); int count = reader.ReadInt32(); if (count < 0) { throw new InvalidDataException("UIText count cannot be negative."); } UITextEntry[] entries = new UITextEntry[count]; for (int i = 0; i < count; i++) { long hashKey = reader.ReadInt64(); int charCount = reader.ReadInt32(); if (charCount < 0) { throw new InvalidDataException($"UIText entry {i} has a negative character count."); } byte[] bytes = reader.ReadBytes(charCount * 2); if (bytes.Length != charCount * 2) { throw new EndOfStreamException($"Unable to read UIText entry {i} string bytes."); } entries[i] = new UITextEntry { HashKey = hashKey, Text = SanitizeXmlString(Encoding.Unicode.GetString(bytes)) }; } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new InvalidDataException($"UIText.bin was not fully consumed. Remaining bytes: {reader.BaseStream.Length - reader.BaseStream.Position}."); } return entries; } public static void ExportToXml(string outputPath, UITextEntry[] entries) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); XElement root = new("UITexts", entries.Select(entry => new XElement("UIText", new XElement(nameof(UITextEntry.HashKey), entry.HashKey.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(UITextEntry.Text), new XAttribute(XNamespace.Xml + "space", "preserve"), SanitizeXmlString(entry.Text))))); SaveXml(outputPath, root); } public static UITextEntry[] ImportFromXml(string inputPath) { XElement root = XElement.Load(inputPath); return root.Elements("UIText") .Select(element => new UITextEntry { HashKey = ReadLong(element, nameof(UITextEntry.HashKey)), Text = ReadString(element, nameof(UITextEntry.Text)) }) .ToArray(); } public static void WriteToBinary(string outputPath, UITextEntry[] entries) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); using BinaryWriter writer = new(File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(entries.Length); foreach (UITextEntry entry in entries) { string text = SanitizeXmlString(entry.Text); byte[] bytes = Encoding.Unicode.GetBytes(text); writer.Write(entry.HashKey); writer.Write(text.Length); writer.Write(bytes); } } 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); } private static string SanitizeXmlString(string value) { return string.Concat((value ?? string.Empty).Where(XmlConvert.IsXmlChar)); } private static long ReadLong(XElement parent, string name) { XElement element = parent.Element(name) ?? throw new InvalidOperationException($"Missing '{name}' element."); return long.Parse(element.Value, CultureInfo.InvariantCulture); } private static string ReadString(XElement parent, string name) { return parent.Element(name)?.Value ?? string.Empty; } }