Files
crtr/Assets/Cryville/Crtr/PropSrc.cs
2022-09-30 17:32:21 +08:00

60 lines
1.7 KiB
C#

using Cryville.Common.Pdt;
using System;
namespace Cryville.Crtr {
public abstract class PropSrc {
int _type;
byte[] _buf = null;
public void Get(out int type, out byte[] value) {
if (_buf == null) InternalGet(out _type, out _buf);
type = _type;
value = _buf;
}
protected abstract void InternalGet(out int type, out byte[] value);
public class Arbitrary : PropSrc {
readonly new int _type;
readonly byte[] _value;
public Arbitrary(int type, byte[] value) {
_type = type;
_value = value;
}
protected override void InternalGet(out int type, out byte[] value) {
type = _type;
value = _value;
}
}
public class Boolean : PropSrc {
readonly Func<bool> _cb;
public Boolean(Func<bool> cb) { _cb = cb; }
protected override void InternalGet(out int type, out byte[] value) {
type = PdtInternalType.Number;
value = new byte[] { _cb() ? (byte)1 : (byte)0, 0, 0, 0 };
}
}
public class Float : PropSrc {
readonly Func<float> _cb;
public Float(Func<float> cb) { _cb = cb; }
protected override void InternalGet(out int type, out byte[] value) {
type = PdtInternalType.Number;
value = BitConverter.GetBytes(_cb());
}
}
public class String : PropSrc {
readonly Func<string> _cb;
public String(Func<string> cb) { _cb = cb; }
protected override unsafe void InternalGet(out int type, out byte[] value) {
type = PdtInternalType.String;
var v = _cb();
int strlen = v.Length;
value = new byte[strlen * sizeof(char) + sizeof(int)];
fixed (byte* _ptr = value) {
char* ptr = (char*)(_ptr + sizeof(int));
*(int*)_ptr = strlen;
int i = 0;
foreach (var c in v) ptr[i++] = c;
}
}
}
}
}