Elin Decompiled Documentation EA 23.331 Nightly
Loading...
Searching...
No Matches
SourceImporter.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using NPOI.SS.UserModel;
7using NPOI.XSSF.UserModel;
8using UnityEngine;
9
10public class SourceImporter : EClass
11{
12 private readonly IReadOnlyDictionary<string, SourceData> _sourceMapping;
13
14 public Dictionary<string, EMod> fileProviders = new Dictionary<string, EMod>(PathComparer.Default);
15
16 public SourceImporter(IReadOnlyDictionary<string, SourceData> sourceMapping)
17 {
18 _sourceMapping = sourceMapping;
19 }
20
21 public SourceData FindSourceByName(string name)
22 {
23 string[] array = new string[3]
24 {
25 "Source" + name,
26 "Lang" + name,
27 name
28 };
29 foreach (string key in array)
30 {
31 if (_sourceMapping.TryGetValue(key, out var value))
32 {
33 return value;
34 }
35 }
36 return null;
37 }
38
39 private (SourceData, SourceData.BaseRow[]) LoadBySheetName(ISheet sheet, string file)
40 {
41 string sheetName = sheet.SheetName;
42 try
43 {
44 SourceData sourceData = FindSourceByName(sheetName);
45 if ((object)sourceData == null)
46 {
47 Debug.Log("#source skipping sheet " + sheetName);
48 return (null, null);
49 }
50 IList list;
51 if (!(sourceData is SourceThingV))
52 {
53 list = sourceData.GetField<IList>("rows");
54 }
55 else
56 {
57 IList rows = EClass.sources.things.rows;
58 list = rows;
59 }
60 IList list2 = list;
61 int count = list2.Count;
62 Debug.Log("#source loading sheet " + sheetName);
63 ExcelParser.path = file;
64 ERROR.lastImported = 0;
65 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
66 if (!sourceData.ImportData(sheet, fileNameWithoutExtension, overwrite: true))
67 {
68 throw new SourceParseException("#source failed to import data " + sourceData.GetType().Name + ":" + fileNameWithoutExtension + "/" + sheetName);
69 }
70 if (ERROR.lastImported > 0)
71 {
72 SourceData.BaseRow[] item = list2.OfType<SourceData.BaseRow>().Skip(count).Take(ERROR.lastImported)
73 .ToArray();
74 return (sourceData, item);
75 }
76 }
77 catch (Exception arg)
78 {
79 Debug.LogError($"#source failed to load sheet {sheetName}\n{arg}");
80 }
81 return (null, Array.Empty<SourceData.BaseRow>());
82 }
83
84 public IEnumerable<SourceData> ImportFilesCached(IEnumerable<string> imports, bool resetData = true)
85 {
86 string[] prefetchSheetNames = new string[2] { "Element", "Material" };
87 SourceCache[] array = imports.Select(SourceCache.GetOrCreate).Distinct().ToArray();
88 Dictionary<SourceCache, (string, ISheet[], ISheet[])> dictionary = (from c in array
89 where c.IsDirtyOrEmpty
90 select PrefetchWorkbook(c.SheetFile.FullName, prefetchSheetNames)).ToArray().ToDictionary(((string file, ISheet[] sheets, ISheet[] fetched) p) => SourceCache.GetOrCreate(p.file));
92 HashSet<SourceData> hashSet = new HashSet<SourceData> { elements };
93 SourceCache[] array2 = array;
94 foreach (SourceCache sourceCache in array2)
95 {
96 string arg = sourceCache.SheetFile.ShortPath();
97 if (fileProviders.TryGetValue(sourceCache.SheetFile.FullName, out var value))
98 {
99 sourceCache.SetMod(value);
100 }
101 if (sourceCache.IsDirtyOrEmpty)
102 {
103 if (!dictionary.TryGetValue(sourceCache, out var value2) || value2.Item3.Length == 0)
104 {
105 continue;
106 }
107 ISheet[] item = value2.Item3;
108 foreach (ISheet sheet in item)
109 {
110 ExcelParser.allowTrimming = true;
111 try
112 {
113 SourceData.BaseRow[] item2 = LoadBySheetName(sheet, value2.Item1).Item2;
114 if (!item2.IsEmpty())
115 {
116 sourceCache.EmplaceCache(sheet.SheetName, item2);
117 value?.sourceRows.UnionWith(item2);
118 Debug.Log($"#source workbook {arg}:{sheet.SheetName}:{item2.Length}");
119 }
120 }
121 finally
122 {
123 ExcelParser.allowTrimming = false;
124 }
125 }
126 continue;
127 }
128 string[] array3 = prefetchSheetNames;
129 foreach (string text in array3)
130 {
131 if (sourceCache.TryGetCache(text, out var rows))
132 {
133 int num = elements.ImportRows(rows);
134 value?.sourceRows.UnionWith(rows);
135 Debug.Log($"#source workbook-cache {arg}:{text}:{num}");
136 }
137 }
138 }
139 array2 = array;
140 foreach (SourceCache sourceCache2 in array2)
141 {
142 string text2 = sourceCache2.SheetFile.ShortPath();
143 string key;
144 if (sourceCache2.IsDirtyOrEmpty)
145 {
146 if (!dictionary.TryGetValue(sourceCache2, out var value3) || value3.Item2.Length == 0)
147 {
148 continue;
149 }
150 Debug.Log("#source workbook " + text2);
151 ISheet[] item = value3.Item2;
152 foreach (ISheet sheet2 in item)
153 {
154 if (prefetchSheetNames.Contains(sheet2.SheetName))
155 {
156 continue;
157 }
158 key = sheet2.SheetName;
159 int allowTrimming;
160 switch (key)
161 {
162 default:
163 allowTrimming = ((!(key == "Word")) ? 1 : 0);
164 break;
165 case "General":
166 case "Game":
167 case "Note":
168 case "List":
169 allowTrimming = 0;
170 break;
171 }
172 ExcelParser.allowTrimming = (byte)allowTrimming != 0;
173 try
174 {
175 var (sourceData, array4) = LoadBySheetName(sheet2, value3.Item1);
176 if ((object)sourceData != null)
177 {
178 int? num2 = array4?.Length;
179 if (num2.HasValue && num2.GetValueOrDefault() > 0)
180 {
181 sourceCache2.EmplaceCache(sheet2.SheetName, array4);
182 sourceCache2.Mod?.sourceRows.UnionWith(array4);
183 hashSet.Add(sourceData);
184 }
185 }
186 }
187 finally
188 {
189 ExcelParser.allowTrimming = false;
190 }
191 }
192 continue;
193 }
194 foreach (KeyValuePair<string, SourceData.BaseRow[]> item3 in sourceCache2.Source)
195 {
196 item3.Deconstruct(out key, out var value4);
197 string text3 = key;
198 SourceData.BaseRow[] array5 = value4;
199 SourceData sourceData2 = FindSourceByName(text3);
200 if (!(sourceData2 is SourceThingV))
201 {
202 if (sourceData2 is SourceElement || (object)sourceData2 == null)
203 {
204 continue;
205 }
206 }
207 else
208 {
209 sourceData2 = EClass.sources.things;
210 }
211 if (array5 == null)
212 {
213 Debug.Log("#source cached rows are empty " + text2 + ":" + text3);
214 continue;
215 }
216 int num3 = sourceData2.ImportRows(array5);
217 sourceCache2.Mod?.sourceRows.UnionWith(array5);
218 Debug.Log($"#source workbook-cache {text2}:{text3}:{num3}");
219 hashSet.Add(sourceData2);
220 }
221 }
222 if (resetData)
223 {
224 HotInit(hashSet);
225 }
226 return hashSet;
227 }
228
229 public static void HotInit(IEnumerable<SourceData> sourceData)
230 {
231 Debug.Log("#source resetting data...");
232 foreach (SourceData sourceDatum in sourceData)
233 {
234 try
235 {
236 sourceDatum.Reset();
237 sourceDatum.Init();
238 }
239 catch (Exception arg)
240 {
241 Debug.LogError($"#source failed to reset dirty data {sourceDatum.GetType().Name}\n{arg}");
242 }
243 }
244 Debug.Log("#source initialized data");
245 }
246
247 private (string file, ISheet[] sheets, ISheet[] fetched) PrefetchWorkbook(string file, string[] prefetchNames)
248 {
249 using FileStream @is = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
250 XSSFWorkbook xSSFWorkbook = new XSSFWorkbook((Stream)@is);
251 List<ISheet> list = new List<ISheet>();
252 List<ISheet> list2 = new List<ISheet>();
253 for (int i = 0; i < xSSFWorkbook.NumberOfSheets; i++)
254 {
255 ISheet sheetAt = xSSFWorkbook.GetSheetAt(i);
256 if (FindSourceByName(sheetAt.SheetName) != null && prefetchNames.Contains(sheetAt.SheetName))
257 {
258 list2.Add(sheetAt);
259 }
260 else
261 {
262 list.Add(sheetAt);
263 }
264 }
265 Debug.Log("#source workbook-prefetch " + file.ShortPath());
266 return (file: file, sheets: list.ToArray(), fetched: list2.ToArray());
267 }
268}
Definition: EClass.cs:6
static SourceManager sources
Definition: EClass.cs:43
Definition: ERROR.cs:2
static int lastImported
Definition: ERROR.cs:5
override bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
Definition: SourceData.cs:137
override int ImportRows(IEnumerable< BaseRow > sourceRows)
Definition: SourceData.cs:238
override void Reset()
Definition: SourceData.cs:116
override void Init()
Definition: SourceData.cs:83
string ISheet[] sheets
IEnumerable< SourceData > ImportFilesCached(IEnumerable< string > imports, bool resetData=true)
static void HotInit(IEnumerable< SourceData > sourceData)
Dictionary< string, EMod > fileProviders
SourceImporter(IReadOnlyDictionary< string, SourceData > sourceMapping)
SourceData FindSourceByName(string name)
readonly IReadOnlyDictionary< string, SourceData > _sourceMapping
string ISheet[] ISheet[] fetched PrefetchWorkbook(string file, string[] prefetchNames)
SourceData.BaseRow[] LoadBySheetName(ISheet sheet, string file)
SourceThing things
SourceElement elements