LocalizationReader.cs 5.8 KB

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