Skip to content

Commit

Permalink
[WzLib] Refractoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lastbattle committed Jan 1, 2022
1 parent 82b4933 commit 76053cb
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 40 deletions.
2 changes: 2 additions & 0 deletions MapleLib/MapleCryptoLib/MapleCryptoConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ public static bool IsDefaultMapleStoryUserKey()
};

/// <summary>
/// ?s_BasicKey@CAESCipher@@2PAEA
/// IV used to create the WzKey for GMS
/// </summary>
public static byte[] WZ_GMSIV = new byte[4] { 0x4D, 0x23, 0xC7, 0x2B };

/// <summary>
/// ?s_BasicKey@CAESCipher@@2PAEA
/// IV used to create the WzKey for the latest version of GMS, MSEA, or KMS
/// </summary>
public static byte[] WZ_MSEAIV = new byte[4] { 0xB9, 0x7D, 0x63, 0xE9 };
Expand Down
4 changes: 2 additions & 2 deletions MapleLib/WzLib/Util/WzBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ public string ReadStringBlock(uint offset)
switch (ReadByte())
{
case 0:
case 0x73:
case WzImage.WzImageHeaderByte_WithoutOffset:
return ReadString();
case 1:
case 0x1B:
case WzImage.WzImageHeaderByte_WithOffset:
return ReadStringAtOffset(offset + ReadInt32());
default:
return "";
Expand Down
23 changes: 18 additions & 5 deletions MapleLib/WzLib/Util/WzBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
along with this program. If not, see <http://www.gnu.org/licenses/>.*/

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using MapleLib.MapleCryptoLib;

namespace MapleLib.WzLib.Util
Expand All @@ -29,15 +27,23 @@ public class WzBinaryWriter : BinaryWriter
{
#region Properties
public WzMutableKey WzKey { get; set; }
public uint Hash { get; set; }
public uint Hash { get; }
public Hashtable StringCache { get; set; }
public WzHeader Header { get; set; }
public bool LeaveOpen { get; internal set; }
#endregion

#region Constructors
public WzBinaryWriter(Stream output, byte[] WzIv)
: this(output, WzIv, false) { }
: this(output, WzIv, false)
{
this.Hash = 0;
}

public WzBinaryWriter(Stream output, byte[] WzIv, uint Hash)
: this(output, WzIv, false) {
this.Hash = Hash;
}

public WzBinaryWriter(Stream output, byte[] WzIv, bool leaveOpen)
: base(output)
Expand All @@ -49,6 +55,12 @@ public WzBinaryWriter(Stream output, byte[] WzIv, bool leaveOpen)
#endregion

#region Methods
/// <summary>
/// ?InternalSerializeString@@YAHPAGPAUIWzArchive@@EE@Z
/// </summary>
/// <param name="s"></param>
/// <param name="withoutOffset"></param>
/// <param name="withOffset"></param>
public void WriteStringValue(string s, int withoutOffset, int withOffset)
{
if (s.Length > 4 && StringCache.ContainsKey(s))
Expand Down Expand Up @@ -102,6 +114,7 @@ public override void Write(string value)
if (value[i] > sbyte.MaxValue)
{
unicode = true;
break;
}
}

Expand Down Expand Up @@ -224,7 +237,7 @@ public void WriteOffset(uint value)
{
uint encOffset = (uint)BaseStream.Position;
encOffset = (encOffset - Header.FStart) ^ 0xFFFFFFFF;
encOffset *= Hash;
encOffset *= Hash; // could this be removed?
encOffset -= MapleCryptoConstants.WZ_OffsetConstant;
encOffset = RotateLeft(encOffset, (byte)(encOffset & 0x1F));
uint writeOffset = encOffset ^ (value - (Header.FStart * 2));
Expand Down
2 changes: 1 addition & 1 deletion MapleLib/WzLib/Util/WzTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public static bool IsDataWzHotfixFile(string path)
{
byte firstByte = reader.ReadByte();

result = firstByte == WzImage.WzImageHeaderByte; // check the first byte. It should be 0x73 that represends a WzImage
result = firstByte == WzImage.WzImageHeaderByte_WithoutOffset; // check the first byte. It should be 0x73 that represends a WzImage
}

return result;
Expand Down
9 changes: 5 additions & 4 deletions MapleLib/WzLib/WzDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ internal void ParseDirectory(bool lazyParse = false)
reader.BaseStream.Position = rememberPos;
fsize = reader.ReadCompressedInt();
checksum = reader.ReadCompressedInt();
offset = reader.ReadOffset();
offset = reader.ReadOffset(); // IWzArchive::Getposition(pArchive)

if (type == 3)
{
Expand Down Expand Up @@ -263,14 +263,15 @@ internal void ParseDirectory(bool lazyParse = false)
}
}

// Offsets are calculated manually
if (this.wzFile != null && this.wzFile.b64BitClient) // This only applies for 64-bit client based WZ files.
// Offsets are calculated manually as a work-around, until I've figured out how
// the new offsets are calculated for 64-bit based WZ file..
if (this.wzFile != null && this.wzFile.b64BitClient)
{
long startOffset = reader.BaseStream.Position;
foreach (WzImage image in WzImages)
{
image.Offset = (uint)startOffset;
startOffset += image.BlockSize; // TODO: Test saving the wz after it is changed. This may break
startOffset += image.BlockSize;
}
}

Expand Down
16 changes: 8 additions & 8 deletions MapleLib/WzLib/WzFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ internal WzFileParseStatus ParseMainWzDirectory(bool lazyParse = false)
this.Header.Ident = reader.ReadString(4);
this.Header.FSize = reader.ReadUInt64();
this.Header.FStart = reader.ReadUInt32();
this.Header.Copyright = reader.ReadString((int)(Header.FStart - 17U));

//this.Header.Copyright = reader.ReadNullTerminatedString();
this.Header.Copyright = reader.ReadString((int)(Header.FStart - 17U));
byte unk1 = reader.ReadByte();
byte[] unk2 = reader.ReadBytes((int)(Header.FStart - (ulong)reader.BaseStream.Position));

reader.Header = this.Header;

this.wzVersionHeader = 0;
Expand Down Expand Up @@ -258,8 +260,8 @@ internal WzFileParseStatus ParseMainWzDirectory(bool lazyParse = false)
if (!this.b64BitClient)
{
this.versionHash = CheckAndGetVersionHash(wzVersionHeader, mapleStoryPatchVersion);
reader.Hash = this.versionHash;
}
reader.Hash = this.versionHash;
WzDirectory directory = new WzDirectory(reader, this.name, this.versionHash, this.WzIv, this);
directory.ParseDirectory();
this.wzDir = directory;
Expand All @@ -276,9 +278,9 @@ private bool TryDecodeWithWZVersionNumber(WzBinaryReader reader, int useWzVersio
this.versionHash = CheckAndGetVersionHash(useWzVersionHeader, mapleStoryPatchVersion);
if (this.versionHash == 0) // ugly hack, but that's the only way if the version number isnt known (nexon stores this in the .exe)
return false;
reader.Hash = this.versionHash;
}

reader.Hash = this.versionHash;
long fallbackOffsetPosition = reader.BaseStream.Position; // save position to rollback to, if should parsing fail from here
WzDirectory testDirectory;
try
Expand Down Expand Up @@ -309,8 +311,8 @@ private bool TryDecodeWithWZVersionNumber(WzBinaryReader reader, int useWzVersio

switch (checkByte)
{
case 0x73:
case 0x1b:
case WzImage.WzImageHeaderByte_WithoutOffset: // 0x73
case WzImage.WzImageHeaderByte_WithOffset:
{
WzDirectory directory = new WzDirectory(reader, this.name, this.versionHash, this.WzIv, this);
directory.ParseDirectory(lazyParse);
Expand Down Expand Up @@ -511,10 +513,8 @@ public void SaveToDisk(string path, bool? bSaveAs64BitWzFile, WzMapleVersion sav

WzTool.StringCache.Clear();

using (WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv))
using (WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv, versionHash))
{
wzWriter.Hash = versionHash;

uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + 2));
Header.FSize = totalLen - Header.FStart;
for (int i = 0; i < 4; i++)
Expand Down
5 changes: 3 additions & 2 deletions MapleLib/WzLib/WzImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class WzImage : WzObject, IPropertyContainer
{
//TODO: nest wzproperties in a wzsubproperty inside of WzImage

public const int WzImageHeaderByte = 0x73;
public const int WzImageHeaderByte_WithoutOffset = 0x73;
public const int WzImageHeaderByte_WithOffset = 0x1B;

#region Fields
internal bool parsed = false;
Expand Down Expand Up @@ -355,7 +356,7 @@ public bool ParseImage(bool forceReadFromData = false)
}
return false; // unhandled for now, if it isnt an .lua image
}
case WzImageHeaderByte:
case WzImageHeaderByte_WithoutOffset:
{
string prop = reader.ReadString();
ushort val = reader.ReadUInt16();
Expand Down
4 changes: 2 additions & 2 deletions MapleLib/WzLib/WzImageProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ internal static WzExtended ParseExtendedProp(WzBinaryReader reader, uint offset,
switch (reader.ReadByte())
{
case 0x01:
case 0x1B:
case WzImage.WzImageHeaderByte_WithOffset:
return ExtractMore(reader, offset, endOfBlock, name, reader.ReadStringAtOffset(offset + reader.ReadInt32()), parent, imgParent);
case 0x00:
case 0x73:
case WzImage.WzImageHeaderByte_WithoutOffset:
return ExtractMore(reader, offset, endOfBlock, name, "", parent, imgParent);
default:
throw new System.Exception("Invalid byte read at ParseExtendedProp");
Expand Down
2 changes: 1 addition & 1 deletion MapleLib/WzLib/WzProperties/WzBinaryProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override void SetValue(object value)
public override void WriteValue(WzBinaryWriter writer)
{
byte[] data = GetBytes(false);
writer.WriteStringValue("Sound_DX8", 0x73, 0x1B);
writer.WriteStringValue("Sound_DX8", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);
writer.Write((byte)0);
writer.WriteCompressedInt(data.Length);
writer.WriteCompressedInt(len_ms);
Expand Down
4 changes: 2 additions & 2 deletions MapleLib/WzLib/WzProperties/WzCanvasProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public override WzImageProperty GetFromPath(string path)
}
return ret;
}
public override void WriteValue(MapleLib.WzLib.Util.WzBinaryWriter writer)
public override void WriteValue(WzBinaryWriter writer)
{
writer.WriteStringValue("Canvas", 0x73, 0x1B);
writer.WriteStringValue("Canvas", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);
writer.Write((byte)0);
if (properties.Count > 0) // subproperty in the canvas
{
Expand Down
8 changes: 5 additions & 3 deletions MapleLib/WzLib/WzProperties/WzConvexProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ public override WzImageProperty GetFromPath(string path)
}
return ret;
}
public override void WriteValue(MapleLib.WzLib.Util.WzBinaryWriter writer)
public override void WriteValue(WzBinaryWriter writer)
{
List<WzExtended> extendedProps = new List<WzExtended>(properties.Count);
foreach (WzImageProperty prop in properties) if (prop is WzExtended) extendedProps.Add((WzExtended)prop);
writer.WriteStringValue("Shape2D#Convex2D", 0x73, 0x1B);
foreach (WzImageProperty prop in properties)
if (prop is WzExtended)
extendedProps.Add((WzExtended)prop);
writer.WriteStringValue("Shape2D#Convex2D", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);
writer.WriteCompressedInt(extendedProps.Count);

foreach (WzImageProperty imgProperty in properties)
Expand Down
2 changes: 1 addition & 1 deletion MapleLib/WzLib/WzProperties/WzStringProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override WzImageProperty DeepClone()
/// The name of the property
/// </summary>
public override string Name { get { return name; } set { name = value; } }
public override void WriteValue(MapleLib.WzLib.Util.WzBinaryWriter writer)
public override void WriteValue(WzBinaryWriter writer)
{
writer.Write((byte)8);
writer.WriteStringValue(Value, 0, 1);
Expand Down
4 changes: 2 additions & 2 deletions MapleLib/WzLib/WzProperties/WzSubProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ public override WzImageProperty GetFromPath(string path)
}
return ret;
}
public override void WriteValue(MapleLib.WzLib.Util.WzBinaryWriter writer)
public override void WriteValue(WzBinaryWriter writer)
{
bool bIsLuaProperty = false;
if (properties.Count == 1 && properties[0] is WzLuaProperty)
{
bIsLuaProperty = true;
}
if (!bIsLuaProperty)
writer.WriteStringValue("Property", 0x73, 0x1B);
writer.WriteStringValue("Property", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);

WzImageProperty.WritePropertyList(writer, properties);
}
Expand Down
4 changes: 2 additions & 2 deletions MapleLib/WzLib/WzProperties/WzUOLProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public override WzImageProperty GetFromPath(string path)
/// </summary>
public override WzPropertyType PropertyType { get { return WzPropertyType.UOL; } }

public override void WriteValue(MapleLib.WzLib.Util.WzBinaryWriter writer)
public override void WriteValue(WzBinaryWriter writer)
{
writer.WriteStringValue("UOL", 0x73, 0x1B);
writer.WriteStringValue("UOL", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);
writer.Write((byte)0);
writer.WriteStringValue(Value, 0, 1);
}
Expand Down
4 changes: 2 additions & 2 deletions MapleLib/WzLib/WzProperties/WzVectorProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public override WzImageProperty DeepClone()
/// The WzPropertyType of the property
/// </summary>
public override WzPropertyType PropertyType { get { return WzPropertyType.Vector; } }
public override void WriteValue(MapleLib.WzLib.Util.WzBinaryWriter writer)
public override void WriteValue(WzBinaryWriter writer)
{
writer.WriteStringValue("Shape2D#Vector2D", 0x73, 0x1B);
writer.WriteStringValue("Shape2D#Vector2D", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);
writer.WriteCompressedInt(X.Value);
writer.WriteCompressedInt(Y.Value);
}
Expand Down
3 changes: 0 additions & 3 deletions MapleLib/WzSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
along with this program. If not, see <http://www.gnu.org/licenses/>.*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapleLib.WzLib.WzProperties;
using System.Reflection;
using MapleLib.WzLib.WzStructure;
Expand Down

1 comment on commit 76053cb

@uper1126
Copy link

@uper1126 uper1126 commented on 76053cb Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modified wz will result in an image offset of 0 in kms.
Is it possible to patch it to work in kms?

Please sign in to comment.