public string InterpolateCommandline(GameServer gameServer) { string outputCommandline = gameServer.CommandLine; var variables = ConfigFileUtils.GetVariablesFromGameServer(gameServer); foreach (KeyValuePair <string, string> kv in variables) { _logger.LogDebug($"Attempting to insert {kv.Key} into commandline with value {kv.Value}"); outputCommandline = outputCommandline.Replace(kv.Key, kv.Value); } return(outputCommandline); }
public async Task Invoke(int gameServerId) { var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(gameServerId)); _repository.List(GameServerConfigFilePolicy.ByServerId(gameServer.Id)); foreach (var gameServerGameConfigFile in gameServer.GameConfigFiles) { _logger.LogDebug($"Processing config {gameServerGameConfigFile.FilePath} for game server {gameServer.Id}"); var variables = ConfigFileUtils.GetVariablesFromGameServer(gameServer); if (File.Exists(Path.Combine(gameServer.HomeDirectory, gameServerGameConfigFile.FilePath))) { _logger.LogDebug($"Found existing config ({gameServerGameConfigFile.FilePath})"); FileStream fileStream = new FileStream(Path.Combine(gameServer.HomeDirectory, gameServerGameConfigFile.FilePath), FileMode.Open); using (StreamReader reader = new StreamReader(fileStream)) { gameServerGameConfigFile.FileContent = reader.ReadToEnd(); } _repository.Update(gameServerGameConfigFile); } var config = ConfigFileUtils.InterpolateConfigFromDict(variables, gameServerGameConfigFile.FileContent); try { using (StreamWriter file = new StreamWriter(Path.Combine(gameServer.HomeDirectory, gameServerGameConfigFile.FilePath))) { file.Write(config); } } catch (Exception e) { _logger.LogError($"Failed to write {gameServerGameConfigFile.FilePath} for server {gameServer.Id}"); } } }
public void GetVariablesFromGameServerShould() { var gameServer = new GameServerFactory().GetGameServer(); var expected = new Dictionary <string, string>() { { "![GameId]", "0" }, { "![IpAddress]", "192.168.1.50" }, { "![GamePort]", "29365" }, { "![QueryPort]", "0" }, { "![ServerName]", "Test Server" }, { "![IsEnabled]", "True" }, { "![HomeDirectory]", @"C:\dev\Server1" }, { "![CommandLine]", "-game csgo -console -usercon" }, { "![Slots]", "0" }, { "![RconPassword]", "password" }, { "![TestVar1]", "thing1" }, { "![TestVar2]", "thing2" }, { "![TestVar3]", "thing3" } }; var result = ConfigFileUtils.GetVariablesFromGameServer(gameServer); Assert.Equal(expected, result); }
public Task <CommandResponseGameServer> Handle(CreateServerCommand request, CancellationToken cancellationToken) { _logger.LogDebug($"Running Handler CreateServerCommandHandler"); var response = new CommandResponseGameServer(); var gameServer = request.gameServer; var game = _repository.Single(DataItemPolicy <Game> .ById(gameServer.GameId)); if (game == null) { response.status = CommandResponseStatusEnum.Error; response.message = $"Unable to locate game with ID {gameServer.GameId}"; _mediator.Publish(new ServerInstallStatusNotification("error", $"Unable to locate game with ID {gameServer.GameId}")); return(Task.FromResult(response)); } var configFiles = _repository.List(GameDefaultConfigPolicy.ByGameId(game.Id)); gameServer.GameConfigFiles = new List <GameServerConfigFile>(); var variables = ConfigFileUtils.GetVariablesFromGameServer(gameServer); foreach (var configFile in configFiles) { gameServer.GameConfigFiles.Add(new GameServerConfigFile() { Description = configFile.Description, FileContent = configFile.Template, FilePath = configFile.FilePath, GameServer = gameServer, GameDefaultConfigFile = configFile }); } gameServer.GamePort = _portProvider.GetNextAvailablePort(game.GamePort, gameServer.IpAddress, game.PortIncrement); gameServer.QueryPort = _portProvider.GetNextAvailablePort(game.QueryPort, gameServer.IpAddress, game.PortIncrement); gameServer.HomeDirectory = Path.Combine(_dirProvider.GetBaseInstallDirectory(), gameServer.Guid.ToString()); gameServer.GameServerCurrentStats = new GameServerCurrentStats(); gameServer.GameServerCurrentStats.Status = ServerStatusStates.Installing; try { _repository.Create(gameServer); } catch (Exception e) { Console.WriteLine(e); response.status = CommandResponseStatusEnum.Success; response.message = e.ToString(); _mediator.Publish(new ServerInstallStatusNotification("error", e.ToString())); return(Task.FromResult(response)); } try { _serverManager.InstallGameServer(gameServer); _mediator.Publish(new ServerInstallStatusNotification("installing", $"Game server install started for server {gameServer.Guid}")); response.status = CommandResponseStatusEnum.Success; response.message = "Game server now installing"; } catch (Exception e) { Console.WriteLine(e); response.status = CommandResponseStatusEnum.Error; response.message = e.ToString(); _mediator.Publish(new ServerInstallStatusNotification("error", e.ToString())); } return(Task.FromResult(response)); }