4 using System.Collections.Generic;
5 using System.Collections.Immutable;
13 private const int AmountOfBoolsInByte = 7;
14 private readonly List<byte> Buffer =
new List<byte>();
16 private bool disposed;
22 int arrayIndex = (int)Math.Floor(index / (
float)AmountOfBoolsInByte);
23 if (arrayIndex >= Buffer.Count) { Buffer.Add(0); }
25 int bitIndex = index % AmountOfBoolsInByte;
26 Buffer[arrayIndex] |= (byte)(b ? 1u << bitIndex : 0);
34 uint range = (uint)(max - min);
35 int numberOfBits = NetUtility.BitsToHoldUInt(range);
37 uint writeValue = (uint)(value - min);
39 for (
int i = 0; i < numberOfBits; i++)
45 public void WriteFloat(
float value,
float min,
float max,
int numberOfBits)
49 float range = max - min;
50 float unit = (value - min) / range;
51 uint maxVal = (1u << numberOfBits) - 1;
53 uint writeValue = (uint)(maxVal * unit);
54 for (
int i = 0; i < numberOfBits; i++)
64 if (Buffer.Count == 0) { Buffer.Add(0); }
66 Buffer[^1] |= 1 << AmountOfBoolsInByte;
68 foreach (
byte b
in Buffer)
81 private void ThrowIfDisposed()
89 private const int AmountOfBoolsInByte = 7;
90 private readonly ImmutableArray<byte> buffer;
95 List<byte> bytes =
new List<byte>();
102 throw new Exception(
"Failed to find the end of the bit field: end of the message reached.");
105 bytes.Add(currentByte);
107 while (!IsBitSet(currentByte, AmountOfBoolsInByte));
109 buffer = bytes.ToImmutableArray();
114 int arrayIndex = (int)MathF.Floor(index / (
float)AmountOfBoolsInByte);
115 int bitIndex = index % AmountOfBoolsInByte;
117 return IsBitSet(buffer[arrayIndex], bitIndex);
122 uint range = (uint)(max - min);
123 int numberOfBits = NetUtility.BitsToHoldUInt(range);
126 for (
int i = 0; i < numberOfBits; i++)
131 return (
int)(min + value);
134 public float ReadFloat(
float min,
float max,
int numberOfBits)
136 int maxInt = (1 << numberOfBits) - 1;
139 for (
int i = 0; i < numberOfBits; i++)
144 float range = max - min;
145 return min + range * value / maxInt;
148 private static bool IsBitSet(
byte b,
int bitIndex) => (b & (1u << bitIndex)) != 0;
int ReadInteger(int min, int max)
float ReadFloat(float min, float max, int numberOfBits)
ReadOnlyBitField(IReadMessage inc)
void WriteInteger(int value, int min, int max)
void WriteFloat(float value, float min, float max, int numberOfBits)
void WriteToMessage(IWriteMessage msg)
void WriteBoolean(bool b)