Example #1
0
        public override bool BackupServer(SettingsLoader.ServerChild ServerData)
        {
            string BackupFrom = string.Format("{0}\\ShooterGame\\Saved\\", ServerData.GameServerPath);
            string BackupTo = ServerData.BackupDirectory;
            if( !Directory.Exists(BackupFrom) )
            {
                _Parent.Log.ConsolePrint(LogLevel.Warning, @"Unable to Backup server, directory ShooterGame\Saved is missing. If this is the first startup then ignore this message");
                return false;
            }
            else if( (BackupTo.Length <= 0) || !Directory.Exists(BackupTo) )
            {
                _Parent.Log.ConsolePrint(LogLevel.Warning, @"Unable to Backup server, Specified backup directory does not exist or is not set.");
                return false;
            }

            _Parent.Log.ConsolePrint(LogLevel.Info, "Starting backup of server {0}", ServerData.GameServerName);
            if( _Parent.ARKConfiguration.Messages.ServerBackupBroadcast.Length >= 1 )
            {
                using( var RCONClient = new ArkRCON("127.0.0.1", ServerData.RCONPort) )
                {
                    try
                    {
                        RCONClient.Authenticate(ServerData.ServerAdminPassword);
                        RCONClient.ExecuteCommand(string.Format("serverchat {0}", _Parent.ARKConfiguration.Messages.ServerBackupBroadcast));
                    } catch( QueryException ) {}
                }
            }

            // Use 7zipSharp to compress the backup directory
            SevenZipCompressor.SetLibraryPath("7za.dll");
            var Compressor = new SevenZipCompressor()
            {
                CompressionLevel = CompressionLevel.Normal,
                ArchiveFormat = OutArchiveFormat.SevenZip,
                IncludeEmptyDirectories = true,
            };

            string BackupPath = string.Format("{0}\\Backup-{1}.7z", BackupTo, Helpers.CurrentUnixStamp);
            Compressor.CompressDirectory(BackupFrom, BackupPath);

            _Parent.Log.ConsolePrint(LogLevel.Success, "Backup of server {0} complete", ServerData.GameServerName);
            return true;
        }
Example #2
0
        public void Run()
        {
            // Get game information
            Log.ConsolePrint(LogLevel.Info, "Fetching public build number for `ARK: Survival Evolved` from Steam3");
            int BuildNumber = SteamInt.GetGameInformation(376030);
            if( BuildNumber == -1 )
            {
                Log.ConsolePrint(LogLevel.Error, "Unable to fetch Build ID from Steam, this may be an issue with your internet connection.");
            }
            else
            {
                Log.ConsolePrint(LogLevel.Success, "Current Build ID for `ARK: Survival Evolved` is: {0}", BuildNumber);
            }

            // Initial Setup
            foreach( var Server in Servers )
            {
                Log.ConsolePrint(LogLevel.Debug, "Init Server '{0}'", Server.ServerData.GameServerName);
                int CurrentAppID = SteamInt.GetGameBuildVersion(Server.ServerData.GameServerPath);

                Server.LastBackedUp = Helpers.CurrentUnixStamp;
                if( (CurrentAppID < BuildNumber) && (CurrentAppID != -1) )
                {
                    // Update Available
                    if( !ServerInt.ServerRunning(Server.ServerData) )
                    {
                        // Update Server
                        Log.ConsolePrint(LogLevel.Info, "Server '{0}' has an update available, Updating before we start the server up.", Server.ServerData.GameServerName);
                        SteamInt.UpdateGame(Server.ServerData.SteamUpdateScript, ARKConfiguration.ShowSteamUpdateInConsole);
                        Log.ConsolePrint(LogLevel.Success, "Server '{0}' update successful, starting server.", Server.ServerData.GameServerName);
                    }

                    var ProcessID = ServerInt.StartServer(Server.ServerData);
                    Server.ProcessID = ProcessID;
                }
                else
                {
                    // Start Server
                    Log.ConsolePrint(LogLevel.Info, "Server '{0}' is up to date. Starting/Connecting to server", Server.ServerData.GameServerName);
                    var ProcessID = ServerInt.StartServer(Server.ServerData);
                    Server.ProcessID = ProcessID;
                }
            }

            // Application Loop
            int LastUpdatePollTime = Helpers.CurrentUnixStamp;
            int LastMinutePollTime = Helpers.CurrentUnixStamp;

            int PreviousBuild = BuildNumber;
            bool UpdatesQueued = false;
            while( _Running )
            {
                if( (LastUpdatePollTime + (60 * ARKConfiguration.UpdatePollingInMinutes) < Helpers.CurrentUnixStamp) && !UpdatesQueued )
                {
                    // Query Steam and check for updates to our servers
                    Log.ConsolePrint(LogLevel.Debug, "Checking with Steam for updates to ARK (Current Build: {0})", BuildNumber);
                    BuildNumber = SteamInt.GetGameInformation(376030);

                    if( BuildNumber != -1 )
                    {
                        LastUpdatePollTime = Helpers.CurrentUnixStamp;
                        if( ( BuildNumber > PreviousBuild ) && ( PreviousBuild != -1 ) ) Log.ConsolePrint(LogLevel.Info, "A new build of `ARK: Survival Evolved` is available. Build number: {0}", BuildNumber);
                    }
                }

                bool MinutePassed = ( LastMinutePollTime + 60 <= Helpers.CurrentUnixStamp ) ? true : false;
                foreach( var Server in Servers )
                {
                    if( Server.MinutesRemaining == -1 )
                    {
                        // Check each server for updates
                        var ServerBuild = SteamInt.GetGameBuildVersion(Server.ServerData.GameServerPath);
                        if( (ServerBuild < BuildNumber) && (ServerBuild != -1) )
                        {
                            // Check for Player Requirement
                            if( ARKConfiguration.PostponeUpdateWhenPlayersHigherThan > 0 )
                            {
                                using( var Query = new SrcQuery("127.0.0.1", Server.ServerData.QueryPort) )
                                {
                                    try
                                    {
                                        var QueryData = Query.QueryServer();
                                        if( Convert.ToInt32(QueryData["CurrentPlayers"]) > ARKConfiguration.PostponeUpdateWhenPlayersHigherThan )
                                        {
                                            Log.ConsolePrint(LogLevel.Info, "Available update for server '{0}' postponed. Players online: {1}", Server.ServerData.GameServerName, QueryData["CurrentPlayers"]);
                                            continue;
                                        }
                                    } catch (QueryException) {}
                                }
                            }

                            // Schedule update on server with the user defined interval.
                            Log.ConsolePrint(LogLevel.Success, "Server '{0}' queued for update successfully. Update will begin in {1} minute(s)", Server.ServerData.GameServerName, ARKConfiguration.UpdateWarningTimeInMinutes);
                            Server.MinutesRemaining = ARKConfiguration.UpdateWarningTimeInMinutes;
                            if( !UpdatesQueued ) UpdatesQueued = true;

                            // Send warning message to Server
                            using( var RCONClient = new ArkRCON("127.0.0.1", Server.ServerData.RCONPort) )
                            {
                                try
                                {
                                    RCONClient.Authenticate(Server.ServerData.ServerAdminPassword);
                                    string RCONWarning = string.Format(ARKConfiguration.Messages.ServerUpdateBroadcast, Server.MinutesRemaining);
                                    RCONClient.ExecuteCommand(string.Format("serverchat {0}", RCONWarning));
                                } catch( QueryException ) {}
                            }
                        }
                    }

                    if( MinutePassed && (Server.MinutesRemaining >= 1) )
                    {
                        Log.ConsolePrint(LogLevel.Debug, "Ticking update minute counter from {0} to {1} for server '{2}'", Server.MinutesRemaining, Server.MinutesRemaining-1, Server.ServerData.GameServerName);
                        Server.MinutesRemaining = (Server.MinutesRemaining - 1);

                        // Send warning message to Server
                        if( Server.MinutesRemaining != 0 )
                        {
                            using( var RCONClient = new ArkRCON("127.0.0.1", Server.ServerData.RCONPort) )
                            {
                                try
                                {
                                    RCONClient.Authenticate(Server.ServerData.ServerAdminPassword);
                                    string RCONWarning = string.Format(ARKConfiguration.Messages.ServerUpdateBroadcast, Server.MinutesRemaining);
                                    RCONClient.ExecuteCommand(string.Format("serverchat {0}", RCONWarning));
                                }
                                catch( QueryException Ex )
                                {
                                    Log.ConsolePrint(LogLevel.Error, Ex.Message);
                                }
                            }
                        }
                    }

                    if( Server.MinutesRemaining == 0 )
                    {
                        // Send warning message to Server
                        using( var RCONClient = new ArkRCON("127.0.0.1", Server.ServerData.RCONPort) )
                        {
                            try
                            {
                                RCONClient.Authenticate(Server.ServerData.ServerAdminPassword);
                                RCONClient.ExecuteCommand(string.Format("serverchat {0}", ARKConfiguration.Messages.ServerShutdownBroadcast));
                            }
                            catch( QueryException Ex )
                            {
                                Log.ConsolePrint(LogLevel.Error, Ex.Message);
                            }
                        }

                        _Sleeper.WaitOne( TimeSpan.FromSeconds(2) );

                        // Shutdown Server
                        var ResetEvent = new AutoResetEvent(false);
                        ServerInt.StopServer(Server.ServerData, ResetEvent);
                        Log.ConsolePrint(LogLevel.Info, "Server '{0}' will now be shutdown for an update", Server.ServerData.GameServerName);

                        ResetEvent.WaitOne();
                        Log.ConsolePrint(LogLevel.Debug, "Server '{0}' now waiting for process exit", Server.ServerData.GameServerName);

                        // Update Server
                        SteamInt.UpdateGame(Server.ServerData.SteamUpdateScript, ARKConfiguration.ShowSteamUpdateInConsole);
                        _Sleeper.WaitOne( TimeSpan.FromSeconds(2) );

                        // Restart Server
                        Log.ConsolePrint(LogLevel.Info, "Server '{0}' update complete, restarting server.", Server.ServerData.GameServerName);
                        var ProcessID = ServerInt.StartServer(Server.ServerData);

                        Server.MinutesRemaining = -1;
                        Server.ProcessID = ProcessID;
                    }

                    if( ARKConfiguration.Backup.EnableBackup )
                    {
                        // Check for Last Backup
                        if( (Server.LastBackedUp + (60 * ARKConfiguration.Backup.BackupIntervalInMinutes) < Helpers.CurrentUnixStamp) && !UpdatesQueued )
                        {
                            Server.LastBackedUp = Helpers.CurrentUnixStamp;
                            BackupInt.BackupServer(Server.ServerData);
                            BackupInt.CleanBackups(Server.ServerData);
                        }
                    }
                }

                // Cleanup before next loop
                int NumberServersUpdateRemaining = Servers.Where(x => x.MinutesRemaining != -1).Count();
                if( NumberServersUpdateRemaining <= 0 && UpdatesQueued ) UpdatesQueued = false;

                PreviousBuild = BuildNumber;
                if( MinutePassed ) LastMinutePollTime = Helpers.CurrentUnixStamp;
                _Sleeper.WaitOne( TimeSpan.FromSeconds(5) );
            }
        }