Elin Decompiled Documentation EA 23.299 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 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
65 if (!sourceData.ImportData(sheet, fileNameWithoutExtension, overwrite: true))
66 {
67 throw new SourceParseException("#source failed to import data " + sourceData.GetType().Name + ":" + fileNameWithoutExtension + "/" + sheetName);
68 }
69 SourceData.BaseRow[] item = Array.Empty<SourceData.BaseRow>();
70 int num = ERROR.msg.Split('/')[^1].ToInt();
71 if (num > 0)
72 {
73 item = list2.OfType<SourceData.BaseRow>().Skip(count).Take(num)
74 .ToArray();
75 }
76 return (sourceData, item);
77 }
78 catch (Exception arg)
79 {
80 Debug.LogError($"#source failed to load sheet {sheetName}\n{arg}");
81 }
82 return (null, null);
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 ISheet[] item = value2.Item3;
107 foreach (ISheet sheet in item)
108 {
109 SourceData.BaseRow[] item2 = LoadBySheetName(sheet, value2.Item1).Item2;
110 sourceCache.EmplaceCache(sheet.SheetName, item2);
111 value?.sourceRows.UnionWith(item2);
112 Debug.Log($"#source workbook {arg}:{sheet.SheetName}:{item2.Length}");
113 }
114 }
115 continue;
116 }
117 string[] array3 = prefetchSheetNames;
118 foreach (string text in array3)
119 {
120 if (sourceCache.TryGetCache(text, out var rows))
121 {
122 int num = elements.ImportRows(rows);
123 value?.sourceRows.UnionWith(rows);
124 Debug.Log($"#source workbook-cache {arg}:{text}:{num}");
125 }
126 }
127 }
128 array2 = array;
129 foreach (SourceCache sourceCache2 in array2)
130 {
131 string text2 = sourceCache2.SheetFile.ShortPath();
132 if (sourceCache2.IsDirtyOrEmpty)
133 {
134 if (!dictionary.TryGetValue(sourceCache2, out var value3) || value3.Item2.Length == 0)
135 {
136 continue;
137 }
138 Debug.Log("#source workbook " + text2);
139 ISheet[] item = value3.Item2;
140 foreach (ISheet sheet2 in item)
141 {
142 if (prefetchSheetNames.Contains(sheet2.SheetName))
143 {
144 continue;
145 }
146 var (sourceData, array4) = LoadBySheetName(sheet2, value3.Item1);
147 if ((object)sourceData != null)
148 {
149 int? num2 = array4?.Length;
150 if (num2.HasValue && num2.GetValueOrDefault() > 0)
151 {
152 sourceCache2.EmplaceCache(sheet2.SheetName, array4);
153 sourceCache2.Mod?.sourceRows.UnionWith(array4);
154 hashSet.Add(sourceData);
155 }
156 }
157 }
158 continue;
159 }
160 foreach (KeyValuePair<string, SourceData.BaseRow[]> item3 in sourceCache2.Source)
161 {
162 item3.Deconstruct(out var key, out var value4);
163 string text3 = key;
164 SourceData.BaseRow[] array5 = value4;
165 SourceData sourceData2 = FindSourceByName(text3);
166 if (!(sourceData2 is SourceThingV))
167 {
168 if (sourceData2 is SourceElement || (object)sourceData2 == null)
169 {
170 continue;
171 }
172 }
173 else
174 {
175 sourceData2 = EClass.sources.things;
176 }
177 if (array5 == null)
178 {
179 Debug.Log("#source cached rows are empty " + text2 + ":" + text3);
180 continue;
181 }
182 int num3 = sourceData2.ImportRows(array5);
183 sourceCache2.Mod?.sourceRows.UnionWith(array5);
184 Debug.Log($"#source workbook-cache {text2}:{text3}:{num3}");
185 hashSet.Add(sourceData2);
186 }
187 }
188 if (resetData)
189 {
190 HotInit(hashSet);
191 }
192 return hashSet;
193 }
194
195 public static void HotInit(IEnumerable<SourceData> sourceData)
196 {
197 Debug.Log("#source resetting data...");
198 foreach (SourceData sourceDatum in sourceData)
199 {
200 try
201 {
202 sourceDatum.Reset();
203 sourceDatum.Init();
204 }
205 catch (Exception arg)
206 {
207 Debug.LogError($"#source failed to reset dirty data {sourceDatum.GetType().Name}\n{arg}");
208 }
209 }
210 Debug.Log("#source initialized data");
211 }
212
213 private (string file, ISheet[] sheets, ISheet[] fetched) PrefetchWorkbook(string file, string[] prefetchNames)
214 {
215 using FileStream @is = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
216 XSSFWorkbook xSSFWorkbook = new XSSFWorkbook((Stream)@is);
217 List<ISheet> list = new List<ISheet>();
218 List<ISheet> list2 = new List<ISheet>();
219 for (int i = 0; i < xSSFWorkbook.NumberOfSheets; i++)
220 {
221 ISheet sheetAt = xSSFWorkbook.GetSheetAt(i);
222 if (FindSourceByName(sheetAt.SheetName) != null && prefetchNames.Contains(sheetAt.SheetName))
223 {
224 list2.Add(sheetAt);
225 }
226 else
227 {
228 list.Add(sheetAt);
229 }
230 }
231 Debug.Log("#source workbook-prefetch " + file.ShortPath());
232 return (file: file, sheets: list.ToArray(), fetched: list2.ToArray());
233 }
234}
$
Definition: ModManager.cs:86
Definition: EClass.cs:6
static SourceManager sources
Definition: EClass.cs:43
Definition: ERROR.cs:2
static string msg
Definition: ERROR.cs:3
override bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
Definition: SourceData.cs:136
override int ImportRows(IEnumerable< BaseRow > sourceRows)
Definition: SourceData.cs:176
override void Reset()
Definition: SourceData.cs:115
override void Init()
Definition: SourceData.cs:82
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