Пример #1
0
        public object GetSetting(string section, string key, object defaultValue = null)
        {
            lock (SyncObject) {
                if (section == null)
                {
                    section = String.Empty;
                }

                if (key == null)
                {
                    key = String.Empty;
                }

                //ConfigFileSection sec = Sections.FirstOrDefault (s => s.UpperName == section.ToUpperInvariant());
                ConfigFileSection sec = Sections.FirstOrDefault(s => s.Name.Equals(section, StringComparison.InvariantCultureIgnoreCase));

                if (sec == null)
                {
                    return(defaultValue);
                }

                //ConfigFileEntry entry = sec.Entries.FirstOrDefault (e => e.UpperKey == key.ToUpperInvariant ());
                ConfigFileEntry entry = sec.Entries.FirstOrDefault(e => e.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
                if (entry == null)
                {
                    return(defaultValue);
                }

                return(entry.Value);
            }
        }
Пример #2
0
 private void LoadCfg()
 {
     if (File.Exists(ConfigPath))
     {
         Log.AddLogMessage("Loading Config...", "MW::LoadCfg", Log.LogLevel.DEBUG);
         string   ConfigFile        = File.ReadAllText(ConfigPath);
         string[] ConfigFileEntries = ConfigFile.Split(';');
         foreach (string ConfigFileEntry in ConfigFileEntries)
         {
             if (ConfigFileEntry == "")
             {
                 continue;
             }
             string key = ConfigFileEntry.Split('=')[0];
             string val = ConfigFileEntry.Split('=')[1];
             Log.AddLogMessage($"|> Config option {key} is set to {val}", "MW::LoadCfg", Log.LogLevel.DEBUG);
             Cfg.Add(key, val);
         }
         Log.AddLogMessage("Config loaded", "MW::LoadCfg", Log.LogLevel.DEBUG);
     }
     else
     {
         Log.AddLogMessage("No config file found, applying default config...", "MW::LoadConfig", Log.LogLevel.DEBUG);
         Cfg["GamePath"] = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\My Games\\Trainsimworld2\\Saved\\SaveGames\\UGCLiveries_0.sav";
         SaveCfg();
         Log.AddLogMessage("Default config applied", "MW::LoadCfg", Log.LogLevel.DEBUG);
     }
 }
Пример #3
0
        protected override void ProcessRecord()
        {
            if (isInitialized())
            {
                try
                {
                    iControl.SystemConfigSyncConfigFileEntry [] config_list =
                        GetiControl().SystemConfigSync.get_configuration_list();
                    foreach (iControl.SystemConfigSyncConfigFileEntry entry in config_list)
                    {
                        ConfigFileEntry cfe = new ConfigFileEntry();
                        cfe.Name      = entry.file_name;
                        cfe.TimeStamp = entry.file_datetime;

                        WriteObject(cfe);
                    }
                }
                catch (Exception ex)
                {
                    handleException(ex);
                }
            }
            else
            {
                handleNotInitialized();
            }
        }
Пример #4
0
        public void SetSetting(string section, string key, object value, object defaultValue = null)
        {
            lock (SyncObject) {
                if (section == null)
                {
                    section = String.Empty;
                }

                if (key == null)
                {
                    key = String.Empty;
                }

                if (value == null)
                {
                    value = defaultValue;
                }

                ConfigFileSection sec   = Sections.FirstOrDefault(s => s.Name.Equals(section, StringComparison.InvariantCultureIgnoreCase));
                ConfigFileEntry   entry = null;

                if (sec == null)
                {
                    sec = new ConfigFileSection(section);
                    Sections.Add(sec);
                    Dirty = true;
                }
                else
                {
                    entry = sec.Entries.FirstOrDefault(e => e.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
                }

                if (entry == null)
                {
                    entry = new ConfigFileEntry(key, value);
                    sec.Entries.Add(entry);
                    Dirty = true;
                }
                else
                {
                    if (entry.Value.SafeString() != value.SafeString())
                    {
                        entry.Value = value;
                        Dirty       = true;
                    }
                }
            }
        }
Пример #5
0
        public void Load()
        {
            ApplyDefaults(false);
            if (File.Exists(ConfigPath))
            {
                Log.AddLogMessage("Loading Config...", "Config::Load", Log.LogLevel.DEBUG);
                string   ConfigFile        = File.ReadAllText(ConfigPath);
                string[] ConfigFileEntries = ConfigFile.Split(';');
                foreach (string ConfigFileEntry in ConfigFileEntries)
                {
                    if (ConfigFileEntry == "")
                    {
                        continue;
                    }
                    string key = ConfigFileEntry.Split('=')[0];
                    string val = ConfigFileEntry.Split('=')[1];
                    Log.AddLogMessage($"|> Config option '{key}' is set to '{val}'", "Config::Load", Log.LogLevel.DEBUG);
                    try
                    {
                        switch (key)
                        {
                        case "GamePath": _gamePath = val; break;

                        case "LibraryPath": _libraryPath = val; break;

                        case "MaxGameLiveries": _maxGameLiveries = int.Parse(val); break;

                        case "NoUpdate": _noUpdate = val == "true"; break;

                        case "DevUpdates": _devUpdates = val == "true"; break;
                        }
                    }
                    catch (Exception)
                    {
                        Log.AddLogMessage("Error loading config; applying default config...", "Config::Load", Log.LogLevel.WARNING);
                        ApplyDefaults();
                    }
                }
                Log.AddLogMessage("Config loaded", "Config::Load", Log.LogLevel.DEBUG);
            }
        }