Skip to content

Commit

Permalink
[HaCreator] a more elegant method to fix it, once and for all.
Browse files Browse the repository at this point in the history
- a more elegant method to fix it, once and for all right from the core in MapleLib without the messy if/else in every part of the code where parsing a certain Wz file is needed.
- also fixed an issue where Sound directory is recursively loaded a dozen more times than needed
  • Loading branch information
lastbattle committed May 23, 2023
1 parent e3fc4c2 commit 68dfbbc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 65 deletions.
35 changes: 17 additions & 18 deletions HaCreator/GUI/Initialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ private bool InitializeWzFiles(string wzPath, WzMapleVersion fileVersion)
ExtractReactorFile();

// Load sound
List<string> soundWzFiles = Program.WzManager.GetWzFileNameListFromBase("sound");
foreach (string soundWzFileName in soundWzFiles)
List<string> soundWzDirs = Program.WzManager.GetWzFileNameListFromBase("sound");
foreach (string soundDirName in soundWzDirs)
{
UpdateUI_CurrentLoadingWzFile(soundWzFileName);
UpdateUI_CurrentLoadingWzFile(soundDirName);

Program.WzManager.LoadWzFile(soundWzFileName, _wzMapleVersion);
ExtractSoundFile();
Program.WzManager.LoadWzFile(soundDirName, _wzMapleVersion);
}
ExtractSoundFile();


// Load maps
Expand Down Expand Up @@ -530,31 +530,31 @@ public void ExtractReactorFile()
/// <summary>
///
/// </summary>
public void ExtractSoundFile()
{
public void ExtractSoundFile() {
List<WzDirectory> soundWzDirs = Program.WzManager.GetWzDirectoriesFromBase("sound");
foreach (WzDirectory soundWzDir in soundWzDirs)

foreach (WzDirectory soundWzDir in soundWzDirs)
{
if (Program.WzManager.IsPreBBDataWzFormat) {
WzDirectory x = (WzDirectory) soundWzDir["Sound"];
}

foreach (WzImage soundImage in soundWzDir.WzImages)
{
if (!soundImage.Name.ToLower().Contains("bgm"))
continue;
if (!soundImage.Parsed)
soundImage.ParseImage();
try
{
foreach (WzImageProperty bgmImage in soundImage.WzProperties)
{
try {
foreach (WzImageProperty bgmImage in soundImage.WzProperties) {
WzBinaryProperty binProperty = null;
if (bgmImage is WzBinaryProperty bgm)
{
if (bgmImage is WzBinaryProperty bgm) {
binProperty = bgm;
}
else if (bgmImage is WzUOLProperty uolBGM) // is UOL property
{
WzObject linkVal = ((WzUOLProperty)bgmImage).LinkValue;
if (linkVal is WzBinaryProperty linkCanvas)
{
if (linkVal is WzBinaryProperty linkCanvas) {
binProperty = linkCanvas;
}
}
Expand All @@ -563,8 +563,7 @@ public void ExtractSoundFile()
Program.InfoManager.BGMs[WzInfoTools.RemoveExtension(soundImage.Name) + @"/" + binProperty.Name] = binProperty;
}
}
catch (Exception e)
{
catch (Exception e) {
string error = string.Format("[ExtractSoundFile] Error parsing {0}, {1} file.\r\nError: {2}", soundWzDir.Name, soundImage.Name, e.ToString());
MapleLib.Helpers.ErrorLogger.Log(ErrorLevel.IncorrectStructure, error);
continue;
Expand Down
24 changes: 5 additions & 19 deletions HaSharedLibrary/Wz/WzInfoTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,28 +260,14 @@ public static WzImage FindMapImage(string mapid, WzFileManager fileManager)
string mapcat = fileManager.Is64Bit ? mapIdNamePadded.Substring(0, 1) : "Map" + mapIdNamePadded.Substring(0, 1);
string baseDir = fileManager.Is64Bit ? "map\\map\\map" + mapcat : "map";

if (fileManager.IsPreBBDataWzFormat) {
WzObject mapObject = fileManager.FindWzImageByName(baseDir, "Map");
WzObject mapObject = fileManager.FindWzImageByName(baseDir, fileManager.Is64Bit ? mapIdNamePadded : "Map");

WzImage mapImage = (WzImage) (mapObject?[mapcat]?[mapIdNamePadded]);
return mapImage;
if (fileManager.Is64Bit) {
return (WzImage)mapObject;
} else {
List<WzDirectory> mapWzDirs = fileManager.GetWzDirectoriesFromBase(baseDir);

foreach (WzDirectory mapWzDir in mapWzDirs) {
WzImage mapImage;

if (fileManager.Is64Bit)
mapImage = (WzImage)mapWzDir?[mapIdNamePadded];
else
mapImage = (WzImage)mapWzDir?["Map"]?[mapcat]?[mapIdNamePadded];

if (mapImage != null) {
return mapImage;
}
}
WzImage mapImage = (WzImage)mapObject?[mapcat]?[mapIdNamePadded];
return mapImage;
}
return null;
}

public static Color XNAToDrawingColor(XNA.Color c)
Expand Down
50 changes: 22 additions & 28 deletions MapleLib/WzFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.ObjectModel;
using System.Threading;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;

namespace MapleLib
{
Expand Down Expand Up @@ -509,6 +510,7 @@ public WzMainDirectory GetMainDirectoryByName(string name)
/// <summary>
/// Get the list of sub wz files by its base name ("mob")
/// i.e 'mob' expands to the list array of files "Mob001", "Mob2"
/// exception: returns Data.wz regardless for pre-bb beta maplestory
/// </summary>
/// <param name="baseName"></param>
/// <returns></returns>
Expand All @@ -535,10 +537,18 @@ public List<WzDirectory> GetWzDirectoriesFromBase(string baseName)
{
List<string> wzDirs = GetWzFileNameListFromBase(baseName);
// Use Select() and Where() to transform and filter the WzDirectory list
return wzDirs
.Select(name => this[name])
.Where(dir => dir != null)
.ToList();
if (_bIsPreBBDataWzFormat) {
return wzDirs
.Select(name => this["data"][baseName] as WzDirectory)
.Where(dir => dir != null)
.ToList();
}
else {
return wzDirs
.Select(name => this[name])
.Where(dir => dir != null)
.ToList();
}
}

/// <summary>
Expand All @@ -550,21 +560,13 @@ public List<WzDirectory> GetWzDirectoriesFromBase(string baseName)
public WzObject FindWzImageByName(string baseWzName, string imageName) {
baseWzName = baseWzName.ToLower();

WzObject image = null;
List<WzDirectory> dirs = GetWzDirectoriesFromBase(baseWzName);
if (_bIsPreBBDataWzFormat) {
image = dirs
.Where(wzFile => wzFile != null && wzFile[baseWzName] != null && (imageName == string.Empty ? true : wzFile[baseWzName][imageName] != null))
.Select(wzFile => (imageName == string.Empty ? wzFile[baseWzName] : wzFile[baseWzName][imageName]))
.FirstOrDefault();
}
else {
// Use Where() and FirstOrDefault() to filter the WzDirectories and find the first matching WzObject
image = dirs
// Use Where() and FirstOrDefault() to filter the WzDirectories and find the first matching WzObject
WzObject image = dirs
.Where(wzFile => wzFile != null && wzFile[imageName] != null)
.Select(wzFile => wzFile[imageName])
.FirstOrDefault();
}

return image;
}

Expand All @@ -578,19 +580,11 @@ public List<WzObject> FindWzImagesByName(string baseWzName, string imageName) {
baseWzName = baseWzName.ToLower();

List<WzDirectory> dirs = GetWzDirectoriesFromBase(baseWzName);
if (_bIsPreBBDataWzFormat) {
return dirs
.Where(wzFile => wzFile != null && wzFile[baseWzName] != null && (imageName == string.Empty ? true : wzFile[baseWzName][imageName] != null))
.Select(wzFile => (imageName == string.Empty ? wzFile[baseWzName] : wzFile[baseWzName][imageName]))
.ToList();
}
else {
// Use Where() and FirstOrDefault() to filter the WzDirectories and find the first matching WzObject
return dirs
.Where(wzFile => wzFile != null && wzFile[imageName] != null)
.Select(wzFile => wzFile[imageName])
.ToList();
}
// Use Where() and FirstOrDefault() to filter the WzDirectories and find the first matching WzObject
return dirs
.Where(wzFile => wzFile != null && wzFile[imageName] != null)
.Select(wzFile => wzFile[imageName])
.ToList();
}

/// <summary>
Expand Down

0 comments on commit 68dfbbc

Please sign in to comment.