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 _cb; public Boolean(Func 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 _cb; public Float(Func 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 _cb; public String(Func 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; } } } } }