| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #if DEBUG && !UNITY_WP_8_1 && !UNITY_WSA_8_1
- using System.Runtime.InteropServices;
- namespace FlyingWormConsole3.LiteNetLib.Utils
- {
- public static class FastBitConverter
- {
- [StructLayout(LayoutKind.Explicit)]
- private struct ConverterHelperDouble
- {
- [FieldOffset(0)]
- public ulong Along;
- [FieldOffset(0)]
- public double Adouble;
- }
- [StructLayout(LayoutKind.Explicit)]
- private struct ConverterHelperFloat
- {
- [FieldOffset(0)]
- public int Aint;
- [FieldOffset(0)]
- public float Afloat;
- }
- private static void WriteLittleEndian(byte[] buffer, int offset, ulong data)
- {
- #if BIGENDIAN
- buffer[offset + 7] = (byte)(data);
- buffer[offset + 6] = (byte)(data >> 8);
- buffer[offset + 5] = (byte)(data >> 16);
- buffer[offset + 4] = (byte)(data >> 24);
- buffer[offset + 3] = (byte)(data >> 32);
- buffer[offset + 2] = (byte)(data >> 40);
- buffer[offset + 1] = (byte)(data >> 48);
- buffer[offset ] = (byte)(data >> 56);
- #else
- buffer[offset] = (byte)(data);
- buffer[offset + 1] = (byte)(data >> 8);
- buffer[offset + 2] = (byte)(data >> 16);
- buffer[offset + 3] = (byte)(data >> 24);
- buffer[offset + 4] = (byte)(data >> 32);
- buffer[offset + 5] = (byte)(data >> 40);
- buffer[offset + 6] = (byte)(data >> 48);
- buffer[offset + 7] = (byte)(data >> 56);
- #endif
- }
- private static void WriteLittleEndian(byte[] buffer, int offset, int data)
- {
- #if BIGENDIAN
- buffer[offset + 3] = (byte)(data);
- buffer[offset + 2] = (byte)(data >> 8);
- buffer[offset + 1] = (byte)(data >> 16);
- buffer[offset ] = (byte)(data >> 24);
- #else
- buffer[offset] = (byte)(data);
- buffer[offset + 1] = (byte)(data >> 8);
- buffer[offset + 2] = (byte)(data >> 16);
- buffer[offset + 3] = (byte)(data >> 24);
- #endif
- }
- public static void WriteLittleEndian(byte[] buffer, int offset, short data)
- {
- #if BIGENDIAN
- buffer[offset + 1] = (byte)(data);
- buffer[offset ] = (byte)(data >> 8);
- #else
- buffer[offset] = (byte)(data);
- buffer[offset + 1] = (byte)(data >> 8);
- #endif
- }
- public static void GetBytes(byte[] bytes, int startIndex, double value)
- {
- ConverterHelperDouble ch = new ConverterHelperDouble { Adouble = value };
- WriteLittleEndian(bytes, startIndex, ch.Along);
- }
- public static void GetBytes(byte[] bytes, int startIndex, float value)
- {
- ConverterHelperFloat ch = new ConverterHelperFloat { Afloat = value };
- WriteLittleEndian(bytes, startIndex, ch.Aint);
- }
- public static void GetBytes(byte[] bytes, int startIndex, short value)
- {
- WriteLittleEndian(bytes, startIndex, value);
- }
- public static void GetBytes(byte[] bytes, int startIndex, ushort value)
- {
- WriteLittleEndian(bytes, startIndex, (short)value);
- }
- public static void GetBytes(byte[] bytes, int startIndex, int value)
- {
- WriteLittleEndian(bytes, startIndex, value);
- }
- public static void GetBytes(byte[] bytes, int startIndex, uint value)
- {
- WriteLittleEndian(bytes, startIndex, (int)value);
- }
- public static void GetBytes(byte[] bytes, int startIndex, long value)
- {
- WriteLittleEndian(bytes, startIndex, (ulong)value);
- }
- public static void GetBytes(byte[] bytes, int startIndex, ulong value)
- {
- WriteLittleEndian(bytes, startIndex, value);
- }
- }
- }
- #endif
|