TestDatabase.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using XGame.Database;
  7. using XGame.Framework.Database;
  8. using XGame.Framework.Json;
  9. using XGame.Framework.Serialization;
  10. namespace FL.Editor.Tests
  11. {
  12. public static class TestDatabase
  13. {
  14. private static string CsvOutput => Application.dataPath.Replace("Assets", "ExtAssets/output/csv");
  15. [MenuItem("Test/TestDatabase/TablesToCsv")]
  16. public static void TablesToCsv()
  17. {
  18. var outputRoot = CsvOutput;
  19. if (Directory.Exists(outputRoot))
  20. {
  21. Directory.Delete(outputRoot, true);
  22. }
  23. Directory.CreateDirectory(outputRoot);
  24. TableToCsv<ChapterTable, ChapterTableRepo>(ChapterTableRepo.TableName);
  25. TableToCsv<MapTable, MapTableRepo>(MapTableRepo.TableName);
  26. TableToCsv<MonsterTable, MonsterTableRepo>(MonsterTableRepo.TableName);
  27. TableToCsv<SimpleConfigTable, SimpleConfigTableRepo>(SimpleConfigTableRepo.TableName);
  28. }
  29. private static void TableToCsv<TTable, TRepository>(string tableName) where TTable : class, ITable where TRepository : TableRepository<TTable, TRepository>
  30. {
  31. var tablePath = $"Assets/Res/Dynamic/Tables/{tableName}.bytes";
  32. var outputPath = $"{CsvOutput}/{tableName.Replace("Table", "")}.csv";
  33. var bytes = AssetDatabase.LoadAssetAtPath<TextAsset>(tablePath).bytes;
  34. var repo = SerializationUtils.Read<TRepository>(bytes);
  35. var sb = new StringBuilder();
  36. var tableType = typeof(TTable);
  37. var properties = tableType.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  38. var fieldNames = string.Empty;
  39. var fieldTypes = string.Empty;
  40. var fieldCount = properties.Length;
  41. for (var i = 0; i < fieldCount; i++)
  42. {
  43. var separator = i == fieldCount - 1 ? "" : ",";
  44. fieldNames += $"\"{properties[i].Name}\"{separator}";
  45. fieldTypes += $"\"{properties[i].PropertyType.Name}\"{separator}";
  46. }
  47. sb.AppendLine(fieldNames);
  48. sb.AppendLine(fieldTypes);
  49. //空两行用于和etoy的csv文件保持一致
  50. sb.AppendLine();
  51. sb.AppendLine();
  52. foreach (var table in repo.GetAllTables())
  53. {
  54. var fieldValues = string.Empty;
  55. for (var i = 0; i < fieldCount; i++)
  56. {
  57. var separator = i == fieldCount - 1 ? "" : ",";
  58. var fieldType = properties[i].PropertyType;
  59. var value = properties[i].GetValue(table);
  60. if (fieldType.IsEnum == false && fieldType.Namespace.Contains("XGame.Database"))
  61. { // 自定义数据结构
  62. if (value is Array array)
  63. {
  64. var arrayVals = "[";
  65. for (var j = 0; j < array.Length; j++)
  66. {
  67. var item = array.GetValue(j);
  68. var itemType = item.GetType();
  69. arrayVals += ObjectToString(itemType, item);
  70. if (j < array.Length - 1)
  71. {
  72. arrayVals += ",";
  73. }
  74. }
  75. arrayVals += "]";
  76. value = arrayVals;
  77. }
  78. else
  79. {
  80. value = ObjectToString(fieldType, value);
  81. }
  82. }
  83. else
  84. {
  85. if (fieldType.IsArray)
  86. {
  87. value = XJson.ToJson(value);
  88. }
  89. }
  90. fieldValues += $"\"{value}\"{separator}";
  91. }
  92. sb.AppendLine(fieldValues);
  93. }
  94. File.WriteAllText(outputPath, sb.ToString());
  95. Debug.Log($"TableToCsv Path:{outputPath}");
  96. }
  97. private static string ObjectToString(Type type, object obj)
  98. {
  99. var fields = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  100. var fieldCount = fields.Length;
  101. var fieldValues = "{";
  102. for (var i = 0; i < fieldCount; i++)
  103. {
  104. var separator = i == fieldCount - 1 ? "" : ",";
  105. var fieldType = fields[i].PropertyType;
  106. var value = fields[i].GetValue(obj);
  107. if (fieldType.IsArray)
  108. { //TODO 二次识别自定义数据结构
  109. value = XJson.ToJson(value);
  110. }
  111. fieldValues += $"{value}{separator}";
  112. }
  113. fieldValues += "}";
  114. return fieldValues;
  115. }
  116. //[UnityEditor.MenuItem("Test/TestDatabase/WriteTables")]
  117. //public static void WriteTables()
  118. //{
  119. // var length = 10;
  120. // var tables = new SimpleTable[length];
  121. // for (int i = 0; i < length; i++)
  122. // {
  123. // var table = new SimpleTable()
  124. // {
  125. // Id = i,
  126. // Name = typeof(SimpleTable).Name + i,
  127. // SimpleType = (SimpleType)(i % 2)
  128. // };
  129. // tables[i] = table;
  130. // }
  131. // var tableRepo = new SimpleTableRepo();
  132. // var fileInfo = typeof(SimpleTableRepo).GetField("_tables", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);
  133. // if (fileInfo == null)
  134. // {
  135. // Debug.LogError($"没找到字段: _tables");
  136. // return;
  137. // }
  138. // fileInfo.SetValue(tableRepo, tables);
  139. // var writer = new Writer();
  140. // (tableRepo as ISerializable).Serialize(writer);
  141. // var bytes = writer.Finish();
  142. // var path = $"Assets/Res/Dynamic/Tables/SimpleTable.bytes";
  143. // File.WriteAllBytes(path, bytes);
  144. // var reader = new Reader(bytes);
  145. // var result = Activator.CreateInstance<SimpleTableRepo>() as ISerializable;
  146. // result.Deserialize(reader);
  147. // AssetDatabase.Refresh();
  148. // Debug.Log($"WriteTables End.");
  149. //}
  150. }
  151. }