private static void StartHostedGameProcess(HostedGame req) { var broadcastPort = AppConfig.Instance.GameBroadcastPort; var args = HostedGameProcess.CreateArguments(req, broadcastPort, false); var argString = string.Join(Environment.NewLine, args); var fileName = "F:\\SASRequests"; if (!Directory.Exists(fileName)) { Directory.CreateDirectory(fileName); } fileName = Path.Combine(fileName, req.Id.ToString() + ".startrequest"); using (var stream = File.Open(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.None)) using (var writer = new StreamWriter(stream)) { writer.WriteLine(req.OctgnVersion); writer.Write(argString); writer.Flush(); } }
public static async Task <Guid> HostGame(HostedGame req) { // Try to kill every other game this asshole started before this one. var others = _gameListener.Games.Where(x => x.HostUser.Equals(req.HostUser)) .ToArray(); foreach (var g in others) { try { g.KillGame(); } catch (InvalidOperationException ex) { Log.Error($"{nameof(HostGame)}: Error killing game. See inner exception for more details.", ex); } } var bport = AppConfig.Instance.GameBroadcastPort; req.Id = Guid.NewGuid(); req.HostAddress = AppConfig.Instance.HostName + ":" + _networkHelper.NextPort.ToString(); var waitTask = _gameListener.WaitForGame(req.Id); var gameProcess = new HostedGameProcess(req, Service.IsDebug, false, AppConfig.Instance.GameBroadcastPort); gameProcess.Start(); await waitTask; return(req.Id); }
async Task StartLocalGame(DataNew.Entities.Game game, string name, string password) { var hg = new HostedGame() { Id = Guid.NewGuid(), Name = name, HostUser = Program.LobbyClient?.Me, GameName = game.Name, GameId = game.Id, GameVersion = game.Version.ToString(), HostAddress = $"0.0.0.0:{HostPort}", Password = password, GameIconUrl = game.IconUrl, Spectators = true, }; if (Program.LobbyClient?.Me != null) { hg.HostUserIconUrl = ApiUserCache.Instance.ApiUser(Program.LobbyClient.Me).IconUrl; } // We don't use a userid here becuase we're doing a local game. var hs = new HostedGameProcess(hg, X.Instance.Debug, true); hs.Start(); Program.GameSettings.UseTwoSidedTable = HostGame.UseTwoSidedTable; Program.IsHost = true; Program.GameEngine = new GameEngine(game, Prefs.Nickname, false, password, true); var ip = IPAddress.Parse("127.0.0.1"); for (var i = 0; i < 5; i++) { try { Program.Client = new Octgn.Networking.ClientSocket(ip, HostPort); await Program.Client.Connect(); return; } catch (Exception e) { Log.Warn("Start local game error", e); if (i == 4) { throw; } } Thread.Sleep(2000); } throw new UserMessageException("Cannot start local game. You may be missing a file."); }
async Task <Process> LaunchGameProcess(Version sasVersion, string[] args, bool isDebug) { var path = HostedGameProcess.GetSASPath(sasVersion, false, isDebug); var pathFileInfo = new FileInfo(path); var completePath = Path.Combine(pathFileInfo.Directory.FullName, "complete"); // wait for file to exist { var waitCount = 0; while (!File.Exists(path) && !File.Exists(completePath)) { if (waitCount >= 10) { if (!File.Exists(path)) { throw new FileNotFoundException($"File Not Found: {path}", path); } else if (!File.Exists(completePath)) { throw new FileNotFoundException($"File Not Found: {completePath}", completePath); } else { break; // file found last minute, #tyjezus } } await Task.Delay(1000, _cancellationTokenSource.Token); waitCount++; } } var process = new Process(); process.StartInfo.Arguments = string.Join(" ", args); process.StartInfo.FileName = path; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); return(process); }
async Task StartLocalGame(DataNew.Entities.Game game, string name, string password) { var hostport = new Random().Next(5000, 6000); while (!NetworkHelper.IsPortAvailable(hostport)) { hostport++; } var hg = new HostedGame() { Id = Guid.NewGuid(), Name = name, HostUser = Program.LobbyClient?.User ?? new User(hostport.ToString(), Username), GameName = game.Name, GameId = game.Id, GameVersion = game.Version.ToString(), HostAddress = $"0.0.0.0:{hostport}", Password = password, GameIconUrl = game.IconUrl, Spectators = true, }; if (Program.LobbyClient?.User != null) { hg.HostUserIconUrl = ApiUserCache.Instance.ApiUser(Program.LobbyClient.User)?.IconUrl; } // Since it's a local game, we want to use the username instead of a userid, since that won't exist. var hs = new HostedGameProcess(hg, X.Instance.Debug, true); hs.Start(); Prefs.Nickname = hg.HostUser.DisplayName; Program.GameEngine = new GameEngine(game, Username, false, password, true); Program.CurrentOnlineGameName = name; Program.IsHost = true; var ip = IPAddress.Parse("127.0.0.1"); for (var i = 0; i < 5; i++) { try { Program.Client = new ClientSocket(ip, hostport); await Program.Client.Connect(); SuccessfulHost = true; return; } catch (Exception e) { Log.Warn("Start local game error", e); if (i == 4) { throw; } } Thread.Sleep(2000); } }