Elin Decompiled Documentation EA 23.310 Nightly Patch 1
Loading...
Searching...
No Matches
ExcelParser.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using NPOI.SS.UserModel;
6using UnityEngine;
7
8public class ExcelParser
9{
10 public static string path;
11
12 public static IRow row;
13
14 public static IRow rowDefault;
15
16 public static IRow rowHeader;
17
18 public static bool IsNull(ICell cell)
19 {
20 if (cell != null && cell.CellType != CellType.Blank)
21 {
22 return cell.CellType == CellType.Unknown;
23 }
24 return true;
25 }
26
27 public static int GetInt(int id)
28 {
29 string str = GetStr(id);
30 if (str.IsEmpty())
31 {
32 return 0;
33 }
34 if (!int.TryParse(str, out var result))
35 {
36 if (float.TryParse(str, out var result2))
37 {
38 return (int)result2;
39 }
40 ReportIllFormat<int>(id);
41 }
42 return result;
43 }
44
45 public static int GetInt(int col, IRow _row)
46 {
47 row = _row;
48 return GetInt(col);
49 }
50
51 public static bool GetBool(int id)
52 {
53 string str = GetStr(id);
54 bool result;
55 return str switch
56 {
57 null => false,
58 "1" => true,
59 "0" => false,
60 _ => bool.TryParse(str, out result) && result,
61 };
62 }
63
64 public static bool GetBool(int col, IRow _row)
65 {
66 row = _row;
67 return GetBool(col);
68 }
69
70 public static double GetDouble(int id)
71 {
72 string str = GetStr(id);
73 if (str.IsEmpty())
74 {
75 return 0.0;
76 }
77 if (!double.TryParse(str, out var result))
78 {
79 ReportIllFormat<double>(id);
80 }
81 return result;
82 }
83
84 public static float GetFloat(int id)
85 {
86 string str = GetStr(id);
87 if (str.IsEmpty())
88 {
89 return 0f;
90 }
91 if (!float.TryParse(str, out var result))
92 {
93 ReportIllFormat<float>(id);
94 }
95 return result;
96 }
97
98 public static float[] GetFloatArray(int id)
99 {
100 string str = GetStr(id);
101 if (str.IsEmpty())
102 {
103 return Array.Empty<float>();
104 }
105 string[] array = str.Split(',');
106 float[] array2 = new float[array.Length];
107 for (int i = 0; i < array.Length; i++)
108 {
109 if (!float.TryParse(array[i], out array2[i]))
110 {
111 ReportIllFormat<float>(id);
112 array2[i] = 0f;
113 }
114 }
115 return array2;
116 }
117
118 public static int[] GetIntArray(int id)
119 {
120 string str = GetStr(id);
121 if (str.IsEmpty())
122 {
123 return Array.Empty<int>();
124 }
125 string[] array = str.Split(',');
126 int[] array2 = new int[array.Length];
127 for (int i = 0; i < array.Length; i++)
128 {
129 if (!int.TryParse(array[i], out array2[i]))
130 {
131 if (float.TryParse(array[i], out var result))
132 {
133 array2[i] = (int)result;
134 continue;
135 }
136 ReportIllFormat<int>(id);
137 array2[i] = 0;
138 }
139 }
140 return array2;
141 }
142
143 public static string[] GetStringArray(int id)
144 {
145 string str = GetStr(id);
146 if (str != null)
147 {
148 return str.Split(',');
149 }
150 return Array.Empty<string>();
151 }
152
153 public static string GetString(int id)
154 {
155 return GetStr(id) ?? "";
156 }
157
158 public static string GetString(int col, IRow _row)
159 {
160 row = _row;
161 return GetStr(col);
162 }
163
164 public static string GetStr(int id, bool useDefault = false)
165 {
166 IRow row = (useDefault ? rowDefault : ExcelParser.row);
167 if (row == null)
168 {
169 if (!useDefault)
170 {
171 return GetStr(id, useDefault: true);
172 }
173 return null;
174 }
175 ICell cell = row.GetCell(id);
176 if (IsNull(cell))
177 {
178 if (!useDefault)
179 {
180 return GetStr(id, useDefault: true);
181 }
182 return null;
183 }
184 cell.SetCellType(CellType.String);
185 if (cell.StringCellValue == "")
186 {
187 if (!useDefault)
188 {
189 return GetStr(id, useDefault: true);
190 }
191 return null;
192 }
193 return cell.StringCellValue;
194 }
195
196 public static string ToLetterId(int id)
197 {
198 string text = "";
199 while (id >= 0)
200 {
201 text = (char)(id % 26 + 65) + text;
202 id /= 26;
203 id--;
204 }
205 return text;
206 }
207
208 public static void ReportIllFormat<T>(int id)
209 {
210 StringBuilder stringBuilder = new StringBuilder();
211 string name = typeof(T).Name;
212 ICell cell = row?.Cells.TryGet(id, returnNull: true);
213 IRow obj = row;
214 string value = ((obj != null && obj.RowNum >= 3) ? $", default:'{rowDefault?.Cells.TryGet(id, returnNull: true)}'" : ", SourceData begins at the 4th row. 3rd row is the default value row.");
215 stringBuilder.AppendLine("#source ill-format file: " + path);
216 object[] array = new object[5];
217 IRow obj2 = row;
218 array[0] = ((obj2 != null) ? new int?(obj2.RowNum + 1) : null);
219 array[1] = id + 1;
220 array[2] = ToLetterId(id);
221 array[3] = name;
222 array[4] = cell;
223 stringBuilder.Append(string.Format("row#{0}, cell#{1}/#{2}, expected:'{3}', read:'{4}'", array));
224 stringBuilder.AppendLine(value);
225 Debug.LogWarning(stringBuilder);
226 }
227
228 public static string GetRowHeaderDiff(IReadOnlyDictionary<string, int> mapping, IReadOnlyDictionary<string, int> header)
229 {
230 StringBuilder stringBuilder = new StringBuilder();
231 List<KeyValuePair<string, int>> list = mapping.OrderBy((KeyValuePair<string, int> c) => c.Value).ToList();
232 int num = list.Max((KeyValuePair<string, int> c) => c.Key.Length);
233 foreach (KeyValuePair<string, int> item in list)
234 {
235 item.Deconstruct(out var key, out var value);
236 string text = key;
237 int index = value;
238 int value2;
239 bool num2 = header.TryGetValue(text, out value2);
240 string text2 = ((num2 && value2 != index) ? $"maybe {value2 + 1,2}/{ToLetterId(value2)} {text}" : "");
241 if (!num2)
242 {
243 text2 = "<missing>";
244 }
245 string text3 = header.FirstOrDefault((KeyValuePair<string, int> c) => c.Value == index).Key ?? "<missing>";
246 text3 = text3.PadRight(num + 3);
247 string text4 = text.PadRight(num);
248 stringBuilder.AppendLine($"{index + 1,2}/{ToLetterId(index),2}: {text4} -> {text3} {text2}");
249 }
250 return stringBuilder.ToString();
251 }
252
253 public static void ClearStaticRows()
254 {
255 row = null;
256 rowDefault = null;
257 rowHeader = null;
258 }
259}
$
Definition: ExcelParser.cs:228
static void ClearStaticRows()
Definition: ExcelParser.cs:253
static bool GetBool(int id)
Definition: ExcelParser.cs:51
static void ReportIllFormat< T >(int id)
Definition: ExcelParser.cs:208
static string GetRowHeaderDiff(IReadOnlyDictionary< string, int > mapping, IReadOnlyDictionary< string, int > header)
Definition: ExcelParser.cs:228
static string ToLetterId(int id)
Definition: ExcelParser.cs:196
static IRow rowDefault
Definition: ExcelParser.cs:14
static bool GetBool(int col, IRow _row)
Definition: ExcelParser.cs:64
static int GetInt(int id)
Definition: ExcelParser.cs:27
static IRow row
Definition: ExcelParser.cs:12
static bool IsNull(ICell cell)
Definition: ExcelParser.cs:18
static string GetString(int id)
Definition: ExcelParser.cs:153
static int[] GetIntArray(int id)
Definition: ExcelParser.cs:118
static int GetInt(int col, IRow _row)
Definition: ExcelParser.cs:45
static float GetFloat(int id)
Definition: ExcelParser.cs:84
static string path
Definition: ExcelParser.cs:10
static double GetDouble(int id)
Definition: ExcelParser.cs:70
static string GetStr(int id, bool useDefault=false)
Definition: ExcelParser.cs:164
static float[] GetFloatArray(int id)
Definition: ExcelParser.cs:98
static IRow rowHeader
Definition: ExcelParser.cs:16
static string GetString(int col, IRow _row)
Definition: ExcelParser.cs:158
static string[] GetStringArray(int id)
Definition: ExcelParser.cs:143