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

@@ -197,8 +197,8 @@ namespace Cryville.Crtr {
const int MAX_DIMENSION = 4; const int MAX_DIMENSION = 4;
readonly InputVectorSrc[] _vecsrcs = new InputVectorSrc[MAX_DEPTH + 1]; readonly InputVectorSrc[] _vecsrcs = new InputVectorSrc[MAX_DEPTH + 1];
readonly InputVectorOp[] _vecops = new InputVectorOp[MAX_DEPTH + 1]; readonly InputVectorOp[] _vecops = new InputVectorOp[MAX_DEPTH + 1];
unsafe class InputVectorSrc : PropSrc.FixedBuffer { unsafe class InputVectorSrc : PropSrc.FixedBuffer<RVector4> {
public InputVectorSrc() : base(PdtInternalType.Vector, MAX_DIMENSION * sizeof(float) + sizeof(int)) { public InputVectorSrc() : base(PdtInternalType.Vector, MAX_DIMENSION * sizeof(float) + sizeof(int), null) {
fixed (byte* ptr = buf) { fixed (byte* ptr = buf) {
*(int*)(ptr + MAX_DIMENSION * sizeof(float)) = PdtInternalType.Number; *(int*)(ptr + MAX_DIMENSION * sizeof(float)) = PdtInternalType.Number;
} }

View File

@@ -455,11 +455,9 @@ namespace Cryville.Crtr {
} }
} }
public unsafe class VectorSrc : PropSrc.FixedBuffer { public unsafe class VectorSrc : PropSrc.FixedBuffer<Vector> {
const int MAX_DIMENSION = 4; const int MAX_DIMENSION = 4;
protected readonly Func<Vector> _cb; public VectorSrc(Func<Vector> cb) : base(PdtInternalType.Vector, MAX_DIMENSION * sizeof(float) + sizeof(int), cb) {
public VectorSrc(Func<Vector> cb) : base(PdtInternalType.Vector, MAX_DIMENSION * sizeof(float) + sizeof(int)) {
_cb = cb;
fixed (byte* rptr = buf) { fixed (byte* rptr = buf) {
var ptr = (float*)rptr; var ptr = (float*)rptr;
*(int*)(ptr + MAX_DIMENSION) = PdtInternalType.Number; *(int*)(ptr + MAX_DIMENSION) = PdtInternalType.Number;
@@ -475,9 +473,8 @@ namespace Cryville.Crtr {
} }
} }
public class VectorOp : PropOp { public class VectorOp : PropOp.Fixed<float[]> {
readonly Action<float[]> _cb; public VectorOp(Action<float[]> cb) : base(cb) { }
public VectorOp(Action<float[]> cb) { _cb = cb; }
protected override unsafe void Execute() { protected override unsafe void Execute() {
var op = GetOperand(0); var op = GetOperand(0);
float[] values; float[] values;

View File

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

View File

@@ -18,11 +18,15 @@ namespace Cryville.Crtr {
value = buf; value = buf;
} }
protected abstract void InternalGet(); protected abstract void InternalGet();
public abstract class FixedBuffer : PropSrc { public abstract class Fixed<T> : PropSrc {
protected readonly Func<T> _cb;
public Fixed(int type, Func<T> cb) : base(type) { _cb = cb; }
}
public abstract class FixedBuffer<T> : Fixed<T> {
bool m_invalidated = true; bool m_invalidated = true;
protected override bool Invalidated { get { return m_invalidated; } } protected override bool Invalidated { get { return m_invalidated; } }
public override void Invalidate() { m_invalidated = true; } public override void Invalidate() { m_invalidated = true; }
public FixedBuffer(int type, int size) : base(type) { buf = new byte[size]; } public FixedBuffer(int type, int size, Func<T> cb) : base(type, cb) { buf = new byte[size]; }
protected override void InternalGet() { protected override void InternalGet() {
m_invalidated = false; m_invalidated = false;
} }
@@ -36,18 +40,16 @@ namespace Cryville.Crtr {
buf = _value; buf = _value;
} }
} }
public class Boolean : FixedBuffer { public class Boolean : FixedBuffer<bool> {
readonly Func<bool> _cb; public Boolean(Func<bool> cb) : base(PdtInternalType.Number, 4, cb) { }
public Boolean(Func<bool> cb) : base(PdtInternalType.Number, 4) { _cb = cb; }
protected override void InternalGet() { protected override void InternalGet() {
base.InternalGet(); base.InternalGet();
buf[0] = _cb() ? (byte)1 : (byte)0; buf[0] = _cb() ? (byte)1 : (byte)0;
} }
} }
public static readonly PropSrc Error = new Arbitrary(PdtInternalType.Error, new byte[0]); public static readonly PropSrc Error = new Arbitrary(PdtInternalType.Error, new byte[0]);
public class Float : FixedBuffer { public class Float : FixedBuffer<float> {
readonly Func<float> _cb; public Float(Func<float> cb) : base(PdtInternalType.Number, 4, cb) { }
public Float(Func<float> cb) : base(PdtInternalType.Number, 4) { _cb = cb; }
protected override unsafe void InternalGet() { protected override unsafe void InternalGet() {
base.InternalGet(); base.InternalGet();
fixed (byte* _ptr = buf) { fixed (byte* _ptr = buf) {
@@ -56,9 +58,8 @@ namespace Cryville.Crtr {
} }
} }
public static readonly PropSrc Null = new Arbitrary(PdtInternalType.Null, new byte[0]); public static readonly PropSrc Null = new Arbitrary(PdtInternalType.Null, new byte[0]);
public class String : PropSrc { public class String : Fixed<string> {
readonly Func<string> _cb; public String(Func<string> cb) : base(PdtInternalType.String, cb) { }
public String(Func<string> cb) : base(PdtInternalType.String) { _cb = cb; }
protected override unsafe void InternalGet() { protected override unsafe void InternalGet() {
var v = _cb(); var v = _cb();
int strlen = v.Length; int strlen = v.Length;
@@ -71,9 +72,8 @@ namespace Cryville.Crtr {
} }
} }
} }
public class Identifier : FixedBuffer { public class Identifier : FixedBuffer<int> {
readonly Func<int> _cb; public Identifier(Func<int> cb) : base(PdtInternalType.Undefined, 4, cb) { }
public Identifier(Func<int> cb) : base(PdtInternalType.Undefined, 4) { _cb = cb; }
protected override unsafe void InternalGet() { protected override unsafe void InternalGet() {
base.InternalGet(); base.InternalGet();
fixed (byte* _ptr = buf) { fixed (byte* _ptr = buf) {
@@ -81,9 +81,8 @@ namespace Cryville.Crtr {
} }
} }
} }
public class BeatTime : FixedBuffer { public class BeatTime : FixedBuffer<RBeatTime> {
readonly Func<RBeatTime> _cb; public BeatTime(Func<RBeatTime> cb) : base(PdtInternalType.Vector, 4 * sizeof(int), cb) { }
public BeatTime(Func<RBeatTime> cb) : base(PdtInternalType.Vector, 4 * sizeof(int)) { _cb = cb; }
protected override unsafe void InternalGet() { protected override unsafe void InternalGet() {
base.InternalGet(); base.InternalGet();
var bt = _cb(); var bt = _cb();
@@ -96,9 +95,8 @@ namespace Cryville.Crtr {
} }
} }
} }
public class Vector4 : FixedBuffer { public class Vector4 : FixedBuffer<RVector4> {
readonly Func<RVector4> _cb; public Vector4(Func<RVector4> cb) : base(PdtInternalType.Vector, 4 * sizeof(float) + sizeof(int), cb) { }
public Vector4(Func<RVector4> cb) : base(PdtInternalType.Vector, 4 * sizeof(float) + sizeof(int)) { _cb = cb; }
protected override unsafe void InternalGet() { protected override unsafe void InternalGet() {
base.InternalGet(); base.InternalGet();
var vec = _cb(); var vec = _cb();