Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
SourceData.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Reflection;
5using NPOI.SS.UserModel;
6using NPOI.SS.Util;
7using NPOI.XSSF.UserModel;
8using UnityEngine;
9
10public class SourceData<T, T2> : SourceData where T : SourceData.BaseRow
11{
12 public class FieldInfo
13 {
14 public string id;
15
16 public string name;
17
18 public bool isStatic;
19
20 public int width;
21
22 public int column;
23
24 public bool IsIndex => id == "id";
25
26 public string GetValue(T r)
27 {
28 if (r.HasField<string>(id))
29 {
30 return r.GetField<string>(id);
31 }
32 if (r.HasField<int>(id))
33 {
34 return r.GetField<int>(id).ToString();
35 }
36 if (r.HasField<string[]>(id))
37 {
38 string text = "";
39 string[] field = r.GetField<string[]>(id);
40 if (field != null)
41 {
42 string[] array = field;
43 foreach (string text2 in array)
44 {
45 text = text + text2 + ",";
46 }
47 }
48 return text.TrimEnd(',');
49 }
50 return "";
51 }
52 }
53
54 public class FieldMap
55 {
57
58 public int column;
59 }
60
61 public List<T> rows = new List<T>();
62
63 public Dictionary<T2, T> map = new Dictionary<T2, T>();
64
65 public Dictionary<string, T> alias = new Dictionary<string, T>();
66
67 [NonSerialized]
68 public bool initialized;
69
70 [NonSerialized]
71 private List<T> _backupRows = new List<T>();
72
73 [NonSerialized]
74 public List<string> editorListString = new List<string>();
75
76 public static ISheet currentSheet;
77
78 public virtual bool AllowHotInitialization => false;
79
80 public virtual string[] ImportFields => new string[1] { "" };
81
82 public override void Init()
83 {
84 if (initialized)
85 {
86 Debug.Log("#init Skipping sourceData.Init:" + base.name);
87 return;
88 }
89 initialized = true;
90 editorListString.Clear();
91 int num = 0;
92 foreach (T row in rows)
93 {
94 SetRow(row);
95 if (row.UseAlias)
96 {
97 alias[row.GetAlias] = row;
98 }
99 row._index = num;
100 num++;
101 }
102 OnInit();
103 }
104
105 public virtual void OnInit()
106 {
107 }
108
109 public virtual void SetRow(T row)
110 {
111 }
112
113 public override void Reset()
114 {
115 initialized = false;
116 if (!Application.isPlaying)
117 {
118 BaseCore.resetRuntime = true;
119 }
120 if (map != null)
121 {
122 map.Clear();
123 }
124 if (alias != null)
125 {
126 alias.Clear();
127 }
128 if (Application.isPlaying && AllowHotInitialization)
129 {
130 Init();
131 }
132 }
133
134 public override bool ImportData(ISheet sheet, string bookname, bool overwrite = false)
135 {
136 if (!overwrite)
137 {
138 rows = new List<T>();
139 }
140 isNew = true;
141 nameSheet = sheet.SheetName;
142 nameBook = bookname;
143 SourceData.rowDefault = sheet.GetRow(2);
144 int num = 0;
145 for (int i = 3; i <= sheet.LastRowNum; i++)
146 {
147 SourceData.row = sheet.GetRow(i);
148 if (SourceData.row == null || SourceData.row.GetCell(0) == null || SourceData.row.GetCell(0).ToString().IsEmpty())
149 {
150 break;
151 }
152 T val = CreateRow();
153 val.OnImportData(this);
154 rows.Add(val);
155 num++;
156 }
157 string text = sheet.SheetName + "/" + sheet.LastRowNum + "/" + num;
158 Debug.Log(text);
159 ERROR.msg = text;
161 initialized = false;
162 return true;
163 }
164
165 public virtual void OnAfterImportData()
166 {
167 }
168
169 public virtual T CreateRow()
170 {
171 return null;
172 }
173
174 public override void BackupSource()
175 {
177 }
178
179 public override void RollbackSource()
180 {
182 }
183
184 public List<string> GetListString()
185 {
186 return BuildEditorList().editorListString;
187 }
188
190 {
191 if (editorListString.Count == 0)
192 {
193 foreach (T row in rows)
194 {
195 editorListString.Add(row.GetEditorListName());
196 }
197 }
198 return this;
199 }
200
201 public List<FieldInfo> GetFields()
202 {
203 List<FieldInfo> list = new List<FieldInfo>();
204 int x = 0;
205 AddField("id", 4096);
206 AddField("version", 4096);
207 AddField("filter", 4096);
208 AddField("name", 4096);
209 AddField("text", 4096);
210 AddField("detail", 4096);
211 AddField("description", 4096);
212 string[] importFields = ImportFields;
213 foreach (string text in importFields)
214 {
215 if (!text.IsEmpty())
216 {
217 AddField(text, 4096);
218 }
219 }
220 return list;
221 void AddField(string id, int width)
222 {
223 bool flag = id == "id" || id == "filter";
224 bool flag2 = id == "version";
225 if (!(typeof(T).GetField(id) == null) || flag2)
226 {
227 list.Add(new FieldInfo
228 {
229 id = id,
230 name = id,
231 isStatic = flag,
232 width = width,
233 column = x
234 });
235 x++;
236 if (!(flag || flag2))
237 {
238 list.Add(new FieldInfo
239 {
240 id = id,
241 name = id + "_EN",
242 isStatic = true,
243 width = width,
244 column = x
245 });
246 x++;
247 list.Add(new FieldInfo
248 {
249 id = id + "_JP",
250 name = id + "_JP",
251 isStatic = true,
252 width = width,
253 column = x
254 });
255 x++;
256 }
257 }
258 }
259 }
260
261 public virtual T GetRow(string id)
262 {
263 return null;
264 }
265
266 public override void ExportTexts(string path, bool update = false)
267 {
268 Debug.Log("Exporting:" + nameSheet + " path:" + path);
269 string text = nameSheet + ".xlsx";
270 XSSFWorkbook xSSFWorkbook = new XSSFWorkbook();
271 List<FieldInfo> fields = GetFields();
272 currentSheet = xSSFWorkbook.CreateSheet(nameSheet);
273 int num = 0;
274 int y = 0;
275 foreach (FieldInfo item in fields)
276 {
277 GetCell(num, y).SetCellValue(item.name);
278 currentSheet.SetColumnWidth(num, item.width);
279 ICellStyle cellStyle = xSSFWorkbook.CreateCellStyle();
280 cellStyle.FillForegroundColor = 44;
281 cellStyle.FillPattern = FillPattern.SolidForeground;
282 if (!item.isStatic && item.id != "version")
283 {
284 GetCell(num, y).CellStyle = cellStyle;
285 }
286 num++;
287 }
288 string cellValue = BaseCore.Instance.version.GetText() ?? "";
289 y = 2;
290 foreach (T row4 in rows)
291 {
292 foreach (FieldInfo item2 in fields)
293 {
294 if (item2.isStatic)
295 {
296 GetCell(item2.column, y).SetCellValue(item2.GetValue(row4));
297 }
298 else if (item2.id == "version")
299 {
300 GetCell(item2.column, y).SetCellValue(cellValue);
301 }
302 }
303 y++;
304 }
305 currentSheet.CreateFreezePane(0, 2);
306 currentSheet.SetAutoFilter(new CellRangeAddress(1, 1, 0, fields.Count - 1));
307 if (update)
308 {
309 Dictionary<string, int> dictionary = new Dictionary<string, int>();
310 if (!File.Exists(path + "_temp/" + text))
311 {
312 return;
313 }
314 XSSFWorkbook xSSFWorkbook2;
315 using (FileStream @is = File.Open(path + "_temp/" + text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
316 {
317 xSSFWorkbook2 = new XSSFWorkbook((Stream)@is);
318 }
319 ISheet sheetAt = xSSFWorkbook2.GetSheetAt(0);
320 IRow row = sheetAt.GetRow(0);
321 int num2 = 0;
322 foreach (FieldInfo item3 in fields)
323 {
324 if (item3.isStatic)
325 {
326 continue;
327 }
328 int num3 = -1;
329 for (int i = 0; i < row.LastCellNum; i++)
330 {
331 string stringCellValue = row.GetCell(i).StringCellValue;
332 if (stringCellValue.IsEmpty())
333 {
334 break;
335 }
336 if (stringCellValue == item3.id)
337 {
338 num3 = i;
339 break;
340 }
341 }
342 if (num3 == -1)
343 {
344 continue;
345 }
346 dictionary.Add(item3.id, 0);
347 for (y = 2; y <= sheetAt.LastRowNum; y++)
348 {
349 IRow row2 = sheetAt.GetRow(y);
350 if (row2 == null)
351 {
352 if (y > 5)
353 {
354 break;
355 }
356 continue;
357 }
358 ICell cell = row2.GetCell(0);
359 if (cell == null)
360 {
361 continue;
362 }
363 string text2 = ((cell.CellType == CellType.Numeric) ? cell.NumericCellValue.ToString() : cell.StringCellValue);
364 if (text2.IsEmpty())
365 {
366 continue;
367 }
368 for (int j = 0; j <= currentSheet.LastRowNum; j++)
369 {
370 IRow row3 = currentSheet.GetRow(j);
371 if (row3 == null)
372 {
373 if (j > 5)
374 {
375 break;
376 }
377 continue;
378 }
379 ICell cell2 = row3.GetCell(0);
380 if (cell2 == null || cell2.StringCellValue != text2)
381 {
382 continue;
383 }
384 string text3 = row2.GetCell(num3)?.StringCellValue ?? "";
385 if (text3.IsEmpty())
386 {
387 continue;
388 }
389 (row3.GetCell(item3.column) ?? row3.CreateCell(item3.column)).SetCellValue(text3);
390 if (item3.id != "version")
391 {
392 ICell cell3 = row3.GetCell(item3.column + 2);
393 ICell cell4 = row2.GetCell(num3 + 2);
394 if (cell3 == null)
395 {
396 continue;
397 }
398 if (cell4 == null || cell3.StringCellValue != cell4.StringCellValue)
399 {
400 row3.GetCell(1).SetCellValue(cellValue);
401 }
402 num2++;
403 }
404 dictionary[item3.id]++;
405 }
406 }
407 }
408 Log.system = Log.system + ((num2 == 0) ? "(No Changes) " : "(Updated) ") + path + "/" + text + Environment.NewLine;
409 if (num2 != 0)
410 {
411 foreach (KeyValuePair<string, int> item4 in dictionary)
412 {
413 Log.system = Log.system + item4.Key + ":" + item4.Value + " ";
414 }
415 Log.system += Environment.NewLine;
416 }
417 Log.system += Environment.NewLine;
418 }
419 if (!Directory.Exists(path))
420 {
421 Directory.CreateDirectory(path);
422 }
423 using FileStream stream = new FileStream(path + "/" + text, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
424 xSSFWorkbook.Write(stream);
425 }
426
427 public override void ValidateLang()
428 {
429 string langImportMod = CorePath.CorePackage.LangImportMod;
430 string text = nameSheet + ".xlsx";
431 Log.system = Log.system + langImportMod + text + Environment.NewLine;
432 Log.system += Environment.NewLine;
433 }
434
435 public override void ImportTexts(string _nameSheet = null)
436 {
437 string langImportMod = CorePath.CorePackage.LangImportMod;
438 string text = _nameSheet.IsEmpty(nameSheet) + ".xlsx";
439 if (!File.Exists(langImportMod + text))
440 {
441 return;
442 }
443 XSSFWorkbook xSSFWorkbook;
444 using (FileStream @is = File.Open(langImportMod + text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
445 {
446 xSSFWorkbook = new XSSFWorkbook((Stream)@is);
447 }
448 ISheet sheetAt = xSSFWorkbook.GetSheetAt(0);
449 List<FieldInfo> fields = GetFields();
450 IRow row = sheetAt.GetRow(0);
451 List<FieldMap> list = new List<FieldMap>();
452 foreach (FieldInfo item in fields)
453 {
454 if (item.isStatic)
455 {
456 continue;
457 }
458 for (int i = 0; i < row.LastCellNum; i++)
459 {
460 string stringCellValue = row.GetCell(i).StringCellValue;
461 if (stringCellValue.IsEmpty())
462 {
463 break;
464 }
465 if (stringCellValue == item.id)
466 {
467 list.Add(new FieldMap
468 {
469 field = item,
470 column = i
471 });
472 break;
473 }
474 }
475 }
476 for (int j = 2; j <= sheetAt.LastRowNum; j++)
477 {
478 IRow row2 = sheetAt.GetRow(j);
479 T val = GetRow(row2.GetCell(0).StringCellValue);
480 if (val == null)
481 {
482 Debug.Log(sheetAt.SheetName + ": id to import no longer exist: " + row2.GetCell(0).StringCellValue);
483 break;
484 }
485 foreach (FieldMap item2 in list)
486 {
487 FieldInfo field = item2.field;
488 System.Reflection.FieldInfo field2 = val.GetType().GetField(field.id + "_L");
489 if (field2 == null)
490 {
491 continue;
492 }
493 if (typeof(string[]).IsAssignableFrom(field2.FieldType))
494 {
495 ICell cell = row2.GetCell(item2.column);
496 string[] array = ((cell == null) ? new string[0] : cell.StringCellValue.Split(','));
497 string[] field3 = val.GetField<string[]>(field.id);
498 if (field3 != null)
499 {
500 field2.SetValue(val, (array.Length >= field3.Length) ? array : field3);
501 }
502 }
503 else
504 {
505 ICell cell2 = row2.GetCell(item2.column);
506 if (cell2 != null)
507 {
508 field2.SetValue(val, cell2.StringCellValue.IsEmpty(val.GetField<string>(field.id)));
509 }
510 }
511 }
512 }
513 }
514
515 public static ICell GetCell(int x, int y)
516 {
517 IRow row = currentSheet.GetRow(y) ?? currentSheet.CreateRow(y);
518 return row.GetCell(x) ?? row.CreateCell(x);
519 }
520}
521public class SourceData : ScriptableObject
522{
523 public enum AutoID
524 {
525 None,
526 Auto
527 }
528
529 [Serializable]
530 public class BaseRow
531 {
532 public int _index;
533
534 public virtual bool UseAlias => false;
535
536 public virtual string GetAlias => "";
537
538 public virtual string GetName()
539 {
540 return GetText();
541 }
542
543 public string GetDetail()
544 {
545 return GetText("detail");
546 }
547
548 public virtual string GetEditorListName()
549 {
550 return this.GetField<int>("id") + "-" + this.GetField<string>("alias") + "(" + this.GetField<string>("name_JP") + ")";
551 }
552
553 public string GetText(string id = "name", bool returnNull = false)
554 {
555 FieldInfo field = GetType().GetField(id + LangSuffix);
556 if (!Lang.isBuiltin && (field == null || (field.GetValue(this) as string).IsEmpty()))
557 {
558 FieldInfo field2 = GetType().GetField(id);
559 if (field2 != null && !(field2.GetValue(this) as string).IsEmpty())
560 {
561 return field2.GetValue(this) as string;
562 }
563 }
564 if (field != null)
565 {
566 return (field.GetValue(this) as string).IsEmpty(returnNull ? null : "");
567 }
568 return "";
569 }
570
571 public string[] GetTextArray(string id)
572 {
573 if (!Lang.isBuiltin)
574 {
575 FieldInfo field = GetType().GetField(id + Lang.suffix);
576 if (field != null && field.GetValue(this) is string[] array && array.Length != 0)
577 {
578 return array;
579 }
580 return GetType().GetField(id).GetValue(this) as string[];
581 }
582 return GetType().GetField(id + Lang.suffix).GetValue(this) as string[];
583 }
584
585 public virtual void SetID(ref int count)
586 {
587 this.SetField("id", count);
588 count++;
589 }
590
591 public virtual void OnImportData(SourceData data)
592 {
593 }
594 }
595
596 public static string LangSuffix;
597
598 public static string dataPath;
599
601
602 public bool isNew = true;
603
604 public string nameSheet;
605
606 public string nameBook;
607
608 public virtual string sheetName => "";
609
610 public virtual string sourcePath => "";
611
612 public static IRow row
613 {
614 get
615 {
616 return ExcelParser.row;
617 }
618 set
619 {
620 ExcelParser.row = value;
621 }
622 }
623
624 public static IRow rowDefault
625 {
626 get
627 {
628 return ExcelParser.rowDefault;
629 }
630 set
631 {
632 ExcelParser.rowDefault = value;
633 }
634 }
635
636 public void BuildFlags(string rawText, Dictionary<string, bool> map)
637 {
638 if (!string.IsNullOrEmpty(rawText))
639 {
640 string[] array = rawText.Split(',');
641 foreach (string key in array)
642 {
643 map.Add(key, value: true);
644 }
645 }
646 }
647
648 public virtual void Reset()
649 {
650 }
651
652 public virtual void InsertData(IRow row)
653 {
654 }
655
656 public virtual void Init()
657 {
658 }
659
660 public virtual string[] GetList(string id)
661 {
662 return null;
663 }
664
665 public virtual bool ImportData(ISheet sheet, string bookname, bool overwrite = false)
666 {
667 return false;
668 }
669
670 public virtual void BackupSource()
671 {
672 }
673
674 public virtual void RollbackSource()
675 {
676 }
677
678 public virtual void BackupPref()
679 {
680 }
681
682 public virtual void RestorePref()
683 {
684 }
685
686 public virtual void ValidatePref()
687 {
688 }
689
690 public virtual void ExportTexts(string path, bool update = false)
691 {
692 }
693
694 public virtual void ImportTexts(string _nameSheet = null)
695 {
696 }
697
698 public virtual void ValidateLang()
699 {
700 }
701
702 public static bool IsNull(ICell cell)
703 {
704 if (cell != null && cell.CellType != CellType.Blank)
705 {
706 return cell.CellType == CellType.Unknown;
707 }
708 return true;
709 }
710
711 public static int GetInt(int id)
712 {
713 return ExcelParser.GetInt(id);
714 }
715
716 public static bool GetBool(int id)
717 {
718 return ExcelParser.GetBool(id);
719 }
720
721 public static double GetDouble(int id)
722 {
723 return ExcelParser.GetDouble(id);
724 }
725
726 public static float GetFloat(int id)
727 {
728 return ExcelParser.GetFloat(id);
729 }
730
731 public static float[] GetFloatArray(int id)
732 {
733 return ExcelParser.GetFloatArray(id);
734 }
735
736 public static int[] GetIntArray(int id)
737 {
738 return ExcelParser.GetIntArray(id);
739 }
740
741 public static string[] GetStringArray(int id)
742 {
743 return ExcelParser.GetStringArray(id);
744 }
745
746 public static string GetString(int id)
747 {
748 return ExcelParser.GetString(id);
749 }
750
751 public static string GetStr(int id, bool useDefault = false)
752 {
753 return ExcelParser.GetStr(id, useDefault);
754 }
755}
Version version
Definition: BaseCore.cs:17
static BaseCore Instance
Definition: BaseCore.cs:11
static string LangImportMod
Definition: CorePath.cs:85
static bool GetBool(int id)
Definition: ExcelParser.cs:34
static IRow rowDefault
Definition: ExcelParser.cs:8
static int GetInt(int id)
Definition: ExcelParser.cs:19
static IRow row
Definition: ExcelParser.cs:6
static string GetString(int id)
Definition: ExcelParser.cs:102
static int[] GetIntArray(int id)
Definition: ExcelParser.cs:82
static float GetFloat(int id)
Definition: ExcelParser.cs:62
static double GetDouble(int id)
Definition: ExcelParser.cs:52
static string GetStr(int id, bool useDefault=false)
Definition: ExcelParser.cs:113
static float[] GetFloatArray(int id)
Definition: ExcelParser.cs:72
static string[] GetStringArray(int id)
Definition: ExcelParser.cs:92
Definition: Lang.cs:6
static string suffix
Definition: Lang.cs:30
static bool isBuiltin
Definition: Lang.cs:42
virtual string GetEditorListName()
Definition: SourceData.cs:548
string GetDetail()
Definition: SourceData.cs:543
string[] GetTextArray(string id)
Definition: SourceData.cs:571
virtual string GetAlias
Definition: SourceData.cs:536
virtual void SetID(ref int count)
Definition: SourceData.cs:585
virtual string GetName()
Definition: SourceData.cs:538
string GetText(string id="name", bool returnNull=false)
Definition: SourceData.cs:553
virtual bool UseAlias
Definition: SourceData.cs:534
virtual void OnImportData(SourceData data)
Definition: SourceData.cs:591
string GetValue(T r)
Definition: SourceData.cs:26
static float GetFloat(int id)
Definition: SourceData.cs:726
virtual string[] ImportFields
Definition: SourceData.cs:80
static string[] GetStringArray(int id)
Definition: SourceData.cs:741
virtual void SetRow(T row)
Definition: SourceData.cs:109
override bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
Definition: SourceData.cs:134
static string GetString(int id)
Definition: SourceData.cs:746
static IRow row
Definition: SourceData.cs:613
static bool GetBool(int id)
Definition: SourceData.cs:716
override void RollbackSource()
Definition: SourceData.cs:179
override void ExportTexts(string path, bool update=false)
Definition: SourceData.cs:266
static bool IsNull(ICell cell)
Definition: SourceData.cs:702
virtual string[] GetList(string id)
Definition: SourceData.cs:660
virtual T CreateRow()
Definition: SourceData.cs:169
virtual void Reset()
Definition: SourceData.cs:648
static double GetDouble(int id)
Definition: SourceData.cs:721
static IRow rowDefault
Definition: SourceData.cs:625
virtual void BackupSource()
Definition: SourceData.cs:670
virtual string sheetName
Definition: SourceData.cs:608
static int GetInt(int id)
Definition: SourceData.cs:711
virtual void InsertData(IRow row)
Definition: SourceData.cs:652
virtual string sourcePath
Definition: SourceData.cs:610
virtual void OnInit()
Definition: SourceData.cs:105
AutoID autoID
Definition: SourceData.cs:600
override void BackupSource()
Definition: SourceData.cs:174
string nameBook
Definition: SourceData.cs:606
void BuildFlags(string rawText, Dictionary< string, bool > map)
Definition: SourceData.cs:636
static string GetStr(int id, bool useDefault=false)
Definition: SourceData.cs:751
static ICell GetCell(int x, int y)
Definition: SourceData.cs:515
virtual void OnAfterImportData()
Definition: SourceData.cs:165
virtual bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
Definition: SourceData.cs:665
override void ValidateLang()
Definition: SourceData.cs:427
static float[] GetFloatArray(int id)
Definition: SourceData.cs:731
static ISheet currentSheet
Definition: SourceData.cs:76
virtual void ValidateLang()
Definition: SourceData.cs:698
List< T > _backupRows
Definition: SourceData.cs:71
string nameSheet
Definition: SourceData.cs:604
static int[] GetIntArray(int id)
Definition: SourceData.cs:736
Dictionary< string, T > alias
Definition: SourceData.cs:65
virtual void ExportTexts(string path, bool update=false)
Definition: SourceData.cs:690
virtual void ValidatePref()
Definition: SourceData.cs:686
virtual void RollbackSource()
Definition: SourceData.cs:674
override void ImportTexts(string _nameSheet=null)
Definition: SourceData.cs:435
virtual void RestorePref()
Definition: SourceData.cs:682
virtual T GetRow(string id)
Definition: SourceData.cs:261
override void Reset()
Definition: SourceData.cs:113
List< string > editorListString
Definition: SourceData.cs:74
static string LangSuffix
Definition: SourceData.cs:596
bool initialized
Definition: SourceData.cs:68
List< FieldInfo > GetFields()
Definition: SourceData.cs:201
List< string > GetListString()
Definition: SourceData.cs:184
SourceData< T, T2 > BuildEditorList()
Definition: SourceData.cs:189
bool isNew
Definition: SourceData.cs:602
virtual void BackupPref()
Definition: SourceData.cs:678
List< T > rows
Definition: SourceData.cs:61
static string dataPath
Definition: SourceData.cs:598
override void Init()
Definition: SourceData.cs:82
virtual void ImportTexts(string _nameSheet=null)
Definition: SourceData.cs:694
Dictionary< T2, T > map
Definition: SourceData.cs:63
virtual void Init()
Definition: SourceData.cs:656
virtual bool AllowHotInitialization
Definition: SourceData.cs:78
string GetText()
Definition: Version.cs:16