public static void RegisterUninstallInfo(XmlData xmlData)
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                return;
            }

            using RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + xmlData.Id);
            key.SetValue("DisplayName", xmlData.Name);
            key.SetValue("ApplicationVersion", xmlData.Version.ToString());
            key.SetValue("DisplayIcon", Path2.InstalledExe(xmlData));
            key.SetValue("DisplayVersion", xmlData.Version.ToString());
            key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd"));
            key.SetValue("UninstallString", $"\"{Path2.SelfUpdatingExe}\" uninstall --app-id {xmlData.Id}");
        }
        private async void frmInstaller_Load(object sender, EventArgs e)
        {
            try
            {
                using var mre = new ManualResetEvent(false);
                IProgress <ProgressData> prog = new Progress <ProgressData>(p =>
                {
                    lblStatus.Text   = p.Status;
                    pbProgress.Value = p.Percent;
                    if (p.Done)
                    {
                        mre.Set();
                    }
                });

                if (_processId > 0)
                {
                    await Task.Run(() =>
                    {
                        prog.Report(new ProgressData("Waiting for previous app to close"));
                        try { Process.GetProcessById(_processId).WaitForExit(); }
                        catch { }
                    });
                }

                var ret = await Installer.InstallAsync(_packageFile, prog, _createShortcuts);

                await mre.WaitOneAsync();

                if (!ret.Success)
                {
                    throw ret.Error;
                }

                string exePath = Path2.InstalledExe(XmlData.Read(Path2.LocalManifest(ret.Id)));
                if (string.IsNullOrWhiteSpace(_relaunchArgs))
                {
                    Process.Start(exePath);
                }
                else
                {
                    string args = Encoding.UTF8.GetString(Convert.FromBase64String(_relaunchArgs));
                    Process.Start(exePath, args);
                }

                ErrorCode = 0;
            }
            catch (AggregateException ex)
            {
                Program.ShowErrors(ex.InnerExceptions);
                if (ex.HResult != 0)
                {
                    ErrorCode = ex.HResult;
                }
            }
            catch (Exception ex)
            {
                Program.ShowErrors(new Exception[] { ex });
                if (ex.HResult != 0)
                {
                    ErrorCode = ex.HResult;
                }
            }

            Close();
        }
        internal static async Task <InstallResult> InstallAsync(string url, IProgress <ProgressData> progress, bool createShortcuts)
        {
            InstallResult ret = new InstallResult {
                Success = true
            };

            if (Debugger.IsAttached)
            {
                return(ret);
            }

#if DEBUG
            return(ret);
#endif

            try
            {
                //Read the package
                progress?.Report(new ProgressData("Reading package"));
                XmlData serverData = await XmlData.ReadAsync(url).ConfigureAwait(false);

                ret.Id      = serverData.Id;
                ret.Version = serverData.Version;

                //If installed, check installed version
                try
                {
                    var localVersion = GetInstalledVersion(ret.Id);
                    if (localVersion >= serverData.Version)
                    {
                        return(ret);
                    }
                }
                catch { }

                //Install!
                progress?.Report(new ProgressData($"Preparing to install v{serverData.Version}"));

                //Unzip from the web
                string status = $"Installing v{serverData.Version}";
                string zipSrc = Path2.DepoPackage(serverData);
                if (StreamHelper.IsWebUrl(zipSrc))
                {
                    using var response = await StreamHelper.GetWebResponseAsync(zipSrc).ConfigureAwait(false);

                    using var source = response.GetResponseStream();
                    await UnzipPackage(source, Path2.InstalledDirectory(serverData.Id), status, progress).ConfigureAwait(false);
                }
                else
                {
                    using var source = StreamHelper.OpenAsyncRead(zipSrc);
                    await UnzipPackage(source, Path2.InstalledDirectory(serverData.Id), status, progress).ConfigureAwait(false);
                }



                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    RegistryEntries.RegisterUninstallInfo(serverData);

                    //Create desktop shortcut
                    string shortcutFile = Path2.DesktopLinkPath(serverData.Name);
                    if (createShortcuts || File.Exists(shortcutFile))
                    {
                        Shortcut.Create(shortcutFile, Path2.InstalledExe(serverData));
                    }
                }
                else
                {
                    Process.Start("chmod", $"+x \"{Path2.InstalledExe(serverData)}\"");
                }

                //Success
                serverData.Save(Path2.LocalManifest(serverData.Id));
                progress?.Report(new ProgressData("Done", 100, true));
            }
            catch (Exception ex)
            {
                ret.Success = false;
                ret.Error   = ex;
            }

            return(ret);
        }
Exemple #4
0
        static int Main(string[] args)
        {
            int ret = 0;

            try
            {
                int    cursorTop  = -1;
                int    cursorLeft = -1;
                int    maxLen     = 0;
                string lastStatus = null;
                try
                {
                    cursorTop  = Console.CursorTop;
                    cursorLeft = Console.CursorLeft;
                }
                catch { }

                ManualResetEvent         mre  = new ManualResetEvent(false);
                IProgress <ProgressData> prog = new Progress <ProgressData>(p =>
                {
                    string status = p.Status;
                    if (p.Percent > 0)
                    {
                        status += $" {p.Percent}%";
                    }
                    if (maxLen > status.Length)
                    {
                        status += new string(' ', maxLen - status.Length);
                    }

                    if (status != lastStatus)
                    {
                        bool setPosWorked = false;
                        try
                        {
                            Console.SetCursorPosition(cursorLeft, cursorTop);
                            setPosWorked = true;
                        }
                        catch { }
                        Console.WriteLine(status);
                        lastStatus = status;
                        if (setPosWorked)
                        {
                            maxLen = Math.Max(maxLen, lastStatus.Length);
                        }
                    }

                    if (p.Done)
                    {
                        mre.Set();
                    }
                });

                var parsed = Parser.Default.ParseArguments <CLOptions.BuildOptions, CLOptions.InstallMeOptions, CLOptions.InstallOptions, CLOptions.UpdateOptions, CLOptions.UninstallOptions>(args);


                parsed.WithParsed <CLOptions.BuildOptions>(opts =>
                {
                    Packager.BuildPackageAsync(opts, prog).Wait();
                    mre.WaitOne();
                });


                parsed.WithParsed <CLOptions.InstallOptions>(opts =>
                {
                    var installed = Installer.InstallAsync(opts.Package, prog, true).Result;
                    mre.WaitOne();
                    if (!installed.Success)
                    {
                        throw installed.Error;
                    }
                    var installedData = XmlData.Read(Path2.LocalManifest(installed.Id));
                    Process.Start(Path2.InstalledExe(installedData));
                });


                parsed.WithParsed <CLOptions.UpdateOptions>(opts =>
                {
                    if (opts.ProcessId > 0)
                    {
                        try { Process.GetProcessById(opts.ProcessId).WaitForExit(); }
                        catch { }
                    }

                    var updated = Installer.InstallAsync(Path2.DepoManifest(XmlData.Read(Path2.LocalManifest(opts.AppId))), prog, false).Result;
                    mre.WaitOne();
                    if (!updated.Success)
                    {
                        throw updated.Error;
                    }
                    string exePath = Path2.InstalledExe(XmlData.Read(Path2.LocalManifest(updated.Id)));
                    if (string.IsNullOrWhiteSpace(opts.RelaunchArgs))
                    {
                        Process.Start(exePath);
                    }
                    else
                    {
                        string relaunchArgs = Encoding.UTF8.GetString(Convert.FromBase64String(opts.RelaunchArgs));
                        Process.Start(exePath, relaunchArgs);
                    }
                });


                parsed.WithParsed <CLOptions.UninstallOptions>(opts =>
                {
                    Uninstaller.UninstallAsync(opts.AppId, prog).Wait();
                    mre.WaitOne();
                });


                parsed.WithParsed <CLOptions.InstallMeOptions>(opts =>
                {
                    if (Manager.InstallMe(opts))
                    {
                        Console.WriteLine("SAUC Installed");
                        if (!opts.NoGui)
                        {
                            Console.ReadLine();
                        }
                    }
                });


                parsed.WithNotParsed <object>(opts =>
                {
                    //Arguments error
                    ret = -1;
                });
            }
            catch (AggregateException ex)
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                foreach (var subEx in ex.InnerExceptions)
                {
                    Console.WriteLine(subEx.Message);
                }
                Console.ResetColor();
                Console.WriteLine();

                ret = ex.HResult == 0 ? -1 : ex.HResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
                Console.WriteLine();

                ret = ex.HResult == 0 ? -1 : ex.HResult;
            }

            return(ret);
        }