|
| 1 | +using System.Linq.Expressions; |
| 2 | +using System.Numerics; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +namespace NetCode; |
| 6 | + |
| 7 | +public abstract class MinMaxConfiguration<T> |
| 8 | + where T : unmanaged |
| 9 | +{ |
| 10 | + internal T Min { get; set; } |
| 11 | + |
| 12 | + internal T Max { get; set; } |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +public abstract class MinMaxPrecisionConfiguration<T> : MinMaxConfiguration<T> |
| 17 | + where T : unmanaged |
| 18 | +{ |
| 19 | + internal T Precision { get; set; } |
| 20 | +} |
| 21 | + |
| 22 | +public sealed class IntFieldConfiguration : MinMaxConfiguration<int> |
| 23 | +{ |
| 24 | + public void Limit(int min, int max) |
| 25 | + { |
| 26 | + Min = min; |
| 27 | + Max = max; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +public sealed class FloatFieldConfiguration : MinMaxPrecisionConfiguration<float> |
| 32 | +{ |
| 33 | + public void Limit(float min, float max, float precision) |
| 34 | + { |
| 35 | + Min = min; |
| 36 | + Max = max; |
| 37 | + Precision = precision; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +public class LimitProfile<T> |
| 42 | +{ |
| 43 | + public LimitProfile<T> Configure(Expression<Func<T, int>> fieldAccessor) |
| 44 | + { |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public LimitProfile<T> Configure(Expression<Func<T, int>> fieldAccessor, Action<IntFieldConfiguration> config) |
| 49 | + { |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + public LimitProfile<T> Configure(Expression<Func<T, float>> fieldAccessor, Action<FloatFieldConfiguration> config) |
| 54 | + { |
| 55 | + return this; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +public class QuantizationBitReader : BitReader |
| 60 | +{ |
| 61 | + public QuantizationBitReader() |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + public QuantizationBitReader(byte[] data) : base(data) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + public QuantizationBitReader(ByteReader byteReader) : base(byteReader) |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + public int ReadInt(int min, int max) |
| 74 | + { |
| 75 | + if (min > max) |
| 76 | + { |
| 77 | + ThrowHelper.ThrowArgumentException(); |
| 78 | + } |
| 79 | + |
| 80 | + var range = max - min; |
| 81 | + var bitsRequired = Mathi.BitsRequired((uint)range); |
| 82 | + var value = (int)ReadBits(bitsRequired); |
| 83 | + return value + min; |
| 84 | + } |
| 85 | + |
| 86 | + public float ReadFloat(float min, float max, float precision) |
| 87 | + { |
| 88 | + if (min > max) |
| 89 | + { |
| 90 | + ThrowHelper.ThrowArgumentException(); |
| 91 | + } |
| 92 | + |
| 93 | + var result = ReadFloat(new FloatLimit(min, max, precision)); |
| 94 | + |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + public float ReadFloat(FloatLimit limit) |
| 99 | + { |
| 100 | + uint integerValue = ReadBits(limit.NumberOfBits); |
| 101 | + float normalizedValue = integerValue / (float)limit.MaxIntegerValue; |
| 102 | + |
| 103 | + return normalizedValue * limit.Delta + limit.Min; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +public class QuantizationBitWriter : BitWriter |
| 109 | +{ |
| 110 | + public QuantizationBitWriter(int capacity = 1500) : base(capacity) |
| 111 | + { |
| 112 | + } |
| 113 | + |
| 114 | + public QuantizationBitWriter(byte[] data) : base(data) |
| 115 | + { |
| 116 | + } |
| 117 | + |
| 118 | + public QuantizationBitWriter(ByteWriter byteWriter) : base(byteWriter) |
| 119 | + { |
| 120 | + } |
| 121 | + |
| 122 | + public void Write(int value, int min, int max) |
| 123 | + { |
| 124 | + if (min > max) |
| 125 | + { |
| 126 | + ThrowHelper.ThrowArgumentException(); |
| 127 | + } |
| 128 | + |
| 129 | + if (value < min || value > max) |
| 130 | + { |
| 131 | + ThrowHelper.ThrowArgumentOutOfRangeException(); |
| 132 | + } |
| 133 | + |
| 134 | + var range = max - min; |
| 135 | + var bitsRequired = Mathi.BitsRequired((uint)range); |
| 136 | + WriteBits(bitsRequired, (uint)(value - min)); |
| 137 | + } |
| 138 | + |
| 139 | + public void Write(uint value, uint min, uint max) |
| 140 | + { |
| 141 | + if (min > max) |
| 142 | + { |
| 143 | + ThrowHelper.ThrowArgumentException(); |
| 144 | + } |
| 145 | + |
| 146 | + if (value < min || value > max) |
| 147 | + { |
| 148 | + ThrowHelper.ThrowArgumentOutOfRangeException(); |
| 149 | + } |
| 150 | + |
| 151 | + var range = max - min; |
| 152 | + var bitsRequired = Mathi.BitsRequired(range); |
| 153 | + WriteBits(bitsRequired, value - min); |
| 154 | + } |
| 155 | + |
| 156 | + public void Write(float value, float min, float max, float precision) |
| 157 | + { |
| 158 | + if (min > max) |
| 159 | + { |
| 160 | + ThrowHelper.ThrowArgumentException(); |
| 161 | + } |
| 162 | + |
| 163 | + if (value < min || value > max) |
| 164 | + { |
| 165 | + ThrowHelper.ThrowArgumentOutOfRangeException(); |
| 166 | + } |
| 167 | + |
| 168 | + Write(value, new FloatLimit(min, max, precision)); |
| 169 | + } |
| 170 | + |
| 171 | + public void Write(float value, FloatLimit limit) |
| 172 | + { |
| 173 | + float normalizedValue = Math.Clamp((value - limit.Min) / limit.Delta, 0, 1); |
| 174 | + uint integerValue = (uint)Math.Floor(normalizedValue * limit.MaxIntegerValue + 0.5f); |
| 175 | + |
| 176 | + WriteBits(limit.NumberOfBits, integerValue); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +public readonly struct FloatLimit |
| 181 | +{ |
| 182 | + public readonly float Min; |
| 183 | + |
| 184 | + public readonly float Delta; |
| 185 | + |
| 186 | + public readonly uint MaxIntegerValue; |
| 187 | + |
| 188 | + public readonly int NumberOfBits; |
| 189 | + |
| 190 | + public FloatLimit(float min, float max, float precision) |
| 191 | + { |
| 192 | + Min = min; |
| 193 | + Delta = max - min; |
| 194 | + float values = Delta / precision; |
| 195 | + MaxIntegerValue = (uint)Math.Ceiling(values); |
| 196 | + NumberOfBits = Mathi.BitsRequired(MaxIntegerValue); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +public static class C |
| 201 | +{ |
| 202 | + public static Action<T, TProperty> GetSetter<T, TProperty>(Expression<Func<T, TProperty>> expression) |
| 203 | + { |
| 204 | + var memberExpression = (MemberExpression)expression.Body; |
| 205 | + var property = (PropertyInfo)memberExpression.Member; |
| 206 | + var setMethod = property.GetSetMethod(); |
| 207 | + |
| 208 | + var parameterT = Expression.Parameter(typeof(T), "x"); |
| 209 | + var parameterTProperty = Expression.Parameter(typeof(TProperty), "y"); |
| 210 | + |
| 211 | + var newExpression = |
| 212 | + Expression.Lambda<Action<T, TProperty>>( |
| 213 | + Expression.Call(parameterT, setMethod, parameterTProperty), |
| 214 | + parameterT, |
| 215 | + parameterTProperty |
| 216 | + ); |
| 217 | + |
| 218 | + return newExpression.Compile(); |
| 219 | + } |
| 220 | + |
| 221 | + public static void M() |
| 222 | + { |
| 223 | + var randomProfile = new LimitProfile<RandomComponent>() |
| 224 | + .Configure(x => x.Seed); |
| 225 | + |
| 226 | + var healthProfile = new LimitProfile<HealthComponent>() |
| 227 | + .Configure(x => x.Health, c => c.Limit(0, 100)) |
| 228 | + .Configure(x => x.MaxHealth, c => c.Limit(0, 100)); |
| 229 | + |
| 230 | + var transformProfile = new LimitProfile<TransformComponent>() |
| 231 | + .Configure(x => x.Position.X, c => c.Limit(-100, 100, 0.01f)) |
| 232 | + .Configure(x => x.Position.Y, c => c.Limit(-3, 13, 0.01f)) |
| 233 | + .Configure(x => x.Position.Z, c => c.Limit(-100, 100, 0.01f)) |
| 234 | + .Configure(x => x.Yaw, c => c.Limit(0, 360, 0.1f)) |
| 235 | + .Configure(x => x.Pitch, c => c.Limit(0, 360, 0.1f)); |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +public struct RandomComponent |
| 240 | +{ |
| 241 | + public int Seed; |
| 242 | +} |
| 243 | + |
| 244 | +public struct HealthComponent |
| 245 | +{ |
| 246 | + public int Health; |
| 247 | + |
| 248 | + public int MaxHealth; |
| 249 | +} |
| 250 | + |
| 251 | +public struct TransformComponent |
| 252 | +{ |
| 253 | + public Vector3 Position; |
| 254 | + |
| 255 | + public float Yaw; |
| 256 | + |
| 257 | + public float Pitch; |
| 258 | +} |
| 259 | + |
0 commit comments