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 ERROR.lastImported = 0;
65 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(
file);
66 if (!sourceData.
ImportData(sheet, fileNameWithoutExtension, overwrite:
true))
68 throw new SourceParseException(
"#source failed to import data " + sourceData.GetType().Name +
":" + fileNameWithoutExtension +
"/" + sheetName);
74 return (sourceData,
item);
79 Debug.LogError(
$"#source failed to load sheet {sheetName}\n{arg}");
84 public IEnumerable<SourceData>
ImportFilesCached(IEnumerable<string> imports,
bool resetData =
true)
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)
96 string arg = sourceCache.SheetFile.ShortPath();
97 if (
fileProviders.TryGetValue(sourceCache.SheetFile.FullName, out var value))
99 sourceCache.SetMod(value);
101 if (sourceCache.IsDirtyOrEmpty)
103 if (!dictionary.TryGetValue(sourceCache, out var value2) || value2.Item3.Length == 0)
107 ISheet[]
item = value2.Item3;
108 foreach (ISheet sheet
in item)
110 ExcelParser.allowTrimming =
true;
114 if (!item2.IsEmpty())
116 sourceCache.EmplaceCache(sheet.SheetName, item2);
117 value?.sourceRows.UnionWith(item2);
118 Debug.Log(
$"#source workbook {arg}:{sheet.SheetName}:{item2.Length}");
123 ExcelParser.allowTrimming =
false;
128 string[] array3 = prefetchSheetNames;
129 foreach (
string text
in array3)
131 if (sourceCache.TryGetCache(text, out var rows))
133 int num = elements.ImportRows(rows);
134 value?.sourceRows.UnionWith(rows);
135 Debug.Log(
$"#source workbook-cache {arg}:{text}:{num}");
140 foreach (SourceCache sourceCache2
in array2)
142 string text2 = sourceCache2.SheetFile.ShortPath();
144 if (sourceCache2.IsDirtyOrEmpty)
146 if (!dictionary.TryGetValue(sourceCache2, out var value3) || value3.Item2.Length == 0)
150 Debug.Log(
"#source workbook " + text2);
151 ISheet[]
item = value3.Item2;
152 foreach (ISheet sheet2
in item)
154 if (prefetchSheetNames.Contains(sheet2.SheetName))
158 key = sheet2.SheetName;
163 allowTrimming = ((!(key ==
"Word")) ? 1 : 0);
172 ExcelParser.allowTrimming = (byte)allowTrimming != 0;
176 if ((
object)sourceData !=
null)
178 int? num2 = array4?.Length;
179 if (num2.HasValue && num2.GetValueOrDefault() > 0)
181 sourceCache2.EmplaceCache(sheet2.SheetName, array4);
182 sourceCache2.Mod?.sourceRows.UnionWith(array4);
183 hashSet.Add(sourceData);
189 ExcelParser.allowTrimming =
false;
194 foreach (KeyValuePair<
string,
SourceData.
BaseRow[]> item3 in sourceCache2.Source)
196 item3.Deconstruct(out key, out var value4);
202 if (sourceData2 is
SourceElement || (
object)sourceData2 ==
null)
213 Debug.Log(
"#source cached rows are empty " + text2 +
":" + text3);
217 sourceCache2.Mod?.sourceRows.UnionWith(array5);
218 Debug.Log(
$"#source workbook-cache {text2}:{text3}:{num3}");
219 hashSet.Add(sourceData2);
229 public static void HotInit(IEnumerable<SourceData> sourceData)
231 Debug.Log(
"#source resetting data...");
232 foreach (
SourceData sourceDatum
in sourceData)
239 catch (Exception arg)
241 Debug.LogError(
$"#source failed to reset dirty data {sourceDatum.GetType().Name}\n{arg}");
244 Debug.Log(
"#source initialized data");
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++)
255 ISheet sheetAt = xSSFWorkbook.GetSheetAt(i);
256 if (
FindSourceByName(sheetAt.SheetName) !=
null && prefetchNames.Contains(sheetAt.SheetName))
265 Debug.Log(
"#source workbook-prefetch " +
file.ShortPath());
266 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)