123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using UnityEngine;
- namespace I2.Loc
- {
- public class LocalizationReader
- {
- public static Dictionary<string, string> ReadTextAsset(TextAsset asset)
- {
- string text = Encoding.UTF8.GetString(asset.bytes, 0, asset.bytes.Length);
- text = text.Replace("\r\n", "\n");
- text = text.Replace("\r", "\n");
- StringReader stringReader = new StringReader(text);
- Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.Ordinal);
- string line;
- while ((line = stringReader.ReadLine()) != null)
- {
- string text2;
- string value;
- string text3;
- string text4;
- string text5;
- if (LocalizationReader.TextAsset_ReadLine(line, out text2, out value, out text3, out text4, out text5))
- {
- if (!string.IsNullOrEmpty(text2) && !string.IsNullOrEmpty(value))
- {
- dictionary[text2] = value;
- }
- }
- }
- return dictionary;
- }
- public static bool TextAsset_ReadLine(string line, out string key, out string value, out string category, out string comment, out string termType)
- {
- key = string.Empty;
- category = string.Empty;
- comment = string.Empty;
- termType = string.Empty;
- value = string.Empty;
- int num = line.LastIndexOf("//");
- if (num >= 0)
- {
- comment = line.Substring(num + 2).Trim();
- comment = LocalizationReader.DecodeString(comment);
- line = line.Substring(0, num);
- }
- int num2 = line.IndexOf("=");
- if (num2 < 0)
- {
- return false;
- }
- key = line.Substring(0, num2).Trim();
- value = line.Substring(num2 + 1).Trim();
- value = value.Replace("\r\n", "\n").Replace("\n", "\\n");
- value = LocalizationReader.DecodeString(value);
- if (key.Length > 2 && key[0] == '[')
- {
- int num3 = key.IndexOf(']');
- if (num3 >= 0)
- {
- termType = key.Substring(1, num3 - 1);
- key = key.Substring(num3 + 1);
- }
- }
- LocalizationReader.ValidateFullTerm(ref key);
- return true;
- }
- public static string ReadCSVfile(string Path, Encoding encoding)
- {
- string text = string.Empty;
- using (StreamReader streamReader = new StreamReader(Path, encoding))
- {
- text = streamReader.ReadToEnd();
- }
- text = text.Replace("\r\n", "\n");
- text = text.Replace("\r", "\n");
- return text;
- }
- public static List<string[]> ReadCSV(string Text, char Separator = ',')
- {
- int i = 0;
- List<string[]> list = new List<string[]>();
- while (i < Text.Length)
- {
- string[] array = LocalizationReader.ParseCSVline(Text, ref i, Separator);
- if (array == null)
- {
- break;
- }
- list.Add(array);
- }
- return list;
- }
- private static string[] ParseCSVline(string Line, ref int iStart, char Separator)
- {
- List<string> list = new List<string>();
- int length = Line.Length;
- int num = iStart;
- bool flag = false;
- while (iStart < length)
- {
- char c = Line[iStart];
- if (flag)
- {
- if (c == '"')
- {
- if (iStart + 1 >= length || Line[iStart + 1] != '"')
- {
- flag = false;
- }
- else if (iStart + 2 < length && Line[iStart + 2] == '"')
- {
- flag = false;
- iStart += 2;
- }
- else
- {
- iStart++;
- }
- }
- }
- else if (c == '\n' || c == Separator)
- {
- LocalizationReader.AddCSVtoken(ref list, ref Line, iStart, ref num);
- if (c == '\n')
- {
- iStart++;
- break;
- }
- }
- else if (c == '"')
- {
- flag = true;
- }
- iStart++;
- }
- if (iStart > num)
- {
- LocalizationReader.AddCSVtoken(ref list, ref Line, iStart, ref num);
- }
- for (int i = 0; i < list.Count; i++)
- {
- if (0 <= list[i].IndexOf("\\n"))
- {
- list[i] = list[i].Replace("\\n", "\n");
- }
- }
- return list.ToArray();
- }
- private static void AddCSVtoken(ref List<string> list, ref string Line, int iEnd, ref int iWordStart)
- {
- string text = Line.Substring(iWordStart, iEnd - iWordStart);
- iWordStart = iEnd + 1;
- text = text.Replace("\"\"", "\"");
- if (text.Length > 1 && text[0] == '"' && text[text.Length - 1] == '"')
- {
- text = text.Substring(1, text.Length - 2);
- }
- list.Add(text);
- }
- public static List<string[]> ReadI2CSV(string Text)
- {
- string[] separator = new string[]
- {
- "[*]"
- };
- string[] separator2 = new string[]
- {
- "[ln]"
- };
- List<string[]> list = new List<string[]>();
- foreach (string text in Text.Split(separator2, StringSplitOptions.None))
- {
- list.Add(text.Split(separator, StringSplitOptions.None));
- }
- return list;
- }
- public static void ValidateFullTerm(ref string Term)
- {
- Term = Term.Replace('\\', '/');
- int num = Term.IndexOf('/');
- if (num < 0)
- {
- return;
- }
- int startIndex;
- while ((startIndex = Term.LastIndexOf('/')) != num)
- {
- Term = Term.Remove(startIndex, 1);
- }
- }
- public static string EncodeString(string str)
- {
- if (string.IsNullOrEmpty(str))
- {
- return string.Empty;
- }
- return str.Replace("\r\n", "<\\n>").Replace("\r", "<\\n>").Replace("\n", "<\\n>");
- }
- public static string DecodeString(string str)
- {
- if (string.IsNullOrEmpty(str))
- {
- return string.Empty;
- }
- return str.Replace("<\\n>", "\r\n");
- }
- }
- }
|