Parse.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using UnityEngine;
  3. namespace Scourt.Utility
  4. {
  5. public static class Parse
  6. {
  7. public static bool TryParse(string text, out Vector2 result, Action<string> errorCallBack = null)
  8. {
  9. result = Vector2.zero;
  10. if (string.IsNullOrEmpty(text))
  11. {
  12. return false;
  13. }
  14. text = text.Trim();
  15. string[] array = text.Substring(1, text.Length - 2).Split(new char[]
  16. {
  17. ','
  18. });
  19. bool flag = array.Length != 2;
  20. if (!flag)
  21. {
  22. flag |= !Parse.TryParse(array[0], out result.x, null);
  23. flag |= !Parse.TryParse(array[1], out result.y, null);
  24. }
  25. if (flag)
  26. {
  27. if (errorCallBack != null)
  28. {
  29. errorCallBack("Vector2のParseに失敗しました");
  30. }
  31. return false;
  32. }
  33. return true;
  34. }
  35. public static bool TryParse(string text, out Vector3 result, Action<string> errorCallBack = null)
  36. {
  37. result = Vector3.zero;
  38. if (string.IsNullOrEmpty(text))
  39. {
  40. return false;
  41. }
  42. text = text.Trim();
  43. string[] array;
  44. if ('0' <= text[0] && text[0] <= '9')
  45. {
  46. array = text.Split(new char[]
  47. {
  48. ','
  49. });
  50. }
  51. else
  52. {
  53. array = text.Substring(1, text.Length - 2).Split(new char[]
  54. {
  55. ','
  56. });
  57. }
  58. bool flag = array.Length != 3;
  59. if (!flag)
  60. {
  61. flag |= !Parse.TryParse(array[0], out result.x, null);
  62. flag |= !Parse.TryParse(array[1], out result.y, null);
  63. flag |= !Parse.TryParse(array[2], out result.z, null);
  64. }
  65. if (flag)
  66. {
  67. if (errorCallBack != null)
  68. {
  69. errorCallBack("Vector3のParseに失敗しました");
  70. }
  71. return false;
  72. }
  73. return true;
  74. }
  75. public static bool TryParse(string text, out Vector4 result, Action<string> errorCallBack = null)
  76. {
  77. result = Vector4.zero;
  78. if (string.IsNullOrEmpty(text))
  79. {
  80. return false;
  81. }
  82. text = text.Trim();
  83. string[] array = text.Substring(1, text.Length - 2).Split(new char[]
  84. {
  85. ','
  86. });
  87. bool flag = array.Length != 4;
  88. if (!flag)
  89. {
  90. flag |= !Parse.TryParse(array[0], out result.x, null);
  91. flag |= !Parse.TryParse(array[1], out result.y, null);
  92. flag |= !Parse.TryParse(array[2], out result.z, null);
  93. flag |= !Parse.TryParse(array[3], out result.w, null);
  94. }
  95. if (flag)
  96. {
  97. if (errorCallBack != null)
  98. {
  99. errorCallBack("Vector4のParseに失敗しました");
  100. }
  101. return false;
  102. }
  103. return true;
  104. }
  105. public static bool TryParse(string text, out Quaternion result, Action<string> errorCallBack = null)
  106. {
  107. result = Quaternion.identity;
  108. if (string.IsNullOrEmpty(text))
  109. {
  110. return false;
  111. }
  112. text = text.Trim();
  113. string[] array = text.Substring(1, text.Length - 2).Split(new char[]
  114. {
  115. ','
  116. });
  117. bool flag = array.Length != 4;
  118. if (!flag)
  119. {
  120. float x;
  121. flag |= !Parse.TryParse(array[0], out x, null);
  122. float y;
  123. flag |= !Parse.TryParse(array[1], out y, null);
  124. float z;
  125. flag |= !Parse.TryParse(array[2], out z, null);
  126. float w;
  127. flag |= !Parse.TryParse(array[3], out w, null);
  128. result = new Quaternion(x, y, z, w);
  129. }
  130. if (flag)
  131. {
  132. if (errorCallBack != null)
  133. {
  134. errorCallBack("QuaternionのParseに失敗しました");
  135. }
  136. return false;
  137. }
  138. return true;
  139. }
  140. public static bool TryParse(string text, out Color result, Action<string> errorCallBack = null)
  141. {
  142. result = Color.black;
  143. if (string.IsNullOrEmpty(text))
  144. {
  145. return false;
  146. }
  147. text = text.Trim().Replace("RGBA", string.Empty);
  148. string[] array = text.Substring(1, text.Length - 2).Split(new char[]
  149. {
  150. ','
  151. });
  152. bool flag = array.Length != 4;
  153. if (!flag)
  154. {
  155. float r;
  156. flag |= !Parse.TryParse(array[0], out r, null);
  157. float g;
  158. flag |= !Parse.TryParse(array[1], out g, null);
  159. float b;
  160. flag |= !Parse.TryParse(array[2], out b, null);
  161. float a;
  162. flag |= !Parse.TryParse(array[3], out a, null);
  163. result = new Color(r, g, b, a);
  164. }
  165. if (flag)
  166. {
  167. if (errorCallBack != null)
  168. {
  169. errorCallBack("ColorのParseに失敗しました");
  170. }
  171. return false;
  172. }
  173. return true;
  174. }
  175. public static bool TryParse(string text, out byte result, Action<string> errorCallBack = null)
  176. {
  177. bool flag = byte.TryParse(text, out result);
  178. if (!flag && errorCallBack != null)
  179. {
  180. errorCallBack("byteのParseに失敗しました");
  181. }
  182. return flag;
  183. }
  184. public static bool TryParse(string text, out int result, Action<string> errorCallBack = null)
  185. {
  186. bool flag = int.TryParse(text, out result);
  187. if (!flag && errorCallBack != null)
  188. {
  189. errorCallBack("intのParseに失敗しました");
  190. }
  191. return flag;
  192. }
  193. public static bool TryParse(string text, out uint result, Action<string> errorCallBack = null)
  194. {
  195. bool flag = uint.TryParse(text, out result);
  196. if (!flag && errorCallBack != null)
  197. {
  198. errorCallBack("uintのParseに失敗しました");
  199. }
  200. return flag;
  201. }
  202. public static bool TryParse(string text, out float result, Action<string> errorCallBack = null)
  203. {
  204. bool flag = float.TryParse(text, out result);
  205. if (!flag && errorCallBack != null)
  206. {
  207. errorCallBack("floatのParseに失敗しました");
  208. }
  209. return flag;
  210. }
  211. public static bool TryParse(string text, out double result, Action<string> errorCallBack = null)
  212. {
  213. bool flag = double.TryParse(text, out result);
  214. if (!flag && errorCallBack != null)
  215. {
  216. errorCallBack("doubleのParseに失敗しました");
  217. }
  218. return flag;
  219. }
  220. public static bool TryParse(string text, out long result, Action<string> errorCallBack = null)
  221. {
  222. bool flag = long.TryParse(text, out result);
  223. if (!flag && errorCallBack != null)
  224. {
  225. errorCallBack("longのParseに失敗しました");
  226. }
  227. return flag;
  228. }
  229. public static bool TryParse<T>(string text, out T result, Action<string> errorCallBack = null) where T : struct
  230. {
  231. try
  232. {
  233. result = (T)((object)Enum.Parse(typeof(T), text));
  234. return true;
  235. }
  236. catch (Exception ex)
  237. {
  238. if (!string.IsNullOrEmpty(text))
  239. {
  240. text = text.ToLower();
  241. foreach (string text2 in Enum.GetNames(typeof(T)))
  242. {
  243. if (text == text2.ToLower())
  244. {
  245. try
  246. {
  247. result = (T)((object)Enum.Parse(typeof(T), text2));
  248. return true;
  249. }
  250. catch (Exception ex2)
  251. {
  252. }
  253. }
  254. }
  255. }
  256. }
  257. result = default(T);
  258. if (errorCallBack != null)
  259. {
  260. errorCallBack(typeof(T).ToString() + "のParseに失敗しました");
  261. }
  262. return false;
  263. }
  264. }
  265. }