2using System.Collections;
3using System.Collections.Generic;
6using NPOI.SS.UserModel;
7using NPOI.XSSF.UserModel;
14 public Dictionary<string, EMod>
fileProviders =
new Dictionary<string, EMod>(PathComparer.Default);
23 string[] array =
new string[3]
29 foreach (
string key
in array)
41 string sheetName = sheet.SheetName;
45 if ((
object)sourceData ==
null)
47 Debug.Log(
"#source skipping sheet " + sheetName);
53 list = sourceData.GetField<IList>(
"rows");
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))
67 throw new SourceParseException(
"#source failed to import data " + sourceData.GetType().Name +
":" + fileNameWithoutExtension +
"/" + sheetName);
70 int num =
ERROR.
msg.Split(
'/')[^1].ToInt();
76 return (sourceData,
item);
80 Debug.LogError(
$"#source failed to load sheet {sheetName}\n{arg}");
85 public IEnumerable<SourceData>
ImportFilesCached(IEnumerable<string> imports,
bool resetData =
true)
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)
97 string arg = sourceCache.SheetFile.ShortPath();
98 if (
fileProviders.TryGetValue(sourceCache.SheetFile.FullName, out var value))
100 sourceCache.SetMod(value);
102 if (sourceCache.IsDirtyOrEmpty)
104 if (dictionary.TryGetValue(sourceCache, out var value2) && value2.Item3.Length != 0)
106 ISheet[]
item = value2.Item3;
107 foreach (ISheet sheet
in item)
110 sourceCache.EmplaceCache(sheet.SheetName, item2);
111 value?.sourceRows.UnionWith(item2);
112 Debug.Log(
$"#source workbook {arg}:{sheet.SheetName}:{item2.Length}");
117 string[] array3 = prefetchSheetNames;
118 foreach (
string text
in array3)
120 if (sourceCache.TryGetCache(text, out var rows))
122 int num = elements.ImportRows(rows);
123 value?.sourceRows.UnionWith(rows);
124 Debug.Log(
$"#source workbook-cache {arg}:{text}:{num}");
129 foreach (SourceCache sourceCache2
in array2)
131 string text2 = sourceCache2.SheetFile.ShortPath();
132 if (sourceCache2.IsDirtyOrEmpty)
134 if (!dictionary.TryGetValue(sourceCache2, out var value3) || value3.Item2.Length == 0)
138 Debug.Log(
"#source workbook " + text2);
139 ISheet[]
item = value3.Item2;
140 foreach (ISheet sheet2
in item)
142 if (prefetchSheetNames.Contains(sheet2.SheetName))
147 if ((
object)sourceData !=
null)
149 int? num2 = array4?.Length;
150 if (num2.HasValue && num2.GetValueOrDefault() > 0)
152 sourceCache2.EmplaceCache(sheet2.SheetName, array4);
153 sourceCache2.Mod?.sourceRows.UnionWith(array4);
154 hashSet.Add(sourceData);
160 foreach (KeyValuePair<
string,
SourceData.
BaseRow[]> item3 in sourceCache2.Source)
162 item3.Deconstruct(out var key, out var value4);
168 if (sourceData2 is
SourceElement || (
object)sourceData2 ==
null)
179 Debug.Log(
"#source cached rows are empty " + text2 +
":" + text3);
183 sourceCache2.Mod?.sourceRows.UnionWith(array5);
184 Debug.Log(
$"#source workbook-cache {text2}:{text3}:{num3}");
185 hashSet.Add(sourceData2);
195 public static void HotInit(IEnumerable<SourceData> sourceData)
197 Debug.Log(
"#source resetting data...");
198 foreach (
SourceData sourceDatum
in sourceData)
205 catch (Exception arg)
207 Debug.LogError(
$"#source failed to reset dirty data {sourceDatum.GetType().Name}\n{arg}");
210 Debug.Log(
"#source initialized data");
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++)
221 ISheet sheetAt = xSSFWorkbook.GetSheetAt(i);
222 if (
FindSourceByName(sheetAt.SheetName) !=
null && prefetchNames.Contains(sheetAt.SheetName))
231 Debug.Log(
"#source workbook-prefetch " +
file.ShortPath());
232 return (
file:
file,
sheets: list.ToArray(), fetched: list2.ToArray());
static SourceManager sources
override bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
override int ImportRows(IEnumerable< BaseRow > sourceRows)
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)