namespace Fusion.Addons.KCC { using System; public sealed unsafe class KCCNetworkEnum : KCCNetworkProperty where TContext : class where TEnum : unmanaged, Enum { // PRIVATE MEMBERS private readonly Action _set; private readonly Func _get; private readonly Func _interpolate; // CONSTRUCTORS public KCCNetworkEnum(TContext context, Action set, Func get, Func interpolate) : base(context, 1) { _set = set; _get = get; _interpolate = interpolate; } // KCCNetworkProperty INTERFACE public override void Read(int* ptr) { _set(Context, EnumConvertor.ToEnum(*ptr)); } public override void Write(int* ptr) { *ptr = EnumConvertor.ToInt(_get(Context)); } public override void Interpolate(KCCInterpolationInfo interpolationInfo) { int fromValue = interpolationInfo.FromBuffer.ReinterpretState(interpolationInfo.Offset); int toValue = interpolationInfo.ToBuffer.ReinterpretState(interpolationInfo.Offset); int value; if (_interpolate != null) { value = EnumConvertor.ToInt(_interpolate(Context, interpolationInfo.Alpha, EnumConvertor.ToEnum(fromValue), EnumConvertor.ToEnum(toValue))); } else { value = interpolationInfo.Alpha < 0.5f ? fromValue: toValue; } _set(Context, EnumConvertor.ToEnum(value)); } private static class EnumConvertor { public static int ToInt(T value) where T : unmanaged, Enum { return *(int*)(&value); } public static T ToEnum(int value) where T : unmanaged, Enum { return *(T*)(&value); } } } }