diff --git a/UpToolEto/UpToolEto.Gtk/Program.cs b/UpToolEto/UpToolEto.Gtk/Program.cs index c5f5234..ee4a328 100644 --- a/UpToolEto/UpToolEto.Gtk/Program.cs +++ b/UpToolEto/UpToolEto.Gtk/Program.cs @@ -8,10 +8,10 @@ namespace UpToolEto.Gtk [STAThread] public static void Main(string[] args) { - UpToolEto.Main.Entry(new Application(Eto.Platforms.Gtk), () => + new Main(new Application(Eto.Platforms.Gtk), () => { - }); + }, args).Entry(); } } } \ No newline at end of file diff --git a/UpToolEto/UpToolEto.Wpf/Program.cs b/UpToolEto/UpToolEto.Wpf/Program.cs index 7232248..50229f6 100644 --- a/UpToolEto/UpToolEto.Wpf/Program.cs +++ b/UpToolEto/UpToolEto.Wpf/Program.cs @@ -10,12 +10,12 @@ namespace UpToolEto.Wpf [STAThread] public static void Main(string[] args) { - UpToolEto.Main.Entry(new Application(Eto.Platforms.Wpf), () => + new Main(new Application(Eto.Platforms.Wpf), () => { Process[] processes = Process.GetProcessesByName("UpTool2"); if (processes.Length > 0) BringProcessToFront(processes[0]); - }); + }, args).Entry(); } public static void BringProcessToFront(Process process) diff --git a/UpToolEto/UpToolEto/Controls/UTMenuBar.cs b/UpToolEto/UpToolEto/Controls/UTMenuBar.cs index 058e4a2..9b8fbbc 100644 --- a/UpToolEto/UpToolEto/Controls/UTMenuBar.cs +++ b/UpToolEto/UpToolEto/Controls/UTMenuBar.cs @@ -1,17 +1,18 @@ using Eto.Forms; using UpToolEto.Forms; +using UpToolLib.DataStructures; using UpToolLib.v2; namespace UpToolEto.Controls { public class UTMenuBar : MenuBar { - public UTMenuBar(MainForm mainForm, RepoManagement repoManagement, AppList appList) + public UTMenuBar(MainForm mainForm, RepoManagement repoManagement, AppList appList, IExternalFunctionality platform) { Command reloadRepos = new() {MenuText = "Reload", ToolBarText = "Reload"}; reloadRepos.Executed += (_, _) => { - if (Main.Platform.YesNoDialog("This may take some time. Are you sure?", true)) + if (platform.YesNoDialog("This may take some time. Are you sure?", true)) { repoManagement.FetchRepos(); repoManagement.GetReposFromDisk(); diff --git a/UpToolEto/UpToolEto/Forms/DownloadDialog.cs b/UpToolEto/UpToolEto/Forms/DownloadDialog.cs index 92eee40..3c617a5 100644 --- a/UpToolEto/UpToolEto/Forms/DownloadDialog.cs +++ b/UpToolEto/UpToolEto/Forms/DownloadDialog.cs @@ -1,52 +1,80 @@ using System; using System.Net; +using System.Threading; using Eto.Drawing; using Eto.Forms; +using UpToolLib.DataStructures; namespace UpToolEto.Forms { public class DownloadDialog : Form { - public bool Success = false; + private readonly Uri _url; + private readonly Application _application; + private readonly IExternalFunctionality _platform; + public State CurrentState = State.NotStarted; public byte[] Download = new byte[0]; - public DownloadDialog(Uri url) + private readonly ProgressBar _bar; + public DownloadDialog(Uri url, Application application, IExternalFunctionality platform) { + _url = url; + _application = application; + _platform = platform; Title = "Downloader"; Resizable = false; Maximizable = false; Minimizable = false; WindowStyle = WindowStyle.Utility; - ProgressBar bar = new(); - bar.MaxValue = 100; + _bar = new(); + _bar.MaxValue = 100; Content = new StackLayout { Padding = 10, Items = { "Downloading " + url, - new StackLayoutItem(bar, HorizontalAlignment.Stretch, true) + new StackLayoutItem(_bar, HorizontalAlignment.Stretch, true) } }; Size = new Size(700, 400); - try + } + + public void StartDownload() + { + CurrentState = State.Downloading; + new Thread(() => { - WebClient client = new(); - client.DownloadProgressChanged += (_, args) => + try { - bar.Value = args.ProgressPercentage; - }; - client.DownloadDataCompleted += (_, args) => + WebClient client = new(); + client.DownloadProgressChanged += (_, args) => + { + _platform.Log($"{args.BytesReceived}/{args.TotalBytesToReceive}"); + _application.Invoke(() => _bar.Value = args.ProgressPercentage); + _application.RunIteration(); + }; + client.DownloadDataCompleted += (_, args) => + { + CurrentState = State.Success; + Download = args.Result; + _application.Invoke(Close); + }; + client.DownloadDataAsync(_url, client); + } + catch { - Success = true; - Download = args.Result; - Close(); - }; - client.DownloadDataAsync(url, client); - } - catch - { - Close(); - } + CurrentState = State.Failed; + _application.Invoke(Close); + } + }).Start(); + } + + public enum State + { + NotStarted, + Downloading, + Failed, + Success } } -} \ No newline at end of file +} diff --git a/UpToolEto/UpToolEto/Forms/InitScreen.cs b/UpToolEto/UpToolEto/Forms/InitScreen.cs index d294736..8b28d1e 100644 --- a/UpToolEto/UpToolEto/Forms/InitScreen.cs +++ b/UpToolEto/UpToolEto/Forms/InitScreen.cs @@ -1,16 +1,19 @@ using System; using Eto.Drawing; using Eto.Forms; +using UpToolLib.DataStructures; namespace UpToolEto.Forms { public class InitScreen : Form { private readonly Application _application; + private readonly IExternalFunctionality _platform; private readonly Label _lab; - public InitScreen(Application application) + public InitScreen(Application application, IExternalFunctionality platform) { _application = application; + _platform = platform; Title = "UpTool2 Init"; Resizable = false; Maximizable = false; @@ -35,9 +38,14 @@ namespace UpToolEto.Forms public void SetText(string text) { - Console.WriteLine(text); - _lab.Text = text; - _application.RunIteration(); + _application.Invoke(() => + { + _platform.Log(text); + _lab.Text = text; + _application.RunIteration(); + }); } + + public override void Close() => _application.Invoke(() => base.Close()); } } \ No newline at end of file diff --git a/UpToolEto/UpToolEto/Forms/MainForm.cs b/UpToolEto/UpToolEto/Forms/MainForm.cs index 7470557..283450f 100644 --- a/UpToolEto/UpToolEto/Forms/MainForm.cs +++ b/UpToolEto/UpToolEto/Forms/MainForm.cs @@ -2,6 +2,7 @@ using Eto.Drawing; using Eto.Forms; using UpToolEto.Controls; using UpToolLib; +using UpToolLib.DataStructures; //TODO implement tasks queue UI //TODO keep track of app state @@ -12,7 +13,7 @@ namespace UpToolEto.Forms public partial class MainForm : Form { private readonly AppPanel _appPanel; - public MainForm(InitScreen init, UpToolLibMain lib) + public MainForm(InitScreen init, UpToolLibMain lib, IExternalFunctionality platform, bool online) { Title = "UpTool2"; MinimumSize = new Size(600, 100); @@ -29,7 +30,7 @@ namespace UpToolEto.Forms }, Orientation = Orientation.Horizontal }; - Menu = new UTMenuBar(this, lib.V2.RepoManagement, appList); + Menu = new UTMenuBar(this, lib.V2.RepoManagement, appList, platform); Shown += (_, _) => init.Close(); } } diff --git a/UpToolEto/UpToolEto/Main.cs b/UpToolEto/UpToolEto/Main.cs index b47b716..a481660 100644 --- a/UpToolEto/UpToolEto/Main.cs +++ b/UpToolEto/UpToolEto/Main.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics; using System.IO; using System.IO.Compression; +using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Threading; @@ -18,59 +19,86 @@ namespace UpToolEto { public class Main { - public static bool Online; - public static IExternalFunctionality Platform; - public static void Entry(Application application, Action activityExistsException) + private readonly IExternalFunctionality _platform; + private readonly UpToolLibMain _lib; + private readonly Application _application; + private readonly Action _activityExistsException; + private readonly InitScreen _init; + private readonly bool _skipFetch; + + public Main(Application application, Action activityExistsException, string[] args) + { + _skipFetch = args.Contains("--skip-fetch"); + _platform = new UTLibFunctions(application); + _lib = new UpToolLibMain(_platform); + _application = application; + _activityExistsException = activityExistsException; + _init = new(application, _platform); + } + + public void Entry() + { + new Thread(InitThread).Start(); + _application.Run(_init); + } + + private void InitThread() { - InitScreen init = new(application); - init.Show(); try { - Platform = new UTLibFunctions(); - UpToolLibMain lib = new(Platform); - init.SetText("Initializing paths"); - if (!Directory.Exists(lib.V1.PathTool.Dir)) - Directory.CreateDirectory(lib.V1.PathTool.Dir); - FixXml(lib.V1.XmlTool, lib.V1.PathTool); - init.SetText("Performing checks"); + _init.SetText("Initializing paths"); + if (!Directory.Exists(_lib.V1.PathTool.Dir)) + Directory.CreateDirectory(_lib.V1.PathTool.Dir); + FixXml(_lib.V1.XmlTool, _lib.V1.PathTool); + _init.SetText("Performing checks"); + bool online = false; UpdateCheck updateCheck = null; try { - updateCheck = lib.V2.UpdateChecker.Check(); - Online = true; + updateCheck = _lib.V2.UpdateChecker.Check(); + online = true; } catch { - Online = false; + _platform.Log("Could not perform update check, starting offline"); } - if (Online) + if (online && UpdateCheck(updateCheck, _lib.V1.PathTool, _init)) { - if (UpdateCheck(updateCheck, lib.V1.PathTool, init)) - { - application.Quit(); - return; - } + _platform.Log("Quitting"); + _application.Quit(); + return; } - if (!Directory.Exists(lib.V1.PathTool.GetRelative("Apps"))) - Directory.CreateDirectory(lib.V1.PathTool.GetRelative("Apps")); - /*if (Online) + if (!Directory.Exists(_lib.V1.PathTool.GetRelative("Apps"))) + Directory.CreateDirectory(_lib.V1.PathTool.GetRelative("Apps")); + if (!_skipFetch && online) { - init.SetText("Fetching repos"); - lib.V2.RepoManagement.FetchRepos(); - }*/ - lib.V2.RepoManagement.GetReposFromDisk(); - init.SetText("Opening"); - application.Run(new MainForm(init, lib)); + _init.SetText("Fetching repos"); + _lib.V2.RepoManagement.FetchRepos(); + } + _lib.V2.RepoManagement.GetReposFromDisk(); + _init.SetText("Opening"); + _application.Invoke(() => _application.Run(new MainForm(_init, _lib, _platform, online))); } catch (MutexLockLockedException) { - init.Close(); - MessageBox.Show("Mutex property of other process, quitting"); - activityExistsException(); + _application.Invoke(() => + { + _init.Close(); + _platform.OkDialog("Mutex property of other process, quitting"); + _activityExistsException(); + }); + } + catch (Exception e) + { + _platform.Log(e.ToString()); + } + finally + { + _lib?.Dispose(); } } - public static void FixXml(XmlTool xmlTool, PathTool pathTool, bool throwOnError = false) + public void FixXml(XmlTool xmlTool, PathTool pathTool, bool throwOnError = false) { try { @@ -79,20 +107,20 @@ namespace UpToolEto catch (XmlException) { if (throwOnError) throw; - MessageBox.Show("Something went wrong while trying to parse XML. Retrying..."); + _platform.OkDialog("Something went wrong while trying to parse XML. Retrying..."); File.Delete(pathTool.InfoXml); FixXml(xmlTool, pathTool); } } - private static bool UpdateCheck(UpdateCheck updateCheck, PathTool pathTool, InitScreen init) + private bool UpdateCheck(UpdateCheck updateCheck, PathTool pathTool, InitScreen init) { init.SetText("Comparing online version"); if (Assembly.GetExecutingAssembly().GetName().Version >= updateCheck.OnlineVersion) return false; if (PlatformCheck.IsWindows) { init.SetText("Downloading latest"); - (bool success, byte[] dl) = Platform.Download(updateCheck.Installer); + (bool success, byte[] dl) = _platform.Download(updateCheck.Installer); if (!success) throw new Exception("Failed to update"); init.SetText("Verifying"); @@ -125,9 +153,9 @@ namespace UpToolEto } else { - MessageBox.Show("A new version is available. Please install it"); + _platform.OkDialog("A new version is available. Please install it"); return false; } } } -} \ No newline at end of file +} diff --git a/UpToolEto/UpToolEto/UTLibFunctions.cs b/UpToolEto/UpToolEto/UTLibFunctions.cs index 6d483ed..9c5106b 100644 --- a/UpToolEto/UpToolEto/UTLibFunctions.cs +++ b/UpToolEto/UpToolEto/UTLibFunctions.cs @@ -12,13 +12,17 @@ namespace UpToolEto { public class UTLibFunctions : IExternalFunctionality { + private readonly Application _application; private const int ImgSize = 32; public Tuple Download(Uri link) { - DownloadDialog dlg = new(link); - dlg.Show(); - while (dlg.Visible) Thread.Sleep(20); - return new Tuple(dlg.Success, dlg.Download); + DownloadDialog dlg = new(link, _application, this); + _application.AsyncInvoke(() => dlg.Show()); + dlg.StartDownload(); + Log("Downloading " + link); + while (dlg.CurrentState == DownloadDialog.State.Downloading) Thread.Sleep(20); + Log("Download complete"); + return new Tuple(dlg.CurrentState == DownloadDialog.State.Success, dlg.Download); } public string FetchImageB64(Uri link) @@ -34,10 +38,11 @@ namespace UpToolEto } public bool YesNoDialog(string text, bool defaultVal) => - MessageBox.Show(text, MessageBoxButtons.YesNo, - defaultButton: defaultVal ? MessageBoxDefaultButton.Yes : MessageBoxDefaultButton.No) == DialogResult.Yes; + _application.Invoke(() => MessageBox.Show(text, MessageBoxButtons.YesNo, + defaultButton: defaultVal ? MessageBoxDefaultButton.Yes : MessageBoxDefaultButton.No)) == + DialogResult.Yes; - public void OkDialog(string text) => MessageBox.Show(text); + public void OkDialog(string text) => _application.Invoke(() => MessageBox.Show(text)); public object GetDefaultIcon() => Bitmap.FromResource("UpToolLib.C_64.ico", typeof(UpToolLibMain)).WithSize(ImgSize, ImgSize); @@ -45,7 +50,10 @@ namespace UpToolEto public void Log(string text) { + Console.WriteLine(text); //TODO implement visual logging } + + public UTLibFunctions(Application application) => _application = application; } } \ No newline at end of file