I2Utils.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine.Networking;
  7. namespace I2.Loc
  8. {
  9. public static class I2Utils
  10. {
  11. public const string ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
  12. public const string NumberChars = "0123456789";
  13. public const string ValidNameSymbols = ".-_$#@*()[]{}+:?!&',^=<>~`";
  14. public static string ReverseText(string source)
  15. {
  16. var len = source.Length;
  17. var output = new char[len];
  18. for (var i = 0; i < len; i++)
  19. {
  20. output[(len - 1) - i] = source[i];
  21. }
  22. return new string(output);
  23. }
  24. public static string RemoveNonASCII(string text, bool allowCategory = false)
  25. {
  26. if (string.IsNullOrEmpty(text))
  27. return text;
  28. //return new string(text.ToCharArray().Where(c => (ValidChars.IndexOf(c)>=0 || c==' ' || (c == '\\' && allowCategory) || (c == '/' && allowCategory))).ToArray());
  29. //return new string(text.ToCharArray().Select(c => (char.IsControl(c) || (c == '\\' && !allowCategory) || (c == '\"') || (c == '/')) ? ' ' : c).ToArray());
  30. //return new string(text.ToCharArray().Select(c => ((allowCategory && (c == '\\' || c == '\"' || (c == '/'))) || char.IsLetterOrDigit(c))?c:' ').ToArray());
  31. // Remove Non-Letter/Digits and collapse all extra espaces into a single space
  32. int current = 0;
  33. char[] output = new char[text.Length];
  34. bool skipped = false;
  35. foreach (char cc in text.Trim().ToCharArray())
  36. {
  37. char c = ' ';
  38. if ((allowCategory && (cc == '\\' || cc == '\"' || (cc == '/'))) ||
  39. char.IsLetterOrDigit(cc) ||
  40. ValidNameSymbols.IndexOf(cc) >= 0)
  41. {
  42. c = cc;
  43. }
  44. if (char.IsWhiteSpace(c))
  45. {
  46. if (!skipped)
  47. {
  48. if (current > 0)
  49. output[current++] = ' ';
  50. skipped = true;
  51. }
  52. }
  53. else
  54. {
  55. skipped = false;
  56. output[current++] = c;
  57. }
  58. }
  59. return new string(output, 0, current);
  60. }
  61. public static string GetValidTermName( string text, bool allowCategory = false)
  62. {
  63. if (text == null)
  64. return null;
  65. text = RemoveTags(text);
  66. return RemoveNonASCII(text, allowCategory);
  67. }
  68. public static string SplitLine(string line, int maxCharacters)
  69. {
  70. if (maxCharacters <= 0 || line.Length < maxCharacters)
  71. return line;
  72. var chars = line.ToCharArray();
  73. bool insideOfLine = true;
  74. bool allowNewLine = false;
  75. for (int i = 0, nCharsInLine = 0; i < chars.Length; ++i)
  76. {
  77. if (insideOfLine)
  78. {
  79. nCharsInLine++;
  80. if (chars[i] == '\n')
  81. {
  82. nCharsInLine = 0;
  83. }
  84. if (nCharsInLine >= maxCharacters && char.IsWhiteSpace(chars[i]))
  85. {
  86. chars[i] = '\n';
  87. insideOfLine = false;
  88. allowNewLine = false;
  89. }
  90. }
  91. else
  92. {
  93. if (!char.IsWhiteSpace(chars[i]))
  94. {
  95. insideOfLine = true;
  96. nCharsInLine = 0;
  97. }
  98. else
  99. {
  100. if (chars[i] != '\n')
  101. {
  102. chars[i] = (char)0;
  103. }
  104. else
  105. {
  106. if (!allowNewLine)
  107. chars[i] = (char)0;
  108. allowNewLine = true;
  109. }
  110. }
  111. }
  112. }
  113. return new string(chars.Where(c => c != (char)0).ToArray());
  114. }
  115. public static bool FindNextTag(string line, int iStart, out int tagStart, out int tagEnd)
  116. {
  117. tagStart = -1;
  118. tagEnd = -1;
  119. int len = line.Length;
  120. // Find where the tag starts
  121. for (tagStart = iStart; tagStart < len; ++tagStart)
  122. if (line[tagStart] == '[' || line[tagStart] == '(' || line[tagStart] == '{' || line[tagStart] == '<')
  123. break;
  124. if (tagStart == len)
  125. return false;
  126. bool isArabic = false;
  127. for (tagEnd = tagStart + 1; tagEnd < len; ++tagEnd)
  128. {
  129. char c = line[tagEnd];
  130. if (c == ']' || c == ')' || c == '}' || c=='>')
  131. {
  132. if (isArabic) return FindNextTag(line, tagEnd + 1, out tagStart, out tagEnd);
  133. else return true;
  134. }
  135. if (c > 255) isArabic = true;
  136. }
  137. // there is an open, but not close character
  138. return false;
  139. }
  140. public static string RemoveTags(string text)
  141. {
  142. return Regex.Replace(text, @"\{\[(.*?)]}|\[(.*?)]|\<(.*?)>", "");
  143. }
  144. public static bool RemoveResourcesPath(ref string sPath)
  145. {
  146. int Ind1 = sPath.IndexOf("\\Resources\\");
  147. int Ind2 = sPath.IndexOf("\\Resources/");
  148. int Ind3 = sPath.IndexOf("/Resources\\");
  149. int Ind4 = sPath.IndexOf("/Resources/");
  150. int Index = Mathf.Max(Ind1, Ind2, Ind3, Ind4);
  151. bool IsResource = false;
  152. if (Index >= 0)
  153. {
  154. sPath = sPath.Substring(Index + 11);
  155. IsResource = true;
  156. }
  157. else
  158. {
  159. // If its not in the Resources, then it has to be in the References
  160. // Therefore, the path has to be stripped and let only the name
  161. Index = sPath.LastIndexOfAny(LanguageSourceData.CategorySeparators);
  162. if (Index > 0)
  163. sPath = sPath.Substring(Index + 1);
  164. }
  165. string Extension = System.IO.Path.GetExtension(sPath);
  166. if (!string.IsNullOrEmpty(Extension))
  167. sPath = sPath.Substring(0, sPath.Length - Extension.Length);
  168. return IsResource;
  169. }
  170. public static bool IsPlaying()
  171. {
  172. if (Application.isPlaying)
  173. return true;
  174. #if UNITY_EDITOR
  175. return UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode;
  176. #else
  177. return false;
  178. #endif
  179. }
  180. public static string GetPath(this Transform tr)
  181. {
  182. var parent = tr.parent;
  183. if (tr == null)
  184. return tr.name;
  185. return parent.GetPath() + "/" + tr.name;
  186. }
  187. #if UNITY_5_3_OR_NEWER
  188. public static Transform FindObject(string objectPath)
  189. {
  190. return FindObject(UnityEngine.SceneManagement.SceneManager.GetActiveScene(), objectPath);
  191. }
  192. public static Transform FindObject(UnityEngine.SceneManagement.Scene scene, string objectPath)
  193. {
  194. //var roots = SceneManager.GetActiveScene().GetRootGameObjects();
  195. var roots = scene.GetRootGameObjects();
  196. for (int i=0; i<roots.Length; ++i)
  197. {
  198. var root = roots[i].transform;
  199. if (root.name == objectPath)
  200. return root;
  201. if (!objectPath.StartsWith(root.name + "/"))
  202. continue;
  203. return FindObject(root, objectPath.Substring(root.name.Length + 1));
  204. }
  205. return null;
  206. }
  207. public static Transform FindObject(Transform root, string objectPath)
  208. {
  209. for (int i=0; i<root.childCount; ++i)
  210. {
  211. var child = root.GetChild(i);
  212. if (child.name == objectPath)
  213. return child;
  214. if (!objectPath.StartsWith(child.name + "/"))
  215. continue;
  216. return FindObject(child, objectPath.Substring(child.name.Length + 1));
  217. }
  218. return null;
  219. }
  220. #endif
  221. public static H FindInParents<H>(Transform tr) where H : Component
  222. {
  223. if (!tr)
  224. return null;
  225. H comp = tr.GetComponent<H>();
  226. while (!comp && tr)
  227. {
  228. comp = tr.GetComponent<H>();
  229. tr = tr.parent;
  230. }
  231. return comp;
  232. }
  233. public static string GetCaptureMatch(Match match)
  234. {
  235. for (int i = match.Groups.Count - 1; i >= 0; --i)
  236. if (match.Groups[i].Success)
  237. {
  238. return match.Groups[i].ToString();
  239. }
  240. return match.ToString();
  241. }
  242. public static void SendWebRequest(UnityWebRequest www )
  243. {
  244. #if UNITY_2017_2_OR_NEWER
  245. www.SendWebRequest();
  246. #else
  247. www.Send();
  248. #endif
  249. }
  250. }
  251. }