From d2ff89fe5fd3a187416be5f8df8e9b0c04f4392d Mon Sep 17 00:00:00 2001 From: lastbattle <4586194+lastbattle@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:53:15 +0800 Subject: [PATCH] Add WzRawDataProperty GMS v220++ ? No idea the exact ver its added --- MapleLib/WzLib/WzImageProperty.cs | 78 +++++++---- .../WzLib/WzProperties/WzRawDataProperty.cs | 132 ++++++++++++++++++ MapleLib/WzLib/WzPropertyType.cs | 1 + 3 files changed, 181 insertions(+), 30 deletions(-) create mode 100644 MapleLib/WzLib/WzProperties/WzRawDataProperty.cs diff --git a/MapleLib/WzLib/WzImageProperty.cs b/MapleLib/WzLib/WzImageProperty.cs index 25d5778..c658203 100644 --- a/MapleLib/WzLib/WzImageProperty.cs +++ b/MapleLib/WzLib/WzImageProperty.cs @@ -213,47 +213,65 @@ internal static WzExtended ExtractMore(WzBinaryReader reader, long offset, long switch (iname) { case "Property": - WzSubProperty subProp = new WzSubProperty(name) { Parent = parent }; - reader.BaseStream.Position += 2; // Reserved? - subProp.AddProperties(WzImageProperty.ParsePropertyList(offset, reader, subProp, imgParent)); - return subProp; + { + WzSubProperty subProp = new WzSubProperty(name) { Parent = parent }; + reader.BaseStream.Position += 2; // Reserved? + subProp.AddProperties(WzImageProperty.ParsePropertyList(offset, reader, subProp, imgParent)); + return subProp; + } case "Canvas": - WzCanvasProperty canvasProp = new WzCanvasProperty(name) { Parent = parent }; - reader.BaseStream.Position++; - if (reader.ReadByte() == 1) { - reader.BaseStream.Position += 2; - canvasProp.AddProperties(WzImageProperty.ParsePropertyList(offset, reader, canvasProp, imgParent)); + WzCanvasProperty canvasProp = new WzCanvasProperty(name) { Parent = parent }; + reader.BaseStream.Position++; + if (reader.ReadByte() == 1) + { + reader.BaseStream.Position += 2; + canvasProp.AddProperties(WzImageProperty.ParsePropertyList(offset, reader, canvasProp, imgParent)); + } + canvasProp.PngProperty = new WzPngProperty(reader, imgParent.ParseEverything) { Parent = canvasProp }; + return canvasProp; } - canvasProp.PngProperty = new WzPngProperty(reader, imgParent.ParseEverything) { Parent = canvasProp }; - return canvasProp; case "Shape2D#Vector2D": - WzVectorProperty vecProp = new WzVectorProperty(name) { Parent = parent }; - vecProp.X = new WzIntProperty("X", reader.ReadCompressedInt()) { Parent = vecProp }; - vecProp.Y = new WzIntProperty("Y", reader.ReadCompressedInt()) { Parent = vecProp }; - return vecProp; + { + WzVectorProperty vecProp = new WzVectorProperty(name) { Parent = parent }; + vecProp.X = new WzIntProperty("X", reader.ReadCompressedInt()) { Parent = vecProp }; + vecProp.Y = new WzIntProperty("Y", reader.ReadCompressedInt()) { Parent = vecProp }; + return vecProp; + } case "Shape2D#Convex2D": - WzConvexProperty convexProp = new WzConvexProperty(name) { Parent = parent }; - int convexEntryCount = reader.ReadCompressedInt(); - convexProp.WzProperties.Capacity = convexEntryCount; - for (int i = 0; i < convexEntryCount; i++) { - convexProp.AddProperty(ParseExtendedProp(reader, offset, 0, name, convexProp, imgParent)); + WzConvexProperty convexProp = new WzConvexProperty(name) { Parent = parent }; + int convexEntryCount = reader.ReadCompressedInt(); + convexProp.WzProperties.Capacity = convexEntryCount; + for (int i = 0; i < convexEntryCount; i++) + { + convexProp.AddProperty(ParseExtendedProp(reader, offset, 0, name, convexProp, imgParent)); + } + return convexProp; } - return convexProp; case "Sound_DX8": - WzBinaryProperty soundProp = new WzBinaryProperty(name, reader, imgParent.ParseEverything) { Parent = parent }; - return soundProp; + { + WzBinaryProperty soundProp = new WzBinaryProperty(name, reader, imgParent.ParseEverything) { Parent = parent }; + return soundProp; + } case "UOL": - reader.BaseStream.Position++; - switch (reader.ReadByte()) { - case 0: - return new WzUOLProperty(name, reader.ReadString()) { Parent = parent }; - case 1: - return new WzUOLProperty(name, reader.ReadStringAtOffset(offset + reader.ReadInt32())) { Parent = parent }; + reader.BaseStream.Position++; + switch (reader.ReadByte()) + { + case 0: + return new WzUOLProperty(name, reader.ReadString()) { Parent = parent }; + case 1: + return new WzUOLProperty(name, reader.ReadStringAtOffset(offset + reader.ReadInt32())) { Parent = parent }; + } + throw new Exception("Unsupported UOL type"); + } + case WzRawDataProperty.RAW_DATA_HEADER: // GMS v220++ + { + return new WzRawDataProperty(name, reader, imgParent.ParseEverything) { + Parent = parent + }; } - throw new Exception("Unsupported UOL type"); default: throw new Exception("Unknown iname: " + iname); } diff --git a/MapleLib/WzLib/WzProperties/WzRawDataProperty.cs b/MapleLib/WzLib/WzProperties/WzRawDataProperty.cs new file mode 100644 index 0000000..e49f920 --- /dev/null +++ b/MapleLib/WzLib/WzProperties/WzRawDataProperty.cs @@ -0,0 +1,132 @@ +using MapleLib.WzLib.Util; +using System.IO; + +namespace MapleLib.WzLib.WzProperties +{ + public class WzRawDataProperty : WzExtended + { + + #region Fields + public const string RAW_DATA_HEADER = "RawData"; + + internal string _name; + internal WzObject _parent; + internal WzBinaryReader _wzReader; + + internal long _offset; + internal int _length; + internal byte[] _bytes; + #endregion + + /// + /// Constructor + /// + /// + /// + /// + public WzRawDataProperty(string name, WzBinaryReader reader, bool parseNow) + { + this._name = name; + this._wzReader = reader; + + this._wzReader.BaseStream.Position++; + this._length = reader.ReadInt32(); + this._offset = reader.BaseStream.Position; + if (parseNow) + GetBytes(true); + else + this._wzReader.BaseStream.Position += _length; + } + + /// + /// Constructor copy + /// + /// + private WzRawDataProperty(WzRawDataProperty copy) + { + this._name = copy._name; + this._bytes = new byte[copy._length]; + copy.GetBytes(false).CopyTo(_bytes, 0); + this._length = copy._length; + } + + #region Inherited Members + public override WzImageProperty DeepClone() + { + return new WzRawDataProperty(this); + } + + public override object WzValue => GetBytes(false); + + public override void SetValue(object value) + { + } + + /// + /// The parent of the object + /// + public override WzObject Parent + { + get => _parent; + internal set => _parent = value; + } + + /// + /// The WzPropertyType of the property + /// + public override WzPropertyType PropertyType => WzPropertyType.Raw; + + public override string Name { get => _name; set => this._name = value; } + + public override void WriteValue(WzBinaryWriter writer) + { + var data = GetBytes(false); + writer.WriteStringValue(RAW_DATA_HEADER, WzImage.WzImageHeaderByte_WithoutOffset, + WzImage.WzImageHeaderByte_WithOffset); + writer.Write((byte)0); + writer.Write(data.Length); + writer.Write(data); + } + + public override void ExportXml(StreamWriter writer, int level) + { + writer.Write(XmlUtil.Indentation(level)); + writer.WriteLine(XmlUtil.EmptyNamedTag(RAW_DATA_HEADER, Name)); + } + + /// + /// Disposes the object + /// + public override void Dispose() + { + this._name = null; + this._bytes = null; + } + #endregion + + public byte[] GetBytes(bool saveInMemory) + { + if (this._bytes != null) // check in-memory + return this._bytes; + + if (this._wzReader == null) + return null; + + // read if none + var currentPos = _wzReader.BaseStream.Position; + this._wzReader.BaseStream.Position = _offset; + this._bytes = _wzReader.ReadBytes(_length); + this._wzReader.BaseStream.Position = currentPos; + if (saveInMemory) + { + return this._bytes; + } + else + { + byte[] ret_bytes = _bytes; + this._bytes = null; + return ret_bytes; + } + } + } +} diff --git a/MapleLib/WzLib/WzPropertyType.cs b/MapleLib/WzLib/WzPropertyType.cs index 179224a..ee3cad2 100644 --- a/MapleLib/WzLib/WzPropertyType.cs +++ b/MapleLib/WzLib/WzPropertyType.cs @@ -34,6 +34,7 @@ public enum WzPropertyType Vector, Convex, Sound, + Raw, UOL, Lua, #endregion