LocalizationReader.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using UnityEngine;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. namespace I2.Loc
  5. {
  6. public class LocalizationReader
  7. {
  8. #region Dictionary Assets
  9. public static Dictionary<string,string> ReadTextAsset( TextAsset asset )
  10. {
  11. string Text = Encoding.UTF8.GetString (asset.bytes, 0, asset.bytes.Length);
  12. Text = Text.Replace("\r\n", "\n");
  13. Text = Text.Replace("\r", "\n");
  14. System.IO.StringReader reader = new System.IO.StringReader(Text);
  15. string s;
  16. Dictionary<string, string> Dict = new Dictionary<string, string>(System.StringComparer.Ordinal);
  17. while ( (s=reader.ReadLine()) != null )
  18. {
  19. string Key, Value, Category, TermType, Comment;
  20. if (!TextAsset_ReadLine(s, out Key, out Value, out Category, out Comment, out TermType))
  21. continue;
  22. if (!string.IsNullOrEmpty(Key) && !string.IsNullOrEmpty(Value))
  23. Dict[Key]=Value;
  24. }
  25. return Dict;
  26. }
  27. public static bool TextAsset_ReadLine( string line, out string key, out string value, out string category, out string comment, out string termType )
  28. {
  29. key = string.Empty;
  30. category= string.Empty;
  31. comment = string.Empty;
  32. termType= string.Empty;
  33. value = string.Empty;
  34. //--[ Comment ]-----------------------
  35. int iComment = line.LastIndexOf("//");
  36. if (iComment>=0)
  37. {
  38. comment = line.Substring(iComment+2).Trim();
  39. comment = DecodeString(comment);
  40. line = line.Substring(0, iComment);
  41. }
  42. //--[ Key ]-----------------------------
  43. int iKeyEnd = line.IndexOf("=");
  44. if (iKeyEnd<0)
  45. {
  46. return false;
  47. }
  48. else
  49. {
  50. key = line.Substring(0, iKeyEnd).Trim();
  51. value = line.Substring(iKeyEnd+1).Trim();
  52. value = value.Replace ("\r\n", "\n").Replace ("\n", "\\n");
  53. value = DecodeString(value);
  54. }
  55. //--[ Type ]---------
  56. if (key.Length>2 && key[0]=='[')
  57. {
  58. int iTypeEnd = key.IndexOf(']');
  59. if (iTypeEnd>=0)
  60. {
  61. termType = key.Substring(1, iTypeEnd-1);
  62. key = key.Substring(iTypeEnd+1);
  63. }
  64. }
  65. ValidateFullTerm( ref key );
  66. return true;
  67. }
  68. #endregion
  69. #region CSV
  70. public static string ReadCSVfile( string Path, Encoding encoding )
  71. {
  72. string Text = string.Empty;
  73. #if (UNITY_WP8 || UNITY_METRO) && !UNITY_EDITOR
  74. byte[] buffer = UnityEngine.Windows.File.ReadAllBytes (Path);
  75. Text = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  76. #else
  77. /*using (System.IO.StreamReader reader = System.IO.File.OpenText(Path))
  78. {
  79. Text = reader.ReadToEnd();
  80. }*/
  81. using (var reader = new System.IO.StreamReader(Path, encoding ))
  82. Text = reader.ReadToEnd();
  83. #endif
  84. Text = Text.Replace("\r\n", "\n");
  85. Text = Text.Replace("\r", "\n");
  86. return Text;
  87. }
  88. public static List<string[]> ReadCSV( string Text, char Separator=',' )
  89. {
  90. int iStart = 0;
  91. List<string[]> CSV = new List<string[]>();
  92. while (iStart < Text.Length)
  93. {
  94. string[] list = ParseCSVline (Text, ref iStart, Separator);
  95. if (list==null) break;
  96. CSV.Add(list);
  97. }
  98. return CSV;
  99. }
  100. static string[] ParseCSVline( string Line, ref int iStart, char Separator )
  101. {
  102. List<string> list = new List<string>();
  103. //Line = "puig,\"placeres,\"\"cab\nr\nera\"\"algo\"\npuig";//\"Frank\npuig\nplaceres\",aaa,frank\nplaceres";
  104. int TextLength = Line.Length;
  105. int iWordStart = iStart;
  106. bool InsideQuote = false;
  107. while (iStart < TextLength)
  108. {
  109. char c = Line[iStart];
  110. if (InsideQuote)
  111. {
  112. if (c=='\"') //--[ Look for Quote End ]------------
  113. {
  114. if (iStart+1 >= TextLength || Line[iStart+1] != '\"') //-- Single Quote: Quotation Ends
  115. {
  116. InsideQuote = false;
  117. }
  118. else
  119. if (iStart+2 < TextLength && Line[iStart+2]=='\"') //-- Tripple Quotes: Quotation ends
  120. {
  121. InsideQuote = false;
  122. iStart+=2;
  123. }
  124. else
  125. iStart++; // Skip Double Quotes
  126. }
  127. }
  128. else //-----[ Separators ]----------------------
  129. if (c=='\n' || c==Separator)
  130. {
  131. AddCSVtoken(ref list, ref Line, iStart, ref iWordStart);
  132. if (c=='\n') // Stop the row on line breaks
  133. {
  134. iStart++;
  135. break;
  136. }
  137. }
  138. else //--------[ Start Quote ]--------------------
  139. if (c=='\"')
  140. InsideQuote = true;
  141. iStart++;
  142. }
  143. if (iStart>iWordStart)
  144. AddCSVtoken(ref list, ref Line, iStart, ref iWordStart);
  145. return list.ToArray();
  146. }
  147. static void AddCSVtoken( ref List<string> list, ref string Line, int iEnd, ref int iWordStart)
  148. {
  149. string Text = Line.Substring(iWordStart, iEnd-iWordStart);
  150. iWordStart = iEnd+1;
  151. Text = Text.Replace("\"\"", "\"" );
  152. if (Text.Length>1 && Text[0]=='\"' && Text[Text.Length-1]=='\"')
  153. Text = Text.Substring(1, Text.Length-2 );
  154. list.Add( Text);
  155. }
  156. #endregion
  157. #region I2CSV
  158. public static List<string[]> ReadI2CSV( string Text )
  159. {
  160. string[] ColumnSeparator = new string[]{"[*]"};
  161. string[] RowSeparator = new string[]{"[ln]"};
  162. List<string[]> CSV = new List<string[]>();
  163. foreach (var line in Text.Split (RowSeparator, System.StringSplitOptions.None))
  164. CSV.Add (line.Split (ColumnSeparator, System.StringSplitOptions.None));
  165. return CSV;
  166. }
  167. #endregion
  168. #region Misc
  169. public static void ValidateFullTerm( ref string Term )
  170. {
  171. Term = Term.Replace('\\', '/');
  172. int First = Term.IndexOf('/');
  173. if (First<0)
  174. return;
  175. int second;
  176. while ( (second=Term.LastIndexOf('/')) != First )
  177. Term = Term.Remove( second,1);
  178. }
  179. // this function encodes \r\n and \n into \\n
  180. public static string EncodeString( string str )
  181. {
  182. if (string.IsNullOrEmpty(str))
  183. return string.Empty;
  184. return str.Replace("\r\n", "<\\n>")
  185. .Replace("\r", "<\\n>")
  186. .Replace("\n", "<\\n>");
  187. }
  188. public static string DecodeString( string str )
  189. {
  190. if (string.IsNullOrEmpty(str))
  191. return string.Empty;
  192. return str.Replace("<\\n>", "\r\n");
  193. }
  194. #endregion
  195. }
  196. }