Beispiel #1
0
        internal static async Task <Version> CheckAndUpdateProgram(bool updateOverride = false)
        {
            if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.None)
            {
                return(null);
            }

            string assemblyFile = Assembly.GetEntryAssembly().Location;

            if (string.IsNullOrEmpty(assemblyFile))
            {
                ArchiLogger.LogNullError(nameof(assemblyFile));
                return(null);
            }

            string targetDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string versionFile     = Path.Combine(targetDirectory, SharedInfo.VersionFile);

            if (!File.Exists(versionFile))
            {
                ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsEmpty, versionFile));
                return(null);
            }

            string version;

            try {
                version = await File.ReadAllTextAsync(versionFile).ConfigureAwait(false);
            } catch (Exception e) {
                ArchiLogger.LogGenericException(e);
                return(null);
            }

            if (string.IsNullOrEmpty(version))
            {
                ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, versionFile));
                return(null);
            }

            version = version.TrimEnd();
            if (string.IsNullOrEmpty(version) || !IsValidVersion(version))
            {
                ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, versionFile));
                return(null);
            }

            if (version.Equals(DefaultVersion))
            {
                return(null);
            }

            if ((AutoUpdatesTimer == null) && Program.GlobalConfig.AutoUpdates)
            {
                TimeSpan autoUpdatePeriod = TimeSpan.FromHours(AutoUpdatePeriodInHours);

                AutoUpdatesTimer = new Timer(
                    async e => await CheckAndUpdateProgram().ConfigureAwait(false),
                    null,
                    autoUpdatePeriod,                    // Delay
                    autoUpdatePeriod                     // Period
                    );

                ArchiLogger.LogGenericInfo(string.Format(Strings.AutoUpdateCheckInfo, autoUpdatePeriod.ToHumanReadable()));
            }

            ArchiLogger.LogGenericInfo(Strings.UpdateCheckingNewVersion);

            // Cleanup from previous update - update directory for old in-use runtime files
            string backupDirectory = Path.Combine(targetDirectory, SharedInfo.UpdateDirectory);

            if (Directory.Exists(backupDirectory))
            {
                // It's entirely possible that old process is still running, wait a short moment for eventual cleanup
                await Task.Delay(5000).ConfigureAwait(false);

                try {
                    Directory.Delete(backupDirectory, true);
                } catch (Exception e) {
                    ArchiLogger.LogGenericException(e);
                    return(null);
                }
            }

            // Cleanup from previous update - old non-runtime in-use files
            foreach (string file in Directory.EnumerateFiles(targetDirectory, "*.old", SearchOption.AllDirectories))
            {
                try {
                    File.Delete(file);
                } catch (Exception e) {
                    ArchiLogger.LogGenericException(e);
                    return(null);
                }
            }

            string releaseURL = SharedInfo.GithubReleaseURL + (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable ? "/latest" : "?per_page=1");

            GitHub.ReleaseResponse releaseResponse;

            if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable)
            {
                releaseResponse = await Program.WebBrowser.UrlGetToJsonResultRetry <GitHub.ReleaseResponse>(releaseURL).ConfigureAwait(false);

                if (releaseResponse == null)
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                    return(null);
                }
            }
            else
            {
                List <GitHub.ReleaseResponse> releases = await Program.WebBrowser.UrlGetToJsonResultRetry <List <GitHub.ReleaseResponse> >(releaseURL).ConfigureAwait(false);

                if ((releases == null) || (releases.Count == 0))
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                    return(null);
                }

                releaseResponse = releases[0];
            }

            if (string.IsNullOrEmpty(releaseResponse.Tag))
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                return(null);
            }

            Version newVersion = new Version(releaseResponse.Tag);

            ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateVersionInfo, SharedInfo.Version, newVersion));

            if (SharedInfo.Version == newVersion)
            {
                return(SharedInfo.Version);
            }

            if (SharedInfo.Version > newVersion)
            {
                ArchiLogger.LogGenericWarning(Strings.WarningPreReleaseVersion);
                await Task.Delay(15 * 1000).ConfigureAwait(false);

                return(SharedInfo.Version);
            }

            if (!updateOverride && !Program.GlobalConfig.AutoUpdates)
            {
                ArchiLogger.LogGenericInfo(Strings.UpdateNewVersionAvailable);
                await Task.Delay(5000).ConfigureAwait(false);

                return(null);
            }

            // Auto update logic starts here
            if (releaseResponse.Assets == null)
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssets);
                return(null);
            }

            string targetFile = SharedInfo.ASF + "-" + version + ".zip";

            GitHub.ReleaseResponse.Asset binaryAsset = releaseResponse.Assets.FirstOrDefault(asset => asset.Name.Equals(targetFile, StringComparison.OrdinalIgnoreCase));

            if (binaryAsset == null)
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssetForThisVersion);
                return(null);
            }

            if (string.IsNullOrEmpty(binaryAsset.DownloadURL))
            {
                ArchiLogger.LogNullError(nameof(binaryAsset.DownloadURL));
                return(null);
            }

            ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateDownloadingNewVersion, newVersion, binaryAsset.Size / 1024 / 1024));

            byte[] result = await Program.WebBrowser.UrlGetToBytesRetry(binaryAsset.DownloadURL).ConfigureAwait(false);

            if (result == null)
            {
                return(null);
            }

            try {
                using (ZipArchive zipArchive = new ZipArchive(new MemoryStream(result))) {
                    UpdateFromArchive(zipArchive, targetDirectory);
                }
            } catch (Exception e) {
                ArchiLogger.LogGenericException(e);
                return(null);
            }

            if (IsUnixVersion(version))
            {
                string executable = Path.Combine(targetDirectory, SharedInfo.AssemblyName);
                if (File.Exists(executable))
                {
                    OS.UnixSetFileAccessExecutable(executable);
                }
            }

            ArchiLogger.LogGenericInfo(Strings.UpdateFinished);
            await RestartOrExit().ConfigureAwait(false);

            return(newVersion);
        }
Beispiel #2
0
        internal static async Task <Version> Update(bool updateOverride = false)
        {
            if (!SharedInfo.BuildInfo.CanUpdate || (GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.None))
            {
                return(null);
            }

            await UpdateSemaphore.WaitAsync().ConfigureAwait(false);

            try {
                // If backup directory from previous update exists, it's a good idea to purge it now
                string backupDirectory = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.UpdateDirectory);

                if (Directory.Exists(backupDirectory))
                {
                    ArchiLogger.LogGenericInfo(Strings.UpdateCleanup);

                    // It's entirely possible that old process is still running, wait a short moment for eventual cleanup
                    await Task.Delay(5000).ConfigureAwait(false);

                    try {
                        Directory.Delete(backupDirectory, true);
                    } catch (Exception e) {
                        ArchiLogger.LogGenericException(e);

                        return(null);
                    }

                    ArchiLogger.LogGenericInfo(Strings.Done);
                }

                ArchiLogger.LogGenericInfo(Strings.UpdateCheckingNewVersion);

                GitHub.ReleaseResponse releaseResponse = await GitHub.GetLatestRelease(GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable).ConfigureAwait(false);

                if (releaseResponse == null)
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);

                    return(null);
                }

                if (string.IsNullOrEmpty(releaseResponse.Tag))
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);

                    return(null);
                }

                Version newVersion = new Version(releaseResponse.Tag);

                ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateVersionInfo, SharedInfo.Version, newVersion));

                if (SharedInfo.Version >= newVersion)
                {
                    if (SharedInfo.Version > newVersion)
                    {
                        ArchiLogger.LogGenericWarning(Strings.WarningPreReleaseVersion);
                        await Task.Delay(15 * 1000).ConfigureAwait(false);
                    }

                    return(newVersion);
                }

                if (!updateOverride && (GlobalConfig.UpdatePeriod == 0))
                {
                    ArchiLogger.LogGenericInfo(Strings.UpdateNewVersionAvailable);
                    await Task.Delay(5000).ConfigureAwait(false);

                    return(null);
                }

                // Auto update logic starts here
                if (releaseResponse.Assets == null)
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssets);

                    return(null);
                }

                string targetFile = SharedInfo.ASF + "-" + SharedInfo.BuildInfo.Variant + ".zip";
                GitHub.ReleaseResponse.Asset binaryAsset = releaseResponse.Assets.FirstOrDefault(asset => asset.Name.Equals(targetFile, StringComparison.OrdinalIgnoreCase));

                if (binaryAsset == null)
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssetForThisVersion);

                    return(null);
                }

                if (string.IsNullOrEmpty(binaryAsset.DownloadURL))
                {
                    ArchiLogger.LogNullError(nameof(binaryAsset.DownloadURL));

                    return(null);
                }

                if (!string.IsNullOrEmpty(releaseResponse.ChangelogPlainText))
                {
                    ArchiLogger.LogGenericInfo(releaseResponse.ChangelogPlainText);
                }

                ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateDownloadingNewVersion, newVersion, binaryAsset.Size / 1024 / 1024));

                WebBrowser.BinaryResponse response = await WebBrowser.UrlGetToBinaryWithProgress(binaryAsset.DownloadURL).ConfigureAwait(false);

                if (response?.Content == null)
                {
                    return(null);
                }

                try {
#if !NETFRAMEWORK
                    await
#endif
                    using MemoryStream memoryStream = new MemoryStream(response.Content);

                    using ZipArchive zipArchive = new ZipArchive(memoryStream);

                    if (!UpdateFromArchive(zipArchive, SharedInfo.HomeDirectory))
                    {
                        ArchiLogger.LogGenericError(Strings.WarningFailed);
                    }
                } catch (Exception e) {
                    ArchiLogger.LogGenericException(e);

                    return(null);
                }

                if (OS.IsUnix)
                {
                    string executable = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.AssemblyName);

                    if (File.Exists(executable))
                    {
                        OS.UnixSetFileAccessExecutable(executable);
                    }
                }

                ArchiLogger.LogGenericInfo(Strings.UpdateFinished);

                return(newVersion);
            } finally {
                UpdateSemaphore.Release();
            }
        }
Beispiel #3
0
        internal static async Task CheckForUpdate(bool updateOverride = false)
        {
            if (Debugging.IsDebugBuild && !updateOverride)
            {
                return;
            }

            string exeFile = Assembly.GetEntryAssembly().Location;

            if (string.IsNullOrEmpty(exeFile))
            {
                ArchiLogger.LogNullError(nameof(exeFile));
                return;
            }

            string oldExeFile = exeFile + ".old";

            // We booted successfully so we can now remove old exe file
            if (File.Exists(oldExeFile))
            {
                // It's entirely possible that old process is still running, allow at least a second before trying to remove the file
                await Task.Delay(1000).ConfigureAwait(false);

                try {
                    File.Delete(oldExeFile);
                } catch (Exception e) {
                    ArchiLogger.LogGenericException(e);
                    ArchiLogger.LogGenericError(string.Format(Strings.ErrorRemovingOldBinary, oldExeFile));
                }
            }

            if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.None)
            {
                return;
            }

            if ((AutoUpdatesTimer == null) && Program.GlobalConfig.AutoUpdates)
            {
                AutoUpdatesTimer = new Timer(
                    async e => await CheckForUpdate().ConfigureAwait(false),
                    null,
                    TimeSpan.FromHours(AutoUpdatePeriodInHours),                    // Delay
                    TimeSpan.FromHours(AutoUpdatePeriodInHours)                     // Period
                    );

                ArchiLogger.LogGenericInfo(string.Format(Strings.AutoUpdateCheckInfo, AutoUpdatePeriodInHours));
            }

            string releaseURL = SharedInfo.GithubReleaseURL;

            if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable)
            {
                releaseURL += "/latest";
            }

            ArchiLogger.LogGenericInfo(Strings.UpdateCheckingNewVersion);

            GitHub.ReleaseResponse releaseResponse;

            if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable)
            {
                releaseResponse = await Program.WebBrowser.UrlGetToJsonResultRetry <GitHub.ReleaseResponse>(releaseURL).ConfigureAwait(false);

                if (releaseResponse == null)
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                    return;
                }
            }
            else
            {
                List <GitHub.ReleaseResponse> releases = await Program.WebBrowser.UrlGetToJsonResultRetry <List <GitHub.ReleaseResponse> >(releaseURL).ConfigureAwait(false);

                if ((releases == null) || (releases.Count == 0))
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                    return;
                }

                releaseResponse = releases[0];
            }

            if (string.IsNullOrEmpty(releaseResponse.Tag))
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                return;
            }

            Version newVersion = new Version(releaseResponse.Tag);

            ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateVersionInfo, SharedInfo.Version, newVersion));

            if (SharedInfo.Version.CompareTo(newVersion) >= 0)               // If local version is the same or newer than remote version
            {
                return;
            }

            if (!updateOverride && !Program.GlobalConfig.AutoUpdates)
            {
                ArchiLogger.LogGenericInfo(Strings.UpdateNewVersionAvailable);
                await Task.Delay(5000).ConfigureAwait(false);

                return;
            }

            if (File.Exists(oldExeFile))
            {
                ArchiLogger.LogGenericError(string.Format(Strings.ErrorRemovingOldBinary, oldExeFile));
                return;
            }

            // Auto update logic starts here
            if (releaseResponse.Assets == null)
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssets);
                return;
            }

            string exeFileName = Path.GetFileName(exeFile);

            GitHub.ReleaseResponse.Asset binaryAsset = releaseResponse.Assets.FirstOrDefault(asset => !string.IsNullOrEmpty(asset.Name) && asset.Name.Equals(exeFileName, StringComparison.OrdinalIgnoreCase));

            if (binaryAsset == null)
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssetForThisBinary);
                return;
            }

            if (string.IsNullOrEmpty(binaryAsset.DownloadURL))
            {
                ArchiLogger.LogNullError(nameof(binaryAsset.DownloadURL));
                return;
            }

            ArchiLogger.LogGenericInfo(Strings.UpdateDownloadingNewVersion);

            byte[] result = await Program.WebBrowser.UrlGetToBytesRetry(binaryAsset.DownloadURL).ConfigureAwait(false);

            if (result == null)
            {
                return;
            }

            string newExeFile = exeFile + ".new";

            // Firstly we create new exec
            try {
                File.WriteAllBytes(newExeFile, result);
            } catch (Exception e) {
                ArchiLogger.LogGenericException(e);
                return;
            }

            // Now we move current -> old
            try {
                File.Move(exeFile, oldExeFile);
            } catch (Exception e) {
                ArchiLogger.LogGenericException(e);
                try {
                    // Cleanup
                    File.Delete(newExeFile);
                } catch {
                    // Ignored
                }
                return;
            }

            // Now we move new -> current
            try {
                File.Move(newExeFile, exeFile);
            } catch (Exception e) {
                ArchiLogger.LogGenericException(e);
                try {
                    // Cleanup
                    File.Move(oldExeFile, exeFile);
                    File.Delete(newExeFile);
                } catch {
                    // Ignored
                }
                return;
            }

            ArchiLogger.LogGenericInfo(Strings.UpdateFinished);
            await RestartOrExit().ConfigureAwait(false);
        }
Beispiel #4
0
        internal static async Task CheckForUpdate(bool updateOverride = false)
        {
            string oldExeFile = ExecutableFile + ".old";

            // We booted successfully so we can now remove old exe file
            if (File.Exists(oldExeFile))
            {
                // It's entirely possible that old process is still running, allow at least a second before trying to remove the file
                await Utilities.SleepAsync(1000).ConfigureAwait(false);

                try {
                    File.Delete(oldExeFile);
                } catch (Exception e) {
                    Logging.LogGenericException(e);
                    Logging.LogGenericError("Could not remove old ASF binary, please remove " + oldExeFile + " manually in order for update function to work!");
                }
            }

            if (GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Unknown)
            {
                return;
            }

            string releaseURL = GithubReleaseURL;

            if (GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable)
            {
                releaseURL += "/latest";
            }

            Logging.LogGenericInfo("Checking new version...");

            string response = await WebBrowser.UrlGetToContentRetry(releaseURL).ConfigureAwait(false);

            if (string.IsNullOrEmpty(response))
            {
                Logging.LogGenericWarning("Could not check latest version!");
                return;
            }

            GitHub.ReleaseResponse releaseResponse;
            if (GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable)
            {
                try {
                    releaseResponse = JsonConvert.DeserializeObject <GitHub.ReleaseResponse>(response);
                } catch (JsonException e) {
                    Logging.LogGenericException(e);
                    return;
                }
            }
            else
            {
                List <GitHub.ReleaseResponse> releases;
                try {
                    releases = JsonConvert.DeserializeObject <List <GitHub.ReleaseResponse> >(response);
                } catch (JsonException e) {
                    Logging.LogGenericException(e);
                    return;
                }

                if ((releases == null) || (releases.Count == 0))
                {
                    Logging.LogGenericWarning("Could not check latest version!");
                    return;
                }

                releaseResponse = releases[0];
            }

            if (string.IsNullOrEmpty(releaseResponse.Tag))
            {
                Logging.LogGenericWarning("Could not check latest version!");
                return;
            }

            Version newVersion = new Version(releaseResponse.Tag);

            Logging.LogGenericInfo("Local version: " + Version + " | Remote version: " + newVersion);

            if (Version.CompareTo(newVersion) >= 0)               // If local version is the same or newer than remote version
            {
                if ((AutoUpdatesTimer != null) || !GlobalConfig.AutoUpdates)
                {
                    return;
                }

                Logging.LogGenericInfo("ASF will automatically check for new versions every 24 hours");

                AutoUpdatesTimer = new Timer(
                    async e => await CheckForUpdate().ConfigureAwait(false),
                    null,
                    TimeSpan.FromDays(1),                    // Delay
                    TimeSpan.FromDays(1)                     // Period
                    );

                return;
            }

            if (!updateOverride && !GlobalConfig.AutoUpdates)
            {
                Logging.LogGenericInfo("New version is available!");
                Logging.LogGenericInfo("Consider updating yourself!");
                await Utilities.SleepAsync(5000).ConfigureAwait(false);

                return;
            }

            if (File.Exists(oldExeFile))
            {
                Logging.LogGenericWarning("Refusing to proceed with auto update as old " + oldExeFile + " binary could not be removed, please remove it manually");
                return;
            }

            // Auto update logic starts here
            if (releaseResponse.Assets == null)
            {
                Logging.LogGenericWarning("Could not proceed with update because that version doesn't include assets!");
                return;
            }

            GitHub.ReleaseResponse.Asset binaryAsset = releaseResponse.Assets.FirstOrDefault(asset => !string.IsNullOrEmpty(asset.Name) && asset.Name.Equals(ExecutableName, StringComparison.OrdinalIgnoreCase));

            if (binaryAsset == null)
            {
                Logging.LogGenericWarning("Could not proceed with update because there is no asset that relates to currently running binary!");
                return;
            }

            if (string.IsNullOrEmpty(binaryAsset.DownloadURL))
            {
                Logging.LogGenericWarning("Could not proceed with update because download URL is empty!");
                return;
            }

            byte[] result = await WebBrowser.UrlGetToBytesRetry(binaryAsset.DownloadURL).ConfigureAwait(false);

            if (result == null)
            {
                return;
            }

            string newExeFile = ExecutableFile + ".new";

            // Firstly we create new exec
            try {
                File.WriteAllBytes(newExeFile, result);
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return;
            }

            // Now we move current -> old
            try {
                File.Move(ExecutableFile, oldExeFile);
            } catch (Exception e) {
                Logging.LogGenericException(e);
                try {
                    // Cleanup
                    File.Delete(newExeFile);
                } catch {
                    // Ignored
                }
                return;
            }

            // Now we move new -> current
            try {
                File.Move(newExeFile, ExecutableFile);
            } catch (Exception e) {
                Logging.LogGenericException(e);
                try {
                    // Cleanup
                    File.Move(oldExeFile, ExecutableFile);
                    File.Delete(newExeFile);
                } catch {
                    // Ignored
                }
                return;
            }

            Logging.LogGenericInfo("Update process finished!");

            if (GlobalConfig.AutoRestart)
            {
                Logging.LogGenericInfo("Restarting...");
                await Utilities.SleepAsync(5000).ConfigureAwait(false);

                Restart();
            }
            else
            {
                Logging.LogGenericInfo("Exiting...");
                await Utilities.SleepAsync(5000).ConfigureAwait(false);

                Exit();
            }
        }
Beispiel #5
0
        internal static async Task <Version> CheckAndUpdateProgram(bool updateOverride = false)
        {
            if (!SharedInfo.BuildInfo.CanUpdate || (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.None))
            {
                return(null);
            }

            if ((AutoUpdatesTimer == null) && (Program.GlobalConfig.UpdatePeriod > 0))
            {
                TimeSpan autoUpdatePeriod = TimeSpan.FromHours(Program.GlobalConfig.UpdatePeriod);

                AutoUpdatesTimer = new Timer(
                    async e => await CheckAndUpdateProgram().ConfigureAwait(false),
                    null,
                    autoUpdatePeriod,                    // Delay
                    autoUpdatePeriod                     // Period
                    );

                ArchiLogger.LogGenericInfo(string.Format(Strings.AutoUpdateCheckInfo, autoUpdatePeriod.ToHumanReadable()));
            }

            ArchiLogger.LogGenericInfo(Strings.UpdateCheckingNewVersion);

            // Cleanup from previous update - update directory for old in-use runtime files
            string backupDirectory = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.UpdateDirectory);

            if (Directory.Exists(backupDirectory))
            {
                // It's entirely possible that old process is still running, wait a short moment for eventual cleanup
                await Task.Delay(5000).ConfigureAwait(false);

                try {
                    Directory.Delete(backupDirectory, true);
                } catch (Exception e) {
                    ArchiLogger.LogGenericException(e);
                    return(null);
                }
            }

            // Cleanup from previous update - old non-runtime in-use files
            try {
                foreach (string file in Directory.EnumerateFiles(SharedInfo.HomeDirectory, "*.old", SearchOption.AllDirectories))
                {
                    File.Delete(file);
                }
            } catch (Exception e) {
                ArchiLogger.LogGenericException(e);
                return(null);
            }

            string releaseURL = SharedInfo.GithubReleaseURL + (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable ? "/latest" : "?per_page=1");

            GitHub.ReleaseResponse releaseResponse;

            if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable)
            {
                WebBrowser.ObjectResponse <GitHub.ReleaseResponse> objectResponse = await Program.WebBrowser.UrlGetToJsonObject <GitHub.ReleaseResponse>(releaseURL).ConfigureAwait(false);

                if (objectResponse?.Content == null)
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                    return(null);
                }

                releaseResponse = objectResponse.Content;
            }
            else
            {
                WebBrowser.ObjectResponse <List <GitHub.ReleaseResponse> > objectResponse = await Program.WebBrowser.UrlGetToJsonObject <List <GitHub.ReleaseResponse> >(releaseURL).ConfigureAwait(false);

                if ((objectResponse?.Content == null) || (objectResponse.Content.Count == 0))
                {
                    ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                    return(null);
                }

                releaseResponse = objectResponse.Content[0];
            }

            if (string.IsNullOrEmpty(releaseResponse.Tag))
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateCheckFailed);
                return(null);
            }

            Version newVersion = new Version(releaseResponse.Tag);

            ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateVersionInfo, SharedInfo.Version, newVersion));

            if (SharedInfo.Version == newVersion)
            {
                return(SharedInfo.Version);
            }

            if (SharedInfo.Version > newVersion)
            {
                ArchiLogger.LogGenericWarning(Strings.WarningPreReleaseVersion);
                await Task.Delay(15 * 1000).ConfigureAwait(false);

                return(SharedInfo.Version);
            }

            if (!updateOverride && (Program.GlobalConfig.UpdatePeriod == 0))
            {
                ArchiLogger.LogGenericInfo(Strings.UpdateNewVersionAvailable);
                await Task.Delay(5000).ConfigureAwait(false);

                return(null);
            }

            // Auto update logic starts here
            if (releaseResponse.Assets == null)
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssets);
                return(null);
            }

            string targetFile = SharedInfo.ASF + "-" + SharedInfo.BuildInfo.Variant + ".zip";

            GitHub.ReleaseResponse.Asset binaryAsset = releaseResponse.Assets.FirstOrDefault(asset => asset.Name.Equals(targetFile, StringComparison.OrdinalIgnoreCase));

            if (binaryAsset == null)
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssetForThisVersion);
                return(null);
            }

            if (string.IsNullOrEmpty(binaryAsset.DownloadURL))
            {
                ArchiLogger.LogNullError(nameof(binaryAsset.DownloadURL));
                return(null);
            }

            ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateDownloadingNewVersion, newVersion, binaryAsset.Size / 1024 / 1024));

            WebBrowser.BinaryResponse response = await Program.WebBrowser.UrlGetToBinaryWithProgress(binaryAsset.DownloadURL).ConfigureAwait(false);

            if (response?.Content == null)
            {
                return(null);
            }

            try {
                using (ZipArchive zipArchive = new ZipArchive(new MemoryStream(response.Content))) {
                    if (!UpdateFromArchive(zipArchive, SharedInfo.HomeDirectory))
                    {
                        ArchiLogger.LogGenericError(Strings.WarningFailed);
                    }
                }
            } catch (Exception e) {
                ArchiLogger.LogGenericException(e);
                return(null);
            }

            if (OS.IsUnix)
            {
                string executable = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.AssemblyName);
                if (File.Exists(executable))
                {
                    OS.UnixSetFileAccessExecutable(executable);
                }
            }

            ArchiLogger.LogGenericInfo(Strings.UpdateFinished);
            await RestartOrExit().ConfigureAwait(false);

            return(newVersion);
        }