From 0f00baae39e569c7df54b3914eaadd68d55072cc Mon Sep 17 00:00:00 2001 From: Nabile Rahmani Date: Fri, 15 Jun 2018 05:20:57 +0200 Subject: [PATCH] - Do not check versions when switching streams. - Manually restore execute bit on Unix. - Use full paths everywhere instead of relying on current working directory. - Less early returns on update operations. - Formatting. --- DotN64.Desktop/Program.cs | 27 ++++++++++++++++----------- DotN64.Desktop/Updater.cs | 28 +++++++++++++++++----------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/DotN64.Desktop/Program.cs b/DotN64.Desktop/Program.cs index fbb8d42..0a5eb04 100644 --- a/DotN64.Desktop/Program.cs +++ b/DotN64.Desktop/Program.cs @@ -24,7 +24,7 @@ namespace DotN64.Desktop } } - public static string ReleaseStream { get; } = Assembly.GetExecutingAssembly().GetCustomAttribute().Stream; + public static string ReleaseStream => Assembly.GetEntryAssembly().GetCustomAttribute().Stream; public static Uri Website { get; } = new Uri("https://nabile.duckdns.org/DotN64"); #endregion @@ -32,7 +32,6 @@ namespace DotN64.Desktop #region Methods private static void Main(string[] args) { - Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; var options = new Options(); for (int i = 0; i < args.Length; i++) @@ -65,16 +64,19 @@ namespace DotN64.Desktop return; case "stream": case "s": - Update(args[++i]); - return; + arg = args[++i]; + + Update(arg, arg != ReleaseStream); + break; default: Update(); - return; + break; } + break; case "--repair": case "-r": Repair(); - return; + break; case "--help": case "-h": ShowHelp(); @@ -93,6 +95,8 @@ namespace DotN64.Desktop private static bool Check(string releaseStream = null) { + Console.Write("Checking for updates... "); + var newVersion = Updater.Check(releaseStream); if (newVersion == null) @@ -148,23 +152,24 @@ namespace DotN64.Desktop var version = assembly.GetName().Version; var description = assembly.GetCustomAttribute().Description; - Console.WriteLine($"{title} v{version} ({BuildDate}){(description != null ? $": {description}" : string.Empty)} ({Website})"); + Console.WriteLine($"{title} v{version} ({BuildDate}){(description != null ? $": {description}" : string.Empty)}"); + Console.WriteLine(Website); } private static void ShowHelp() { ShowInfo(); Console.WriteLine(); - Console.WriteLine($"Usage: {Path.GetFileName(Assembly.GetEntryAssembly().Location)} [Options] "); + Console.WriteLine($"Usage: {Path.GetFileName(Assembly.GetEntryAssembly().Location)} [Options] [ROM image]"); Console.WriteLine(); Console.WriteLine("ROM image: Opens the file as a game cartridge."); Console.WriteLine("Options:"); Console.WriteLine("\t--pif-rom : Loads the PIF's boot ROM into the machine."); Console.WriteLine("\t-d, --debug: Launches the debugger for the Nintendo 64's CPU."); Console.WriteLine("\t-u, --update [action]: Updates the program."); - Console.WriteLine("\t[action = 'check', 'c']: Checks for a new update."); - Console.WriteLine("\t[action = 'list', 'l']: Lists the release streams available for download."); - Console.WriteLine("\t[action = 'stream', 's'] : Downloads an update from the specified release stream."); + Console.WriteLine("\t\t[action = 'check', 'c']: Checks for a new update."); + Console.WriteLine("\t\t[action = 'list', 'l']: Lists the release streams available for download."); + Console.WriteLine("\t\t[action = 'stream', 's'] : Downloads an update from the specified release stream."); Console.WriteLine("\t-r, --repair: Repairs the installation by redownloading the full program."); Console.WriteLine("\t-h, --help: Shows this help."); } diff --git a/DotN64.Desktop/Updater.cs b/DotN64.Desktop/Updater.cs index 2bb6783..16faec9 100644 --- a/DotN64.Desktop/Updater.cs +++ b/DotN64.Desktop/Updater.cs @@ -50,7 +50,7 @@ namespace DotN64.Desktop using (var client = new WebClient()) { var remoteVersion = new Version(client.DownloadString(new Uri(Program.Website, $"{ProjectName}/Download/{releaseStream}/version"))); - var currentVersion = Assembly.GetExecutingAssembly().GetName().Version; + var currentVersion = Assembly.GetEntryAssembly().GetName().Version; return remoteVersion > currentVersion ? remoteVersion : null; } @@ -97,19 +97,24 @@ namespace DotN64.Desktop { var updateDirectory = new DirectoryInfo(Path.Combine(InstallDirectory, StagingDirectory)); var executableName = Path.GetFileName(Assembly.GetEntryAssembly().Location); - var isWindow = Environment.OSVersion.Platform == PlatformID.Win32NT; - var shouldRestart = false; + var platform = Environment.OSVersion.Platform; foreach (var file in updateDirectory.GetFiles()) { var destination = Path.Combine(InstallDirectory, file.Name); - if (isWindow && file.Name == executableName) + if (file.Name == executableName) { - file.MoveTo(destination + NewFileExtension); - - shouldRestart = true; - continue; + switch (platform) + { + case PlatformID.Unix: + case PlatformID.MacOSX: + Process.Start(new ProcessStartInfo("chmod", $"+x {file.FullName}")).WaitForExit(); // ZipArchive does not preserve permission bits, so we fix it. + break; + case PlatformID.Win32NT: + file.MoveTo(destination + NewFileExtension); + continue; + } } File.Delete(destination); @@ -118,14 +123,14 @@ namespace DotN64.Desktop updateDirectory.Delete(true); - if (isWindow && shouldRestart) + if (platform == PlatformID.Win32NT) ApplyWindowsUpdate(executableName); } private static void ApplyWindowsUpdate(string executableName) { var newExecutableName = executableName + NewFileExtension; - var scriptFile = new FileInfo("CompleteUpdate.cmd"); + var scriptFile = new FileInfo(Path.Combine(InstallDirectory, "CompleteUpdate.cmd")); var processID = Process.GetCurrentProcess().Id; using (var writer = scriptFile.CreateText()) @@ -149,7 +154,8 @@ namespace DotN64.Desktop Process.Start(new ProcessStartInfo(scriptFile.FullName) { UseShellExecute = false, - CreateNoWindow = true + CreateNoWindow = true, + WorkingDirectory = InstallDirectory }); Environment.Exit(0); }