示例#1
0
 public SerializableFloatConfig(float defaultValue, float max)
 {
     this.Constraint = new FloatConstraint(defaultValue, max);
 }
        private void ReadConstraintConfig()
        {
            using (FileStream fs = File.Open(configPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                // If doesnt exist, create it
                // If Exists, get all values from this and start applying it to config

                try
                {
                    StreamReader sr = new StreamReader(fs);
                    string       line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = Regex.Replace(line, @"\s+", ""); // Remove All whitespaces

                        // Check if contains # at the start, if does, ignore. does this contain an =?
                        String[] commentSplit = line.Split(new char[] { '#' }, 2);

                        if (commentSplit[0].Length == 0)
                        {
                            continue;                              // If there is nothing, just skip the next split
                        }
                        String[] configSplit = commentSplit[0].Split(new char[] { '=' }, 2);

                        String key   = configSplit[0];
                        String value = configSplit[1];

                        configValidationSet.Remove(key);

                        if (floatConfigs.ContainsKey(key))
                        {
                            FloatConstraint floatConstraint = floatConfigs[key];

                            configFieldInfos[key].SetValue(Config, floatConstraint.Constrain(float.Parse(value)));
                        }
                        else if (booleanConfigs.ContainsKey(key))
                        {
                            BooleanConstraint booleanConstraint = booleanConfigs[key];

                            configFieldInfos[key].SetValue(Config, booleanConstraint.Constrain(Boolean.Parse(value)));
                        }
                        else
                        {
                            PluginLog.Warn("ConfigUtility", "Unknown Key " + key + ". You can remove this from the config file, its not being used by this plugin.");
                        }

                        // Do config binding here.
                    }

                    StreamWriter sw = new StreamWriter(fs);

                    // Write missing to end of file.
                    foreach (String val in configValidationSet.ToArray())
                    {
                        if (floatConfigs.ContainsKey(val))
                        {
                            sw.WriteLine(val + " = " + floatConfigs[val].DefaultValue);
                            PluginLog.Debug("ConfigUtility", "Could not find " + val + " writing to file");
                        }
                        else if (booleanConfigs.ContainsKey(val))
                        {
                            sw.WriteLine(val + " = " + booleanConfigs[val].DefaultValue);
                            PluginLog.Debug("ConfigUtility", "Could not find " + val + " writing to file");
                        }

                        sw.Flush(); // Have to flush after every line written.
                    }
                }
                catch (FieldAccessException e)
                {
                    PluginLog.Error("ConfigUtility", "FieldAccessException: This error should not occur and indicates an untested case. Please file ticket " + e.Message);
                }
                catch (TargetException e)
                {
                    PluginLog.Error("ConfigUtility", "TargetException: This error should not occur and indicates an untested case. Please file ticket " + e.Message);
                }
                catch (ArgumentException e)
                {
                    PluginLog.Error("ConfigUtility", "ArgumentException: This error should not occur and indicates an untested case. Please file ticket " + e.Message);
                }

                Config.PrintContents();

                PluginLog.Debug("ConfigUtility", "Settings are now Loaded");

                Config.ready = true;
            }
        }