2using System.Collections.Generic;
4using System.Reflection;
5using NPOI.SS.UserModel;
7using NPOI.XSSF.UserModel;
28 if (r.HasField<
string>(
id))
30 return r.GetField<
string>(
id);
32 if (r.HasField<
int>(
id))
34 return r.GetField<
int>(
id).ToString();
36 if (r.HasField<
string[]>(
id))
39 string[] field = r.GetField<
string[]>(
id);
42 string[] array = field;
43 foreach (
string text2
in array)
45 text = text + text2 +
",";
48 return text.TrimEnd(
',');
61 public List<T>
rows =
new List<T>();
63 public Dictionary<T2, T>
map =
new Dictionary<T2, T>();
65 public Dictionary<string, T>
alias =
new Dictionary<string, T>();
82 public override void Init()
84 Debug.Log(
"Initializing:" + base.name);
87 Debug.Log(
"#init Skipping sourceData.Init:" + base.name);
118 if (!Application.isPlaying)
120 BaseCore.resetRuntime =
true;
136 public override bool ImportData(ISheet sheet,
string bookname,
bool overwrite =
false)
140 rows =
new List<T>();
145 SourceData.rowDefault = sheet.GetRow(2);
147 for (
int i = 3; i <= sheet.LastRowNum; i++)
149 SourceData.row = sheet.GetRow(i);
155 val.OnImportData(
this);
159 string text = sheet.SheetName +
"/" + sheet.LastRowNum +
"/" + num;
176 public override int ImportRows(IEnumerable<BaseRow> sourceRows)
179 foreach (
BaseRow sourceRow
in sourceRows)
181 if (sourceRow is T val)
183 val.OnImportData(
this);
195 HashSet<T2> hashSet =
new HashSet<T2>();
196 List<T> list =
new List<T>(
rows.Count);
197 string arg = GetType().Name;
198 System.Reflection.FieldInfo field = typeof(T).GetField(
"id");
199 bool flag = typeof(
LangRow).IsAssignableFrom(typeof(T));
204 if (field.FieldType != typeof(T2))
206 Debug.LogError(
$"#source override: {arg} id field mismatch {field.FieldType} != {typeof(T2)}");
208 for (
int num =
rows.Count - 1; num >= 0; num--)
211 T2 val2 = (T2)field.GetValue(val);
212 if (hashSet.Add(val2))
218 Debug.Log(
$"#source override: {arg} {val2}");
221 if (
rows.Count != list.Count)
223 Debug.Log(
$"#source override: {arg} total/{rows.Count} -> unique/{list.Count}");
258 List<FieldInfo> list =
new List<FieldInfo>();
260 AddField(
"id", 4096);
261 AddField(
"version", 4096);
262 AddField(
"filter", 4096);
263 AddField(
"name", 4096);
264 AddField(
"text", 4096);
265 AddField(
"detail", 4096);
266 AddField(
"description", 4096);
268 foreach (
string text
in importFields)
272 AddField(text, 4096);
276 void AddField(
string id,
int width)
278 bool flag =
id ==
"id" ||
id ==
"filter";
279 bool flag2 =
id ==
"version";
280 if (!(typeof(T).GetField(
id) ==
null) || flag2)
291 if (!(flag || flag2))
321 public override void ExportTexts(
string path,
bool update =
false)
325 XSSFWorkbook xSSFWorkbook =
new XSSFWorkbook();
334 ICellStyle cellStyle = xSSFWorkbook.CreateCellStyle();
335 cellStyle.FillForegroundColor = 44;
336 cellStyle.FillPattern = FillPattern.SolidForeground;
337 if (!
item.isStatic &&
item.id !=
"version")
339 GetCell(num, y).CellStyle = cellStyle;
345 foreach (T row4
in rows)
353 else if (item2.
id ==
"version")
361 currentSheet.SetAutoFilter(
new CellRangeAddress(1, 1, 0, fields.Count - 1));
364 Dictionary<string, int> dictionary =
new Dictionary<string, int>();
365 if (!File.Exists(path +
"_temp/" + text))
369 XSSFWorkbook xSSFWorkbook2;
370 using (FileStream @is = File.Open(path +
"_temp/" + text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
372 xSSFWorkbook2 =
new XSSFWorkbook((Stream)@is);
374 ISheet sheetAt = xSSFWorkbook2.GetSheetAt(0);
375 IRow
row = sheetAt.GetRow(0);
384 for (
int i = 0; i <
row.LastCellNum; i++)
386 string stringCellValue =
row.GetCell(i).StringCellValue;
387 if (stringCellValue.IsEmpty())
391 if (stringCellValue == item3.
id)
401 dictionary.Add(item3.
id, 0);
402 for (y = 2; y <= sheetAt.LastRowNum; y++)
404 IRow row2 = sheetAt.GetRow(y);
413 ICell cell = row2.GetCell(0);
418 string text2 = ((cell.CellType == CellType.Numeric) ? cell.NumericCellValue.ToString() : cell.StringCellValue);
434 ICell cell2 = row3.GetCell(0);
435 if (cell2 ==
null || cell2.StringCellValue != text2)
439 string text3 = row2.GetCell(num3)?.StringCellValue ??
"";
444 (row3.GetCell(item3.
column) ?? row3.CreateCell(item3.
column)).SetCellValue(text3);
445 if (item3.
id !=
"version")
447 ICell cell3 = row3.GetCell(item3.
column + 2);
448 ICell cell4 = row2.GetCell(num3 + 2);
453 if (cell4 ==
null || cell3.StringCellValue != cell4.StringCellValue)
455 row3.GetCell(1).SetCellValue(cellValue);
459 dictionary[item3.
id]++;
463 Log.system = Log.system + ((num2 == 0) ?
"(No Changes) " :
"(Updated) ") + path +
"/" + text + Environment.NewLine;
466 foreach (KeyValuePair<string, int> item4
in dictionary)
468 Log.system = Log.system + item4.Key +
":" + item4.Value +
" ";
470 Log.system += Environment.NewLine;
472 Log.system += Environment.NewLine;
474 if (!Directory.Exists(path))
476 Directory.CreateDirectory(path);
478 using FileStream stream =
new FileStream(path +
"/" + text, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
479 xSSFWorkbook.Write(stream);
486 Log.system = Log.system + langImportMod + text + Environment.NewLine;
487 Log.system += Environment.NewLine;
493 string text = _nameSheet.IsEmpty(
nameSheet) +
".xlsx";
494 if (!File.Exists(langImportMod + text))
498 XSSFWorkbook xSSFWorkbook;
499 using (FileStream @is = File.Open(langImportMod + text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
501 xSSFWorkbook =
new XSSFWorkbook((Stream)@is);
503 ISheet sheetAt = xSSFWorkbook.GetSheetAt(0);
505 IRow
row = sheetAt.GetRow(0);
506 List<FieldMap> list =
new List<FieldMap>();
513 for (
int i = 0; i <
row.LastCellNum; i++)
515 string stringCellValue =
row.GetCell(i).StringCellValue;
516 if (stringCellValue.IsEmpty())
520 if (stringCellValue ==
item.id)
531 for (
int j = 2; j <= sheetAt.LastRowNum; j++)
533 IRow row2 = sheetAt.GetRow(j);
534 T val =
GetRow(row2.GetCell(0).StringCellValue);
537 Debug.Log(sheetAt.SheetName +
": id to import no longer exist: " + row2.GetCell(0).StringCellValue);
543 System.Reflection.FieldInfo field2 = val.GetType().GetField(field.
id +
"_L");
548 if (typeof(
string[]).IsAssignableFrom(field2.FieldType))
550 ICell cell = row2.GetCell(item2.
column);
551 string[] array = ((cell ==
null) ?
new string[0] : cell.StringCellValue.Split(
','));
552 string[] field3 = val.GetField<
string[]>(field.
id);
555 field2.SetValue(val, (array.Length >= field3.Length) ? array : field3);
560 ICell cell2 = row2.GetCell(item2.
column);
563 field2.SetValue(val, cell2.StringCellValue.IsEmpty(val.GetField<
string>(field.
id)));
573 return row.GetCell(x) ??
row.CreateCell(x);
587 private static readonly Dictionary<Type, Dictionary<string, FieldInfo>>
_fieldCache =
new Dictionary<Type, Dictionary<string, FieldInfo>>();
597 Type type = GetType();
602 value =
new Dictionary<string, FieldInfo>();
603 FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
606 value[fieldInfo.Name] = fieldInfo;
623 return this.GetField<int>(
"id") +
"-" + this.GetField<string>(
"alias") +
"(" + this.GetField<string>(
"name_JP") +
")";
626 public string GetText(
string id =
"name",
bool returnNull =
false)
628 Dictionary<string, FieldInfo> rowFields =
GetRowFields();
629 if (rowFields.TryGetValue(
id +
LangSuffix, out var value))
631 string text = value.GetValue(
this) as string;
637 if (!
Lang.
isBuiltin && rowFields.TryGetValue(
id, out var value2))
639 string text2 = value2.GetValue(
this) as string;
640 if (!text2.IsEmpty())
654 Dictionary<string, FieldInfo> rowFields =
GetRowFields();
655 if (rowFields.TryGetValue(
id +
LangSuffix, out var value) && value.GetValue(
this) is
string[] { Length: >0 } array)
659 if (!
Lang.
isBuiltin && rowFields.TryGetValue(
id, out var value2) && value2.GetValue(
this) is
string[] { Length: >0 } array2)
663 return Array.Empty<
string>();
666 public virtual void SetID(ref
int count)
668 this.SetField(
"id", count);
676 public virtual IDictionary<string, string>
ExportTexts(
string idField =
"id")
678 Dictionary<string, FieldInfo> rowFields =
GetRowFields();
679 object obj = rowFields.GetValueOrDefault(idField)?.GetValue(
this);
680 SortedDictionary<string, string> sortedDictionary =
new SortedDictionary<string, string>();
683 return sortedDictionary;
685 string name = GetType().DeclaringType.Name;
686 foreach (var (text2, jp2) in rowFields)
688 if (!text2.EndsWith(
"_JP"))
692 string text3 = text2[..^3];
693 if (rowFields.TryGetValue(text3, out var value) && rowFields.ContainsKey(text3 +
"_L"))
695 string text4 = GetFieldText(jp2, value);
696 if (!text4.IsEmpty())
698 sortedDictionary[
$"{name}.{obj}.{text3}"] = text4;
702 return sortedDictionary;
714 if (obj2 is
string str)
716 return str.IsEmpty(obj3 as
string);
718 if (obj2 is
string[] array)
720 return string.Join(
',', (array.Length != 0) ? array : (obj3 as
string[]));
726 public virtual void ImportTexts(IReadOnlyDictionary<string, string> texts,
string idField =
"id")
728 Dictionary<string, FieldInfo> rowFields =
GetRowFields();
729 object obj = rowFields.GetValueOrDefault(idField)?.GetValue(
this);
734 string name = GetType().DeclaringType.Name;
735 foreach (var (text2, fieldInfo2) in rowFields)
737 if (!text2.EndsWith(
"_L"))
741 string text3 = text2[..^2];
742 if (rowFields.ContainsKey(text3) && rowFields.ContainsKey(text3 +
"_JP"))
744 fieldInfo2.SetValue(
this,
null);
745 if (texts.TryGetValue(
$"{name}.{obj}.{text3}", out var value2) && !value2.IsEmpty())
747 SetFieldText(fieldInfo2, value2);
751 void SetFieldText(
FieldInfo l,
string value)
753 if (l.FieldType == typeof(
string))
755 l.SetValue(
this, value);
757 else if (l.FieldType == typeof(
string[]))
759 l.SetValue(
this, value.IsEmpty() ? Array.Empty<
string>() : value.Split(
','));
789 ExcelParser.row = value;
801 ExcelParser.rowDefault = value;
807 if (!
string.IsNullOrEmpty(rawText))
809 string[] array = rawText.Split(
',');
810 foreach (
string key
in array)
812 map.Add(key, value:
true);
834 public virtual bool ImportData(ISheet sheet,
string bookname,
bool overwrite =
false)
839 public virtual int ImportRows(IEnumerable<BaseRow> sourceRows)
878 if (cell !=
null && cell.CellType != CellType.Blank)
880 return cell.CellType == CellType.Unknown;
925 public static string GetStr(
int id,
bool useDefault =
false)
static string LangImportMod
static bool GetBool(int id)
static int GetInt(int id)
static string GetString(int id)
static int[] GetIntArray(int id)
static float GetFloat(int id)
static double GetDouble(int id)
static string GetStr(int id, bool useDefault=false)
static float[] GetFloatArray(int id)
static string[] GetStringArray(int id)
virtual string GetEditorListName()
string[] GetTextArray(string id)
virtual void SetID(ref int count)
string GetText(string id="name", bool returnNull=false)
virtual IDictionary< string, string > ExportTexts(string idField="id")
static readonly Dictionary< Type, Dictionary< string, FieldInfo > > _fieldCache
virtual void OnImportData(SourceData data)
virtual void ImportTexts(IReadOnlyDictionary< string, string > texts, string idField="id")
Dictionary< string, FieldInfo > GetRowFields()
static float GetFloat(int id)
virtual string[] ImportFields
static string[] GetStringArray(int id)
virtual void SetRow(T row)
override bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
static string GetString(int id)
static bool GetBool(int id)
override void RollbackSource()
override void ExportTexts(string path, bool update=false)
static bool IsNull(ICell cell)
virtual string[] GetList(string id)
static double GetDouble(int id)
virtual void BackupSource()
static int GetInt(int id)
virtual void InsertData(IRow row)
virtual string sourcePath
override void BackupSource()
void BuildFlags(string rawText, Dictionary< string, bool > map)
static string GetStr(int id, bool useDefault=false)
static ICell GetCell(int x, int y)
virtual void OnAfterImportData()
virtual bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
override void ValidateLang()
static float[] GetFloatArray(int id)
static ISheet currentSheet
virtual void ValidateLang()
virtual void RemoveDuplicateRows()
static int[] GetIntArray(int id)
Dictionary< string, T > alias
virtual void ExportTexts(string path, bool update=false)
virtual void ValidatePref()
override int ImportRows(IEnumerable< BaseRow > sourceRows)
virtual void RollbackSource()
override void ImportTexts(string _nameSheet=null)
virtual void RestorePref()
virtual T GetRow(string id)
List< string > editorListString
List< FieldInfo > GetFields()
List< string > GetListString()
SourceData< T, T2 > BuildEditorList()
virtual void BackupPref()
virtual int ImportRows(IEnumerable< BaseRow > sourceRows)
virtual void ImportTexts(string _nameSheet=null)
virtual bool AllowHotInitialization