I2Utils.cs 10 KB

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