From 101ea5843a728d28f5a4cbd72e8edb2aba4051b4 Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Tue, 24 Mar 2020 21:27:41 +0100 Subject: [PATCH] Fix some stuff --- UpTool2/MainForm.cs | 6 ++--- UpToolCLI/Program.cs | 29 ++++++++++++++-------- UpToolCLI/UpToolCLI.csproj | 4 +++ UpToolLib/Tool/AppExtras.cs | 31 +++++++++++++----------- UpToolLib/Tool/AppInstall.cs | 47 +++++++++++++++++++++++++++--------- 5 files changed, 79 insertions(+), 38 deletions(-) diff --git a/UpTool2/MainForm.cs b/UpTool2/MainForm.cs index c257bdc..4a317bf 100644 --- a/UpTool2/MainForm.cs +++ b/UpTool2/MainForm.cs @@ -51,7 +51,7 @@ namespace UpTool2 try { #endif - AppInstall.Install((App) action_install.Tag); + AppInstall.Install((App) action_install.Tag, true); ReloadElements(); trying = false; #if !DEBUG @@ -91,7 +91,7 @@ namespace UpTool2 id = Guid.NewGuid(); App appI = new App(AppNameDialog.Show(), "Locally installed package, removal only", GlobalVariables.minimumVer, "", true, "", id, Color.Red, Resources.C_64.ToBitmap(), false, ""); - AppInstall.InstallZip(searchPackageDialog.FileName, appI); + AppInstall.InstallZip(searchPackageDialog.FileName, appI, true); #if !DEBUG } catch (Exception e1) @@ -173,7 +173,7 @@ namespace UpTool2 { try { - AppExtras.Update((App) action_install.Tag); + AppExtras.Update((App) action_install.Tag, false); } catch (Exception e1) { diff --git a/UpToolCLI/Program.cs b/UpToolCLI/Program.cs index 44c96a8..2ce6061 100644 --- a/UpToolCLI/Program.cs +++ b/UpToolCLI/Program.cs @@ -22,23 +22,26 @@ namespace UpToolCLI Command install = new Command("install", "Install a package") { - Handler = CommandHandler.Create(Install) + Handler = CommandHandler.Create(Install) }; install.AddOption(new Option(new[] { "--identifier", "-i" }, "Something to identify the app")); + install.AddOption(new Option(new[] { "--force", "-f" }, "Overwrites older files")); rootCommand.AddCommand(install); Command upgrade = new Command("upgrade", "Upgrade a package") { - Handler = CommandHandler.Create(Upgrade) + Handler = CommandHandler.Create(Upgrade) }; upgrade.AddOption(new Option(new[] { "--identifier", "-i" }, "Something to identify the app")); + upgrade.AddOption(new Option(new[] { "--force", "-f" }, "Overwrites older files")); rootCommand.AddCommand(upgrade); Command reinstall = new Command("reinstall", "Reinstall a package") { - Handler = CommandHandler.Create(Reinstall) + Handler = CommandHandler.Create(Reinstall) }; reinstall.AddOption(new Option(new[] { "--identifier", "-i" }, "Something to identify the app")); + reinstall.AddOption(new Option(new[] { "--force", "-f" }, "Overwrites older files")); rootCommand.AddCommand(reinstall); Command remove = new Command("remove", "Remove a package") @@ -109,8 +112,9 @@ namespace UpToolCLI foreach (KeyValuePair app in GlobalVariables.Apps.Where(s => (s.Value.status & Status.Updatable) == Status.Updatable)) { Console.WriteLine($"Updating {app.Value.Name}"); - AppExtras.Update(app.Value); + AppExtras.Update(app.Value, false); } + Console.WriteLine("Done!"); } private static void Show(string identifier) @@ -132,7 +136,7 @@ namespace UpToolCLI Console.WriteLine($"{apps[i].Name} ({apps[i].Id}"); } - private static void Upgrade(string identifier) + private static void Upgrade(string identifier, bool force) { RepoManagement.GetReposFromDisk(); App[] apps = AppExtras.FindApps(identifier); @@ -144,14 +148,15 @@ namespace UpToolCLI if ((tmp.status & Status.Updatable) == Status.Updatable) { Console.WriteLine($"Upgrading {tmp.Name}"); - AppExtras.Update(tmp); + AppExtras.Update(tmp, force); } else Console.WriteLine("Package is up-to-date"); } + Console.WriteLine("Done!"); } - private static void Reinstall(string identifier) + private static void Reinstall(string identifier, bool force) { RepoManagement.GetReposFromDisk(); App[] apps = AppExtras.FindApps(identifier); @@ -161,8 +166,9 @@ namespace UpToolCLI { App tmp = apps.First(); Console.WriteLine($"Reinstalling {tmp.Name}"); - AppExtras.Update(tmp); + AppExtras.Update(tmp, force); } + Console.WriteLine("Done!"); } private static void Remove(string identifier) @@ -182,6 +188,7 @@ namespace UpToolCLI else Console.WriteLine("Package is not installed"); } + Console.WriteLine("Done!"); } private static void Purge(string identifier) @@ -201,9 +208,10 @@ namespace UpToolCLI else Console.WriteLine("Package is not installed"); } + Console.WriteLine("Done!"); } - private static void Install(string identifier) + private static void Install(string identifier, bool force) { RepoManagement.GetReposFromDisk(); App[] apps = AppExtras.FindApps(identifier); @@ -217,9 +225,10 @@ namespace UpToolCLI else { Console.WriteLine($"Installing {tmp.Name}"); - AppInstall.Install(tmp); + AppInstall.Install(tmp, true); } } + Console.WriteLine("Done!"); } } } \ No newline at end of file diff --git a/UpToolCLI/UpToolCLI.csproj b/UpToolCLI/UpToolCLI.csproj index 5daf562..db399c3 100644 --- a/UpToolCLI/UpToolCLI.csproj +++ b/UpToolCLI/UpToolCLI.csproj @@ -5,6 +5,10 @@ netcoreapp3.1 uptool + + + ..\UpTool2.ico + diff --git a/UpToolLib/Tool/AppExtras.cs b/UpToolLib/Tool/AppExtras.cs index 35961b3..7624e8d 100644 --- a/UpToolLib/Tool/AppExtras.cs +++ b/UpToolLib/Tool/AppExtras.cs @@ -18,10 +18,10 @@ namespace UpToolLib.Tool WorkingDirectory = app.dataPath }); - public static void Update(App app) + public static void Update(App app, bool overwrite) { - Remove(app, false); - AppInstall.Install(app); + Remove(app, overwrite); + AppInstall.Install(app, overwrite); } public static void Remove(App app, bool deleteAll) @@ -30,22 +30,25 @@ namespace UpToolLib.Tool if (Directory.Exists(tmp)) Directory.Delete(tmp, true); Directory.CreateDirectory(tmp); - ZipFile.ExtractToDirectory(Path.Combine(app.appPath, "package.zip"), tmp); - Process.Start(new ProcessStartInfo + if (File.Exists(Path.Combine(app.appPath, "package.zip"))) { - FileName = "cmd.exe", - Arguments = $"/C \"{Path.Combine(tmp, "Remove.bat")}\"", - WorkingDirectory = Path.Combine(app.appPath, "app"), - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden - }).WaitForExit(); - if (!deleteAll) CheckDirecory(Path.Combine(tmp, "Data"), app.dataPath); - Directory.Delete(tmp, true); + ZipFile.ExtractToDirectory(Path.Combine(app.appPath, "package.zip"), tmp); + Process.Start(new ProcessStartInfo + { + FileName = "cmd.exe", + Arguments = $"/C \"{Path.Combine(tmp, "Remove.bat")}\"", + WorkingDirectory = Path.Combine(app.appPath, "app"), + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden + }).WaitForExit(); + if (!deleteAll) CheckDirecory(Path.Combine(tmp, "Data"), app.dataPath); + Directory.Delete(tmp, true); + } if (File.Exists(app.infoPath)) File.Delete(app.infoPath); if (File.Exists(Path.Combine(app.appPath, "package.zip"))) File.Delete(Path.Combine(app.appPath, "package.zip")); - if (deleteAll || Directory.GetFiles(app.dataPath).Length + Directory.GetDirectories(app.dataPath).Length == 0) + if (deleteAll || (Directory.Exists(app.dataPath) && Directory.GetFiles(app.dataPath).Length + Directory.GetDirectories(app.dataPath).Length == 0)) Directory.Delete(app.appPath, true); } diff --git a/UpToolLib/Tool/AppInstall.cs b/UpToolLib/Tool/AppInstall.cs index b74f45d..775e36d 100644 --- a/UpToolLib/Tool/AppInstall.cs +++ b/UpToolLib/Tool/AppInstall.cs @@ -14,8 +14,8 @@ namespace UpToolLib.Tool /// Install an application /// /// The app to install - /// A method to download files. Input: app file, Outputs: whether the download was successful and the data - public static void Install(App appI) + /// Set to true to overwrite all old data + public static void Install(App appI, bool force) { string app = ""; string tmp = ""; @@ -28,9 +28,17 @@ namespace UpToolLib.Tool if (Directory.Exists(tmp)) Directory.Delete(tmp, true); Directory.CreateDirectory(tmp); - if (Directory.Exists(app)) - Directory.Delete(app, true); - Directory.CreateDirectory(app); + if (force) + { + if (Directory.Exists(app)) + Directory.Delete(app, true); + Directory.CreateDirectory(app); + } + else + { + if (!Directory.Exists(app)) + Directory.CreateDirectory(app); + } (bool dlSuccess, byte[] dlData) = ExternalFunctionalityManager.instance.Download(new Uri(appI.File)); if (!dlSuccess) throw new Exception("Download failed"); @@ -44,7 +52,7 @@ Package: {pkgHash} Online: {appI.Hash.ToUpper()}"); } File.WriteAllBytes(Path.Combine(app, "package.zip"), dlData); - CompleteInstall(appI); + CompleteInstall(appI, force); #if !DEBUG } catch @@ -63,7 +71,7 @@ Online: {appI.Hash.ToUpper()}"); #endif } - public static void InstallZip(string zipPath, App meta) + public static void InstallZip(string zipPath, App meta, bool force) { string app = ""; string tmp = ""; @@ -76,7 +84,7 @@ Online: {appI.Hash.ToUpper()}"); Directory.Delete(tmp, true); Directory.CreateDirectory(tmp); File.Copy(zipPath, Path.Combine(app, "package.zip")); - CompleteInstall(meta); + CompleteInstall(meta, force); } catch { @@ -94,13 +102,19 @@ Online: {appI.Hash.ToUpper()}"); //Use //PowerShell -Command "Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('Hello World')" //for message boxes - private static void CompleteInstall(App app) => CompleteInstall(app.appPath, app.Name, app.Description, app.Version, app.MainFile); + private static void CompleteInstall(App app, bool force) => CompleteInstall(app.appPath, app.Name, app.Description, app.Version, app.MainFile, force); - private static void CompleteInstall(string appPath, string name, string description, Version version, string mainFile) + private static void CompleteInstall(string appPath, string name, string description, Version version, string mainFile, bool force) { string tmp = PathTool.tempPath; ZipFile.ExtractToDirectory(Path.Combine(appPath, "package.zip"), tmp); - Directory.Move(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app")); + if (force) + Directory.Move(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app")); + else + { + CopyAll(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app", "Data")); + Directory.Delete(Path.Combine(tmp, "Data"), true); + } XElement el = new XElement("app", new XElement("Name", name), new XElement("Description", description), new XElement("Version", version)); if (mainFile != null) @@ -115,5 +129,16 @@ Online: {appI.Hash.ToUpper()}"); WindowStyle = ProcessWindowStyle.Hidden }).WaitForExit(); } + + private static void CopyAll(string source, string target) + { + if (string.Equals(Path.GetFullPath(source), Path.GetFullPath(target), StringComparison.CurrentCultureIgnoreCase)) + return; + if (!Directory.Exists(target)) + Directory.CreateDirectory(target); + foreach (string file in Directory.GetFiles(source)) File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true); + foreach (string dir in Directory.GetDirectories(source)) + CopyAll(dir, Path.Combine(target, Path.GetFileName(dir))); + } } } \ No newline at end of file