Skip to content

Commit

Permalink
Add WzRawDataProperty
Browse files Browse the repository at this point in the history
GMS v220++ ? No idea the exact ver its added
  • Loading branch information
lastbattle committed Dec 23, 2024
1 parent b06955e commit d2ff89f
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 30 deletions.
78 changes: 48 additions & 30 deletions MapleLib/WzLib/WzImageProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
132 changes: 132 additions & 0 deletions MapleLib/WzLib/WzProperties/WzRawDataProperty.cs
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// Constructor
/// </summary>
/// <param name="name"></param>
/// <param name="reader"></param>
/// <param name="parseNow"></param>
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;
}

/// <summary>
/// Constructor copy
/// </summary>
/// <param name="copy"></param>
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)
{
}

/// <summary>
/// The parent of the object
/// </summary>
public override WzObject Parent
{
get => _parent;
internal set => _parent = value;
}

/// <summary>
/// The WzPropertyType of the property
/// </summary>
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));
}

/// <summary>
/// Disposes the object
/// </summary>
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;
}
}
}
}
1 change: 1 addition & 0 deletions MapleLib/WzLib/WzPropertyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum WzPropertyType
Vector,
Convex,
Sound,
Raw,
UOL,
Lua,
#endregion
Expand Down

0 comments on commit d2ff89f

Please sign in to comment.