private void bindConfig(IConfiguration config)
        {
            _config = new RconConfig();
            config.GetSection(RconConfigSection).Bind(_config);

            if (string.IsNullOrWhiteSpace(_config.Hostname))
            {
                throw new Exception("Rcon config is invalid. Missing hostname.");
            }

            if (_config.Port == 0)
            {
                throw new Exception("Rcon config is invalid. Missing port.");
            }

            if (string.IsNullOrWhiteSpace(_config.Password))
            {
                throw new Exception("Rcon config is invalid. Missing password.");
            }
        }
Пример #2
0
        public Configuration(string pathToConfig)
        {
            XmlReaderSettings configReaderSettings = new XmlReaderSettings();
            Stream s = typeof(Configuration).Assembly.GetManifestResourceStream("Project2Q.SDK.ConfigSchema.xsd");
            StreamReader configStreamReader = new StreamReader(pathToConfig);

            configReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
            configReaderSettings.ValidationType = ValidationType.Schema;
            configReaderSettings.IgnoreWhitespace = true;
            configReaderSettings.IgnoreComments = true;
            configReaderSettings.ValidationEventHandler += new ValidationEventHandler(configReaderSettings_ValidationEventHandler);
            configReaderSettings.Schemas.Add(null, XmlReader.Create(s));

            XmlReader cfr = XmlReader.Create(configStreamReader, configReaderSettings);
            try {

                #region Config::Settings
                cfr.Read(); // jump from <Configuration> to <Settings>
                NextNode(cfr);
                string defaultNickName = cfr.Value;
                NextNode(cfr);
                string defaultAlternateNickName = cfr.Value;
                NextNode(cfr);
                string defaultUserName = cfr.Value;
                NextNode(cfr);
                string defaultUserInfo = cfr.Value;
                NextNode(cfr);
                int defaultPort = int.Parse(cfr.Value);
                NextNode(cfr);
                string defaultQuitMessage = cfr.Value;
                NextNode( cfr );
                bool defaultAutoJoinOnInvite = bool.Parse(cfr.Value);
                NextNode(cfr);
                int defaultOperationTimeout = int.Parse(cfr.Value);
                NextNode(cfr);
                int defaultRetryTimeout = int.Parse(cfr.Value);
                NextNode(cfr);
                int defaultSocketBufferSize = int.Parse(cfr.Value);
                NextNode(cfr);
                int defaultSendInhibit = int.Parse(cfr.Value);

                #endregion

                #region Config::Servers

                //<Servers>
                NextNode(cfr);

                ServerConfig serv;
                string[] hostList;
                string serverName, username, userInfo, nickName, altNick, quitMessage;
                int sendInhibit, serverPort, operationTimeout, retryTimeout, socketBufferSize;
                bool autoJoinOnInvite, jj;
                uint ii;
                this.serverTempList = new LinkedList<ServerConfig>();

                while (cfr.Name.Equals("Server")) { //Quick Cleanup
                    hostList = new string[32];
                    serverName = cfr.GetAttribute(0);
                    nickName = defaultNickName;
                    username = defaultUserName;
                    userInfo = defaultUserInfo;
                    altNick = defaultAlternateNickName;
                    autoJoinOnInvite = defaultAutoJoinOnInvite;
                    operationTimeout = defaultOperationTimeout;
                    retryTimeout = defaultRetryTimeout;
                    socketBufferSize = defaultSocketBufferSize;
                    sendInhibit = defaultSendInhibit;
                    serverPort = defaultPort;
                    quitMessage = defaultQuitMessage;

                    ii = 0;
                    jj = true;
                    cfr.Read();

                    while (jj) {
                        switch (cfr.Name) {
                        case "dns":
                            cfr.Read();
                            if (ii < 32) hostList[ii++] = cfr.Value;
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "nickname":
                            cfr.Read();
                            nickName = cfr.Value;
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "alternate":
                            cfr.Read();
                            altNick = cfr.Value;
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "username":
                            cfr.Read();
                            username = cfr.Value;
                            cfr.Read();
                            cfr.Read();
                            break;
                        case "info":
                            cfr.Read();
                            userInfo = cfr.Value;
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "port":
                            cfr.Read();
                            serverPort = int.Parse(cfr.Value);
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "quitMessage":
                            cfr.Read();
                            quitMessage = cfr.Value;
                            cfr.Read();
                            cfr.Read();
                            break;
                        case "autoJoinOnInvite":
                            cfr.Read();
                            autoJoinOnInvite = bool.Parse(cfr.Value);
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "operationTimeout":
                            cfr.Read();
                            operationTimeout = int.Parse( cfr.Value );
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "retryTimeout":
                            cfr.Read();
                            retryTimeout = int.Parse( cfr.Value );
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "socketBufferSize":
                            cfr.Read();
                            socketBufferSize = int.Parse( cfr.Value );
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        case "sendInhibit":
                            cfr.Read();
                            sendInhibit = int.Parse(cfr.Value);
                            cfr.Read(); //This will read past the data
                            cfr.Read(); //This will read to the next node.
                            break;
                        default:
                            cfr.Read();
                            jj = false;
                            break;
                        }
                    }
                    serv = new ServerConfig(nickName, altNick, username, userInfo, serverName, autoJoinOnInvite,
                        hostList, serverPort, sendInhibit, operationTimeout, retryTimeout, socketBufferSize, quitMessage);

                    serverTempList.AddLast(serv);
                }

                #endregion

                #region Config::Modules

                if (cfr.Name.Equals("Modules")) {

                    moduleTempList = new LinkedList<ModuleConfig>();
                    string[] fileNames, servers = null;
                    string[] includes = new string[128];
                    string moduleName = null, moduleLang = null, prettyName = null;
                    ModuleConfig.ModulePath = cfr.GetAttribute( "modulePath" );
                    ModuleConfig.IncludePath = cfr.GetAttribute( "includePath" );
                    ModuleConfig.FrameworkPath = cfr.GetAttribute( "frameworkPath" );
                    ModuleConfig.ModulePrefix = cfr.GetAttribute( "prefix" );
                    ModuleConfig.ModulePrefix = ModuleConfig.ModulePrefix == null ? "?" : ModuleConfig.ModulePrefix;
                    ModuleConfig.FrameworkPath = ModuleConfig.FrameworkPath == null ?
                        System.IO.Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR"), @"Microsoft.NET\Framework\v2.0.50727")
                        : ModuleConfig.FrameworkPath;

                    cfr.Read();

                    while (cfr.Name.Equals("Module")) {
                        moduleName = cfr.GetAttribute(0);
                        cfr.Read();

                        if ( cfr.Name.Equals( "prettyname" ) ) {
                            cfr.Read();
                            prettyName = cfr.Value;
                            cfr.Read(); cfr.Read(); cfr.Read();
                        }
                        else {
                            prettyName = moduleName;
                            cfr.Read();
                        }

                        fileNames = cfr.Value.Split(';');
                        cfr.Read(); cfr.Read();

                        servers = null;
                        bool isScript = false;

                        if (cfr.Name.Equals("script")) {
                            cfr.Read();
                            cfr.Read();
                            includes = cfr.Value.Split(';');

                            NextNode(cfr);
                            moduleLang = cfr.Value;
                            NextNode(cfr);

                            isScript = true;
                        }

                        if (cfr.Name.Equals("servers")) {
                            cfr.Read();
                            servers = cfr.Value.Split(';');
                            Array.Sort<string>( servers );
                            NextNode(cfr);
                        }

                        if ((cfr.Name.Equals("Module")) && (cfr.NodeType.Equals(System.Xml.XmlNodeType.EndElement)))
                            cfr.Read();

                        this.moduleTempList.AddLast(new ModuleConfig(moduleName, prettyName, moduleLang, fileNames, includes, servers, isScript));
                     }
                }

                #endregion

                #region Config::RemoteConsole

                cfr.Read();
                if (cfr.Name.Equals("RemoteConsole")) {
                    int rconOperationTimeout = defaultOperationTimeout;
                    int rconRetryTimeout = defaultRetryTimeout;
                    int rconSocketBufferSize = defaultSocketBufferSize;
                    int rconSendInhibit = defaultSendInhibit;
                    cfr.Read(); cfr.Read();
                    string initialAuth = cfr.Value;
                    NextNode(cfr);
                    int rconPort = int.Parse(cfr.Value);
                    cfr.Read(); cfr.Read();

                    if (cfr.Name.Equals("operationTimeout")) {
                        cfr.Read();
                        rconOperationTimeout = int.Parse(cfr.Value);
                        cfr.Read(); cfr.Read();
                    }
                    if (cfr.Name.Equals("retryTimeout")) {
                        cfr.Read();
                        rconRetryTimeout = int.Parse(cfr.Value);
                        cfr.Read(); cfr.Read();
                    }
                    if (cfr.Name.Equals("socketBufferSize")) {
                        cfr.Read();
                        rconSocketBufferSize = int.Parse(cfr.Value);
                        cfr.Read(); cfr.Read();
                    }
                    if (cfr.Name.Equals("sendInhibit")) {
                        cfr.Read();
                        rconSendInhibit = int.Parse(cfr.Value);
                        cfr.Read(); cfr.Read();
                    }

                    this.rconConfig = new RconConfig(initialAuth, rconOperationTimeout, rconRetryTimeout,
                        rconSendInhibit, rconSocketBufferSize, rconPort);
                }

                #endregion

            }

            finally {
                cfr.Close();
                configStreamReader.Close();
                s.Close();
            }
        }
Пример #3
0
 public void SetConfig(RconConfig config)
 {
     _config = config;
     Port    = config.ListenPort;
 }