public void StartGame()
{
Client client;
client = new Client();
string addr = LobbyModel.GameServerConnectionString;
string[] args = addr.Split(':');
Trace.Assert(args.Length == 2);
client.IpString = args[0];
client.PortNumber = int.Parse(args[1]);
busyIndicator.BusyContent = Resources["Busy.JoinGame"];
busyIndicator.IsBusy = true;
LobbyViewModel.Instance.OnChat -= LobbyModel_OnChat;
chatBox.Document.Blocks.Clear();
//client.Start(isReplay, FileStream = file.open(...))
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (o, ea) =>
{
try
{
ea.Result = false;
var stream = FileRotator.CreateFile("./Replays", "SGSREPLAY", ".sgs", 10);
stream.Write(BitConverter.GetBytes((int)0), 0, 4);
client.RecordStream = stream;
client.Start(stream, LobbyModel.LoginToken);
MainGame game = null;
Application.Current.Dispatcher.Invoke((ThreadStart) delegate()
{
try
{
game = new MainGame();
game.OnNavigateBack += (oo, s) =>
{
s.Navigate(this);
};
game.NetworkClient = client;
if (NavigationService != null)
{
MainGame.BackwardNavigationService = this.NavigationService;
}
else
{
ViewModelBase.IsDetached = true;
}
}
catch (Exception)
{
game = null;
}
});
if (game != null)
{
game.Start();
ea.Result = true;
}
}
catch (Exception e)
{
Trace.TraceError("Connection failure : " + e.StackTrace);
}
};
worker.RunWorkerCompleted += (o, ea) =>
{
if ((bool)ea.Result)
{
return;
}
else
{
busyIndicator.IsBusy = false;
LobbyViewModel.Instance.OnChat += LobbyModel_OnChat;
MessageBox.Show("Failed to create connection for " + LobbyModel.GameServerConnectionString);
Trace.Assert(false);
}
};
worker.RunWorkerAsync();
}