Replace TrustedAsOfLength with safe As and Set.

This commit is contained in:
2023-02-10 15:47:20 +08:00
parent c04e50e959
commit b437925f92
7 changed files with 65 additions and 33 deletions

View File

@@ -5,7 +5,10 @@ using System.Collections.Generic;
using System.Reflection;
using RBeatTime = Cryville.Crtr.BeatTime;
using RClip = Cryville.Crtr.Clip;
using RColor = UnityEngine.Color;
using RTargetString = Cryville.Common.Buffers.TargetString;
using RVector2 = UnityEngine.Vector2;
using RVector3 = UnityEngine.Vector3;
namespace Cryville.Crtr {
public abstract class PropOp : PdtOperator {
@@ -60,16 +63,14 @@ namespace Cryville.Crtr {
public class TargetString : PropOp {
readonly Func<RTargetString> _cb;
public TargetString(Func<RTargetString> cb) { _cb = cb; }
protected override unsafe void Execute() {
protected override void Execute() {
var target = _cb();
var op = GetOperand(0);
if (op.Type != PdtInternalType.String) throw new ArgumentException("Not a string");
var ptr = (byte*)op.TrustedAsOfLength(op.Length);
var len = *(int*)ptr;
int len = op.As<int>();
target.Length = len;
var cptr = (char*)(ptr + sizeof(int));
for (int i = 0; i < len; i++)
target[i] = cptr[i];
target[i] = op.As<char>(sizeof(int) + i * sizeof(char));
target.Validate();
}
}
@@ -105,22 +106,22 @@ namespace Cryville.Crtr {
public BeatTime(Action<RBeatTime> cb) { _cb = cb; }
protected override unsafe void Execute() {
var o = GetOperand(0);
_cb(*(RBeatTime*)o.TrustedAsOfLength(sizeof(RBeatTime)));
_cb(o.As<RBeatTime>());
}
}
public class Vector2 : PropOp {
readonly Action<UnityEngine.Vector2> _cb;
public Vector2(Action<UnityEngine.Vector2> cb) { _cb = cb; }
readonly Action<RVector2> _cb;
public Vector2(Action<RVector2> cb) { _cb = cb; }
protected override unsafe void Execute() {
var o = GetOperand(0);
switch ((o.Length - 4) / 4) {
case 0: // Number
case 1: // Array[1]
float num = o.AsNumber();
_cb(new UnityEngine.Vector2(num, num));
_cb(new RVector2(num, num));
break;
case 2:
_cb(*(UnityEngine.Vector2*)o.TrustedAsOfLength(sizeof(UnityEngine.Vector2)));
_cb(o.As<RVector2>());
break;
default:
throw new InvalidOperationException("Invalid array size");
@@ -128,21 +129,21 @@ namespace Cryville.Crtr {
}
}
public class Vector3 : PropOp {
readonly Action<UnityEngine.Vector3> _cb;
public Vector3(Action<UnityEngine.Vector3> cb) { _cb = cb; }
readonly Action<RVector3> _cb;
public Vector3(Action<RVector3> cb) { _cb = cb; }
protected override unsafe void Execute() {
var o = GetOperand(0);
switch ((o.Length - 4) / 4) {
case 0: // Number
case 1: // Array[1]
float num = o.AsNumber();
_cb(new UnityEngine.Vector3(num, num));
_cb(new RVector3(num, num));
break;
case 2:
_cb(*(UnityEngine.Vector2*)o.TrustedAsOfLength(sizeof(UnityEngine.Vector2)));
_cb(o.As<RVector2>());
break;
case 3:
_cb(*(UnityEngine.Vector3*)o.TrustedAsOfLength(sizeof(UnityEngine.Vector3)));
_cb(o.As<RVector3>());
break;
default:
throw new InvalidOperationException("Invalid array size");
@@ -150,17 +151,16 @@ namespace Cryville.Crtr {
}
}
public class Color : PropOp {
readonly Action<UnityEngine.Color> _cb;
public Color(Action<UnityEngine.Color> cb) { _cb = cb; }
readonly Action<RColor> _cb;
public Color(Action<RColor> cb) { _cb = cb; }
protected override unsafe void Execute() {
var o = GetOperand(0);
switch ((o.Length - 4) / 4) {
case 3:
var ptr = (float*)o.TrustedAsOfLength(sizeof(float) * 3);
_cb(new UnityEngine.Color(ptr[0], ptr[1], ptr[2]));
_cb(new RColor(o.As<float>(), o.As<float>(sizeof(float)), o.As<float>(2 * sizeof(float))));
break;
case 4:
_cb(*(UnityEngine.Color*)o.TrustedAsOfLength(sizeof(UnityEngine.Color)));
_cb(o.As<RColor>());
break;
default:
throw new InvalidOperationException("Invalid array size");