using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

namespace FacilityFlag
{
	public static class Util
	{
		public static bool ToBoolean(BinaryReader br)
		{
			return br.ReadBoolean();
		}

		public static byte ToByte(BinaryReader br)
		{
			return br.ReadByte();
		}

		public static short ToInt16(BinaryReader br)
		{
			return br.ReadInt16();
		}

		public static int ToInt32(BinaryReader br)
		{
			return br.ReadInt32();
		}

		public static long ToInt64(BinaryReader br)
		{
			return br.ReadInt64();
		}

		public static char ToChar(BinaryReader br)
		{
			return br.ReadChar();
		}

		public static decimal ToDecimal(BinaryReader br)
		{
			return br.ReadDecimal();
		}

		public static double ToDouble(BinaryReader br)
		{
			return br.ReadDouble();
		}

		public static float ToSingle(BinaryReader br)
		{
			return br.ReadSingle();
		}

		public static string ToString(BinaryReader br)
		{
			return br.ReadString();
		}

		public static ushort ToUInt16(BinaryReader br)
		{
			return br.ReadUInt16();
		}

		public static uint ToUInt32(BinaryReader br)
		{
			return br.ReadUInt32();
		}

		public static ulong ToUInt64(BinaryReader br)
		{
			return br.ReadUInt64();
		}

		public static SimpleExperienceSystem ToSimpleExp(BinaryReader br)
		{
			SimpleExperienceSystem simpleExperienceSystem = new SimpleExperienceSystem();
			simpleExperienceSystem.Deserialize(br, 0);
			return simpleExperienceSystem;
		}

		public static byte[] GetBytes(string value)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(value);
			int value2 = bytes.Length;
			byte[] array = Util.CreatePrefix(value2);
			return Util.AppendBuffer(new Array[]
			{
				array,
				bytes
			});
		}

		public static byte[] GetBytes(float value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(ulong value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(uint value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(ushort value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(long value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(double value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(short value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(char value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(bool value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(int value)
		{
			return BitConverter.GetBytes(value);
		}

		public static byte[] GetBytes(SimpleExperienceSystem simpleExp)
		{
			byte[] result;
			try
			{
				using (MemoryStream memoryStream = new MemoryStream())
				{
					using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
					{
						simpleExp.Serialize(binaryWriter);
					}
					byte[] array = memoryStream.ToArray();
					result = array;
				}
			}
			catch
			{
				result = new byte[0];
			}
			return result;
		}

		public static byte[] CreatePrefix(int value)
		{
			List<byte> prefixBytes = new List<byte>();
			byte b = 0;
			Action f = null;
			f = delegate()
			{
				b = (byte)(value & 127);
				value >>= 7;
				b |= ((value <= 0) ? 0 : 128);
				prefixBytes.Add(b);
				if ((b & 128) > 0)
				{
					f();
				}
			};
			f();
			return prefixBytes.ToArray();
		}

		public static int GetPrefix(byte[] buffer, long readStartPos)
		{
			Func<int, int> f = null;
			byte b = 0;
			f = delegate(int callCount)
			{
				b = buffer[(int)(checked((IntPtr)(unchecked(readStartPos + (long)callCount))))];
				int num = (int)(b & 127);
				num <<= callCount * 7;
				if ((b & 128) == 128)
				{
					num += f(callCount + 1);
				}
				return num;
			};
			return f(0);
		}

		public static int GetPrefix(Stream stream, long readStartPos, bool isEnableSeek = false)
		{
			long position = stream.Position;
			Func<int, int> f = null;
			byte b = 0;
			f = delegate(int callCount)
			{
				b = (byte)stream.ReadByte();
				int num = (int)(b & 127);
				num <<= callCount * 7;
				if ((b & 128) == 128)
				{
					num += f(callCount + 1);
				}
				return num;
			};
			int result = f(0);
			if (!isEnableSeek)
			{
				stream.Seek(position, SeekOrigin.Begin);
			}
			return result;
		}

		public static byte[] AppendBuffer(params Array[] bufferArray)
		{
			try
			{
				long num = 0L;
				for (int i = 0; i < bufferArray.Length; i++)
				{
					num += ((byte[])bufferArray[i]).LongLength;
				}
				byte[] array = new byte[num];
				int num2 = 0;
				for (int j = 0; j < bufferArray.Length; j++)
				{
					Buffer.BlockCopy(bufferArray[j], 0, array, num2, bufferArray[j].Length);
					num2 += bufferArray[j].Length;
				}
				return array;
			}
			catch (Exception message)
			{
				Debug.LogError(message);
			}
			return null;
		}
	}
}
