Skip to content

Commit

Permalink
Fix exporting XML/wz issues when an image or a directory contains an …
Browse files Browse the repository at this point in the history
  • Loading branch information
lastbattle committed Jan 1, 2020
1 parent 98c4962 commit 59164e0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
8 changes: 6 additions & 2 deletions HaRepacker/GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,12 +776,16 @@ private void RunWzImgDirsExtraction(object param)

foreach (WzImage img in imgsToDump)
{
serializer.SerializeImage(img, Path.Combine(baseDir, img.Name));
string escapedPath = Path.Combine(baseDir, ProgressingWzSerializer.EscapeInvalidFilePathNames(img.Name));

serializer.SerializeImage(img, escapedPath);
UpdateProgressBar(MainPanel.mainProgressBar, 1, false, false);
}
foreach (WzDirectory dir in dirsToDump)
{
serializer.SerializeDirectory(dir, Path.Combine(baseDir, dir.Name));
string escapedPath = Path.Combine(baseDir, ProgressingWzSerializer.EscapeInvalidFilePathNames(dir.Name));

serializer.SerializeDirectory(dir, escapedPath);
UpdateProgressBar(MainPanel.mainProgressBar, 1, false, false);
}
threadDone = true;
Expand Down
62 changes: 48 additions & 14 deletions MapleLib/WzLib/WzSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace MapleLib.WzLib.Serialization
{
Expand All @@ -39,7 +40,9 @@ public abstract class ProgressingWzSerializer

protected static void createDirSafe(ref string path)
{
if (path.Substring(path.Length - 1, 1) == @"\") path = path.Substring(0, path.Length - 1);
if (path.Substring(path.Length - 1, 1) == @"\")
path = path.Substring(0, path.Length - 1);

string basePath = path;
int curridx = 0;
while (Directory.Exists(path) || File.Exists(path))
Expand All @@ -49,6 +52,17 @@ 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)));
/// <summary>
/// Escapes invalid file name and paths (if nexon uses any illegal character that causes issue during saving)
/// </summary>
/// <param name="path"></param>
public static string EscapeInvalidFilePathNames(string path)
{
return regex_invalidPath.Replace(path, "");
}
}

public abstract class WzXmlSerializer : ProgressingWzSerializer
Expand Down Expand Up @@ -252,6 +266,7 @@ public void SerializeImage(WzImage img, string outPath)
{
outPath += ".img";
}

using (FileStream stream = File.Create(outPath))
{
using (WzBinaryWriter wzWriter = new WzBinaryWriter(stream, ((WzDirectory)img.parent).WzIv))
Expand All @@ -265,6 +280,7 @@ public void SerializeDirectory(WzDirectory dir, string outPath)
{
total = dir.CountImages();
curr = 0;

if (!Directory.Exists(outPath))
WzXmlSerializer.createDirSafe(ref outPath);

Expand Down Expand Up @@ -365,8 +381,14 @@ public void SerializeObject(WzObject obj, string outPath)
//imagesToUnparse.Clear();
total = 0; curr = 0;
this.outPath = outPath;
if (!Directory.Exists(outPath)) WzXmlSerializer.createDirSafe(ref outPath);
if (outPath.Substring(outPath.Length - 1, 1) != @"\") outPath += @"\";
if (!Directory.Exists(outPath))
{
WzXmlSerializer.createDirSafe(ref outPath);
}

if (outPath.Substring(outPath.Length - 1, 1) != @"\")
outPath += @"\";

total = CalculateTotal(obj);
ExportRecursion(obj, outPath);
/*foreach (WzImage img in imagesToUnparse)
Expand Down Expand Up @@ -405,34 +427,47 @@ private void ExportRecursion(WzObject currObj, string outPath)
ExportRecursion(((WzFile)currObj).WzDirectory, outPath);
else if (currObj is WzDirectory)
{
outPath += currObj.Name + @"\";
if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);
outPath += ProgressingWzSerializer.EscapeInvalidFilePathNames(currObj.Name) + @"\";
if (!Directory.Exists(outPath))
Directory.CreateDirectory(outPath);
foreach (WzDirectory subdir in ((WzDirectory)currObj).WzDirectories)
{
ExportRecursion(subdir, outPath + subdir.Name + @"\");
}
foreach (WzImage subimg in ((WzDirectory)currObj).WzImages)
{
ExportRecursion(subimg, outPath + subimg.Name + @"\");
}
}
else if (currObj is WzCanvasProperty)
{
Bitmap bmp = ((WzCanvasProperty)currObj).PngProperty.GetPNG(false);
string path = outPath + currObj.Name + ".png";

string path = outPath + ProgressingWzSerializer.EscapeInvalidFilePathNames(currObj.Name) + ".png";

bmp.Save(path, ImageFormat.Png);
//curr++;
}
else if (currObj is WzSoundProperty)
{
string path = outPath + currObj.Name + ".mp3";
string path = outPath + ProgressingWzSerializer.EscapeInvalidFilePathNames(currObj.Name) + ".mp3";
((WzSoundProperty)currObj).SaveToFile(path);
}
else if (currObj is WzImage)
{
outPath += currObj.Name + @"\";
if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);
outPath += ProgressingWzSerializer.EscapeInvalidFilePathNames(currObj.Name) + @"\";
if (!Directory.Exists(outPath))
Directory.CreateDirectory(outPath);

bool parse = ((WzImage)currObj).Parsed || ((WzImage)currObj).Changed;
if (!parse) ((WzImage)currObj).ParseImage();
if (!parse)
((WzImage)currObj).ParseImage();
foreach (WzImageProperty subprop in ((IPropertyContainer)currObj).WzProperties)
{
ExportRecursion(subprop, outPath);
if (!parse) ((WzImage)currObj).UnparseImage();
}
if (!parse)
((WzImage)currObj).UnparseImage();
curr++;
}
else if (currObj is IPropertyContainer)
Expand All @@ -459,7 +494,6 @@ private void exportXmlInternal(WzImage img, string path)
img.ParseImage();
curr++;


using (TextWriter tw = new StreamWriter(File.Create(path)))
{
tw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + lineBreak);
Expand All @@ -483,11 +517,11 @@ private void exportDirXmlInternal(WzDirectory dir, string path)

foreach (WzDirectory subdir in dir.WzDirectories)
{
exportDirXmlInternal(subdir, path + subdir.name + @"\");
exportDirXmlInternal(subdir, path + ProgressingWzSerializer.EscapeInvalidFilePathNames(subdir.name) + @"\");
}
foreach (WzImage subimg in dir.WzImages)
{
exportXmlInternal(subimg, path + subimg.Name + ".xml");
exportXmlInternal(subimg, path + ProgressingWzSerializer.EscapeInvalidFilePathNames(subimg.Name) + ".xml");
}
}

Expand Down

0 comments on commit 59164e0

Please sign in to comment.