Skip to content

Commit

Permalink
The output to the console is not printed if the console is hidden
Browse files Browse the repository at this point in the history
All Forms and Dialogs (except Folder/FileOpenDialogs) now open in the center of the parent window
The reliability of ProgressBar has been improved again
  • Loading branch information
DmitriySalnikov committed Oct 13, 2023
1 parent 2d0d26d commit 4b539ec
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 68 deletions.
1 change: 1 addition & 0 deletions GodotPCKExplorer.UI/Forms/BackgroundProgress.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 30 additions & 3 deletions GodotPCKExplorer.UI/Forms/BackgroundProgress.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

Expand All @@ -12,14 +11,32 @@ public partial class BackgroundProgress : Form
DateTime prevUpdateTime = DateTime.UtcNow;
float delta_time = 1 / 30; // 30 fps
int prevPercent = 0;

Thread work = null;
Action<CancellationToken> work_action;
CancellationTokenSource cancellationTokenSource;

public BackgroundProgress(CancellationTokenSource token)
public BackgroundProgress(Action<CancellationToken> action)
{
InitializeComponent();

work_action = action;
Icon = Properties.Resources.icon;
l_status.Text = "";
cancellationTokenSource = token;
cancellationTokenSource = new CancellationTokenSource();
}

private void BackgroundProgress_Shown(object sender, EventArgs e)
{
work = new Thread(new ThreadStart(() =>
{
work_action(cancellationTokenSource.Token);
if (!cancellationTokenSource.IsCancellationRequested)
{
BeginInvoke(new Action(Close));
}
}));
work.Start();
}

public void ReportProgress(string operation, int number, string customPrefix = null)
Expand Down Expand Up @@ -60,11 +77,21 @@ public void ReportProgress(string operation, int number, string customPrefix = n
private void btn_cancel_Click(object sender, EventArgs e)
{
cancellationTokenSource.Cancel();
Close();
}

private void BackgroundProgress_FormClosing(object sender, FormClosingEventArgs e)
{
cancellationTokenSource.Cancel();
if (work != null)
{
if (work.IsAlive)
work.Join();
}
work = null;

work_action = null;
cancellationTokenSource.Dispose();
}
}
}
3 changes: 1 addition & 2 deletions GodotPCKExplorer.UI/Forms/CreatePCKEncryption.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GodotPCKExplorer;
using System;
using System;
using System.Security.Cryptography;
using System.Windows.Forms;

Expand Down
17 changes: 11 additions & 6 deletions GodotPCKExplorer.UI/Forms/CreatePCKFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void SetFolderPath(string path)
List<PCKPacker.FileToPack> filesScan = new List<PCKPacker.FileToPack>();

if (Directory.Exists(path))
Program.DoTaskWithProgressBar((t) => filesScan = PCKUtils.ScanFoldersForFiles(Path.GetFullPath(path), cancellationToken: t));
Program.DoTaskWithProgressBar((t) => filesScan = PCKUtils.ScanFoldersForFiles(Path.GetFullPath(path), cancellationToken: t),
this);

GC.Collect();
files = filesScan.ToDictionary((f) => f.OriginalPath);
Expand Down Expand Up @@ -104,12 +105,12 @@ private void btn_create_Click(object sender, EventArgs e)

if (cb_embed.Checked)
{
res = ofd_pack_into.ShowDialog();
res = ofd_pack_into.ShowDialog(this);
file = ofd_pack_into.FileName;
}
else
{
res = sfd_save_pack.ShowDialog();
res = sfd_save_pack.ShowDialog(this);
file = sfd_save_pack.FileName;
}

Expand All @@ -129,7 +130,7 @@ private void btn_create_Click(object sender, EventArgs e)
GUIConfig.Instance.EncryptFiles && cb_enable_encryption.Checked,
t
);
});
}, this);

GUIConfig.Instance.PackedVersion = ver;
GUIConfig.Instance.EmbedPCK = cb_embed.Checked;
Expand Down Expand Up @@ -163,7 +164,7 @@ private void tb_folder_path_KeyDown(object sender, KeyEventArgs e)

private void btn_browse_Click(object sender, EventArgs e)
{
if (fbd_pack_folder.ShowDialog() == DialogResult.OK)
if (fbd_pack_folder.ShowDialog(this) == DialogResult.OK)
{
tb_folder_path.Text = Path.GetFullPath(fbd_pack_folder.SelectedPath);
SetFolderPath(tb_folder_path.Text);
Expand Down Expand Up @@ -200,7 +201,11 @@ private void btn_match_case_Click(object sender, EventArgs e)

private void btn_generate_key_Click(object sender, EventArgs e)
{
new CreatePCKEncryption().ShowDialog();
using (var tmp = new CreatePCKEncryption())
{
tmp.StartPosition = FormStartPosition.CenterParent;
tmp.ShowDialog(this);
}
}
}
}
64 changes: 38 additions & 26 deletions GodotPCKExplorer.UI/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ public MainForm()

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
var tmpAbout = new AboutBox1();
tmpAbout.ShowDialog();
using (var tmpAbout = new AboutBox1())
{
tmpAbout.StartPosition = FormStartPosition.CenterParent;
tmpAbout.ShowDialog(this);
}
}

private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
var res = ofd_open_pack.ShowDialog();
var res = ofd_open_pack.ShowDialog(this);

if (res == DialogResult.OK)
{
Expand Down Expand Up @@ -183,7 +186,6 @@ void UpdateRecentList()
recentToolStripMenuItem.DropDownItems.Add(
new ToolStripButton(f.Path, null, (s, e) => OpenFile(f.Path, f.EncryptionKey)));
}

}
else
{
Expand Down Expand Up @@ -230,7 +232,8 @@ public void OpenFile(string path, string encKey = null)

using (var d = new OpenWithPCKEncryption(item?.EncryptionKey ?? ""))
{
var res = d.ShowDialog();
d.StartPosition = FormStartPosition.CenterParent;
var res = d.ShowDialog(this);

if (res == DialogResult.Cancel)
{
Expand Down Expand Up @@ -382,31 +385,35 @@ private void exitToolStripMenuItem_Click(object sender, EventArgs e)

private void extractFileToolStripMenuItem_Click(object sender, EventArgs e)
{
var res = fbd_extract_folder.ShowDialog();
var res = fbd_extract_folder.ShowDialog(this);
if (res == DialogResult.OK)
{
List<string> rows = new List<string>();
foreach (DataGridViewRow i in dataGridView1.SelectedRows)
rows.Add((string)i.Cells[0].Value);

Program.DoTaskWithProgressBar((t) => pckReader.ExtractFiles(rows, fbd_extract_folder.SelectedPath, overwriteExported.Checked, GUIConfig.Instance.CheckMD5Extracted, cancellationToken: t));
Program.DoTaskWithProgressBar((t) => pckReader.ExtractFiles(rows, fbd_extract_folder.SelectedPath, overwriteExported.Checked, GUIConfig.Instance.CheckMD5Extracted, cancellationToken: t),
this);
}
}

private void extractAllToolStripMenuItem_Click(object sender, EventArgs e)
{
var res = fbd_extract_folder.ShowDialog();
var res = fbd_extract_folder.ShowDialog(this);
if (res == DialogResult.OK)
{
Program.DoTaskWithProgressBar((t) => pckReader.ExtractFiles(pckReader.Files.Select((f) => f.Key), fbd_extract_folder.SelectedPath, overwriteExported.Checked, GUIConfig.Instance.CheckMD5Extracted, cancellationToken: t));
Program.DoTaskWithProgressBar((t) => pckReader.ExtractFiles(pckReader.Files.Select((f) => f.Key), fbd_extract_folder.SelectedPath, overwriteExported.Checked, GUIConfig.Instance.CheckMD5Extracted, cancellationToken: t),
this);
}
}

private void packFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
var dlg = new CreatePCKFile();
dlg.ShowDialog();
dlg.Dispose();
using (var dlg = new CreatePCKFile())
{
dlg.StartPosition = FormStartPosition.CenterParent;
dlg.ShowDialog(this);
}
}

private void closeFileToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -515,7 +522,7 @@ private void checkMD5OnExportToolStripMenuItem_Click(object sender, EventArgs e)

private void ripPackFromFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofd_rip_select_pck.ShowDialog() == DialogResult.OK)
if (ofd_rip_select_pck.ShowDialog(this) == DialogResult.OK)
{
using (var pck = new PCKReader())
{
Expand All @@ -530,16 +537,17 @@ private void ripPackFromFileToolStripMenuItem_Click(object sender, EventArgs e)
}
}

if (sfd_rip_save_pack.ShowDialog() == DialogResult.OK)
if (sfd_rip_save_pack.ShowDialog(this) == DialogResult.OK)
{
Program.DoTaskWithProgressBar((t) => PCKActions.RipPCKRun(ofd_rip_select_pck.FileName, sfd_rip_save_pack.FileName, cancellationToken: t));
Program.DoTaskWithProgressBar((t) => PCKActions.RipPCKRun(ofd_rip_select_pck.FileName, sfd_rip_save_pack.FileName, cancellationToken: t),
this);
}
}
}

private void splitExeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofd_split_exe_open.ShowDialog() == DialogResult.OK)
if (ofd_split_exe_open.ShowDialog(this) == DialogResult.OK)
{
using (var pck = new PCKReader())
if (!pck.OpenFile(ofd_split_exe_open.FileName, log_names_progress: false))
Expand All @@ -553,43 +561,47 @@ private void splitExeToolStripMenuItem_Click(object sender, EventArgs e)
}

sfd_split_new_file.Filter = $"Original file extension|*{Path.GetExtension(ofd_split_exe_open.FileName)}|All Files|*.*";
if (sfd_split_new_file.ShowDialog() == DialogResult.OK)
if (sfd_split_new_file.ShowDialog(this) == DialogResult.OK)
{
Program.DoTaskWithProgressBar((t) => PCKActions.SplitPCKRun(ofd_split_exe_open.FileName, sfd_split_new_file.FileName, cancellationToken: t));
Program.DoTaskWithProgressBar((t) => PCKActions.SplitPCKRun(ofd_split_exe_open.FileName, sfd_split_new_file.FileName, cancellationToken: t),
this);
}
}
}

private void mergePackIntoFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofd_merge_pck.ShowDialog() == DialogResult.OK)
if (ofd_merge_pck.ShowDialog(this) == DialogResult.OK)
{
using (var pck = new PCKReader())
if (!pck.OpenFile(ofd_merge_pck.FileName, log_names_progress: false))
{
return;
}

if (ofd_merge_target.ShowDialog() == DialogResult.OK)
if (ofd_merge_target.ShowDialog(this) == DialogResult.OK)
{
Program.DoTaskWithProgressBar((t) => PCKActions.MergePCKRun(ofd_merge_pck.FileName, ofd_merge_target.FileName, cancellationToken: t));
Program.DoTaskWithProgressBar((t) => PCKActions.MergePCKRun(ofd_merge_pck.FileName, ofd_merge_target.FileName, cancellationToken: t),
this);
}
}
}

private void removePackFromFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofd_remove_pck_from_exe.ShowDialog() == DialogResult.OK)
if (ofd_remove_pck_from_exe.ShowDialog(this) == DialogResult.OK)
{
Program.DoTaskWithProgressBar((t) => PCKActions.RipPCKRun(ofd_remove_pck_from_exe.FileName, cancellationToken: t));
Program.DoTaskWithProgressBar((t) => PCKActions.RipPCKRun(ofd_remove_pck_from_exe.FileName, cancellationToken: t),
this);
}
}

private void splitExeInPlaceToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofd_split_in_place.ShowDialog() == DialogResult.OK)
if (ofd_split_in_place.ShowDialog(this) == DialogResult.OK)
{
Program.DoTaskWithProgressBar((t) => PCKActions.SplitPCKRun(ofd_split_in_place.FileName, null, false, cancellationToken: t));
Program.DoTaskWithProgressBar((t) => PCKActions.SplitPCKRun(ofd_split_in_place.FileName, null, false, cancellationToken: t),
this);
}
}

Expand Down Expand Up @@ -623,7 +635,7 @@ private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEv

private void changePackVersionToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofd_change_version.ShowDialog() == DialogResult.OK)
if (ofd_change_version.ShowDialog(this) == DialogResult.OK)
{
var cv = new ChangePCKVersion();
cv.ShowAndOpenFile(ofd_change_version.FileName);
Expand Down
11 changes: 8 additions & 3 deletions GodotPCKExplorer.UI/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static FastConsole()
Console.OutputEncoding = Encoding.Unicode; // crucial

// avoid special "ShadowBuffer" for hard-coded size 0x14000 in 'BufferedStream'
str = new BufferedStream(Console.OpenStandardOutput(), 256000);
str = new BufferedStream(Console.OpenStandardOutput(), 128 * 1024);
}

public static void WriteLine(string s)
Expand All @@ -40,6 +40,8 @@ public static void Write(string s)

public class Logger : IDisposable
{
public bool DuplicateToConsole = true;

readonly string saveFile;
TextWriter logWriter = null;
DeferredAction flushFileAction = null;
Expand All @@ -54,6 +56,8 @@ public Logger(string saveFile)

try
{
flushFastConsole = new DeferredAction(() => FastConsole.Flush(), 500);

string dir_name = Path.GetDirectoryName(this.saveFile);
if (!Directory.Exists(dir_name))
Directory.CreateDirectory(dir_name);
Expand All @@ -62,7 +66,6 @@ public Logger(string saveFile)
File.Delete(this.saveFile);
logWriter = new StreamWriter(File.Open(this.saveFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read));
flushFileAction = new DeferredAction(() => logWriter.Flush(), 500);
flushFastConsole = new DeferredAction(() => FastConsole.Flush(), 500);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -111,7 +114,9 @@ public void Write(string txt)

lock (dataLock)
{
FastConsole.WriteLine(txt);
if (DuplicateToConsole)
FastConsole.WriteLine(txt);

// Force write or continue buffering
if ((DateTime.UtcNow - timeFlushConsole).TotalMilliseconds > 500)
{
Expand Down
Loading

0 comments on commit 4b539ec

Please sign in to comment.