context: namespace FalloutSnip.Framework
  {
      using System;
      using System.Runtime.InteropServices;
      using System.Text;
  
      using Encoding = FalloutSnip.Framework.Services.Encoding;
  
      [StructLayout(LayoutKind.Explicit)]
      public struct TypeConverter
      {
          [FieldOffset(0)]
          private uint i;
  
          [FieldOffset(0)]
          private int si;
  
          [FieldOffset(0)]
          private ushort s;
  
          [FieldOffset(0)]
          private short ss;
  
          [FieldOffset(0)]
          private float f;
  
          [FieldOffset(0)]
          private byte b1;
  
          [FieldOffset(1)]
          private byte b2;
  
          [FieldOffset(2)]
          private byte b3;
  
          [FieldOffset(3)]
          private byte b4;
  
          [FieldOffset(0)]
          private readonly sbyte sb1;
  
          private static TypeConverter tc;
  
          private static readonly byte[] bytes = new byte[4];
  
          /*public static float i2f(uint i) {
              tc.i=i;
              return tc.f;
          }*/
          /*public static uint f2i(float f) {
              tc.f=f;
              return tc.i;
          }*/
          public static float h2f(byte b1, byte b2, byte b3, byte b4)
          {
            try
            {
              tc.b1 = b1;
              tc.b2 = b2;
              tc.b3 = b3;
              tc.b4 = b4;
              return tc.f;
            }
            catch (Exception)
            {
              throw;
            }
  
          }
  
          public static object GetObject<T>(byte[] data, int offset)
          {
            try
            {
              return (T)GetObject<T>(new ArraySegment<byte>(data, offset, data.Length - offset));
            }
            catch (Exception)
            {
              throw;
            }
  
          }
  
          public static object GetObject<T>(ArraySegment<byte> data)
          {
            try
            {
  
  
              T result = default(T);
              if (result is float)
              {
                  return h2f(data);
              }
  
              if (result is int)
              {
                  return h2si(data);
              }
  
              if (result is uint)
              {
                  return h2i(data);
              }
  
              if (result is short)
              {
                  return h2ss(data);
              }
  
              if (result is ushort)
              {
                  return h2s(data);
              }
  
              if (result is sbyte)
              {
                  return h2sb(data);
              }
  
              if (result is byte)
              {
                  return h2b(data);
              }
  
              if (result is string)
              {
                  return GetString(data);
              }
  
              return default(T);
            }
            catch (Exception)
            {
              throw;
            }
          }
  
          public static object GetValue<T>(ArraySegment<byte> data)
          {
            try
            {
              return (T) GetObject<T>(data);
            }
            catch (Exception)
            {
              throw;
            }
          }
  
          public static bool TryGetObject<T>(byte[] data, int offset, out object result)
          {
            try
            {
              result = GetObject<T>(new ArraySegment<byte>(data, offset, data.Length - offset));
              return true;
            }
            catch (Exception)
            {
              throw;
            }
  
          }
  
          public static ArraySegment<byte> GetData<T>(object value)
          {
            try
            {
  
  
              Type t = typeof(T);
              if (t == typeof(float))
              {
                  return new ArraySegment<byte>(f2h(Extensions.CastValue<float>(value)), 0, 4);
              }
  
              if (t == typeof(int))
              {
                  return new ArraySegment<byte>(si2h(Extensions.CastValue<int>(value)), 0, 4);
              }
  
              if (t == typeof(uint))
              {
                  return new ArraySegment<byte>(i2h(Extensions.CastValue<uint>(value)), 0, 4);
              }
  
              if (t == typeof(short))
              {
                  return new ArraySegment<byte>(ss2h(Extensions.CastValue<short>(value)), 0, 2);
              }
  
              if (t == typeof(ushort))
              {
                  return new ArraySegment<byte>(s2h(Extensions.CastValue<ushort>(value)), 0, 2);
              }
  
              if (t == typeof(sbyte))
              {
                  return new ArraySegment<byte>(sb2h(Extensions.CastValue<sbyte>(value)), 0, 1);
              }
  
              if (t == typeof(byte))
              {
                  return new ArraySegment<byte>(si2h(Extensions.CastValue<byte>(value)), 0, 1);
              }
  
              if (t == typeof(string))
              {
                  return new ArraySegment<byte>(str2h(Extensions.CastValue<string>(value)));
              }
  
              if (t == typeof(ArraySegment<byte>))
              {
                  return (ArraySegment<byte>)value;
              }
  
              return new ArraySegment<byte>(new byte[0]);
            }
            catch (Exception)
            {
              throw;
            }
          }
  
          public static bool TrySetValue<T>(ArraySegment<byte> data, object value)
          {
            try
            {
              var seg = GetData<T>(value);
              if (seg.Count == data.Count)
              {
                Array.Copy(seg.Array, seg.Offset, data.Array, data.Offset, data.Count);
                return true;
              }
  
              return false;
            }
            catch (Exception)
            {
              throw;
            }
  
          }
  
          public static float h2f(byte[] data, int offset)
          {
            try
            {
  
  
              if (offset + sizeof(float) > data.Length)
              {
                  return default(float);
              }
  
              tc.b1 = data[offset + 0];
              tc.b2 = data[offset + 1];
              tc.b3 = data[offset + 2];
              tc.b4 = data[offset + 3];
              return tc.f;
            }
            catch (Exception)
            {
              throw;
            }
          }
  
          public static float h2f(ArraySegment<byte> data)
          {
              //float f = 0;
              try
              {
                  if (data.Count >= 4)
                  {
                      tc.b1 = data.Array[data.Offset + 0];
                      tc.b2 = data.Array[data.Offset + 1];
                      tc.b3 = data.Array[data.Offset + 2];
                      tc.b4 = data.Array[data.Offset + 3];
                      return tc.f;
                  }
                  return default(float);
              }
              catch (Exception)
              {
                  throw;
              }
          }
  
          public static uint h2i(byte b1, byte b2, byte b3, byte b4)
          {
            try
            {
              tc.b1 = b1;
              tc.b2 = b2;
              tc.b3 = b3;
              tc.b4 = b4;
              return tc.i;
            }
            catch (Exception)
            {
              throw;
            }
   
          }
  
          public static uint h2i(ArraySegment<byte> data)
          {
            try
            {
              if (data.Count >= 4)
              {
                tc.b1 = data.Array[data.Offset + 0];
                tc.b2 = data.Array[data.Offset + 1];
                tc.b3 = data.Array[data.Offset + 2];
                tc.b4 = data.Array[data.Offset + 3];
                return tc.i;
              }
  
              return 0;
            }
            catch (Exception)
            {
              throw;
            }
  
          }
  
          public static int h2si(byte b1, byte b2, byte b3, byte b4)
          {
            try
            {
              tc.b1 = b1;
              tc.b2 = b2;
              tc.b3 = b3;
              tc.b4 = b4;
              return tc.si;
            }
            catch (Exception)
            {
              throw;
            }
  
          }
  
          public static int h2si(byte[] data, int offset)
          {
            try
            {
              if (data.Length >= 4)
              {
  
gt:               tc.b1 = data[offset + 0];