Finished the remaing bot commands

This commit is contained in:
Brett Hewitson 2021-12-05 15:08:37 +10:00
parent 0f3c6e6be9
commit aa62646f0f
23 changed files with 1041 additions and 287 deletions

View file

@ -117,7 +117,7 @@ namespace ServerManagerTool.Lib
_startTime = DateTime.Now;
}
private void BackupServer()
private void BackupServer(CancellationToken cancellationToken)
{
if (_profile == null)
{
@ -146,58 +146,67 @@ namespace ServerManagerTool.Lib
if (_serverRunning)
{
// check if RCON is enabled
//if (_profile.RconEnabled)
//{
// try
// {
// emailMessage.AppendLine();
try
{
emailMessage.AppendLine();
// // perform a world save
// if (!string.IsNullOrWhiteSpace(Config.Default.ServerBackup_WorldSaveMessage))
// {
// ProcessAlert(AlertType.Backup, Config.Default.ServerBackup_WorldSaveMessage);
// sent = SendMessage(Config.Default.ServerBackup_WorldSaveMessage, CancellationToken.None);
// if (sent)
// {
// emailMessage.AppendLine("sent server save message.");
// }
// }
var sent = false;
// sent = SendCommand(Config.Default.ServerSaveCommand, false);
// if (sent)
// {
// emailMessage.AppendLine("sent server save command.");
// Task.Delay(Config.Default.ServerShutdown_WorldSaveDelay * 1000).Wait();
// }
// }
// catch (Exception ex)
// {
// CloseRconConsole();
// perform a world save
if (!string.IsNullOrWhiteSpace(Config.Default.ServerBackup_WorldSaveMessage))
{
ProcessAlert(AlertType.Backup, Config.Default.ServerBackup_WorldSaveMessage);
sent = SendMessageAsync(Config.Default.ServerBackup_WorldSaveMessage, CancellationToken.None).Result;
if (sent)
{
emailMessage.AppendLine("sent server save message.");
}
}
// Debug.WriteLine($"RCON> {Config.Default.ServerSaveCommand} command.\r\n{ex.Message}");
// }
//}
//else
//{
// LogProfileMessage("RCON not enabled.");
//}
sent = SendCommandAsync(Config.Default.ServerSaveCommand, false).Result;
if (sent)
{
emailMessage.AppendLine("sent server save command.");
Task.Delay(Config.Default.ServerShutdown_WorldSaveDelay * 1000).Wait();
}
}
catch (Exception ex)
{
CloseRconConsole();
Debug.WriteLine($"RCON> {Config.Default.ServerSaveCommand} command.\r\n{ex.Message}");
}
}
if (ExitCode != EXITCODE_NORMALEXIT)
return;
if (cancellationToken.IsCancellationRequested)
{
ExitCode = EXITCODE_CANCELLED;
return;
}
// make a backup of the current profile and config files.
CreateProfileBackupArchiveFile();
if (ExitCode != EXITCODE_NORMALEXIT)
return;
if (cancellationToken.IsCancellationRequested)
{
ExitCode = EXITCODE_CANCELLED;
return;
}
// make a backup of the current world file.
CreateServerBackupArchiveFile(emailMessage);
if (ExitCode != EXITCODE_NORMALEXIT)
return;
if (cancellationToken.IsCancellationRequested)
{
ExitCode = EXITCODE_CANCELLED;
return;
}
if (Config.Default.EmailNotify_AutoBackup)
{
@ -266,7 +275,7 @@ namespace ServerManagerTool.Lib
try
{
ServerStatusChangeCallback?.Invoke(ServerStatus.Updating);
UpgradeLocal(true, steamCmdRemoveQuit, cancellationToken, true);
UpgradeLocal(true, true, steamCmdRemoveQuit, cancellationToken);
}
finally
{
@ -307,17 +316,20 @@ namespace ServerManagerTool.Lib
return;
}
// check if the server was previously running before the update.
if (!_serverRunning && !_profile.AutoRestartIfShutdown)
// check if the server was previously running.
if (!_serverRunning)
{
LogProfileMessage("Server was not running, server will not be started.");
if (_profile.AutoRestartIfShutdown)
{
LogProfileMessage("Server was not running, server will be started as the setting to restart if shutdown is TRUE.");
}
else
{
LogProfileMessage("Server was not running, server will not be started.");
ExitCode = EXITCODE_NORMALEXIT;
return;
}
if (!_serverRunning && _profile.AutoRestartIfShutdown)
{
LogProfileMessage("Server was not running, server will be started as the setting to restart if shutdown is TRUE.");
ExitCode = EXITCODE_NORMALEXIT;
return;
}
}
// Find the server process.
@ -390,10 +402,15 @@ namespace ServerManagerTool.Lib
_serverRunning = true;
LogProfileMessage($"Server process found PID {process.Id}.");
QueryMaster.Server gameServer = null;
bool sent = false;
try
{
// create a connection to the server
var endPoint = new IPEndPoint(_profile.ServerIPAddress, _profile.QueryPort);
gameServer = QueryMaster.ServerQuery.GetServerInstance(QueryMaster.EngineType.Source, endPoint);
// check if there is a shutdown reason
if (!string.IsNullOrWhiteSpace(ShutdownReason) && !Config.Default.ServerShutdown_AllMessagesShowReason)
{
@ -404,10 +421,6 @@ namespace ServerManagerTool.Lib
}
LogProfileMessage("Starting shutdown timer...");
if (!CheckForOnlinePlayers)
{
LogProfileMessage("CheckForOnlinePlayers disabled, shutdown timer will not perform online player check.");
}
var minutesLeft = ShutdownInterval;
while (minutesLeft > 0)
@ -430,8 +443,11 @@ namespace ServerManagerTool.Lib
{
try
{
var gameFile = GetServerWorldFile();
var playerCount = DataContainer.GetOnlinePlayerCount(gameFile);
// BH - commented out until Funcom fix the Online player status column in the world save database
//var gameFile = GetServerWorldFile();
//var playerCount = DataContainer.GetOnlinePlayerCount(gameFile);
var playerInfo = gameServer?.GetPlayers()?.Where(p => !string.IsNullOrWhiteSpace(p.Name?.Trim())).ToList();
var playerCount = playerInfo?.Count ?? -1;
// check if anyone is logged into the server
if (playerCount <= 0)
@ -449,7 +465,8 @@ namespace ServerManagerTool.Lib
}
else
{
Debug.WriteLine($"CheckForOnlinePlayers disabled");
Debug.WriteLine($"CheckForOnlinePlayers disabled, shutdown timer cancelled.");
break;
}
var message = string.Empty;
@ -516,7 +533,7 @@ namespace ServerManagerTool.Lib
// BH - commented out until funcom provide a way to send a save command
// check if we need to perform a world save
//if (_profile.RconEnabled && Config.Default.ServerShutdown_EnableWorldSave)
//if (Config.Default.ServerShutdown_EnableWorldSave)
//{
// try
// {
@ -525,10 +542,10 @@ namespace ServerManagerTool.Lib
// {
// LogProfileMessage(Config.Default.ServerShutdown_WorldSaveMessage);
// ProcessAlert(AlertType.ShutdownMessage, Config.Default.ServerShutdown_WorldSaveMessage);
// SendMessage(Config.Default.ServerShutdown_WorldSaveMessage, cancellationToken);
// SendMessageAsync(Config.Default.ServerShutdown_WorldSaveMessage, cancellationToken).Wait(cancellationToken);
// }
// if (SendCommand(Config.Default.ServerSaveCommand, false))
// if (SendCommandAsync(Config.Default.ServerSaveCommand, false).Result)
// {
// try
// {
@ -576,7 +593,8 @@ namespace ServerManagerTool.Lib
}
finally
{
CloseRconConsole();
gameServer?.Dispose();
gameServer = null;
}
if (cancellationToken.IsCancellationRequested)
@ -589,8 +607,6 @@ namespace ServerManagerTool.Lib
SendMessageAsync(Config.Default.ServerShutdown_CancelMessage, CancellationToken.None).Wait();
}
CloseRconConsole();
ExitCode = EXITCODE_CANCELLED;
return;
}
@ -664,7 +680,7 @@ namespace ServerManagerTool.Lib
ExitCode = EXITCODE_SHUTDOWN_TIMEOUT;
}
private void UpgradeLocal(bool validate, bool steamCmdRemoveQuit, CancellationToken cancellationToken, bool updateMods)
private void UpgradeLocal(bool validate, bool updateMods, bool steamCmdRemoveQuit, CancellationToken cancellationToken)
{
if (_profile == null)
{
@ -1182,7 +1198,7 @@ namespace ServerManagerTool.Lib
{
// perform a steamcmd validate to confirm all the files
LogProfileMessage("Validating server files (*new*).");
UpgradeLocal(true, false, CancellationToken.None, false);
UpgradeLocal(true, false, false, CancellationToken.None);
LogProfileMessage("Validated server files (*new*).");
}
@ -1703,17 +1719,6 @@ namespace ServerManagerTool.Lib
ExitCode = EXITCODE_NORMALEXIT;
}
private void CloseRconConsole()
{
if (_rconConsole != null)
{
_rconConsole.Dispose();
_rconConsole = null;
Task.Delay(1000).Wait();
}
}
public void CreateProfileBackupArchiveFile(ServerProfileSnapshot profile = null)
{
var oldProfile = _profile;
@ -2413,36 +2418,43 @@ namespace ServerManagerTool.Lib
int rconRetries = 0;
int maxRetries = retryIfFailed ? RCON_MAXRETRIES : 1;
while (retries < maxRetries && rconRetries < RCON_MAXRETRIES)
try
{
SetupRconConsole();
if (_rconConsole == null)
while (retries < maxRetries && rconRetries < RCON_MAXRETRIES)
{
LogProfileMessage($"RCON> {command} - attempt {rconRetries + 1} (a).", false);
LogProfileMessage("RCON connection could not be created.", false);
rconRetries++;
}
else
{
rconRetries = 0;
try
{
_rconConsole.SendCommand(command);
LogProfileMessage($"RCON> {command}");
SetupRconConsole();
return true;
}
catch (Exception ex)
if (_rconConsole == null)
{
LogProfileMessage($"RCON> {command} - attempt {retries + 1} (b).", false);
LogProfileMessage($"{ex.Message}", false);
LogProfileMessage($"{ex.StackTrace}", false);
LogProfileMessage($"RCON> {command} - attempt {rconRetries + 1} (a).", false);
LogProfileMessage("RCON connection could not be created.", false);
rconRetries++;
}
else
{
rconRetries = 0;
try
{
_rconConsole.SendCommand(command);
LogProfileMessage($"RCON> {command}");
retries++;
return true;
}
catch (Exception ex)
{
LogProfileMessage($"RCON> {command} - attempt {retries + 1} (b).", false);
LogProfileMessage($"{ex.Message}", false);
LogProfileMessage($"{ex.StackTrace}", false);
}
retries++;
}
}
}
finally
{
CloseRconConsole();
}
return false;
}
@ -2504,6 +2516,17 @@ namespace ServerManagerTool.Lib
}
}
private void CloseRconConsole()
{
if (_rconConsole != null)
{
_rconConsole.Dispose();
_rconConsole = null;
Task.Delay(1000).Wait();
}
}
private void SetupRconConsole()
{
CloseRconConsole();
@ -2513,7 +2536,7 @@ namespace ServerManagerTool.Lib
try
{
var endPoint = new IPEndPoint(IPAddress.Parse(_profile.ServerIP), _profile.RconPort);
var endPoint = new IPEndPoint(_profile.ServerIPAddress, _profile.RconPort);
var server = QueryMaster.ServerQuery.GetServerInstance(QueryMaster.EngineType.Source, endPoint, sendTimeOut: 10000, receiveTimeOut: 10000);
if (server == null)
{
@ -2551,7 +2574,7 @@ namespace ServerManagerTool.Lib
}
}
public int PerformProfileBackup(ServerProfileSnapshot profile)
public int PerformProfileBackup(ServerProfileSnapshot profile, CancellationToken cancellationToken)
{
_profile = profile;
@ -2573,7 +2596,7 @@ namespace ServerManagerTool.Lib
// check if the mutex was established
if (createdNew)
{
BackupServer();
BackupServer(cancellationToken);
if (ExitCode != EXITCODE_NORMALEXIT)
{
@ -2926,7 +2949,7 @@ namespace ServerManagerTool.Lib
SendEmails = true,
ServerProcess = ServerProcessType.AutoBackup
};
exitCodes.TryAdd(profile, app.PerformProfileBackup(profile));
exitCodes.TryAdd(profile, app.PerformProfileBackup(profile, CancellationToken.None));
});
foreach (var profile in _profiles.Keys)