LocalizationReader.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace I2.Loc
  7. {
  8. public class LocalizationReader
  9. {
  10. public static Dictionary<string, string> ReadTextAsset(TextAsset asset)
  11. {
  12. string text = Encoding.UTF8.GetString(asset.bytes, 0, asset.bytes.Length);
  13. text = text.Replace("\r\n", "\n");
  14. text = text.Replace("\r", "\n");
  15. StringReader stringReader = new StringReader(text);
  16. Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.Ordinal);
  17. string line;
  18. while ((line = stringReader.ReadLine()) != null)
  19. {
  20. string text2;
  21. string value;
  22. string text3;
  23. string text4;
  24. string text5;
  25. if (LocalizationReader.TextAsset_ReadLine(line, out text2, out value, out text3, out text4, out text5))
  26. {
  27. if (!string.IsNullOrEmpty(text2) && !string.IsNullOrEmpty(value))
  28. {
  29. dictionary[text2] = value;
  30. }
  31. }
  32. }
  33. return dictionary;
  34. }
  35. public static bool TextAsset_ReadLine(string line, out string key, out string value, out string category, out string comment, out string termType)
  36. {
  37. key = string.Empty;
  38. category = string.Empty;
  39. comment = string.Empty;
  40. termType = string.Empty;
  41. value = string.Empty;
  42. int num = line.LastIndexOf("//");
  43. if (num >= 0)
  44. {
  45. comment = line.Substring(num + 2).Trim();
  46. comment = LocalizationReader.DecodeString(comment);
  47. line = line.Substring(0, num);
  48. }
  49. int num2 = line.IndexOf("=");
  50. if (num2 < 0)
  51. {
  52. return false;
  53. }
  54. key = line.Substring(0, num2).Trim();
  55. value = line.Substring(num2 + 1).Trim();
  56. value = value.Replace("\r\n", "\n").Replace("\n", "\\n");
  57. value = LocalizationReader.DecodeString(value);
  58. if (key.Length > 2 && key[0] == '[')
  59. {
  60. int num3 = key.IndexOf(']');
  61. if (num3 >= 0)
  62. {
  63. termType = key.Substring(1, num3 - 1);
  64. key = key.Substring(num3 + 1);
  65. }
  66. }
  67. LocalizationReader.ValidateFullTerm(ref key);
  68. return true;
  69. }
  70. public static string ReadCSVfile(string Path, Encoding encoding)
  71. {
  72. string text = string.Empty;
  73. using (StreamReader streamReader = new StreamReader(Path, encoding))
  74. {
  75. text = streamReader.ReadToEnd();
  76. }
  77. text = text.Replace("\r\n", "\n");
  78. text = text.Replace("\r", "\n");
  79. return text;
  80. }
  81. public static List<string[]> ReadCSV(string Text, char Separator = ',')
  82. {
  83. int i = 0;
  84. List<string[]> list = new List<string[]>();
  85. while (i < Text.Length)
  86. {
  87. string[] array = LocalizationReader.ParseCSVline(Text, ref i, Separator);
  88. if (array == null)
  89. {
  90. break;
  91. }
  92. list.Add(array);
  93. }
  94. return list;
  95. }
  96. private static string[] ParseCSVline(string Line, ref int iStart, char Separator)
  97. {
  98. List<string> list = new List<string>();
  99. int length = Line.Length;
  100. int num = iStart;
  101. bool flag = false;
  102. while (iStart < length)
  103. {
  104. char c = Line[iStart];
  105. if (flag)
  106. {
  107. if (c == '"')
  108. {
  109. if (iStart + 1 >= length || Line[iStart + 1] != '"')
  110. {
  111. flag = false;
  112. }
  113. else if (iStart + 2 < length && Line[iStart + 2] == '"')
  114. {
  115. flag = false;
  116. iStart += 2;
  117. }
  118. else
  119. {
  120. iStart++;
  121. }
  122. }
  123. }
  124. else if (c == '\n' || c == Separator)
  125. {
  126. LocalizationReader.AddCSVtoken(ref list, ref Line, iStart, ref num);
  127. if (c == '\n')
  128. {
  129. iStart++;
  130. break;
  131. }
  132. }
  133. else if (c == '"')
  134. {
  135. flag = true;
  136. }
  137. iStart++;
  138. }
  139. if (iStart > num)
  140. {
  141. LocalizationReader.AddCSVtoken(ref list, ref Line, iStart, ref num);
  142. }
  143. return list.ToArray();
  144. }
  145. private static void AddCSVtoken(ref List<string> list, ref string Line, int iEnd, ref int iWordStart)
  146. {
  147. string text = Line.Substring(iWordStart, iEnd - iWordStart);
  148. iWordStart = iEnd + 1;
  149. text = text.Replace("\"\"", "\"");
  150. if (text.Length > 1 && text[0] == '"' && text[text.Length - 1] == '"')
  151. {
  152. text = text.Substring(1, text.Length - 2);
  153. }
  154. list.Add(text);
  155. }
  156. public static List<string[]> ReadI2CSV(string Text)
  157. {
  158. string[] separator = new string[]
  159. {
  160. "[*]"
  161. };
  162. string[] separator2 = new string[]
  163. {
  164. "[ln]"
  165. };
  166. List<string[]> list = new List<string[]>();
  167. foreach (string text in Text.Split(separator2, StringSplitOptions.None))
  168. {
  169. list.Add(text.Split(separator, StringSplitOptions.None));
  170. }
  171. return list;
  172. }
  173. public static void ValidateFullTerm(ref string Term)
  174. {
  175. Term = Term.Replace('\\', '/');
  176. int num = Term.IndexOf('/');
  177. if (num < 0)
  178. {
  179. return;
  180. }
  181. int startIndex;
  182. while ((startIndex = Term.LastIndexOf('/')) != num)
  183. {
  184. Term = Term.Remove(startIndex, 1);
  185. }
  186. }
  187. public static string EncodeString(string str)
  188. {
  189. if (string.IsNullOrEmpty(str))
  190. {
  191. return string.Empty;
  192. }
  193. return str.Replace("\r\n", "<\\n>").Replace("\r", "<\\n>").Replace("\n", "<\\n>");
  194. }
  195. public static string DecodeString(string str)
  196. {
  197. if (string.IsNullOrEmpty(str))
  198. {
  199. return string.Empty;
  200. }
  201. return str.Replace("<\\n>", "\r\n");
  202. }
  203. }
  204. }