123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using System;
- using UnityEngine;
- namespace Scourt.Utility
- {
- public static class Parse
- {
- public static bool TryParse(string text, out Vector2 result, Action<string> errorCallBack = null)
- {
- result = Vector2.zero;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- text = text.Trim();
- string[] array = text.Substring(1, text.Length - 2).Split(new char[]
- {
- ','
- });
- bool flag = array.Length != 2;
- if (!flag)
- {
- flag |= !Parse.TryParse(array[0], out result.x, null);
- flag |= !Parse.TryParse(array[1], out result.y, null);
- }
- if (flag)
- {
- if (errorCallBack != null)
- {
- errorCallBack("Vector2のParseに失敗しました");
- }
- return false;
- }
- return true;
- }
- public static bool TryParse(string text, out Vector3 result, Action<string> errorCallBack = null)
- {
- result = Vector3.zero;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- text = text.Trim();
- string[] array;
- if ('0' <= text[0] && text[0] <= '9')
- {
- array = text.Split(new char[]
- {
- ','
- });
- }
- else
- {
- array = text.Substring(1, text.Length - 2).Split(new char[]
- {
- ','
- });
- }
- bool flag = array.Length != 3;
- if (!flag)
- {
- flag |= !Parse.TryParse(array[0], out result.x, null);
- flag |= !Parse.TryParse(array[1], out result.y, null);
- flag |= !Parse.TryParse(array[2], out result.z, null);
- }
- if (flag)
- {
- if (errorCallBack != null)
- {
- errorCallBack("Vector3のParseに失敗しました");
- }
- return false;
- }
- return true;
- }
- public static bool TryParse(string text, out Vector4 result, Action<string> errorCallBack = null)
- {
- result = Vector4.zero;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- text = text.Trim();
- string[] array = text.Substring(1, text.Length - 2).Split(new char[]
- {
- ','
- });
- bool flag = array.Length != 4;
- if (!flag)
- {
- flag |= !Parse.TryParse(array[0], out result.x, null);
- flag |= !Parse.TryParse(array[1], out result.y, null);
- flag |= !Parse.TryParse(array[2], out result.z, null);
- flag |= !Parse.TryParse(array[3], out result.w, null);
- }
- if (flag)
- {
- if (errorCallBack != null)
- {
- errorCallBack("Vector4のParseに失敗しました");
- }
- return false;
- }
- return true;
- }
- public static bool TryParse(string text, out Quaternion result, Action<string> errorCallBack = null)
- {
- result = Quaternion.identity;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- text = text.Trim();
- string[] array = text.Substring(1, text.Length - 2).Split(new char[]
- {
- ','
- });
- bool flag = array.Length != 4;
- if (!flag)
- {
- float x;
- flag |= !Parse.TryParse(array[0], out x, null);
- float y;
- flag |= !Parse.TryParse(array[1], out y, null);
- float z;
- flag |= !Parse.TryParse(array[2], out z, null);
- float w;
- flag |= !Parse.TryParse(array[3], out w, null);
- result = new Quaternion(x, y, z, w);
- }
- if (flag)
- {
- if (errorCallBack != null)
- {
- errorCallBack("QuaternionのParseに失敗しました");
- }
- return false;
- }
- return true;
- }
- public static bool TryParse(string text, out Color result, Action<string> errorCallBack = null)
- {
- result = Color.black;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- text = text.Trim().Replace("RGBA", string.Empty);
- string[] array = text.Substring(1, text.Length - 2).Split(new char[]
- {
- ','
- });
- bool flag = array.Length != 4;
- if (!flag)
- {
- float r;
- flag |= !Parse.TryParse(array[0], out r, null);
- float g;
- flag |= !Parse.TryParse(array[1], out g, null);
- float b;
- flag |= !Parse.TryParse(array[2], out b, null);
- float a;
- flag |= !Parse.TryParse(array[3], out a, null);
- result = new Color(r, g, b, a);
- }
- if (flag)
- {
- if (errorCallBack != null)
- {
- errorCallBack("ColorのParseに失敗しました");
- }
- return false;
- }
- return true;
- }
- public static bool TryParse(string text, out byte result, Action<string> errorCallBack = null)
- {
- bool flag = byte.TryParse(text, out result);
- if (!flag && errorCallBack != null)
- {
- errorCallBack("byteのParseに失敗しました");
- }
- return flag;
- }
- public static bool TryParse(string text, out int result, Action<string> errorCallBack = null)
- {
- bool flag = int.TryParse(text, out result);
- if (!flag && errorCallBack != null)
- {
- errorCallBack("intのParseに失敗しました");
- }
- return flag;
- }
- public static bool TryParse(string text, out uint result, Action<string> errorCallBack = null)
- {
- bool flag = uint.TryParse(text, out result);
- if (!flag && errorCallBack != null)
- {
- errorCallBack("uintのParseに失敗しました");
- }
- return flag;
- }
- public static bool TryParse(string text, out float result, Action<string> errorCallBack = null)
- {
- bool flag = float.TryParse(text, out result);
- if (!flag && errorCallBack != null)
- {
- errorCallBack("floatのParseに失敗しました");
- }
- return flag;
- }
- public static bool TryParse(string text, out double result, Action<string> errorCallBack = null)
- {
- bool flag = double.TryParse(text, out result);
- if (!flag && errorCallBack != null)
- {
- errorCallBack("doubleのParseに失敗しました");
- }
- return flag;
- }
- public static bool TryParse(string text, out long result, Action<string> errorCallBack = null)
- {
- bool flag = long.TryParse(text, out result);
- if (!flag && errorCallBack != null)
- {
- errorCallBack("longのParseに失敗しました");
- }
- return flag;
- }
- public static bool TryParse<T>(string text, out T result, Action<string> errorCallBack = null) where T : struct
- {
- try
- {
- result = (T)((object)Enum.Parse(typeof(T), text));
- return true;
- }
- catch (Exception ex)
- {
- if (!string.IsNullOrEmpty(text))
- {
- text = text.ToLower();
- foreach (string text2 in Enum.GetNames(typeof(T)))
- {
- if (text == text2.ToLower())
- {
- try
- {
- result = (T)((object)Enum.Parse(typeof(T), text2));
- return true;
- }
- catch (Exception ex2)
- {
- }
- }
- }
- }
- }
- result = default(T);
- if (errorCallBack != null)
- {
- errorCallBack(typeof(T).ToString() + "のParseに失敗しました");
- }
- return false;
- }
- }
- }
|