Fix potential error on vector property source.

This commit is contained in:
2023-03-05 23:05:48 +08:00
parent ae5f4a8c16
commit 55f7790f89

View File

@@ -951,16 +951,22 @@ namespace Cryville.Crtr {
}
}
public class VectorSrc : PropSrc.FixedBuffer {
public unsafe class VectorSrc : PropSrc.FixedBuffer {
const int MAX_DIMENSION = 8;
protected readonly Func<Vector> _cb;
public VectorSrc(Func<Vector> cb) : base(PdtInternalType.Vector, 8 * sizeof(float) + sizeof(int)) { _cb = cb; }
protected override unsafe void InternalGet() {
public VectorSrc(Func<Vector> cb) : base(PdtInternalType.Vector, MAX_DIMENSION * sizeof(float) + sizeof(int)) {
_cb = cb;
fixed (byte* rptr = buf) {
var ptr = (float*)rptr;
*(int*)(ptr + MAX_DIMENSION) = PdtInternalType.Number;
}
}
protected override void InternalGet() {
var v = _cb();
if (v.Dimension > 8) throw new NotSupportedException("Vector dimension too large");
if (v.Dimension > MAX_DIMENSION) throw new NotSupportedException("Vector dimension too large");
fixed (byte* rptr = buf) {
var ptr = (float*)rptr;
v.ToArray(ptr);
*(int*)(ptr + v.Dimension) = PdtInternalType.Number;
}
}
}