using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes { public static class MapCharLightPackage { public sealed class Group { public int Index; public List Lights { get; } = new(); } public abstract class LightBase { public uint s_dwMapID; public bool s_bChar; public bool s_bShadow; public int s_nType; public bool s_bEnableLight; public float[] s_fDiffuse = new float[3]; public float[] s_fAmbient = new float[3]; public float[] s_fSpecular = new float[3]; public float s_fPower; } public sealed class DirectionLight : LightBase { public float[,] s_fRot = new float[3, 3]; public float[] s_fPos = new float[3]; public float s_fScale; } public sealed class PointLight : LightBase { public float[] s_Pos = new float[3]; public float s_C; public float s_L; public float s_Q; public float s_Range; } public static Group[] ReadFromBinary(string filePath) { using BitReader reader = new BitReader(File.OpenRead(filePath)); int groupCount = reader.ReadInt(); Group[] groups = new Group[groupCount]; for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) { Group group = new Group { Index = groupIndex }; int lightCount = reader.ReadInt(); for (int lightIndex = 0; lightIndex < lightCount; lightIndex++) { int recordType = reader.ReadInt(); LightBase light = recordType switch { 0 => ReadDirectionLight(reader), 1 => ReadPointLight(reader), _ => throw new InvalidDataException($"Unknown MapCharLight record type {recordType} at group {groupIndex}, light {lightIndex}.") }; if (light.s_nType != recordType) { throw new InvalidDataException($"MapCharLight type mismatch at group {groupIndex}, light {lightIndex}. Outer type={recordType}, inner type={light.s_nType}."); } group.Lights.Add(light); } groups[groupIndex] = group; } return groups; } public static void ExportToXml(Group[] groups, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); XmlWriterSettings settings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) }; using XmlWriter writer = XmlWriter.Create(filePath, settings); writer.WriteStartDocument(); writer.WriteStartElement("MapCharLightTable"); WriteElement(writer, "GroupCount", groups.Length); writer.WriteStartElement("Groups"); foreach (Group group in groups) { writer.WriteStartElement("Group"); WriteElement(writer, "Index", group.Index); WriteElement(writer, "LightCount", group.Lights.Count); if (group.Lights.Count > 0) { WriteElement(writer, "MapID", group.Lights[0].s_dwMapID); } writer.WriteStartElement("Lights"); foreach (LightBase light in group.Lights) { if (light is DirectionLight direction) { writer.WriteStartElement("DirectionLight"); WriteCommon(writer, direction); writer.WriteStartElement("Rotation"); for (int row = 0; row < 3; row++) { writer.WriteStartElement("Row"); WriteElement(writer, "X", direction.s_fRot[row, 0]); WriteElement(writer, "Y", direction.s_fRot[row, 1]); WriteElement(writer, "Z", direction.s_fRot[row, 2]); writer.WriteEndElement(); } writer.WriteEndElement(); WriteVector(writer, "Position", direction.s_fPos); WriteElement(writer, "s_fScale", direction.s_fScale); writer.WriteEndElement(); } else if (light is PointLight point) { writer.WriteStartElement("PointLight"); WriteCommon(writer, point); WriteVector(writer, "Position", point.s_Pos); WriteElement(writer, "s_C", point.s_C); WriteElement(writer, "s_L", point.s_L); WriteElement(writer, "s_Q", point.s_Q); WriteElement(writer, "s_Range", point.s_Range); writer.WriteEndElement(); } } writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } public static Group[] ImportFromXml(string filePath) { XDocument document = XDocument.Load(filePath); XElement root = document.Root ?? throw new InvalidDataException("MapCharLight.xml is missing the root element."); XElement groupsElement = root.Element("Groups") ?? throw new InvalidDataException("MapCharLight.xml is missing the Groups element."); List groups = new List(); foreach (XElement groupElement in groupsElement.Elements("Group")) { Group group = new Group { Index = GetInt(groupElement, "Index") }; XElement lightsElement = groupElement.Element("Lights") ?? new XElement("Lights"); foreach (XElement lightElement in lightsElement.Elements()) { LightBase light = lightElement.Name.LocalName switch { "DirectionLight" => ReadDirectionLightFromXml(lightElement), "PointLight" => ReadPointLightFromXml(lightElement), _ => throw new InvalidDataException($"Unknown MapCharLight XML light node '{lightElement.Name.LocalName}'.") }; group.Lights.Add(light); } groups.Add(group); } return groups.ToArray(); } public static void WriteToBinary(Group[] groups, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create, FileAccess.Write)); writer.Write(groups.Length); foreach (Group group in groups) { writer.Write(group.Lights.Count); foreach (LightBase light in group.Lights) { writer.Write(light.s_nType); switch (light) { case DirectionLight direction: WriteDirectionLight(writer, direction); break; case PointLight point: WritePointLight(writer, point); break; default: throw new InvalidDataException("Unsupported MapCharLight light type."); } } } } private static DirectionLight ReadDirectionLight(BitReader reader) { DirectionLight light = new DirectionLight(); ReadCommon(reader, light); for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { light.s_fRot[row, col] = reader.ReadFloat(); } } for (int i = 0; i < 3; i++) { light.s_fPos[i] = reader.ReadFloat(); } light.s_fScale = reader.ReadFloat(); return light; } private static PointLight ReadPointLight(BitReader reader) { PointLight light = new PointLight(); ReadCommon(reader, light); for (int i = 0; i < 3; i++) { light.s_Pos[i] = reader.ReadFloat(); } light.s_C = reader.ReadFloat(); light.s_L = reader.ReadFloat(); light.s_Q = reader.ReadFloat(); light.s_Range = reader.ReadFloat(); return light; } private static void ReadCommon(BitReader reader, LightBase light) { light.s_dwMapID = reader.ReadUInt(); light.s_bChar = reader.ReadByte() != 0; light.s_bShadow = reader.ReadByte() != 0; reader.Skip(2); light.s_nType = reader.ReadInt(); light.s_bEnableLight = reader.ReadByte() != 0; reader.Skip(3); for (int i = 0; i < 3; i++) { light.s_fDiffuse[i] = reader.ReadFloat(); } for (int i = 0; i < 3; i++) { light.s_fAmbient[i] = reader.ReadFloat(); } for (int i = 0; i < 3; i++) { light.s_fSpecular[i] = reader.ReadFloat(); } light.s_fPower = reader.ReadFloat(); } private static void WriteDirectionLight(BinaryWriter writer, DirectionLight light) { WriteCommon(writer, light); for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { writer.Write(light.s_fRot[row, col]); } } for (int i = 0; i < 3; i++) { writer.Write(light.s_fPos[i]); } writer.Write(light.s_fScale); } private static void WritePointLight(BinaryWriter writer, PointLight light) { WriteCommon(writer, light); for (int i = 0; i < 3; i++) { writer.Write(light.s_Pos[i]); } writer.Write(light.s_C); writer.Write(light.s_L); writer.Write(light.s_Q); writer.Write(light.s_Range); } private static void WriteCommon(BinaryWriter writer, LightBase light) { writer.Write(light.s_dwMapID); writer.Write((byte)(light.s_bChar ? 1 : 0)); writer.Write((byte)(light.s_bShadow ? 1 : 0)); writer.Write((ushort)0); writer.Write(light.s_nType); writer.Write((byte)(light.s_bEnableLight ? 1 : 0)); writer.Write(new byte[3]); for (int i = 0; i < 3; i++) { writer.Write(light.s_fDiffuse[i]); } for (int i = 0; i < 3; i++) { writer.Write(light.s_fAmbient[i]); } for (int i = 0; i < 3; i++) { writer.Write(light.s_fSpecular[i]); } writer.Write(light.s_fPower); } private static void WriteCommon(XmlWriter writer, LightBase light) { WriteElement(writer, "s_dwMapID", light.s_dwMapID); WriteElement(writer, "s_bChar", light.s_bChar ? 1 : 0); WriteElement(writer, "s_bShadow", light.s_bShadow ? 1 : 0); WriteElement(writer, "s_nType", light.s_nType); WriteElement(writer, "s_bEnableLight", light.s_bEnableLight ? 1 : 0); WriteVector(writer, "s_fDiffuse", light.s_fDiffuse); WriteVector(writer, "s_fAmbient", light.s_fAmbient); WriteVector(writer, "s_fSpecular", light.s_fSpecular); WriteElement(writer, "s_fPower", light.s_fPower); } private static DirectionLight ReadDirectionLightFromXml(XElement element) { DirectionLight light = new DirectionLight(); ReadCommonFromXml(element, light); XElement rotationElement = element.Element("Rotation") ?? throw new InvalidDataException("DirectionLight is missing Rotation."); XElement[] rows = rotationElement.Elements("Row").ToArray(); if (rows.Length != 3) { throw new InvalidDataException("DirectionLight Rotation must contain exactly 3 rows."); } for (int row = 0; row < 3; row++) { light.s_fRot[row, 0] = GetFloat(rows[row], "X"); light.s_fRot[row, 1] = GetFloat(rows[row], "Y"); light.s_fRot[row, 2] = GetFloat(rows[row], "Z"); } light.s_fPos = GetVector(element.Element("Position"), "DirectionLight.Position"); light.s_fScale = GetFloat(element, "s_fScale"); return light; } private static PointLight ReadPointLightFromXml(XElement element) { PointLight light = new PointLight(); ReadCommonFromXml(element, light); light.s_Pos = GetVector(element.Element("Position"), "PointLight.Position"); light.s_C = GetFloat(element, "s_C"); light.s_L = GetFloat(element, "s_L"); light.s_Q = GetFloat(element, "s_Q"); light.s_Range = GetFloat(element, "s_Range"); return light; } private static void ReadCommonFromXml(XElement element, LightBase light) { light.s_dwMapID = GetUInt(element, "s_dwMapID"); light.s_bChar = GetBool(element, "s_bChar"); light.s_bShadow = GetBool(element, "s_bShadow"); light.s_nType = GetInt(element, "s_nType"); light.s_bEnableLight = GetBool(element, "s_bEnableLight"); light.s_fDiffuse = GetVector(element.Element("s_fDiffuse"), "s_fDiffuse"); light.s_fAmbient = GetVector(element.Element("s_fAmbient"), "s_fAmbient"); light.s_fSpecular = GetVector(element.Element("s_fSpecular"), "s_fSpecular"); light.s_fPower = GetFloat(element, "s_fPower"); } private static void WriteVector(XmlWriter writer, string elementName, float[] vector) { writer.WriteStartElement(elementName); WriteElement(writer, "X", vector[0]); WriteElement(writer, "Y", vector[1]); WriteElement(writer, "Z", vector[2]); writer.WriteEndElement(); } private static float[] GetVector(XElement? element, string name) { if (element == null) { throw new InvalidDataException($"MapCharLight XML is missing {name}."); } return new[] { GetFloat(element, "X"), GetFloat(element, "Y"), GetFloat(element, "Z") }; } private static void WriteElement(XmlWriter writer, string name, object value) { writer.WriteElementString(name, Convert.ToString(value, CultureInfo.InvariantCulture) ?? string.Empty); } private static int GetInt(XElement parent, string name) { return int.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static uint GetUInt(XElement parent, string name) { return uint.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static float GetFloat(XElement parent, string name) { return float.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static bool GetBool(XElement parent, string name) { string value = parent.Element(name)?.Value ?? "0"; return value == "1" || value.Equals("true", StringComparison.OrdinalIgnoreCase); } } }