using System.Globalization; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes { public static class ScenePackage { private const int SceneInfoSize = 212; private const int SceneInfoBoolOffset = 4; private const int SceneInfoTitleOffset = 6; private const int MaxSceneTargets = 5; private const int TitleCharCount = 64; private const int SceneInfoMapOffset = 136; private const int SceneInfoRegionOffset = 140; private const int SceneInfoSkipOffset = 144; private const int SceneInfoMusicOffset = 145; private const int MusicByteCount = 64; private const int TalkCharCount = 1024; private const int EffectByteCount = 64; private const int SoundByteCount = 64; private const int TargetNameCharCount = 32; public sealed class SceneInfoEntry { public uint s_dwSceneID { get; set; } public bool s_bUse { get; set; } public string s_szTitle { get; set; } = string.Empty; public int s_nMap { get; set; } public int s_nResion { get; set; } public bool s_bSkip { get; set; } public string s_szMusic { get; set; } = string.Empty; public List Actions { get; } = new(); } public sealed class SceneActionEntry { public uint s_dwMainTarget { get; set; } public uint[] s_dwTarget { get; } = new uint[MaxSceneTargets]; public uint s_dwAni { get; set; } public string s_szTalk { get; set; } = string.Empty; public string s_szEffect { get; set; } = string.Empty; public string s_szSound { get; set; } = string.Empty; } public sealed class SceneTargetEntry { public uint s_dwUID { get; set; } public uint s_dwModelID { get; set; } public float s_fDist { get; set; } public float s_fHeight { get; set; } public string s_szName { get; set; } = string.Empty; } public static (SceneInfoEntry[] Scenes, SceneTargetEntry[] Targets) ReadFromBinary(string filePath) { using BinaryReader reader = new(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)); int sceneCount = reader.ReadInt32(); if (sceneCount < 0) { throw new InvalidDataException($"Scene count is invalid: {sceneCount}."); } SceneInfoEntry[] scenes = new SceneInfoEntry[sceneCount]; for (int i = 0; i < sceneCount; i++) { byte[] sceneBytes = reader.ReadBytes(SceneInfoSize); if (sceneBytes.Length != SceneInfoSize) { throw new EndOfStreamException("Unable to read the expected SceneInfo block."); } SceneInfoEntry scene = new() { s_dwSceneID = BitConverter.ToUInt32(sceneBytes, 0), s_bUse = sceneBytes[SceneInfoBoolOffset] != 0, s_szTitle = ReadFixedUnicode(sceneBytes, SceneInfoTitleOffset, TitleCharCount), s_nMap = BitConverter.ToInt32(sceneBytes, SceneInfoMapOffset), s_nResion = BitConverter.ToInt32(sceneBytes, SceneInfoRegionOffset), s_bSkip = sceneBytes[SceneInfoSkipOffset] != 0, s_szMusic = ReadFixedAscii(sceneBytes, SceneInfoMusicOffset, MusicByteCount) }; int actionCount = reader.ReadInt32(); if (actionCount < 0) { throw new InvalidDataException($"Scene {scene.s_dwSceneID} has invalid action count {actionCount}."); } for (int actionIndex = 0; actionIndex < actionCount; actionIndex++) { SceneActionEntry action = new() { s_dwMainTarget = reader.ReadUInt32() }; for (int targetIndex = 0; targetIndex < MaxSceneTargets; targetIndex++) { action.s_dwTarget[targetIndex] = reader.ReadUInt32(); } action.s_dwAni = reader.ReadUInt32(); action.s_szTalk = ReadFixedUnicode(reader, TalkCharCount); action.s_szEffect = ReadFixedAscii(reader, EffectByteCount); action.s_szSound = ReadFixedAscii(reader, SoundByteCount); scene.Actions.Add(action); } scenes[i] = scene; } int targetCount = reader.ReadInt32(); if (targetCount < 0) { throw new InvalidDataException($"Scene target count is invalid: {targetCount}."); } SceneTargetEntry[] targets = new SceneTargetEntry[targetCount]; for (int i = 0; i < targetCount; i++) { targets[i] = new SceneTargetEntry { s_dwUID = reader.ReadUInt32(), s_dwModelID = reader.ReadUInt32(), s_fDist = reader.ReadSingle(), s_fHeight = reader.ReadSingle(), s_szName = ReadFixedUnicode(reader, TargetNameCharCount) }; } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new InvalidDataException($"Scene.bin was not fully consumed. Remaining bytes: {reader.BaseStream.Length - reader.BaseStream.Position}."); } return (scenes, targets); } public static void ExportToXml(string sceneXmlPath, string targetXmlPath, SceneInfoEntry[] scenes, SceneTargetEntry[] targets) { Directory.CreateDirectory(Path.GetDirectoryName(sceneXmlPath)!); Directory.CreateDirectory(Path.GetDirectoryName(targetXmlPath)!); using (XmlWriter writer = CreateWriter(sceneXmlPath)) { writer.WriteStartDocument(); writer.WriteStartElement("SceneDataTable"); writer.WriteElementString("Count", scenes.Length.ToString(CultureInfo.InvariantCulture)); writer.WriteStartElement("Scenes"); foreach (SceneInfoEntry scene in scenes) { writer.WriteStartElement("SceneData"); WriteElement(writer, "s_dwSceneID", scene.s_dwSceneID); WriteElement(writer, "s_bUse", scene.s_bUse ? 1 : 0); WriteStringElement(writer, "s_szTitle", scene.s_szTitle); WriteElement(writer, "s_nMap", scene.s_nMap); WriteElement(writer, "s_nResion", scene.s_nResion); WriteElement(writer, "s_bSkip", scene.s_bSkip ? 1 : 0); WriteStringElement(writer, "s_szMusic", scene.s_szMusic); writer.WriteStartElement("Actions"); foreach (SceneActionEntry action in scene.Actions) { writer.WriteStartElement("Action"); WriteElement(writer, "s_dwMainTarget", action.s_dwMainTarget); writer.WriteStartElement("Targets"); for (int i = 0; i < MaxSceneTargets; i++) { writer.WriteStartElement("Target"); writer.WriteAttributeString("Index", (i + 1).ToString(CultureInfo.InvariantCulture)); writer.WriteString(action.s_dwTarget[i].ToString(CultureInfo.InvariantCulture)); writer.WriteEndElement(); } writer.WriteEndElement(); WriteElement(writer, "s_dwAni", action.s_dwAni); WriteStringElement(writer, "s_szTalk", action.s_szTalk); WriteStringElement(writer, "s_szEffect", action.s_szEffect); WriteStringElement(writer, "s_szSound", action.s_szSound); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } using (XmlWriter writer = CreateWriter(targetXmlPath)) { writer.WriteStartDocument(); writer.WriteStartElement("SceneTargetTable"); writer.WriteElementString("Count", targets.Length.ToString(CultureInfo.InvariantCulture)); writer.WriteStartElement("Targets"); foreach (SceneTargetEntry target in targets) { writer.WriteStartElement("SceneTarget"); WriteElement(writer, "s_dwUID", target.s_dwUID); WriteElement(writer, "s_dwModelID", target.s_dwModelID); WriteElement(writer, "s_fDist", target.s_fDist); WriteElement(writer, "s_fHeight", target.s_fHeight); WriteStringElement(writer, "s_szName", target.s_szName); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } } public static (SceneInfoEntry[] Scenes, SceneTargetEntry[] Targets) ImportFromXml(string sceneXmlPath, string targetXmlPath) { XDocument sceneDocument = XDocument.Load(sceneXmlPath); XElement sceneRoot = sceneDocument.Root ?? throw new InvalidDataException("SceneData.xml is missing the root element."); XElement scenesElement = sceneRoot.Element("Scenes") ?? throw new InvalidDataException("SceneData.xml is missing the Scenes element."); List scenes = new(); foreach (XElement sceneElement in scenesElement.Elements("SceneData")) { SceneInfoEntry scene = new() { s_dwSceneID = ReadUInt(sceneElement, "s_dwSceneID"), s_bUse = ReadBool(sceneElement, "s_bUse"), s_szTitle = ReadString(sceneElement, "s_szTitle"), s_nMap = ReadInt(sceneElement, "s_nMap"), s_nResion = ReadInt(sceneElement, "s_nResion"), s_bSkip = ReadBool(sceneElement, "s_bSkip"), s_szMusic = ReadString(sceneElement, "s_szMusic") }; XElement? actionsElement = sceneElement.Element("Actions"); if (actionsElement != null) { foreach (XElement actionElement in actionsElement.Elements("Action")) { SceneActionEntry action = new() { s_dwMainTarget = ReadUInt(actionElement, "s_dwMainTarget"), s_dwAni = ReadUInt(actionElement, "s_dwAni"), s_szTalk = ReadString(actionElement, "s_szTalk"), s_szEffect = ReadString(actionElement, "s_szEffect"), s_szSound = ReadString(actionElement, "s_szSound") }; foreach (XElement targetElement in actionElement.Element("Targets")?.Elements("Target") ?? Enumerable.Empty()) { int index = ReadIntAttribute(targetElement, "Index"); if (index >= 1 && index <= MaxSceneTargets) { action.s_dwTarget[index - 1] = ParseUInt(targetElement.Value); } } scene.Actions.Add(action); } } scenes.Add(scene); } XDocument targetDocument = XDocument.Load(targetXmlPath); XElement targetRoot = targetDocument.Root ?? throw new InvalidDataException("SceneTargets.xml is missing the root element."); XElement targetsElement = targetRoot.Element("Targets") ?? throw new InvalidDataException("SceneTargets.xml is missing the Targets element."); List targets = new(); foreach (XElement targetElement in targetsElement.Elements("SceneTarget")) { targets.Add(new SceneTargetEntry { s_dwUID = ReadUInt(targetElement, "s_dwUID"), s_dwModelID = ReadUInt(targetElement, "s_dwModelID"), s_fDist = ReadFloat(targetElement, "s_fDist"), s_fHeight = ReadFloat(targetElement, "s_fHeight"), s_szName = ReadString(targetElement, "s_szName") }); } return (scenes.ToArray(), targets.ToArray()); } public static void WriteToBinary(string filePath, SceneInfoEntry[] scenes, SceneTargetEntry[] targets) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using BinaryWriter writer = new(File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(scenes.Length); foreach (SceneInfoEntry scene in scenes) { byte[] sceneBytes = new byte[SceneInfoSize]; Array.Copy(BitConverter.GetBytes(scene.s_dwSceneID), 0, sceneBytes, 0, sizeof(uint)); sceneBytes[SceneInfoBoolOffset] = (byte)(scene.s_bUse ? 1 : 0); WriteFixedUnicode(sceneBytes, SceneInfoTitleOffset, scene.s_szTitle, TitleCharCount); Array.Copy(BitConverter.GetBytes(scene.s_nMap), 0, sceneBytes, SceneInfoMapOffset, sizeof(int)); Array.Copy(BitConverter.GetBytes(scene.s_nResion), 0, sceneBytes, SceneInfoRegionOffset, sizeof(int)); sceneBytes[SceneInfoSkipOffset] = (byte)(scene.s_bSkip ? 1 : 0); WriteFixedAscii(sceneBytes, SceneInfoMusicOffset, scene.s_szMusic, MusicByteCount); writer.Write(sceneBytes); writer.Write(scene.Actions.Count); foreach (SceneActionEntry action in scene.Actions) { writer.Write(action.s_dwMainTarget); for (int i = 0; i < MaxSceneTargets; i++) { writer.Write(action.s_dwTarget[i]); } writer.Write(action.s_dwAni); WriteFixedUnicode(writer, action.s_szTalk, TalkCharCount); WriteFixedAscii(writer, action.s_szEffect, EffectByteCount); WriteFixedAscii(writer, action.s_szSound, SoundByteCount); } } writer.Write(targets.Length); foreach (SceneTargetEntry target in targets) { writer.Write(target.s_dwUID); writer.Write(target.s_dwModelID); writer.Write(target.s_fDist); writer.Write(target.s_fHeight); WriteFixedUnicode(writer, target.s_szName, TargetNameCharCount); } } private static XmlWriter CreateWriter(string path) { XmlWriterSettings settings = new() { Indent = true, Encoding = new UTF8Encoding(false) }; return XmlWriter.Create(path, settings); } private static string ReadFixedUnicode(BinaryReader reader, int charCount) { byte[] bytes = reader.ReadBytes(charCount * 2); if (bytes.Length != charCount * 2) { throw new EndOfStreamException("Unable to read the expected Unicode fixed-length field."); } return SanitizeXmlString(TrimAtNull(Encoding.Unicode.GetString(bytes))); } private static string ReadFixedUnicode(byte[] buffer, int offset, int charCount) { byte[] bytes = new byte[charCount * 2]; Array.Copy(buffer, offset, bytes, 0, bytes.Length); return SanitizeXmlString(TrimAtNull(Encoding.Unicode.GetString(bytes))); } private static string ReadFixedAscii(BinaryReader reader, int byteCount) { byte[] bytes = reader.ReadBytes(byteCount); if (bytes.Length != byteCount) { throw new EndOfStreamException("Unable to read the expected ASCII fixed-length field."); } return SanitizeXmlString(TrimAtNull(Encoding.ASCII.GetString(bytes))); } private static string ReadFixedAscii(byte[] buffer, int offset, int byteCount) { byte[] bytes = new byte[byteCount]; Array.Copy(buffer, offset, bytes, 0, bytes.Length); return SanitizeXmlString(TrimAtNull(Encoding.ASCII.GetString(bytes))); } private static void WriteFixedUnicode(BinaryWriter writer, string value, int charCount) { byte[] bytes = new byte[charCount * 2]; Encoding.Unicode.GetBytes(value ?? string.Empty, 0, Math.Min((value ?? string.Empty).Length, charCount), bytes, 0); writer.Write(bytes); } private static void WriteFixedAscii(BinaryWriter writer, string value, int byteCount) { byte[] bytes = new byte[byteCount]; Encoding.ASCII.GetBytes(value ?? string.Empty, 0, Math.Min((value ?? string.Empty).Length, byteCount), bytes, 0); writer.Write(bytes); } private static void WriteFixedUnicode(byte[] buffer, int offset, string value, int charCount) { byte[] bytes = new byte[charCount * 2]; Encoding.Unicode.GetBytes(value ?? string.Empty, 0, Math.Min((value ?? string.Empty).Length, charCount), bytes, 0); Array.Copy(bytes, 0, buffer, offset, bytes.Length); } private static void WriteFixedAscii(byte[] buffer, int offset, string value, int byteCount) { byte[] bytes = new byte[byteCount]; Encoding.ASCII.GetBytes(value ?? string.Empty, 0, Math.Min((value ?? string.Empty).Length, byteCount), bytes, 0); Array.Copy(bytes, 0, buffer, offset, bytes.Length); } private static string TrimAtNull(string value) { int index = value.IndexOf('\0'); return index >= 0 ? value[..index] : value; } private static string SanitizeXmlString(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 WriteElement(XmlWriter writer, string name, object value) { writer.WriteElementString(name, Convert.ToString(value, CultureInfo.InvariantCulture) ?? string.Empty); } private static void WriteStringElement(XmlWriter writer, string name, string value) { writer.WriteStartElement(name); writer.WriteString(value ?? string.Empty); writer.WriteEndElement(); } private static string ReadString(XElement parent, string name) { return parent.Element(name)?.Value ?? string.Empty; } private static int ReadInt(XElement parent, string name) { return int.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static int ReadIntAttribute(XElement element, string name) { return int.Parse(element.Attribute(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static uint ReadUInt(XElement parent, string name) { return uint.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static uint ParseUInt(string value) { return uint.Parse(string.IsNullOrWhiteSpace(value) ? "0" : value, CultureInfo.InvariantCulture); } private static float ReadFloat(XElement parent, string name) { return float.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static bool ReadBool(XElement parent, string name) { string value = parent.Element(name)?.Value ?? "0"; return value == "1" || value.Equals("true", StringComparison.OrdinalIgnoreCase); } } }