Пример #1
0
        internal void LoadProfile(ServerProfile profile)
        {
            try
            {
                if (profile == null) return;
                BnetServerIp.Text = profile.MooNetServerIp;
                GameServerIp.Text = profile.GameServerIp;
                PublicServerIp.Text = profile.NatIp;
                BnetServerPort.Text = profile.MooNetServerPort;
                GameServerPort.Text = profile.GameServerPort;
                MotdTxtBox.Text = profile.MooNetServerMotd;
                NATcheckBox.Checked = profile.NatEnabled;

                //Loading a profile means it has the correct values for every box, so first we disable every red cross that might be out there.
                ErrorBnetServerIp.Visible = false;
                ErrorBnetServerPort.Visible = false;
                ErrorGameServerIp.Visible = false;
                ErrorGameServerPort.Visible = false;
                ErrorGameServerPort.Visible = false;
                ErrorPublicServerIp.Visible = false;
                //Loading a profile means it has the correct values for every box, so we change everything to green ticked.
                TickBnetServerIP.Visible = true;
                TickBnetServerPort.Visible = true;
                TickGameServerIp.Visible = true;
                TickGameServerPort.Visible = true;
                TickGameServerPort.Visible = true;
                TickPublicServerIp.Visible = true;
                Console.WriteLine("Loaded server profile [{0}] succesfully.", profile);
                Configuration.MadCow.CurrentProfile = profile;
            }
            catch (Exception e)
            {
                Console.WriteLine("[Error] While loading server profile.");
            }
        }
Пример #2
0
        private void SaveProfile_Click(object sender, EventArgs e)
        {
            //Match Pattern
            const string pattern = @"(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|255[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
            var check = new Regex(pattern);
            string[] ipFields = { BnetServerIp.Text, GameServerIp.Text, PublicServerIp.Text };
            var i = 0; //Foreach IP counter.
            var j = 0; //Foreach PORT counter.
            var invalidIP = false;

            //Error handling if textbox null -> Giving feedback in the textbox field.
            for (var x = 0; x < ipFields.Length; x++)
            {
                if (string.IsNullOrEmpty(ipFields[x]))
                {
                    IpErrorHandler(x);
                    invalidIP = true;
                }
            }

            //VALIDATION FOR IP
            if (invalidIP == false)
            {
                foreach (var value in ipFields)
                {
                    for (var lp = 0; lp < 999; lp++)
                    {
                        var ipAddress = string.Format("{0}.{0}.{0}.{0}", lp);

                        if (Regex.Match(ipFields[i], pattern).Success)
                        {
                            IpCorrectHandler(i);
                        }
                        else
                        {
                            IpErrorHandler(i);
                            invalidIP = true;
                            break;
                        }
                    }
                    i++;
                }
            }

            //VALIDATION FOR PORT

            string[] portFields = { BnetServerPort.Text, GameServerPort.Text };
            int Number;
            bool isNumber;

            for (var x = 0; x < portFields.Length; x++)
            {
                if (string.IsNullOrEmpty(portFields[x]))
                {
                    PortErrorHandler(x);
                    invalidIP = true;
                }
            }

            foreach (var value in portFields)
            {
                isNumber = int.TryParse(portFields[j], out Number);

                if (!isNumber || portFields[j].Length > 4 || portFields[j].Length < 4)
                {
                    PortErrorHandler(j);
                    invalidIP = true;
                }
                else
                {
                    PortCorrectHandler(j);
                }
                j++;
            }

            if (invalidIP) return;
            //If invalidIP == false which means all fields are valid, we hide any error cross that might be around.
            ErrorBnetServerIp.Visible = false;
            ErrorBnetServerPort.Visible = false;
            ErrorGameServerIp.Visible = false;
            ErrorGameServerPort.Visible = false;
            ErrorGameServerPort.Visible = false;
            ErrorPublicServerIp.Visible = false;

            //If invalidIP == false which means all fields are valid, we show all green ticks!
            TickBnetServerIP.Visible = true;
            TickBnetServerPort.Visible = true;
            TickGameServerIp.Visible = true;
            TickGameServerPort.Visible = true;
            TickGameServerPort.Visible = true;
            TickPublicServerIp.Visible = true;

            //We proceed to ask the user where to save the file.
            var saveProfile = new SaveFileDialog
                                  {
                                      Title = "Save Server Profile",
                                      DefaultExt = ".mdc",
                                      Filter = "MadCow Profile|*.mdc",
                                      InitialDirectory = Paths.ServerProfilesPath
                                  };
            saveProfile.ShowDialog();

            if (string.IsNullOrEmpty(saveProfile.FileName))
            {
                Console.WriteLine("You didn't specify a profile name");
            }
            else
            {
                var profile = new ServerProfile(saveProfile.FileName)
                                  {
                                      MooNetServerIp = BnetServerIp.Text,
                                      GameServerIp = GameServerIp.Text,
                                      NatIp = PublicServerIp.Text,
                                      MooNetServerPort = BnetServerPort.Text,
                                      GameServerPort = GameServerPort.Text,
                                      MooNetServerMotd = MotdTxtBox.Text,
                                      NatEnabled = NATcheckBox.Checked
                                  };
                Console.WriteLine("Saved profile [{0}] succesfully.", profile);
                profile.Save();
                Configuration.MadCow.CurrentProfile = profile;
            }
        }