Skip to content

Commit

Permalink
[HaCreator] fixed an issue whereby WZ files that isnt changed is flag…
Browse files Browse the repository at this point in the history
…ged as changed.
  • Loading branch information
lastbattle committed May 23, 2023
1 parent 4f7b37f commit 43bce58
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
9 changes: 3 additions & 6 deletions HaCreator/GUI/Repack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace HaCreator.GUI
{
public partial class Repack : Form
{
private readonly List<WzFile> toRepack = new List<WzFile>();
private readonly List<WzFile> toRepack;

/// <summary>
/// Constructor
Expand All @@ -31,12 +31,9 @@ public Repack()
{
InitializeComponent();

foreach (WzFile wzf in Program.WzManager.WzFileList)
toRepack = Program.WzManager.GetUpdatedWzFiles();
foreach (WzFile wzf in toRepack)
{
Program.WzManager.SetWzFileUpdated(wzf);

toRepack.Add(wzf);

checkedListBox_changedFiles.Items.Add(wzf.Name, CheckState.Checked);
}
}
Expand Down
37 changes: 29 additions & 8 deletions MapleLib/WzFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private set { }

private readonly ReaderWriterLockSlim _readWriteLock = new ReaderWriterLockSlim(); // for '_wzFiles', '_wzFilesUpdated', '_updatedImages', & '_wzDirs'
private readonly Dictionary<string, WzFile> _wzFiles = new Dictionary<string, WzFile>();
private readonly Dictionary<string, bool> _wzFilesUpdated = new Dictionary<string, bool>(); // key = filepath, flag for the list of WZ files changed to be saved later via Repack
private readonly Dictionary<WzFile, bool> _wzFilesUpdated = new Dictionary<WzFile, bool>(); // key = WzFile, flag for the list of WZ files changed to be saved later via Repack
private readonly HashSet<WzImage> _updatedWzImages = new HashSet<WzImage>();
private readonly Dictionary<string, WzMainDirectory> _wzDirs = new Dictionary<string, WzMainDirectory>();

Expand Down Expand Up @@ -292,15 +292,15 @@ public WzFile LoadWzFile(string baseName, WzMapleVersion encVersion)

string fileName_ = baseName.ToLower().Replace(".wz", "");

if (_wzFilesUpdated.ContainsKey(wzf.FilePath)) // some safety check
if (_wzFilesUpdated.ContainsKey(wzf)) // some safety check
throw new Exception(string.Format("Wz {0} at the path {1} has already been loaded, and cannot be loaded again. Remove it from memory first.", fileName_, wzf.FilePath));

// write lock to begin adding to the dictionary
_readWriteLock.EnterWriteLock();
try
{
_wzFiles[fileName_] = wzf;
_wzFilesUpdated[wzf.FilePath] = false;
_wzFilesUpdated[wzf] = false;
_wzDirs[fileName_] = new WzMainDirectory(wzf);
}
finally
Expand Down Expand Up @@ -329,15 +329,15 @@ public bool LoadLegacyDataWzFile(string baseName, WzMapleVersion encVersion)

baseName = baseName.ToLower();

if (_wzFilesUpdated.ContainsKey(wzf.FilePath)) // some safety check
if (_wzFilesUpdated.ContainsKey(wzf)) // some safety check
throw new Exception(string.Format("Wz file {0} at the path {1} has already been loaded, and cannot be loaded again.", baseName, wzf.FilePath));

// write lock to begin adding to the dictionary
_readWriteLock.EnterWriteLock();
try
{
_wzFiles[baseName] = wzf;
_wzFilesUpdated[wzf.FilePath] = false;
_wzFilesUpdated[wzf] = false;
_wzDirs[baseName] = new WzMainDirectory(wzf);
}
finally
Expand Down Expand Up @@ -393,13 +393,13 @@ public void SetWzFileUpdated(string name, WzImage img)
/// <exception cref="Exception"></exception>
public void SetWzFileUpdated(WzFile wzFile)
{
if (_wzFilesUpdated.ContainsKey(wzFile.FilePath))
if (_wzFilesUpdated.ContainsKey(wzFile))
{
// write lock to begin adding to the dictionary
_readWriteLock.EnterWriteLock();
try
{
_wzFilesUpdated[wzFile.FilePath] = true;
_wzFilesUpdated[wzFile] = true;
}
finally
{
Expand All @@ -410,6 +410,27 @@ public void SetWzFileUpdated(WzFile wzFile)
throw new Exception("wz file to be flagged do not exist in memory " + wzFile.FilePath);
}

/// <summary>
/// Gets the list of updated or changed WZ files.
/// </summary>
/// <returns></returns>
public List<WzFile> GetUpdatedWzFiles() {
List<WzFile> updatedWzFiles = new List<WzFile>();
// readlock
_readWriteLock.EnterReadLock();
try {
foreach (KeyValuePair<WzFile, bool> wzFileUpdated in _wzFilesUpdated) {
if (wzFileUpdated.Value == true) {
updatedWzFiles.Add(wzFileUpdated.Key);
}
}
}
finally {
_readWriteLock.ExitReadLock();
}
return updatedWzFiles;
}

/// <summary>
/// Unload the wz file from memory
/// </summary>
Expand All @@ -424,7 +445,7 @@ public void UnloadWzFile(WzFile wzFile, string wzFilePath)
try
{
_wzFiles.Remove(baseName);
_wzFilesUpdated.Remove(wzFilePath);
_wzFilesUpdated.Remove(wzFile);
_wzDirs.Remove(baseName);
}
finally
Expand Down

0 comments on commit 43bce58

Please sign in to comment.