Implement custom ruleset config. (2)

This commit is contained in:
2023-08-02 17:28:55 +08:00
parent 3b22d4fce3
commit 9044631fe7
7 changed files with 96 additions and 27 deletions

View File

@@ -5,21 +5,14 @@ using System.Collections.Generic;
namespace Cryville.Crtr.Config {
internal class RulesetConfigPropertyMasterAdapter : IPropertyMasterAdapter {
readonly List<IPropertyAdapter> _props;
readonly Dictionary<string, object> _values;
readonly List<RulesetConfigPropertyAdapter> _props = new List<RulesetConfigPropertyAdapter>();
readonly RulesetConfigStore _store;
public RulesetConfigPropertyMasterAdapter(Dictionary<Identifier, ConfigDefinition> defs, Dictionary<string, object> values) {
_values = values;
_props = new List<IPropertyAdapter>();
PdtEvaluator.Instance.ContextCascadeInsert();
_store = new RulesetConfigStore(defs, values);
PdtEvaluator.Instance.ContextRulesetConfig = _store;
if (defs == null) return;
foreach (var def in defs) {
var name = (string)def.Key.Name;
if (!_values.ContainsKey(name)) {
float value = 0;
PdtEvaluator.Instance.Evaluate(new PropOp.Float(v => value = v), def.Value.@default);
_values.Add(name, value);
}
_props.Add(new RulesetConfigPropertyAdapter(def.Key, def.Value, this));
}
}
@@ -29,11 +22,14 @@ namespace Cryville.Crtr.Config {
public IEnumerable<IPropertyAdapter> GetProperties() { return _props; }
public object GetValue(string key) {
return _values[key];
return _store[key];
}
public void SetValue(string key, object value) {
_values[key] = value;
_store[key] = value;
foreach (var prop in _props) {
prop.OnValueChanged();
}
}
}
@@ -54,9 +50,6 @@ namespace Cryville.Crtr.Config {
m_range[0] = (double)v.Behind;
m_range[1] = (double)v.Ahead;
});
_numsrc = new PropSrc.Float(() => _buf);
_numop = new PropOp.Float(v => _buf = v);
PdtEvaluator.Instance.ContextCascadeUpdate(key.Key, _numsrc);
}
public string Category { get { return _def.category; } }
@@ -83,19 +76,22 @@ namespace Cryville.Crtr.Config {
_master.SetValue(Name, value);
}
public event Action ValueChanged;
public void OnValueChanged() {
var ev = ValueChanged;
if (ev != null) ev();
}
public bool SetMapped { get { return false; } }
float _buf;
readonly PropSrc _numsrc;
readonly PropOp _numop;
readonly PropStores.Float _numst = new PropStores.Float();
public object MapValue(object value) {
_buf = (float)(double)value;
if (_def.value == null) return _buf;
_numsrc.Invalidate();
PdtEvaluator.Instance.ContextSelfValue = _numsrc;
PdtEvaluator.Instance.Evaluate(_numop, _def.value);
_numst.Value = (float)(double)value;
if (_def.value == null) return _numst.Value;
PdtEvaluator.Instance.ContextSelfValue = _numst.Source;
PdtEvaluator.Instance.Evaluate(_numst.Target, _def.value);
PdtEvaluator.Instance.ContextSelfValue = null;
return _buf;
return _numst.Value;
}
public object MapValueInverse(object value) { throw new NotSupportedException(); }