Restart() static private method

static private Restart ( ) : void
return void
Example #1
0
        private async void OnFriendMsg(SteamFriends.FriendMsgCallback callback)
        {
            if (callback == null)
            {
                return;
            }

            if (callback.EntryType != EChatEntryType.ChatMsg)
            {
                return;
            }

            ulong steamID = callback.Sender;

            if (steamID != SteamMasterID)
            {
                return;
            }

            string message = callback.Message;

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            if (message.Length == 17 && message[5] == '-' && message[11] == '-')
            {
                ArchiHandler.RedeemKey(message);
                return;
            }

            if (!message.StartsWith("!"))
            {
                return;
            }

            if (!message.Contains(" "))
            {
                switch (message)
                {
                case "!exit":
                    await ShutdownAllBots().ConfigureAwait(false);

                    break;

                case "!farm":
                    await CardsFarmer.StartFarming().ConfigureAwait(false);

                    SendMessageToUser(steamID, "Done!");
                    break;

                case "!restart":
                    await Program.Restart().ConfigureAwait(false);

                    break;

                case "!status":
                    ResponseStatus(steamID);
                    break;

                case "!stop":
                    await Shutdown().ConfigureAwait(false);

                    break;
                }
            }
            else
            {
                string[] args = message.Split(' ');
                switch (args[0])
                {
                case "!start":
                    ResponseStart(steamID, args[1]);
                    break;

                case "!stop":
                    await ResponseStop(steamID, args[1]).ConfigureAwait(false);

                    break;
                }
            }
        }
Example #2
0
        internal static async Task CheckForUpdate(bool updateOverride = false)
        {
            string exeFile = Assembly.GetEntryAssembly().Location;

            if (string.IsNullOrEmpty(exeFile))
            {
                Logging.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) {
                    Logging.LogGenericException(e);
                    Logging.LogGenericError("Could not remove old ASF binary, please remove " + oldExeFile + " manually in order for update function to work!");
                }
            }

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

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

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

            string releaseURL = SharedInfo.GithubReleaseURL;

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

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

            GitHub.ReleaseResponse releaseResponse;

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

                if (releaseResponse == null)
                {
                    Logging.LogGenericWarning("Could not check latest version!");
                    return;
                }
            }
            else
            {
                List <GitHub.ReleaseResponse> releases = await Program.WebBrowser.UrlGetToJsonResultRetry <List <GitHub.ReleaseResponse> >(releaseURL).ConfigureAwait(false);

                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: " + SharedInfo.Version + " | Remote 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)
            {
                Logging.LogGenericInfo("New version is available!");
                Logging.LogGenericInfo("Consider updating yourself!");
                await Task.Delay(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;
            }

            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)
            {
                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;
            }

            Logging.LogGenericInfo("Downloading new version...");
            Logging.LogGenericInfo("While waiting, consider donating if you appreciate the work being done :)");

            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) {
                Logging.LogGenericException(e);
                return;
            }

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

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

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

            if (Program.GlobalConfig.AutoRestart)
            {
                Logging.LogGenericInfo("Restarting...");
                await Task.Delay(5000).ConfigureAwait(false);

                Program.Restart();
            }
            else
            {
                Logging.LogGenericInfo("Exiting...");
                await Task.Delay(5000).ConfigureAwait(false);

                Program.Exit();
            }
        }
Example #3
0
        private static async void OnChanged(object sender, FileSystemEventArgs e)
        {
            if ((sender == null) || (e == null))
            {
                ArchiLogger.LogNullError(nameof(sender) + " || " + nameof(e));
                return;
            }

            string botName = Path.GetFileNameWithoutExtension(e.Name);

            if (string.IsNullOrEmpty(botName))
            {
                return;
            }

            if (botName.Equals(SharedInfo.ASF))
            {
                ArchiLogger.LogGenericError("Global config file has been changed, restarting...");
                Program.Restart();
                return;
            }

            Bot bot;

            if (!Bot.Bots.TryGetValue(botName, out bot))
            {
                return;
            }

            DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);

            DateTime savedLastWriteTime;

            if (LastWriteTimes.TryGetValue(bot, out savedLastWriteTime))
            {
                if (savedLastWriteTime >= lastWriteTime)
                {
                    return;
                }
            }

            LastWriteTimes[bot] = lastWriteTime;

            // It's entirely possible that some process is still accessing our file, allow at least a second before trying to read it
            await Task.Delay(1000).ConfigureAwait(false);

            // It's also possible that we got some other event in the meantime
            if (LastWriteTimes.TryGetValue(bot, out savedLastWriteTime))
            {
                if (lastWriteTime != savedLastWriteTime)
                {
                    return;
                }

                if (LastWriteTimes.TryRemove(bot, out savedLastWriteTime))
                {
                    if (lastWriteTime != savedLastWriteTime)
                    {
                        return;
                    }
                }
            }

            bot.OnNewConfigLoaded(new BotConfigEventArgs(BotConfig.Load(e.FullPath))).Forget();
        }