コード例 #1
0
ファイル: SimpleMessages.cs プロジェクト: josemaripl/MBCon
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new SimpleMessagesException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting("SimpleMessages", "Enabled");
            if (!_enabled)
                throw new SimpleMessagesException(String.Format("{0} has been disabled", Name));

            Int32 _interval;
            try
            {
                _interval = Convert.ToInt32(_ini.GetSetting("SimpleMessages", "Interval", "60"));
            }
            catch
            {
                _interval = 60;
            }

            _prefix = _ini.GetSetting("SimpleMessages", "Prefix");

            _repeat = _ini.GetBoolSetting("SimpleMessages", "Repeat");
            var _random = _ini.GetBoolSetting("SimpleMessages", "Random");
            _messages = _ini.EnumSection("Messages");
            if (_random)
            {
                var rnd = new Random();
                _messages = _messages.OrderBy(x => rnd.Next()).ToArray();
            }
            _timer = new Timer {Interval = (_interval*1000)};
            _timer.Elapsed += _timer_Elapsed;
            _timer.Enabled = true;
        }
コード例 #2
0
ファイル: Settings.cs プロジェクト: josemaripl/MBCon
        public Settings(IniParser ini)
        {
            string beconfig;

            if (!IPAddress.TryParse(ini.GetSetting("General", "IP").Trim(), out _address))
            {
                throw new SettingsException("Port is not set.");
            }

            try
            {
                _port = Convert.ToInt32(ini.GetSetting("General", "Port").Trim());
                if (_port > 65535)
                    throw new SettingsException("Port is too high.");
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Port is not set or invalid: {0}", ex.Message));
            }

            try
            {
                _bepath = ini.GetSetting("General", "BEPath").Trim();
                if (!Directory.Exists(_bepath))
                    throw new SettingsException("BEPath does not exist.");
                beconfig = Path.Combine(_bepath, "BEServer.cfg");
                if (!File.Exists(beconfig))
                {
                    var files = Directory.GetFiles(_bepath, "*.cfg")
                     .Where(path => path.Contains("BEServer_active_"))
                     .ToList();
                    if (files.Count == 0)
                        throw new SettingsException("Cound not find BEServer.cfg.");
                    beconfig = Path.Combine(_bepath, files[0]);
                }
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("BEPath is invalid: {0}", ex.Message));
            }

            try
            {
                var file = new StreamReader(beconfig);
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    if (line.StartsWith("RConPassword "))
                    {
                        _rconpass = line.Replace("RConPassword ", "");
                        break;
                    }
                }
                if (_rconpass == null)
                    throw new SettingsException("Could not find rcon password in BEServer.cfg.");
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Error reading BEServer.cfg: {0}", ex.Message));
            }

            try
            {
                _pluginpath = ini.GetSetting("General", "PluginPath").Trim();
                if (_pluginpath == "")
                {
                    _pluginpath = Path.Combine(Core.BasePath, "plugins");
                }
                if (!Directory.Exists(_pluginpath))
                    Directory.CreateDirectory(_pluginpath);
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Error reading plugin directory: {0}", ex.Message));
            }

            try
            {
                _admins = new Dictionary<string, string>();
                var adminsList = ini.EnumSection("Admins");
                foreach (var guid in adminsList)
                {
                    var name = ini.GetSetting("Admins", guid);
                    _admins.Add(guid.Trim(), name.Trim());
                }
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Error reading settings: {0}", ex.Message));
            }

        }