Skip to content

Commit

Permalink
[MapleLib] Fix wz saving --- off by 2 bytes due to previous 64-bit co…
Browse files Browse the repository at this point in the history
…mpatibility implementation

ebf107e
  • Loading branch information
lastbattle committed Dec 20, 2022
1 parent c5c9c70 commit 3dd093d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 50 deletions.
95 changes: 57 additions & 38 deletions MapleLib/WzLib/WzFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ internal WzFileParseStatus ParseMainWzDirectory(bool lazyParse = false)
// it can be any number if the client is 64-bit. Assigning 777 is just for convenience when calculating the versionHash.
this.wzVersionHeader = this.wz_withEncryptVersionHeader ? reader.ReadUInt16() : wzVersionHeader64bit_start;

Debug.WriteLine("----------------------------------------");
Debug.WriteLine(string.Format("Read Wz File {0}", this.Name));
Debug.WriteLine(string.Format("wz_withEncryptVersionHeader: {0}", wz_withEncryptVersionHeader));
Debug.WriteLine(string.Format("wzVersionHeader: {0}", wzVersionHeader));
Debug.WriteLine("----------------------------------------");

if (mapleStoryPatchVersion == -1)
{
// for 64-bit client, return immediately if version 777 works correctly.
Expand All @@ -268,6 +274,8 @@ internal WzFileParseStatus ParseMainWzDirectory(bool lazyParse = false)

for (int j = maplestoryVerDetectedFromClient; j < MAX_PATCH_VERSION; j++)
{
//Debug.WriteLine("Try decode 1 with maplestory ver: " + j);

if (TryDecodeWithWZVersionNumber(reader, wzVersionHeader, j, lazyParse))
{
return WzFileParseStatus.Success;
Expand Down Expand Up @@ -561,61 +569,72 @@ public void SaveToDisk(string path, bool? override_saveAs64BitWZ = null, WzMaple
// MapleStory UserKey
bool bIsWzUserKeyDefault = MapleCryptoConstants.IsDefaultMapleStoryUserKey(); // check if its saving to the same UserKey.
// Save WZ as 64-bit wz format
bool bWZ_withEncryptVersionHeader = this.wz_withEncryptVersionHeader;
bool bSaveAs64BitWz = this.wz_withEncryptVersionHeader;
if (override_saveAs64BitWZ != null)
{
bWZ_withEncryptVersionHeader = (bool)override_saveAs64BitWZ;
bSaveAs64BitWz = (bool)override_saveAs64BitWZ;
}

CreateWZVersionHash();
wzDir.SetVersionHash(versionHash);

string tempFile = Path.GetFileNameWithoutExtension(path) + ".TEMP";
File.Create(tempFile).Close();
using (FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write))
{
wzDir.GenerateDataFile(bIsWzIvSimilar ? null : WzIv, bIsWzUserKeyDefault, fs);
}
Debug.WriteLine("----------------------------------------");
Debug.WriteLine(string.Format("Saving Wz File {0}", this.Name));
Debug.WriteLine(string.Format("wzVersionHeader: {0}", wzVersionHeader));
Debug.WriteLine(string.Format("bSaveAs64BitWz: {0}", bSaveAs64BitWz));
Debug.WriteLine("----------------------------------------");

WzTool.StringCache.Clear();

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

uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + (bWZ_withEncryptVersionHeader ? 2u: 0)));
Header.FSize = totalLen - Header.FStart;
for (int i = 0; i < 4; i++)
string tempFile = Path.GetFileNameWithoutExtension(path) + ".TEMP";
File.Create(tempFile).Close();
using (FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write))
{
wzWriter.Write((byte)Header.Ident[i]);
wzDir.GenerateDataFile(bIsWzIvSimilar ? null : WzIv, bIsWzUserKeyDefault, fs);
}
wzWriter.Write((long)Header.FSize);
wzWriter.Write(Header.FStart);
wzWriter.WriteNullTerminatedString(Header.Copyright);

long extraHeaderLength = Header.FStart - wzWriter.BaseStream.Position;
if (extraHeaderLength > 0)
WzTool.StringCache.Clear();

using (WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv))
{
wzWriter.Write(new byte[(int)extraHeaderLength]);
}
if (bWZ_withEncryptVersionHeader)
wzWriter.Write(wzVersionHeader);
wzWriter.Hash = versionHash;

wzWriter.Header = Header;
wzDir.SaveDirectory(wzWriter);
wzWriter.StringCache.Clear();
uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + (!bSaveAs64BitWz ? 2u : 0)));
Header.FSize = totalLen - Header.FStart;
for (int i = 0; i < 4; i++)
{
wzWriter.Write((byte)Header.Ident[i]);
}
wzWriter.Write((long)Header.FSize);
wzWriter.Write(Header.FStart);
wzWriter.WriteNullTerminatedString(Header.Copyright);

using (FileStream fs = File.OpenRead(tempFile))
{
wzDir.SaveImages(wzWriter, fs);
}
File.Delete(tempFile);
long extraHeaderLength = Header.FStart - wzWriter.BaseStream.Position;
if (extraHeaderLength > 0)
{
wzWriter.Write(new byte[(int)extraHeaderLength]);
}
if (!bSaveAs64BitWz) // 64 bit doesnt have a version number.
wzWriter.Write((ushort) wzVersionHeader);

wzWriter.StringCache.Clear();
}
wzWriter.Header = Header;
wzDir.SaveDirectory(wzWriter);
wzWriter.StringCache.Clear();

GC.Collect();
GC.WaitForPendingFinalizers();
using (FileStream fs = File.OpenRead(tempFile))
{
wzDir.SaveImages(wzWriter, fs);
}
File.Delete(tempFile);

wzWriter.StringCache.Clear();
}
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

public void ExportXml(string path, bool oneFile)
Expand Down
14 changes: 9 additions & 5 deletions MapleLib/WzLib/WzObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ public abstract class WzObject : IDisposable
{
get
{
if (this is WzFile)
WzObject wzObject = this;

if (wzObject is WzFile)
{
return ((WzFile)this)[name];
}
else if (this is WzDirectory)
else if (wzObject is WzDirectory)
{
return ((WzDirectory)this)[name];
}
else if (this is WzImage)
else if (wzObject is WzImage)
{
return ((WzImage)this)[name];
}
else if (this is WzImageProperty)
else if (wzObject is WzImageProperty)
{
return ((WzImageProperty)this)[name];
}
Expand Down Expand Up @@ -99,7 +101,9 @@ public string FullPath
{
get
{
if (this is WzFile) return ((WzFile)this).WzDirectory.Name;
if (this is WzFile file)
return file.WzDirectory.Name;

string result = this.Name;
WzObject currObj = this;
while (currObj.Parent != null)
Expand Down
14 changes: 7 additions & 7 deletions MapleLib/WzLib/WzSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class ProgressingWzSerializer
public int Total { get { return total; } }
public int Current { get { return curr; } }

protected static void createDirSafe(ref string path)
protected static void CreateDirSafe(ref string path)
{
if (path.Substring(path.Length - 1, 1) == @"\")
path = path.Substring(0, path.Length - 1);
Expand All @@ -56,8 +56,8 @@ protected static void createDirSafe(ref string path)
Directory.CreateDirectory(path);
}

private static string regexSearch = ":" + new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
private static Regex regex_invalidPath = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
private readonly static string regexSearch = ":" + new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
private readonly static Regex regex_invalidPath = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
/// <summary>
/// Escapes invalid file name and paths (if nexon uses any illegal character that causes issue during saving)
/// </summary>
Expand Down Expand Up @@ -609,7 +609,7 @@ public void SerializeDirectory(WzDirectory dir, string outPath)
curr = 0;

if (!Directory.Exists(outPath))
WzSerializer.createDirSafe(ref outPath);
WzSerializer.CreateDirSafe(ref outPath);

if (outPath.Substring(outPath.Length - 1, 1) != @"\")
{
Expand Down Expand Up @@ -718,7 +718,7 @@ public void SerializeObject(WzObject obj, string outPath)
this.outPath = outPath;
if (!Directory.Exists(outPath))
{
WzSerializer.createDirSafe(ref outPath);
WzSerializer.CreateDirSafe(ref outPath);
}

if (outPath.Substring(outPath.Length - 1, 1) != @"\")
Expand Down Expand Up @@ -908,7 +908,7 @@ private void ExportInternal(WzImage img, string path)
private void exportDirInternal(WzDirectory dir, string path)
{
if (!Directory.Exists(path))
createDirSafe(ref path);
CreateDirSafe(ref path);

if (path.Substring(path.Length - 1) != @"\")
path += @"\";
Expand Down Expand Up @@ -979,7 +979,7 @@ private void exportXmlInternal(WzImage img, string path)
private void exportDirXmlInternal(WzDirectory dir, string path)
{
if (!Directory.Exists(path))
createDirSafe(ref path);
CreateDirSafe(ref path);

if (path.Substring(path.Length - 1) != @"\")
path += @"\";
Expand Down

0 comments on commit 3dd093d

Please sign in to comment.