summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/I2.Loc/LocalizationReader.cs
blob: a940e76c7710f8969a82f38f0ffdb132d565c08d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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)
	{
		StringReader stringReader = new StringReader(Encoding.UTF8.GetString(asset.bytes, 0, asset.bytes.Length).Replace("\r\n", "\n").Replace("\r", "\n"));
		Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.Ordinal);
		string line;
		while ((line = stringReader.ReadLine()) != null)
		{
			if (TextAsset_ReadLine(line, out var key, out var value, out var _, out var _, out var _) && !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
			{
				dictionary[key] = 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("//", StringComparison.Ordinal);
		if (num >= 0)
		{
			comment = line.Substring(num + 2).Trim();
			comment = DecodeString(comment);
			line = line.Substring(0, num);
		}
		int num2 = line.IndexOf("=", StringComparison.Ordinal);
		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 = 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);
			}
		}
		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");
		return text.Replace("\r", "\n");
	}

	public static List<string[]> ReadCSV(string Text, char Separator = ',')
	{
		int iStart = 0;
		List<string[]> list = new List<string[]>();
		while (iStart < Text.Length)
		{
			string[] array = ParseCSVline(Text, ref iStart, 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 iWordStart = 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)
			{
				AddCSVtoken(ref list, ref Line, iStart, ref iWordStart);
				if (c == '\n')
				{
					iStart++;
					break;
				}
			}
			else if (c == '"')
			{
				flag = true;
			}
			iStart++;
		}
		if (iStart > iWordStart)
		{
			AddCSVtoken(ref list, ref Line, iStart, ref iWordStart);
		}
		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[1] { "[*]" };
		string[] separator2 = new string[1] { "[ln]" };
		List<string[]> list = new List<string[]>();
		string[] array = Text.Split(separator2, StringSplitOptions.None);
		foreach (string text in array)
		{
			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)
		{
			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");
	}
}