コード例 #1
0
        internal async Task <Steam.ConfirmationDetails> GetConfirmationDetails(string deviceID, string confirmationHash, uint time, uint confirmationID)
        {
            if (string.IsNullOrEmpty(deviceID) || string.IsNullOrEmpty(confirmationHash) || (time == 0) || (confirmationID == 0))
            {
                Logging.LogNullError(nameof(deviceID) + " || " + nameof(confirmationHash) + " || " + nameof(time) + " || " + nameof(confirmationID), Bot.BotName);
                return(null);
            }

            if (!await RefreshSessionIfNeeded().ConfigureAwait(false))
            {
                return(null);
            }

            string request = SteamCommunityURL + "/mobileconf/details/" + confirmationID + "?p=" + deviceID + "&a=" + SteamID + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&t=" + time + "&m=android&tag=conf";

            string json = await WebBrowser.UrlGetToContentRetry(request).ConfigureAwait(false);

            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            Steam.ConfirmationDetails response;

            try {
                response = JsonConvert.DeserializeObject <Steam.ConfirmationDetails>(json);
            } catch (JsonException e) {
                Logging.LogGenericException(e, Bot.BotName);
                return(null);
            }

            if (response == null)
            {
                Logging.LogNullError(nameof(response), Bot.BotName);
                return(null);
            }

            response.ConfirmationID = confirmationID;
            return(response);
        }
コード例 #2
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();
            }
        }