Skip to content

Commit

Permalink
[HaRepacker] Reset progress bar to 0 after extraction completion
Browse files Browse the repository at this point in the history
see: #181
  • Loading branch information
lastbattle committed Nov 2, 2022
1 parent 91cc533 commit e81fc84
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions HaRepacker/GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ private void RunWzFilesExtraction(object param)
string baseDir = (string)((object[])param)[1];
WzMapleVersion version = GetWzMapleVersionByWzEncryptionBoxSelection((int)(((object[])param)[2]));
IWzFileSerializer serializer = (IWzFileSerializer)((object[])param)[3];

UpdateProgressBar(MainPanel.mainProgressBar, 0, false, true);
UpdateProgressBar(MainPanel.mainProgressBar, wzFilesToDump.Length, true, true);

Expand All @@ -1125,6 +1126,10 @@ private void RunWzFilesExtraction(object param)
}
MapleLib.Helpers.ErrorLogger.SaveToFile("WzExtract_Errors.txt");

// Reset progress bar to 0
UpdateProgressBar(MainPanel.mainProgressBar, 0, false, true);
UpdateProgressBar(MainPanel.mainProgressBar, 0, true, true);

threadDone = true;
}

Expand Down Expand Up @@ -1162,22 +1167,27 @@ private void RunWzImgDirsExtraction(object param)
}
MapleLib.Helpers.ErrorLogger.SaveToFile("WzExtract_Errors.txt");

threadDone = true;
// Reset progress bar to 0
UpdateProgressBar(MainPanel.mainProgressBar, 0, false, true);
UpdateProgressBar(MainPanel.mainProgressBar, 0, true, true);

threadDone = true;
}

private void RunWzObjExtraction(object param)
{
ChangeApplicationState(false);

#if DEBUG
var watch = new System.Diagnostics.Stopwatch();
var watch = new Stopwatch();
watch.Start();
#endif
List<WzObject> objsToDump = (List<WzObject>)((object[])param)[0];
string path = (string)((object[])param)[1];
ProgressingWzSerializer serializers = (ProgressingWzSerializer)((object[])param)[2];

UpdateProgressBar(MainPanel.mainProgressBar, 0, false, true);

if (serializers is IWzObjectSerializer serializer)
{
UpdateProgressBar(MainPanel.mainProgressBar, objsToDump.Count, true, true);
Expand All @@ -1201,6 +1211,10 @@ private void RunWzObjExtraction(object param)
Debug.WriteLine($"WZ files Extracted. Execution Time: {watch.ElapsedMilliseconds} ms");
#endif

// Reset progress bar to 0
UpdateProgressBar(MainPanel.mainProgressBar, 0, false, true);
UpdateProgressBar(MainPanel.mainProgressBar, 0, true, true);

threadDone = true;
}

Expand Down Expand Up @@ -1244,36 +1258,36 @@ private void xMLToolStripMenuItem_Click(object sender, EventArgs e)
WzClassicXmlSerializer serializer = new WzClassicXmlSerializer(
Program.ConfigurationManager.UserSettings.Indentation,
Program.ConfigurationManager.UserSettings.LineBreakType, false);

threadDone = false;
new Thread(new ParameterizedThreadStart(RunWzFilesExtraction)).Start((object)new object[] { dialog.FileNames, folderDialog.SelectedPath, encryptionBox.SelectedIndex, serializer });
new Thread(new ParameterizedThreadStart(ProgressBarThread)).Start(serializer);
}

private delegate void UpdateProgressBarDelegate(ToolStripProgressBar pbar, int value, bool max, bool absolute); //max for .Maximum, !max for .Value
private void UpdateProgressBarCallback(System.Windows.Controls.ProgressBar pbar, int value, bool max, bool absolute)
{
if (max)
{
if (absolute)
pbar.Maximum = value;
else pbar.Maximum += value;
}
else
{
if (absolute)
pbar.Value = value;
else pbar.Value += value;
}
}
private void UpdateProgressBar(System.Windows.Controls.ProgressBar pbar, int value, bool max, bool absolute)
/// <summary>
/// Updates the progress bar
/// </summary>
/// <param name="pbar"></param>
/// <param name="value"></param>
/// <param name="setMaxValue"></param>
/// <param name="absolute"></param>
private void UpdateProgressBar(System.Windows.Controls.ProgressBar pbar, int value, bool setMaxValue, bool absolute)
{
pbar.Dispatcher.Invoke(() =>
{
UpdateProgressBarCallback(pbar, value, max, absolute);
if (setMaxValue)
{
if (absolute)
pbar.Maximum = value;
else pbar.Maximum += value;
}
else
{
if (absolute)
pbar.Value = value;
else pbar.Value += value;
}
});
/* if (pbar.ProgressBar.InvokeRequired)
pbar.ProgressBar.Invoke(new UpdateProgressBarDelegate(UpdateProgressBarCallback), new object[] { pbar, value, max, absolute });
else UpdateProgressBarCallback(pbar, value, max, absolute);*/
}


Expand All @@ -1287,9 +1301,14 @@ private void ProgressBarThread(object param)
UpdateProgressBar(MainPanel.secondaryProgressBar, Math.Min(total, serializer.Current), false, true);
Thread.Sleep(500);
}
UpdateProgressBar(MainPanel.mainProgressBar, 0, true, true);
UpdateProgressBar(MainPanel.mainProgressBar, 1, true, true);
UpdateProgressBar(MainPanel.mainProgressBar, 0, false, true);

UpdateProgressBar(MainPanel.secondaryProgressBar, 1, true, true);
UpdateProgressBar(MainPanel.secondaryProgressBar, 0, false, true);

ChangeApplicationState(true);

threadDone = false;
}

Expand Down Expand Up @@ -1405,6 +1424,7 @@ private void pNGsToolStripMenuItem_Click(object sender, EventArgs e)

WzPngMp3Serializer serializer = new WzPngMp3Serializer();
threadDone = false;

runningThread = new Thread(new ParameterizedThreadStart(RunWzObjExtraction));
runningThread.Start((object)new object[] { objs, outPath, serializer });
new Thread(new ParameterizedThreadStart(ProgressBarThread)).Start(serializer);
Expand Down Expand Up @@ -1464,6 +1484,7 @@ private void ExportBsonJsonInternal(bool isJson)
}
WzJsonBsonSerializer serializer = new WzJsonBsonSerializer(Program.ConfigurationManager.UserSettings.Indentation, Program.ConfigurationManager.UserSettings.LineBreakType, bIncludeBase64BinData, isJson);
threadDone = false;

runningThread = new Thread(new ParameterizedThreadStart(RunWzImgDirsExtraction));
runningThread.Start((object)new object[] { dirs, imgs, outPath, serializer });

Expand Down Expand Up @@ -1501,6 +1522,7 @@ private void privateServerToolStripMenuItem_Click(object sender, EventArgs e)
Program.ConfigurationManager.UserSettings.Indentation,
Program.ConfigurationManager.UserSettings.LineBreakType, false);
threadDone = false;

runningThread = new Thread(new ParameterizedThreadStart(RunWzImgDirsExtraction));
runningThread.Start((object)new object[] { dirs, imgs, outPath, serializer });

Expand Down Expand Up @@ -1538,8 +1560,10 @@ private void classicToolStripMenuItem_Click(object sender, EventArgs e)
Program.ConfigurationManager.UserSettings.Indentation,
Program.ConfigurationManager.UserSettings.LineBreakType, true);
threadDone = false;

runningThread = new Thread(new ParameterizedThreadStart(RunWzImgDirsExtraction));
runningThread.Start((object)new object[] { dirs, imgs, outPath, serializer });

new Thread(new ParameterizedThreadStart(ProgressBarThread)).Start(serializer);
}

Expand Down Expand Up @@ -1568,8 +1592,10 @@ private void newToolStripMenuItem1_Click(object sender, EventArgs e)
Program.ConfigurationManager.UserSettings.Indentation,
Program.ConfigurationManager.UserSettings.LineBreakType);
threadDone = false;

runningThread = new Thread(new ParameterizedThreadStart(RunWzObjExtraction));
runningThread.Start((object)new object[] { objs, dialog.FileName, serializer });

new Thread(new ParameterizedThreadStart(ProgressBarThread)).Start(serializer);
}

Expand Down Expand Up @@ -2007,6 +2033,7 @@ private void nXForamtToolStripMenuItem_Click(object sender, EventArgs e)

WzToNxSerializer serializer = new WzToNxSerializer();
threadDone = false;

runningThread = new Thread(new ParameterizedThreadStart(RunWzFilesExtraction));
runningThread.Start((object)new object[] { dialog.FileNames, outPath, encryptionBox.SelectedIndex, serializer });
new Thread(new ParameterizedThreadStart(ProgressBarThread)).Start(serializer);
Expand Down

0 comments on commit e81fc84

Please sign in to comment.