Elin Decompiled Documentation EA 23.291 Nightly Patch 1
Loading...
Searching...
No Matches
ExcelParser.cs
Go to the documentation of this file.
1using System;
2using System.Text;
3using NPOI.SS.UserModel;
4using UnityEngine;
5
6public class ExcelParser
7{
8 public static string path;
9
10 public static IRow row;
11
12 public static IRow rowDefault;
13
14 public static bool IsNull(ICell cell)
15 {
16 if (cell != null && cell.CellType != CellType.Blank)
17 {
18 return cell.CellType == CellType.Unknown;
19 }
20 return true;
21 }
22
23 public static int GetInt(int id)
24 {
25 string str = GetStr(id);
26 if (str.IsEmpty())
27 {
28 return 0;
29 }
30 if (!int.TryParse(str, out var result))
31 {
32 ReportIllFormat<int>(id);
33 }
34 return result;
35 }
36
37 public static int GetInt(int col, IRow _row)
38 {
39 row = _row;
40 return GetInt(col);
41 }
42
43 public static bool GetBool(int id)
44 {
45 string str = GetStr(id);
46 bool result;
47 return str switch
48 {
49 null => false,
50 "1" => true,
51 "0" => false,
52 _ => bool.TryParse(str, out result) && result,
53 };
54 }
55
56 public static bool GetBool(int col, IRow _row)
57 {
58 row = _row;
59 return GetBool(col);
60 }
61
62 public static double GetDouble(int id)
63 {
64 string str = GetStr(id);
65 if (str.IsEmpty())
66 {
67 return 0.0;
68 }
69 if (!double.TryParse(str, out var result))
70 {
71 ReportIllFormat<double>(id);
72 }
73 return result;
74 }
75
76 public static float GetFloat(int id)
77 {
78 string str = GetStr(id);
79 if (str.IsEmpty())
80 {
81 return 0f;
82 }
83 if (!float.TryParse(str, out var result))
84 {
85 ReportIllFormat<float>(id);
86 }
87 return result;
88 }
89
90 public static float[] GetFloatArray(int id)
91 {
92 string str = GetStr(id);
93 if (str.IsEmpty())
94 {
95 return Array.Empty<float>();
96 }
97 string[] array = str.Split(',');
98 float[] array2 = new float[array.Length];
99 for (int i = 0; i < array.Length; i++)
100 {
101 if (!float.TryParse(array[i], out array2[i]))
102 {
103 ReportIllFormat<float>(id);
104 array2[i] = 0f;
105 }
106 }
107 return array2;
108 }
109
110 public static int[] GetIntArray(int id)
111 {
112 string str = GetStr(id);
113 if (str.IsEmpty())
114 {
115 return Array.Empty<int>();
116 }
117 string[] array = str.Split(',');
118 int[] array2 = new int[array.Length];
119 for (int i = 0; i < array.Length; i++)
120 {
121 if (!int.TryParse(array[i], out array2[i]))
122 {
123 ReportIllFormat<int>(id);
124 array2[i] = 0;
125 }
126 }
127 return array2;
128 }
129
130 public static string[] GetStringArray(int id)
131 {
132 string str = GetStr(id);
133 if (str != null)
134 {
135 return str.Split(',');
136 }
137 return Array.Empty<string>();
138 }
139
140 public static string GetString(int id)
141 {
142 return GetStr(id) ?? "";
143 }
144
145 public static string GetString(int col, IRow _row)
146 {
147 row = _row;
148 return GetStr(col);
149 }
150
151 public static string GetStr(int id, bool useDefault = false)
152 {
153 IRow row = (useDefault ? rowDefault : ExcelParser.row);
154 if (row == null)
155 {
156 if (!useDefault)
157 {
158 return GetStr(id, useDefault: true);
159 }
160 return null;
161 }
162 ICell cell = row.GetCell(id);
163 if (IsNull(cell))
164 {
165 if (!useDefault)
166 {
167 return GetStr(id, useDefault: true);
168 }
169 return null;
170 }
171 cell.SetCellType(CellType.String);
172 if (cell.StringCellValue == "")
173 {
174 if (!useDefault)
175 {
176 return GetStr(id, useDefault: true);
177 }
178 return null;
179 }
180 return cell.StringCellValue;
181 }
182
183 public static string ToLetterId(int id)
184 {
185 string text = "";
186 while (id >= 0)
187 {
188 text = (char)(id % 26 + 65) + text;
189 id /= 26;
190 id--;
191 }
192 return text;
193 }
194
195 public static void ReportIllFormat<T>(int id)
196 {
197 StringBuilder stringBuilder = new StringBuilder();
198 string name = typeof(T).Name;
199 ICell cell = row?.Cells.TryGet(id, returnNull: true);
200 IRow obj = row;
201 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.");
202 stringBuilder.AppendLine("$source ill-format file: " + path);
203 object[] array = new object[5];
204 IRow obj2 = row;
205 array[0] = ((obj2 != null) ? new int?(obj2.RowNum + 1) : null);
206 array[1] = id + 1;
207 array[2] = ToLetterId(id);
208 array[3] = name;
209 array[4] = cell;
210 stringBuilder.Append(string.Format("row#{0}, cell'{1}'/'{2}', expected:'{3}', read:'{4}'", array));
211 stringBuilder.AppendLine(value);
212 Debug.LogError(stringBuilder);
213 }
214}
$
Definition: ModManager.cs:86
static bool GetBool(int id)
Definition: ExcelParser.cs:43
static void ReportIllFormat< T >(int id)
Definition: ExcelParser.cs:195
static string ToLetterId(int id)
Definition: ExcelParser.cs:183
static IRow rowDefault
Definition: ExcelParser.cs:12
static bool GetBool(int col, IRow _row)
Definition: ExcelParser.cs:56
static int GetInt(int id)
Definition: ExcelParser.cs:23
static IRow row
Definition: ExcelParser.cs:10
static bool IsNull(ICell cell)
Definition: ExcelParser.cs:14
static string GetString(int id)
Definition: ExcelParser.cs:140
static int[] GetIntArray(int id)
Definition: ExcelParser.cs:110
static int GetInt(int col, IRow _row)
Definition: ExcelParser.cs:37
static float GetFloat(int id)
Definition: ExcelParser.cs:76
static string path
Definition: ExcelParser.cs:8
static double GetDouble(int id)
Definition: ExcelParser.cs:62
static string GetStr(int id, bool useDefault=false)
Definition: ExcelParser.cs:151
static float[] GetFloatArray(int id)
Definition: ExcelParser.cs:90
static string GetString(int col, IRow _row)
Definition: ExcelParser.cs:145
static string[] GetStringArray(int id)
Definition: ExcelParser.cs:130