From 28a5d3c3f7c50406d9dc9034798e07a7fd361122 Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Tue, 12 Nov 2019 21:23:14 +0100 Subject: [PATCH] Fixes, semantic Versions for apps --- ToDo and Notes.txt | 2 -- UpTool2/App.cs | 18 ++++++------------ UpTool2/AppInstall.cs | 16 +++++++++++----- UpTool2/GlobalVariables.cs | 1 + UpTool2/MainForm.cs | 15 +++++++++------ UpTool2/Program.cs | 2 +- UpTool2/RepoManagement.cs | 12 ++++++++---- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/ToDo and Notes.txt b/ToDo and Notes.txt index bf56fd4..8478973 100644 --- a/ToDo and Notes.txt +++ b/ToDo and Notes.txt @@ -1,8 +1,6 @@ More Icons for Apps More apps: Laptop Sim (when done) Automatically push Meta.xml changes after building -Support semantic versioning for app updates (App object versions will have to be converted) -Use binary version for Updates Less code in MainForm.cs! diff --git a/UpTool2/App.cs b/UpTool2/App.cs index 2a293a4..a76cd3e 100644 --- a/UpTool2/App.cs +++ b/UpTool2/App.cs @@ -11,7 +11,7 @@ namespace UpTool2 { public string name; public string description; - public int version; + public Version version; public string file; public bool local; public string hash; @@ -21,7 +21,7 @@ namespace UpTool2 public bool runnable; public string mainFile; - public App(string name, string description, int version, string file, bool local, string hash, Guid iD, Color color, Image icon, bool runnable, string mainFile) + public App(string name, string description, Version version, string file, bool local, string hash, Guid iD, Color color, Image icon, bool runnable, string mainFile) { this.name = name ?? throw new ArgumentNullException(nameof(name)); this.description = description ?? throw new ArgumentNullException(nameof(description)); @@ -34,24 +34,18 @@ namespace UpTool2 this.icon = icon ?? throw new ArgumentNullException(nameof(icon)); this.runnable = runnable; this.mainFile = mainFile ?? throw new ArgumentNullException(nameof(mainFile)); -#if DEBUG - Console.WriteLine(";" + mainFile + ";" + this.mainFile); -#endif } public Status status { get { - string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\UpTool2"; - string xml = dir + @"\Apps\" + ID.ToString() + @"\info.xml"; + string xml = GlobalVariables.getInfoPath(ID); if (File.Exists(xml)) { - if (int.Parse(XDocument.Load(xml).Element("app").Element("Version").Value) < version) - return Status.Updatable; + if (Version.TryParse(XDocument.Load(xml).Element("app").Element("Version").Value, out Version ver) && ver >= version) + return local ? (Status.Installed | Status.Local) : Status.Installed; else - { - return local ? Status.Installed | Status.Local : Status.Installed; - } + return Status.Installed | Status.Updatable; } else return Status.Not_Installed; diff --git a/UpTool2/AppInstall.cs b/UpTool2/AppInstall.cs index 620ca14..a0e1f33 100644 --- a/UpTool2/AppInstall.cs +++ b/UpTool2/AppInstall.cs @@ -19,13 +19,15 @@ namespace UpTool2 { string app = ""; string tmp = ""; +#if !DEBUG try { +#endif app = appI.appPath; tmp = GlobalVariables.dir + @"\tmp"; - if (Directory.Exists("")) - Directory.Delete("", true); - Directory.CreateDirectory(""); + if (Directory.Exists(tmp)) + Directory.Delete(tmp, true); + Directory.CreateDirectory(tmp); if (Directory.Exists(app)) Directory.Delete(app, true); Directory.CreateDirectory(app); @@ -38,6 +40,7 @@ namespace UpTool2 throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkghash + "\r\nOnline: " + appI.hash.ToUpper()); } completeInstall(appI); +#if !DEBUG } catch { @@ -47,9 +50,12 @@ namespace UpTool2 } finally { +#endif if (tmp != "" && Directory.Exists(tmp)) - Directory.Delete(tmp, true); + Directory.Delete(tmp, true); +#if !DEBUG } +#endif } public static void installZip(string zipPath, App meta) @@ -82,7 +88,7 @@ namespace UpTool2 static void completeInstall(App app) => completeInstall(app.appPath, app.name, app.description, app.version, app.mainFile); - static void completeInstall(string appPath, string name, string description, int version, string mainFile) + static void completeInstall(string appPath, string name, string description, Version version, string mainFile) { #if !DEBUG try diff --git a/UpTool2/GlobalVariables.cs b/UpTool2/GlobalVariables.cs index fe22c9b..24c35cd 100644 --- a/UpTool2/GlobalVariables.cs +++ b/UpTool2/GlobalVariables.cs @@ -18,5 +18,6 @@ namespace UpTool2 public static string getInfoPath(Guid app) => getAppPath(app) + "\\info.xml"; public static bool relE = true; public static Action reloadElements; + public static Version minimumVer => Version.Parse("0.0.0.0"); } } diff --git a/UpTool2/MainForm.cs b/UpTool2/MainForm.cs index 927cb6f..624c264 100644 --- a/UpTool2/MainForm.cs +++ b/UpTool2/MainForm.cs @@ -2,11 +2,14 @@ using System.Drawing; using System.Windows.Forms; using UpTool2.Properties; -using System.Xml.Linq; using System.IO; using System.Diagnostics; using System.IO.Compression; using Microsoft.VisualBasic; +#if DEBUG +using System.Threading; +using System.Linq; +#endif namespace UpTool2 { @@ -63,7 +66,7 @@ namespace UpTool2 Guid ID = Guid.NewGuid(); while (GlobalVariables.apps.ContainsKey(ID) || Directory.Exists(GlobalVariables.getAppPath(ID))) ID = Guid.NewGuid(); - App appI = new App(Interaction.InputBox("Name:"), "Locally installed package, removal only", -1, "", true, "", ID, Color.Red, Resources.C_64.ToBitmap(), false, ""); + App appI = new App(Interaction.InputBox("Name:"), "Locally installed package, removal only", GlobalVariables.minimumVer, "", true, "", ID, Color.Red, Resources.C_64.ToBitmap(), false, ""); AppInstall.installZip(searchPackageDialog.FileName, appI); } #if !DEBUG @@ -111,6 +114,7 @@ namespace UpTool2 sidebarIcon.Size = new Size(70, 70); sidebarIcon.BackgroundImage = app.icon; sidebarIcon.BackgroundImageLayout = ImageLayout.Stretch; + bool updatable = (!app.local) && ((app.status & Status.Updatable) == Status.Updatable); sidebarIcon.Click += (object sender, EventArgs e) => { infoPanel_Title.Text = app.name; @@ -121,12 +125,11 @@ namespace UpTool2 action_remove.Tag = app; action_remove.Enabled = Directory.Exists(app.appPath); action_update.Tag = app; - string ver = XDocument.Load(app.infoPath).Element("app").Element("Version").Value; - action_update.Enabled = (!app.local) && File.Exists(app.infoPath) && int.Parse(ver) < app.version; + action_update.Enabled = updatable; action_run.Tag = app; action_run.Enabled = (!app.local) && app.runnable && Directory.Exists(app.appPath); }; - if ((!app.local) && File.Exists(app.infoPath) && int.Parse(XDocument.Load(app.infoPath).Element("app").Element("Version").Value) < app.version) + if (updatable) availableUpdates++; toolTip.SetToolTip(sidebarIcon, app.name); sidebarPanel.Controls.Add(sidebarIcon); @@ -185,7 +188,7 @@ namespace UpTool2 { searchBox.Text = "!DEBUG:PRINT"; string _tmp_file = Path.GetTempFileName(); - File.WriteAllText(_tmp_file, string.Join("\r\n\r\n", apps.Select(app => app.Value).Select(app => app.ToString()).ToArray())); + File.WriteAllText(_tmp_file, string.Join("\r\n\r\n", GlobalVariables.apps.Values.Select(app => app.ToString()).ToArray())); new Thread(() => { Process.Start("notepad", _tmp_file).WaitForExit(); File.Delete(_tmp_file); diff --git a/UpTool2/Program.cs b/UpTool2/Program.cs index 8a55fef..abcd6bc 100644 --- a/UpTool2/Program.cs +++ b/UpTool2/Program.cs @@ -146,7 +146,7 @@ namespace UpTool2 if (int.TryParse(ver, out int version)) updatable = int.Parse(XDocument.Load(xml).Element("meta").Element("Version").Value) < version; else - updatable = Assembly.GetExecutingAssembly().GetName().Version.CompareTo(Version.Parse(ver)) < 0; + updatable = Assembly.GetExecutingAssembly().GetName().Version < Version.Parse(ver); if (updatable) { using (DownloadDialog dlg = new DownloadDialog(meta.Element("File").Value, dir + @"\update.tmp")) diff --git a/UpTool2/RepoManagement.cs b/UpTool2/RepoManagement.cs index e2ff53a..fc74809 100644 --- a/UpTool2/RepoManagement.cs +++ b/UpTool2/RepoManagement.cs @@ -37,7 +37,7 @@ namespace UpTool2 repArr.AddRange(repo.Element("repo").Elements("repolink").Select(s => s.Value)); XElement[] tmp_apparray = repo.Element("repo").Elements("app").Where(app => tmp_apps_list.Where(a => a.Element("ID").Value == app.Element("ID").Value).Count() == 0 || tmp_apps_list.Where(a => a.Element("ID").Value == app.Element("ID").Value) - .Where(a => int.Parse(a.Element("Version").Value) >= int.Parse(app.Element("Version").Value)).Count() == 0).ToArray() + .Where(a => a.Element("Version").getVer() >= app.Element("Version").getVer()).Count() == 0).ToArray() .Concat(repo.Element("repo").Elements("applink").Select(s => XDocument.Load(s.Value).Element("app"))).ToArray(); for (int i1 = 0; i1 < tmp_apparray.Length; i1++) { @@ -108,6 +108,8 @@ namespace UpTool2 meta.Save(xml); } + static Version getVer(this XElement el) => int.TryParse(el.Value, out int i) ? new Version(0, 0, 0, i) : Version.Parse(el.Value); + public static void getReposFromDisk() { GlobalVariables.apps.Clear(); @@ -117,10 +119,12 @@ namespace UpTool2 Guid id = Guid.Parse(app.Element("ID").Value); string locInPath = GlobalVariables.getInfoPath(id); XElement locIn = File.Exists(locInPath) ? XDocument.Load(locInPath).Element("app") : app; + if (int.TryParse(app.Element("Version").Value, out int iputareallylongnameheresoiwillnotuseitlaterbyaccident)) + app.Element("Version").Value = GlobalVariables.minimumVer.ToString(); GlobalVariables.apps.Add(id, new App( name: locIn.Element("Name").Value, description: locIn.Element("Description").Value, - version: int.Parse(app.Element("Version").Value), + version: Version.Parse(app.Element("Version").Value), file: app.Element("File").Value, local: false, hash: app.Element("Hash").Value, @@ -132,7 +136,7 @@ namespace UpTool2 )); #if DEBUG Console.WriteLine(locIn.Element("MainFile") == null ? "NULL" : locIn.Element("MainFile").Value); - Console.WriteLine(apps[id].mainFile); + Console.WriteLine(GlobalVariables.apps[id].mainFile); #endif }); Directory.GetDirectories(GlobalVariables.dir + @"\Apps\").Where(s => !GlobalVariables.apps.ContainsKey(Guid.Parse(Path.GetFileName(s)))).ToList().ForEach(s => @@ -141,7 +145,7 @@ namespace UpTool2 try { XElement data = XDocument.Load(GlobalVariables.getInfoPath(tmp)).Element("app"); - GlobalVariables.apps.Add(tmp, new App("(local) " + data.Element("Name").Value, data.Element("Description").Value, -1, "", true, "", tmp, Color.Red, Resources.C_64.ToBitmap(), data.Element("MainFile") != null, data.Element("MainFile") == null ? "" : data.Element("MainFile").Value)); + GlobalVariables.apps.Add(tmp, new App("(local) " + data.Element("Name").Value, data.Element("Description").Value, GlobalVariables.minimumVer, "", true, "", tmp, Color.Red, Resources.C_64.ToBitmap(), data.Element("MainFile") != null, data.Element("MainFile") == null ? "" : data.Element("MainFile").Value)); } catch (Exception e) {