using System; using UnityEngine; namespace Scourt.Utility { public static class Parse { public static bool TryParse(string text, out Vector2 result, Action 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 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 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 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 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 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 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 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 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 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 errorCallBack = null) { bool flag = long.TryParse(text, out result); if (!flag && errorCallBack != null) { errorCallBack("longのParseに失敗しました"); } return flag; } public static bool TryParse(string text, out T result, Action 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; } } }