private void GameHosted(object sender, GameHostedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                if (!snooperClosing)
                {
                    ExitSnooper = e.Arguments;

                    startedGameType = StartedGameTypes.Host;
                    lobbyWindow     = IntPtr.Zero;
                    gameWindow      = IntPtr.Zero;
                    gameProcess     = new Process();
                    gameProcess.StartInfo.UseShellExecute        = false;
                    gameProcess.StartInfo.CreateNoWindow         = true;
                    gameProcess.StartInfo.RedirectStandardOutput = true;
                    gameProcess.StartInfo.FileName  = Path.GetFullPath("Hoster.exe");
                    gameProcess.StartInfo.Arguments = e.Parameters;
                    if (gameProcess.Start())
                    {
                        string success = gameProcess.StandardOutput.ReadLine();

                        if (success == "1")
                        {
                            HostingWindow.Close();

                            if (Properties.Settings.Default.HostInfoToChannel) // GameListChannel is ok, coz it can't be changed since the Hosting window is opened
                            {
                                SendMessageToChannel("/me is hosting a game: " + Properties.Settings.Default.HostGameName, gameListChannel);
                            }

                            if (ExitSnooper)
                            {
                                snooperClosing = true;
                                this.Close();
                                return;
                            }

                            if (Properties.Settings.Default.MarkAway)
                            {
                                SendMessageToChannel("/away", null);
                            }
                        }
                        else
                        {
                            HostingWindow.RestoreHostButton();
                            MessageBox.Show(this, "Failed to host a game. You may host too many games recently. Please wait some minutes!", "Failed to host a game", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
                                              ));
        }
示例#2
0
        private void CreateGame(object sender, RoutedEventArgs e)
        {
            if (!PassRegex.IsMatch(GamePassword.Text))
            {
                MessageBox.Show(this, "Password can contain only characters from the English alphabet!", "Wrong characters", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            string WN = "0";

            if (WormNat2.IsChecked.Value)
            {
                WN = "1";

                MessageBox.Show(
                    "Greetings WormNAT2 user!" + Environment.NewLine + Environment.NewLine +
                    "This is a reminder message to remind you that WormNAT2" + Environment.NewLine +
                    "is a free service. Using WormNAT2 tunnels all data" + Environment.NewLine +
                    "through a proxy server hosted by the community, thus" + Environment.NewLine +
                    "consuming bandwidth and other resources. Therefore," + Environment.NewLine +
                    "we''d like to ask you to only use WormNAT2 when you" + Environment.NewLine +
                    "have already tried configuring hosting the proper way." + Environment.NewLine + Environment.NewLine +
                    "Don''t forget that you can find instructions on how" + Environment.NewLine +
                    "to set up hosting here:" + Environment.NewLine + Environment.NewLine +
                    "http://worms2d.info/Hosting", "A friendly reminder", MessageBoxButton.OK, MessageBoxImage.Information);
            }


            // A stringbuilder, because we want to modify the game name
            StringBuilder sb = new StringBuilder(GameName.Text.Trim());

            // Remove illegal characters
            for (int i = 0; i < sb.Length; i++)
            {
                char ch = sb[i];
                if (!WormNetCharTable.EncodeGame.ContainsKey(ch))
                {
                    sb.Remove(i, 1);
                    i -= 1;
                }
            }
            string tmp = sb.ToString().Trim();

            sb.Clear();
            sb.Append(tmp);

            // Save settings
            Properties.Settings.Default.HostGameName      = tmp;
            Properties.Settings.Default.HostUseWormnat    = WormNat2.IsChecked.Value;
            Properties.Settings.Default.HostInfoToChannel = InfoToChannel.IsChecked.Value;
            Properties.Settings.Default.Save();


            // Encode the Game name text
            for (int i = 0; i < sb.Length; i++)
            {
                char ch = sb[i];
                if (ch == '"' || ch == '&' || ch == '\'' || ch == '<' || ch == '>' || ch == '\\')
                {
                    sb.Remove(i, 1);
                    sb.Insert(i, "%" + WormNetCharTable.EncodeGame[ch].ToString("X"));
                    i += 2;
                }
                else if (ch == '#' || ch == '+' || ch == '%')
                {
                    sb.Remove(i, 1);
                    sb.Insert(i, "%" + WormNetCharTable.EncodeGame[ch].ToString("X"));
                    i += 2;
                }
                else if (ch == ' ')
                {
                    sb.Remove(i, 1);
                    sb.Insert(i, "%A0");
                    i += 2;
                }
                else if (WormNetCharTable.EncodeGame[ch] >= 0x80)
                {
                    sb.Remove(i, 1);
                    sb.Insert(i, "%" + WormNetCharTable.EncodeGame[ch].ToString("X"));
                    i += 2;
                }
            }


            Container.IsEnabled = false;

            if (e != null)
            {
                e.Handled = true;
            }

            string highPriority      = Properties.Settings.Default.WAHighPriority ? "1" : "0";
            GameHostedEventArgs args = new GameHostedEventArgs(ServerAddress + " \"" + Properties.Settings.Default.WaExe + "\" " + GlobalManager.User.Name + " \"" + sb.ToString() + "\" \"" + GamePassword.Text + "\" " + ChannelName + " " + ChannelScheme + " " + GlobalManager.User.Country.ID.ToString() + " " + CC + " " + WN + " " + highPriority, ExitSnooper.IsChecked.Value);

            GameHosted.BeginInvoke(this, args, null, null);
        }