Reconstruct property sources and operators to generic types.

This commit is contained in:
2023-07-31 15:12:41 +08:00
parent 3db8f61e3b
commit b78e99c59c
4 changed files with 57 additions and 73 deletions

View File

@@ -24,46 +24,45 @@ namespace Cryville.Crtr {
PdtEvaluator.Instance.ContextCascadeUpdate(Name, new PropSrc.Arbitrary(op.Type, value));
}
}
public class Boolean : PropOp {
readonly Action<bool> _cb;
public Boolean(Action<bool> cb) { _cb = cb; }
public abstract class Fixed<T> : PropOp {
protected readonly Action<T> _cb;
public Fixed(Action<T> cb) { _cb = cb; }
public Fixed(int pc, Action<T> cb) : base(pc) { _cb = cb; }
}
public class Boolean : Fixed<bool> {
public Boolean(Action<bool> cb) : base(cb) { }
protected override void Execute() {
_cb(GetOperand(0).AsNumber() > 0);
}
}
public class Clip : PropOp {
readonly Action<RClip> _cb;
public Clip(Action<RClip> cb) : base(2) { _cb = cb; }
public class Clip : Fixed<RClip> {
public Clip(Action<RClip> cb) : base(2, cb) { }
protected override void Execute() {
if (LoadedOperandCount < 2)
throw new ArgumentException("Invalid clip syntax");
_cb(new RClip(GetOperand(0).AsNumber(), GetOperand(1).AsNumber()));
}
}
public class Integer : PropOp {
readonly Action<int> _cb;
public Integer(Action<int> cb) { _cb = cb; }
public class Integer : Fixed<int> {
public Integer(Action<int> cb) : base(cb) { }
protected override void Execute() {
_cb((int)GetOperand(0).AsNumber());
}
}
public class Float : PropOp {
readonly Action<float> _cb;
public Float(Action<float> cb) { _cb = cb; }
public class Float : Fixed<float> {
public Float(Action<float> cb) : base(cb) { }
protected override void Execute() {
_cb(GetOperand(0).AsNumber());
}
}
public class String : PropOp {
readonly Action<string> _cb;
public String(Action<string> cb) { _cb = cb; }
public class String : Fixed<string> {
public String(Action<string> cb) : base(cb) { }
protected override void Execute() {
_cb(GetOperand(0).AsString());
}
}
public class StringArray : PropOp {
readonly Action<string[]> _cb;
public StringArray(Action<string[]> cb) { _cb = cb; }
public class StringArray : Fixed<string[]> {
public StringArray(Action<string[]> cb) : base(cb) { }
protected override unsafe void Execute() {
var op = GetOperand(0);
int arrtype; int len;
@@ -93,25 +92,22 @@ namespace Cryville.Crtr {
target.Validate();
}
}
public class Identifier : PropOp {
readonly Action<int> _cb;
public Identifier(Action<int> cb) { _cb = cb; }
public class Identifier : Fixed<int> {
public Identifier(Action<int> cb) : base(cb) { }
protected override void Execute() {
_cb(GetOperand(0).AsIdentifier());
}
}
public class Enum : PropOp {
public class Enum : Fixed<object> {
readonly Type _type;
readonly IntKeyedDictionary<int> _cache = new IntKeyedDictionary<int>();
readonly Action<object> _cb;
public Enum(Type type, Action<object> cb) {
public Enum(Type type, Action<object> cb) : base(cb) {
if (!type.IsEnum)
throw new ArgumentException("Type is not enum");
_type = type;
var names = type.GetFields(BindingFlags.Public | BindingFlags.Static);
for (int i = 0; i < names.Length; i++)
_cache[IdentifierManager.Shared.Request(names[i].Name)] = Convert.ToInt32(names[i].GetValue(null));
_cb = cb;
}
protected override void Execute() {
int result = 0;
@@ -120,9 +116,8 @@ namespace Cryville.Crtr {
_cb(System.Enum.ToObject(_type, result));
}
}
public class Enum<T> : PropOp {
public class Enum<T> : Fixed<T> {
static readonly IntKeyedDictionary<int> _cache = new IntKeyedDictionary<int>();
readonly Action<T> _cb;
readonly Func<int, T> _caster;
static Enum() {
if (!typeof(T).IsEnum)
@@ -131,8 +126,7 @@ namespace Cryville.Crtr {
for (int i = 0; i < names.Length; i++)
_cache[IdentifierManager.Shared.Request(names[i].Name)] = Convert.ToInt32(names[i].GetValue(null));
}
public Enum(Action<T> cb, Func<int, T> caster) {
_cb = cb;
public Enum(Action<T> cb, Func<int, T> caster) : base(cb) {
_caster = caster;
}
protected override void Execute() {
@@ -142,17 +136,15 @@ namespace Cryville.Crtr {
_cb(_caster(result));
}
}
public class BeatTime : PropOp {
readonly Action<RBeatTime> _cb;
public BeatTime(Action<RBeatTime> cb) { _cb = cb; }
public class BeatTime : Fixed<RBeatTime> {
public BeatTime(Action<RBeatTime> cb) : base(cb) { }
protected override unsafe void Execute() {
var o = GetOperand(0);
_cb(o.As<RBeatTime>());
}
}
public class Vector2 : PropOp {
readonly Action<RVector2> _cb;
public Vector2(Action<RVector2> cb) { _cb = cb; }
public class Vector2 : Fixed<RVector2> {
public Vector2(Action<RVector2> cb) : base(cb) { }
protected override unsafe void Execute() {
var o = GetOperand(0);
switch ((o.Length - 4) / 4) {
@@ -169,9 +161,8 @@ namespace Cryville.Crtr {
}
}
}
public class Vector3 : PropOp {
readonly Action<RVector3> _cb;
public Vector3(Action<RVector3> cb) { _cb = cb; }
public class Vector3 : Fixed<RVector3> {
public Vector3(Action<RVector3> cb) : base(cb) { }
protected override unsafe void Execute() {
var o = GetOperand(0);
switch ((o.Length - 4) / 4) {
@@ -186,9 +177,8 @@ namespace Cryville.Crtr {
}
}
}
public class Vector4 : PropOp {
readonly Action<RVector4> _cb;
public Vector4(Action<RVector4> cb) { _cb = cb; }
public class Vector4 : Fixed<RVector4> {
public Vector4(Action<RVector4> cb) : base(cb) { }
protected override unsafe void Execute() {
var o = GetOperand(0);
switch ((o.Length - 4) / 4) {
@@ -204,9 +194,8 @@ namespace Cryville.Crtr {
}
}
}
public class Color : PropOp {
readonly Action<RColor> _cb;
public Color(Action<RColor> cb) { _cb = cb; }
public class Color : Fixed<RColor> {
public Color(Action<RColor> cb) : base(cb) { }
protected override unsafe void Execute() {
var o = GetOperand(0);
switch ((o.Length - 4) / 4) {