LocalizationReader.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. for (int i = 0; i < list.Count; i++)
  144. {
  145. if (0 <= list[i].IndexOf("\\n"))
  146. {
  147. list[i] = list[i].Replace("\\n", "\n");
  148. }
  149. }
  150. return list.ToArray();
  151. }
  152. private static void AddCSVtoken(ref List<string> list, ref string Line, int iEnd, ref int iWordStart)
  153. {
  154. string text = Line.Substring(iWordStart, iEnd - iWordStart);
  155. iWordStart = iEnd + 1;
  156. text = text.Replace("\"\"", "\"");
  157. if (text.Length > 1 && text[0] == '"' && text[text.Length - 1] == '"')
  158. {
  159. text = text.Substring(1, text.Length - 2);
  160. }
  161. list.Add(text);
  162. }
  163. public static List<string[]> ReadI2CSV(string Text)
  164. {
  165. string[] separator = new string[]
  166. {
  167. "[*]"
  168. };
  169. string[] separator2 = new string[]
  170. {
  171. "[ln]"
  172. };
  173. List<string[]> list = new List<string[]>();
  174. foreach (string text in Text.Split(separator2, StringSplitOptions.None))
  175. {
  176. list.Add(text.Split(separator, StringSplitOptions.None));
  177. }
  178. return list;
  179. }
  180. public static void ValidateFullTerm(ref string Term)
  181. {
  182. Term = Term.Replace('\\', '/');
  183. int num = Term.IndexOf('/');
  184. if (num < 0)
  185. {
  186. return;
  187. }
  188. int startIndex;
  189. while ((startIndex = Term.LastIndexOf('/')) != num)
  190. {
  191. Term = Term.Remove(startIndex, 1);
  192. }
  193. }
  194. public static string EncodeString(string str)
  195. {
  196. if (string.IsNullOrEmpty(str))
  197. {
  198. return string.Empty;
  199. }
  200. return str.Replace("\r\n", "<\\n>").Replace("\r", "<\\n>").Replace("\n", "<\\n>");
  201. }
  202. public static string DecodeString(string str)
  203. {
  204. if (string.IsNullOrEmpty(str))
  205. {
  206. return string.Empty;
  207. }
  208. return str.Replace("<\\n>", "\r\n");
  209. }
  210. }
  211. }