Elin Decompiled Documentation EA 23.344.1 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 source = list;
61 HashSet<SourceData.BaseRow> existing = new HashSet<SourceData.BaseRow>(source.OfType<SourceData.BaseRow>());
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 = (from r in source.OfType<SourceData.BaseRow>()
73 where !existing.Contains(r)
74 select r).Take(ERROR.lastImported).ToArray();
75 return (sourceData, item);
76 }
77 }
78 catch (Exception arg)
79 {
80 Debug.LogError($"#source failed to load sheet {sheetName}\n{arg}");
81 }
82 return (null, Array.Empty<SourceData.BaseRow>());
83 }
84
85 public IEnumerable<SourceData> ImportFilesCached(IEnumerable<string> imports, bool resetData = true)
86 {
87 string[] prefetchSheetNames = new string[2] { "Element", "Material" };
88 SourceCache[] array = imports.Select(SourceCache.GetOrCreate).Distinct().ToArray();
89 Dictionary<SourceCache, (string, ISheet[], ISheet[])> dictionary = (from c in array
90 where c.IsDirtyOrEmpty
91 select PrefetchWorkbook(c.SheetFile.FullName, prefetchSheetNames)).ToArray().ToDictionary(((string file, ISheet[] sheets, ISheet[] fetched) p) => SourceCache.GetOrCreate(p.file));
93 HashSet<SourceData> hashSet = new HashSet<SourceData> { elements };
94 SourceCache[] array2 = array;
95 foreach (SourceCache sourceCache in array2)
96 {
97 string arg = sourceCache.SheetFile.ShortPath();
98 if (fileProviders.TryGetValue(sourceCache.SheetFile.FullName, out var value))
99 {
100 sourceCache.SetMod(value);
101 }
102 if (sourceCache.IsDirtyOrEmpty)
103 {
104 if (!dictionary.TryGetValue(sourceCache, out var value2) || value2.Item3.Length == 0)
105 {
106 continue;
107 }
108 ISheet[] item = value2.Item3;
109 foreach (ISheet sheet in item)
110 {
111 ExcelParser.allowTrimming = true;
112 try
113 {
114 SourceData.BaseRow[] item2 = LoadBySheetName(sheet, value2.Item1).Item2;
115 if (!item2.IsEmpty())
116 {
117 sourceCache.EmplaceCache(sheet.SheetName, item2);
118 value?.sourceRows.UnionWith(item2);
119 Debug.Log($"#source workbook {arg}:{sheet.SheetName}:{item2.Length}");
120 }
121 }
122 finally
123 {
124 ExcelParser.allowTrimming = false;
125 }
126 }
127 continue;
128 }
129 string[] array3 = prefetchSheetNames;
130 foreach (string text in array3)
131 {
132 if (sourceCache.TryGetCache(text, out var rows))
133 {
134 int num3 = elements.ImportRows(rows);
135 value?.sourceRows.UnionWith(rows);
136 Debug.Log($"#source workbook-cache {arg}:{text}:{num3}");
137 }
138 }
139 }
140 array2 = array;
141 foreach (SourceCache sourceCache2 in array2)
142 {
143 string text2 = sourceCache2.SheetFile.ShortPath();
144 string key;
145 if (sourceCache2.IsDirtyOrEmpty)
146 {
147 if (!dictionary.TryGetValue(sourceCache2, out var value3) || value3.Item2.Length == 0)
148 {
149 continue;
150 }
151 Debug.Log("#source workbook " + text2);
152 ISheet[] item = value3.Item2;
153 foreach (ISheet sheet2 in item)
154 {
155 if (prefetchSheetNames.Contains(sheet2.SheetName))
156 {
157 continue;
158 }
159 key = sheet2.SheetName;
160 int allowTrimming;
161 switch (key)
162 {
163 default:
164 allowTrimming = ((!(key == "Word")) ? 1 : 0);
165 break;
166 case "General":
167 case "Game":
168 case "Note":
169 case "List":
170 allowTrimming = 0;
171 break;
172 }
173 ExcelParser.allowTrimming = (byte)allowTrimming != 0;
174 try
175 {
176 var (sourceData, array4) = LoadBySheetName(sheet2, value3.Item1);
177 if ((object)sourceData != null)
178 {
179 int? num4 = array4?.Length;
180 if (num4.HasValue && num4.GetValueOrDefault() > 0)
181 {
182 sourceCache2.EmplaceCache(sheet2.SheetName, array4);
183 sourceCache2.Mod?.sourceRows.UnionWith(array4);
184 hashSet.Add(sourceData);
185 }
186 }
187 }
188 finally
189 {
190 ExcelParser.allowTrimming = false;
191 }
192 }
193 continue;
194 }
195 foreach (KeyValuePair<string, SourceData.BaseRow[]> item3 in sourceCache2.Source)
196 {
197 item3.Deconstruct(out key, out var value4);
198 string text3 = key;
199 SourceData.BaseRow[] array5 = value4;
200 SourceData sourceData2 = FindSourceByName(text3);
201 if (!(sourceData2 is SourceThingV))
202 {
203 if (sourceData2 is SourceElement || (object)sourceData2 == null)
204 {
205 continue;
206 }
207 }
208 else
209 {
210 sourceData2 = EClass.sources.things;
211 }
212 if (array5 == null)
213 {
214 Debug.Log("#source cached rows are empty " + text2 + ":" + text3);
215 continue;
216 }
217 int num5 = sourceData2.ImportRows(array5);
218 sourceCache2.Mod?.sourceRows.UnionWith(array5);
219 Debug.Log($"#source workbook-cache {text2}:{text3}:{num5}");
220 hashSet.Add(sourceData2);
221 }
222 }
223 if (resetData)
224 {
225 HotInit(hashSet);
226 }
227 return hashSet;
228 }
229
230 public static void HotInit(IEnumerable<SourceData> sourceData)
231 {
232 Debug.Log("#source resetting data...");
233 SourceData[] order = new SourceData[7]
234 {
241 EClass.sources.objs
242 };
243 List<SourceData> list = sourceData.Distinct().OrderBy(delegate(SourceData s)
244 {
245 int num = Array.IndexOf(order, s);
246 return (num >= 0) ? num : order.Length;
247 }).ToList();
248 if (list.Any((SourceData s) => Array.IndexOf(order, s) > 0))
249 {
250 if (!list.Contains(EClass.sources.elements))
251 {
252 EClass.sources.elements.Init();
253 }
254 if (!list.Contains(EClass.sources.materials))
255 {
256 EClass.sources.materials.Init();
257 }
258 }
259 if (list.Contains(EClass.sources.blocks) && !list.Contains(EClass.sources.floors))
260 {
261 EClass.sources.floors.Init();
262 }
263 foreach (SourceData item in list)
264 {
265 try
266 {
267 item.Reset();
268 item.Init();
269 }
270 catch (Exception arg)
271 {
272 Debug.LogError($"#source failed to reset dirty data {item.GetType().Name}\n{arg}");
273 }
274 }
275 if (EClass.sources.blocks.initialized && (list.Contains(EClass.sources.floors) || list.Contains(EClass.sources.blocks)))
276 {
278 }
279 if (list.Contains(EClass.sources.things) || list.Contains(EClass.sources.charas))
280 {
282 }
283 if (list.Any((SourceData s) => s == EClass.sources.blocks || s == EClass.sources.floors || s == EClass.sources.objs || s == EClass.sources.decos || s == EClass.sources.cellEffects))
284 {
286 }
287 RecipeManager.rebuild = true;
288 Debug.Log("#source initialized data");
289 }
290
291 private (string file, ISheet[] sheets, ISheet[] fetched) PrefetchWorkbook(string file, string[] prefetchNames)
292 {
293 using FileStream @is = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
294 XSSFWorkbook xSSFWorkbook = new XSSFWorkbook((Stream)@is);
295 List<ISheet> list = new List<ISheet>();
296 List<ISheet> list2 = new List<ISheet>();
297 for (int i = 0; i < xSSFWorkbook.NumberOfSheets; i++)
298 {
299 ISheet sheetAt = xSSFWorkbook.GetSheetAt(i);
300 if (FindSourceByName(sheetAt.SheetName) != null && prefetchNames.Contains(sheetAt.SheetName))
301 {
302 list2.Add(sheetAt);
303 }
304 else
305 {
306 list.Add(sheetAt);
307 }
308 }
309 Debug.Log("#source workbook-prefetch " + file.ShortPath());
310 return (file: file, sheets: list.ToArray(), fetched: list2.ToArray());
311 }
312}
Definition: EClass.cs:6
static SourceManager sources
Definition: EClass.cs:43
Definition: ERROR.cs:2
static int lastImported
Definition: ERROR.cs:5
void Init()
Definition: SourceCard.cs:15
override bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
Definition: SourceData.cs:137
override int ImportRows(IEnumerable< BaseRow > sourceRows)
Definition: SourceData.cs:238
void OnAfterInit()
Definition: SourceFloor.cs:301
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)
SourceMaterial materials
SourceCard cards
SourceObj objs
SourceThing things
SourceDeco decos
SourceCellEffect cellEffects
SourceBlock blocks
SourceElement elements
SourceChara charas
SourceFloor floors
static void ReapplyRows()
Definition: TileManager.cs:156