using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;

public class UTY : MonoBehaviour
{
	public static string[] GetStringList(string sss)
	{
		List<string> list = new List<string>();
		if (sss == null)
		{
			sss = string.Empty;
		}
		int length = sss.Length;
		int i = 0;
		string text = string.Empty;
		while (i < length)
		{
			while (i < length && (sss[i] == ' ' || sss[i] == '\t' || sss[i] == '\u3000' || sss[i] == '\r'))
			{
				i++;
			}
			if (i == length)
			{
				break;
			}
			if (i < length && sss[i] == '"')
			{
				i++;
				while (i < length && sss[i] != '"')
				{
					text += sss[i];
					i++;
				}
				i++;
			}
			else
			{
				while (i < length && sss[i] != ' ' && sss[i] != '\t' && sss[i] != '\u3000' && sss[i] != '\r')
				{
					text += sss[i];
					i++;
				}
			}
			list.Add(text);
			text = string.Empty;
		}
		return list.ToArray();
	}

	public static float DistLineToPointPrim3(Vector3 p0, Vector3 p1, Vector3 c)
	{
		Vector3 vector = p1 - p0;
		if (vector == Vector3.zero)
		{
			return 0f;
		}
		Vector3 rhs = c - p0;
		float magnitude = vector.magnitude;
		float d = Vector3.Dot(vector, rhs) / magnitude;
		return (c - (p0 + vector * d)).magnitude;
	}

	public static Vector3 LineToPointPrim3(Vector3 p0, Vector3 p1, Vector3 c)
	{
		Vector3 vector = p1 - p0;
		if (vector == Vector3.zero)
		{
			return Vector3.zero;
		}
		Vector3 rhs = c - p0;
		float sqrMagnitude = vector.sqrMagnitude;
		float d = Vector3.Dot(vector, rhs) / sqrMagnitude;
		return p0 + vector * d;
	}

	public static string GetStringCom(string sss)
	{
		int length = sss.Length;
		int num = 0;
		string text = string.Empty;
		while (num < length && (sss[num] == ' ' || sss[num] == '\t' || sss[num] == '\u3000'))
		{
			num++;
		}
		if (num < length && sss[num] == '/')
		{
			return string.Empty;
		}
		if (num < length && sss[num] == '"')
		{
			num++;
			while (num < length && sss[num] != '"')
			{
				text += sss[num];
				num++;
			}
			num++;
		}
		else
		{
			while (num < length && sss[num] != ' ' && sss[num] != '\t' && sss[num] != '\u3000')
			{
				text += sss[num];
				num++;
			}
		}
		return text.ToLower();
	}

	public static string[] GetStringListSS(string sss)
	{
		List<string> list = new List<string>();
		int length = sss.Length;
		int i = 0;
		string text = string.Empty;
		while (i < length)
		{
			while (i < length && (sss[i] == ' ' || sss[i] == '\t' || sss[i] == '\u3000'))
			{
				i++;
			}
			if (i == length)
			{
				break;
			}
			if (i < length && sss[i] == '"')
			{
				i++;
				while (i < length && sss[i] != '"')
				{
					text += sss[i];
					i++;
				}
				i++;
			}
			else if (i < length && sss[i] == '『')
			{
				i++;
				while (i < length && sss[i] != '』')
				{
					text += sss[i];
					i++;
				}
				i++;
			}
			else
			{
				while (i < length && sss[i] != ' ' && sss[i] != '\t' && sss[i] != '\u3000')
				{
					text += sss[i];
					i++;
				}
			}
			list.Add(text);
			text = string.Empty;
		}
		return list.ToArray();
	}

	public static string[] GetStringListSS_2(string sss)
	{
		List<string> list = new List<string>();
		int length = sss.Length;
		int i = 0;
		string text = string.Empty;
		while (i < length)
		{
			while (i < length && (sss[i] == ' ' || sss[i] == '\t' || sss[i] == '\u3000'))
			{
				i++;
			}
			if (i == length)
			{
				break;
			}
			if (i < length && sss[i] == '"')
			{
				i++;
				text += "\"";
				while (i < length && sss[i] != '"')
				{
					text += sss[i];
					i++;
				}
				i++;
				text += "\"";
			}
			else if (i < length && sss[i] == '『')
			{
				i++;
				text += "『";
				while (i < length && sss[i] != '』')
				{
					text += sss[i];
					i++;
				}
				i++;
				text += "』";
			}
			else
			{
				while (i < length && sss[i] != ' ' && sss[i] != '\t' && sss[i] != '\u3000')
				{
					text += sss[i];
					i++;
				}
			}
			list.Add(text);
			text = string.Empty;
		}
		return list.ToArray();
	}

	public static int Clamp(int val, int min, int max)
	{
		if (val > max)
		{
			return max;
		}
		if (val < min)
		{
			return min;
		}
		return val;
	}

	public static int GetRnd(int n)
	{
		if (UTY.rnd == null)
		{
			UTY.rnd = new System.Random();
		}
		return UTY.rnd.Next(n);
	}

	public static float COSS2(float v, float pw = 4f)
	{
		if (v < 0.5f)
		{
			v *= 2f;
			float num = Mathf.Pow(v, pw);
			return num * 0.5f;
		}
		float num2 = (1f - v) * 2f;
		num2 = 1f - Mathf.Pow(num2, pw);
		return num2 * 0.5f + 0.5f;
	}

	public static float COSS(float v)
	{
		return Mathf.Cos(3.14159274f * (1f - v)) * 0.5f + 0.5f;
	}

	private bool Texture2Image(RenderTexture f_rtSrc, string f_strSaveFileName)
	{
		return true;
	}

	public static string FileNameEscape(string f_strSrc)
	{
		return Regex.Replace(f_strSrc, UTY.m_strEscapePattern, "_");
	}

	public static void InitDll()
	{
	}

	public static void FreeDll()
	{
	}

	public static GameObject GetChildObject(GameObject f_goParent, string f_strObjName, bool f_bNoError = false)
	{
		Transform transform = f_goParent.transform.Find(f_strObjName);
		if (f_bNoError)
		{
			if (transform == null)
			{
				return null;
			}
		}
		else if (transform == null)
		{
			NDebug.Assert(f_goParent.name + " の子から " + f_strObjName + " が見つかりませんでした。相対パス指定が必要です。", false);
		}
		return transform.gameObject;
	}

	public static GameObject GetChildObjectNoError(GameObject f_goParent, string f_strObjName, bool f_bNoError = false)
	{
		Transform transform = f_goParent.transform.Find(f_strObjName);
		if (f_bNoError)
		{
			if (transform == null)
			{
				return null;
			}
		}
		else if (transform == null)
		{
			return null;
		}
		return transform.gameObject;
	}

	public static string GetObjectTreePath(GameObject obj)
	{
		string text = string.Empty;
		if (obj == null)
		{
			return text;
		}
		text += obj.name;
		while (obj.transform.parent != null)
		{
			obj = obj.transform.parent.gameObject;
			text = obj.name + "/" + text;
		}
		return text;
	}

	public static byte[] GetPixelArray(Texture2D f_texSrc)
	{
		return ColorPalette.GetPixelArrayNativeFromRGBA8(f_texSrc);
	}

	public static void ConvertColor(MaidParts.PartsColor f_cColor, byte[] f_bySrc, ref Texture2D f_texDest)
	{
		UTY.m_colStatus.base_color.hue = f_cColor.m_nMainHue;
		UTY.m_colStatus.base_color.sat = f_cColor.m_nMainChroma;
		UTY.m_colStatus.base_color.brightness = f_cColor.m_nMainBrightness;
		UTY.m_colStatus.base_color.contrast = f_cColor.m_nMainContrast;
		UTY.m_colStatus.shadow_color.hue = f_cColor.m_nShadowHue;
		UTY.m_colStatus.shadow_color.sat = f_cColor.m_nShadowChroma;
		UTY.m_colStatus.shadow_color.brightness = f_cColor.m_nShadowBrightness;
		UTY.m_colStatus.shadow_color.contrast = f_cColor.m_nShadowContrast;
		UTY.m_colStatus.shadow_threshold = f_cColor.m_nShadowRate;
		ColorPalette.ConvertColor(UTY.m_colStatus, f_bySrc, ref f_texDest);
	}

	public static void ConvertColorHue(int f_nHue, byte[] f_bySrc, ref Texture2D f_texDest)
	{
		UTY.m_colStatus.base_color.hue = f_nHue;
		UTY.m_colStatus.base_color.sat = 128;
		UTY.m_colStatus.base_color.brightness = 255;
		UTY.m_colStatus.base_color.contrast = 0;
		UTY.m_colStatus.shadow_color.hue = 0;
		UTY.m_colStatus.shadow_color.sat = 0;
		UTY.m_colStatus.shadow_color.brightness = 0;
		UTY.m_colStatus.shadow_color.contrast = 0;
		UTY.m_colStatus.shadow_threshold = 0;
		ColorPalette.ConvertColor(UTY.m_colStatus, f_bySrc, ref f_texDest);
	}

	public static void ConvertColor(ColorPalette.Status f_cColor, byte[] f_bySrc, ref Texture2D f_texDest)
	{
		ColorPalette.ConvertColor(f_cColor, f_bySrc, ref f_texDest);
	}

	public static void UpdateColorTableTexture(MaidParts.PartsColor f_cColor, ref Texture2D f_texDest)
	{
		UTY.m_colStatus.base_color.hue = f_cColor.m_nMainHue;
		UTY.m_colStatus.base_color.sat = f_cColor.m_nMainChroma;
		UTY.m_colStatus.base_color.brightness = f_cColor.m_nMainBrightness;
		UTY.m_colStatus.base_color.contrast = f_cColor.m_nMainContrast;
		UTY.m_colStatus.shadow_color.hue = f_cColor.m_nShadowHue;
		UTY.m_colStatus.shadow_color.sat = f_cColor.m_nShadowChroma;
		UTY.m_colStatus.shadow_color.brightness = f_cColor.m_nShadowBrightness;
		UTY.m_colStatus.shadow_color.contrast = f_cColor.m_nShadowContrast;
		UTY.m_colStatus.shadow_threshold = f_cColor.m_nShadowRate;
		ColorPalette.WriteColorTableTexture(UTY.m_colStatus, ref f_texDest);
	}

	public static void SaveImage(RenderTexture f_rtSrc, string f_strFileName = null, TextureFormat f_TexFormat = TextureFormat.ARGB32)
	{
		Texture2D texture2D = new Texture2D(f_rtSrc.width, f_rtSrc.height, f_TexFormat, false);
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = f_rtSrc;
		texture2D.ReadPixels(new Rect(0f, 0f, (float)f_rtSrc.width, (float)f_rtSrc.height), 0, 0);
		RenderTexture.active = active;
		UTY.SaveImage(texture2D, f_strFileName);
	}

	public static string SaveImage(Texture2D f_texSrc, string f_strFileName = null)
	{
		string text = f_strFileName;
		if (text == null || text == string.Empty)
		{
			text = "img" + DateTime.Now.ToString("yyyyMMddHHmmss.fff") + ".png";
		}
		byte[] bytes = f_texSrc.EncodeToPNG();
		File.WriteAllBytes(text, bytes);
		return text;
	}

	public static byte[] LoadImage(string f_strFileName)
	{
		FileStream fileStream = new FileStream(f_strFileName, FileMode.Open);
		if (fileStream == null)
		{
			NDebug.Assert("イメージファイルがみつかりません。" + f_strFileName, false);
		}
		byte[] array = new byte[fileStream.Length];
		fileStream.Read(array, 0, (int)fileStream.Length);
		fileStream.Close();
		fileStream.Dispose();
		return array;
	}

	public static Texture2D LoadTexture(string f_strFileName)
	{
		Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
		texture2D.LoadImage(UTY.LoadImage(f_strFileName));
		return texture2D;
	}

	public static void ClearRT(RenderTexture target, Color col)
	{
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = target;
		GL.Clear(true, true, col);
		RenderTexture.active = active;
	}

	public static bool IsLowercaseAlphanumeric(string target)
	{
		return new Regex("^[!-~]*$()").IsMatch(target);
	}

	public static T DeepCopy<T>(T src)
	{
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		MemoryStream memoryStream = new MemoryStream();
		T result;
		try
		{
			binaryFormatter.Serialize(memoryStream, src);
			memoryStream.Position = 0L;
			result = (T)((object)binaryFormatter.Deserialize(memoryStream));
		}
		finally
		{
			memoryStream.Close();
		}
		return result;
	}

	public static string GetExePath()
	{
		string dataPath = Application.dataPath;
		int num = 0;
		for (int i = dataPath.Length - 1; i >= 0; i--)
		{
			if (dataPath[i] == '/')
			{
				num = i + 1;
				break;
			}
		}
		string str = dataPath.Substring(num, dataPath.Length - num).ToLower().Replace("_data", ".exe");
		string str2 = dataPath.Substring(0, num);
		return str2 + str;
	}

	private static System.Random rnd;

	private static string m_strEscapePattern = "(\\\\|/|:|\\*|\\?|\"|\\<|\\>|\\||\\.|\\,)";

	private static ColorPalette.Status m_colStatus = new ColorPalette.Status();

	private class Crypt
	{
		public static string Encrypt(string text)
		{
			ICryptoTransform transform = new RijndaelManaged
			{
				BlockSize = 128,
				KeySize = 128,
				Padding = PaddingMode.Zeros,
				Mode = CipherMode.CBC,
				Key = Encoding.UTF8.GetBytes("feaeaeantydhgs"),
				IV = Encoding.UTF8.GetBytes("ujuuiaefharsbs")
			}.CreateEncryptor();
			MemoryStream memoryStream = new MemoryStream();
			CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
			byte[] bytes = Encoding.UTF8.GetBytes(text);
			cryptoStream.Write(bytes, 0, bytes.Length);
			cryptoStream.FlushFinalBlock();
			byte[] inArray = memoryStream.ToArray();
			return Convert.ToBase64String(inArray);
		}

		public static string Decrypt(string cryptText)
		{
			ICryptoTransform transform = new RijndaelManaged
			{
				BlockSize = 128,
				KeySize = 128,
				Padding = PaddingMode.Zeros,
				Mode = CipherMode.CBC,
				Key = Encoding.UTF8.GetBytes("feaeaeantydhgs"),
				IV = Encoding.UTF8.GetBytes("ujuuiaefharsbs")
			}.CreateDecryptor();
			byte[] array = Convert.FromBase64String(cryptText);
			byte[] array2 = new byte[array.Length];
			MemoryStream stream = new MemoryStream(array);
			CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read);
			cryptoStream.Read(array2, 0, array2.Length);
			return Encoding.UTF8.GetString(array2);
		}

		private const string AesIV = "ujuuiaefharsbs";

		private const string AesKey = "feaeaeantydhgs";
	}
}
