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

@@ -1,4 +1,5 @@
using System;
using UnsafeIL;
namespace Cryville.Common.Pdt {
/// <summary>
@@ -126,10 +127,43 @@ namespace Cryville.Common.Pdt {
throw new InvalidCastException("Not an identifier");
return *(int*)(_ptr + offset);
}
internal void* TrustedAsOfLength(int len) {
if (Length < len)
throw new InvalidCastException("Type not matched");
return _ptr;
/// <summary>
/// Gets the memory of the span as an instance of the specified type.
/// </summary>
/// <typeparam name="T">The specified type.</typeparam>
/// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>An instance of the specified type.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset" /> is not less than the length of the span.</exception>
/// <exception cref="InvalidCastException">The length of the span is not sufficient.</exception>
/// <remarks>
/// <para>Use <see cref="AsNumber(int)" /> instead while reading an unaligned number.</para>
/// </remarks>
public T As<T>(int offset = 0) {
var len = Unsafe.SizeOf<T>();
if (offset >= Length)
throw new ArgumentOutOfRangeException("offset");
if (offset + len > Length)
throw new InvalidCastException("Frame length not sufficient");
return Unsafe.Read<T>(_ptr + offset);
}
/// <summary>
/// Sets the memory of the span to an instance of the specified type.
/// </summary>
/// <typeparam name="T">The specified type.</typeparam>
/// <param name="value">The value.</param>
/// <param name="offset">The offset from the start of the span.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset" /> is not less than the length of the span.</exception>
/// <exception cref="InvalidCastException">The length of the span is not sufficient.</exception>
/// <remarks>
/// <para>Use <see cref="SetNumber(float, int)" /> instead while writing an unaligned number.</para>
/// </remarks>
public void Set<T>(T value, int offset = 0) {
var len = Unsafe.SizeOf<T>();
if (offset >= Length)
throw new ArgumentOutOfRangeException("offset");
if (offset + len > Length)
throw new InvalidCastException("Frame length not sufficient");
Unsafe.Write(_ptr + offset, value);
}
/// <summary>
/// Gets the array suffix.

View File

@@ -80,11 +80,10 @@ namespace Cryville.Crtr.Components {
var o = GetOperand(0);
if (o.Type != PdtInternalType.Vector) throw new ArgumentException("Not a vector");
_self._shapeLength = (o.Length - sizeof(int)) / sizeof(Vector2);
var ptr = (Vector2*)o.TrustedAsOfLength(sizeof(Vector2));
if (_self._shape != null) _shapePool.Return(_self._shape);
_self._shape = _shapePool.Rent(_self._shapeLength);
for (int i = 0; i < _self._shapeLength; i++) {
_self._shape[i] = ptr[i];
_self._shape[i] = o.As<Vector2>(i * sizeof(Vector2));
}
}
}

View File

@@ -16,10 +16,10 @@ namespace Cryville.Crtr.Components {
public op_set_bound(SpriteBase self) : base(2) {
_self = self;
}
protected unsafe override void Execute() {
protected override void Execute() {
_self.SetBound(
*(Vector2*)GetOperand(0).TrustedAsOfLength(sizeof(Vector2)),
*(Vector3*)GetOperand(1).TrustedAsOfLength(sizeof(Vector3))
GetOperand(0).As<Vector2>(),
GetOperand(1).As<Vector3>()
);
}
}

View File

@@ -390,9 +390,8 @@ namespace Cryville.Crtr {
var ray = new Ray(ctx.position, ctx.rotation * Vector3.forward);
ChartPlayer.frustumPlanes[(int)GetOperand(0).AsNumber()].Raycast(ray, out dist);
var ret = GetReturnFrame(PdtInternalType.Vector, sizeof(Vector3) + sizeof(int));
var ptr = (Vector3*)ret.TrustedAsOfLength(sizeof(Vector3) + sizeof(int));
*ptr++ = ray.GetPoint(dist);
*(int*)ptr = PdtInternalType.Number;
ret.Set(ray.GetPoint(dist));
ret.SetArraySuffix(PdtInternalType.Number);
}
}
class func_int : PdtOperator {

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");

Binary file not shown.

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d927992f95b82f740a0a95bb6d617d73
guid: 1313487c790be7545a3e1e2ca59e986e
PluginImporter:
externalObjects: {}
serializedVersion: 2