_Test.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using Godot;
  2. using System;
  3. using System.Linq;
  4. using System.Text.Json;
  5. using System.Collections.Generic;
  6. // using System.IO;
  7. using AvatarDoll;
  8. using ProjectSettings = AvatarDoll.ProjectSettings;
  9. public partial class _Test : Node2D
  10. {
  11. // Called when the node enters the scene tree for the first time.
  12. public override void _Ready()
  13. {
  14. // AvatarDataTest();
  15. // AvatarProjectTest();
  16. }
  17. // Called every frame. 'delta' is the elapsed time since the previous frame.
  18. public override void _Process(double delta)
  19. {
  20. }
  21. public void LubanTest()
  22. {
  23. // string path = "res://DataTables/Table/demo_tbitem.bytes";
  24. // //byte[] buffer = File.ReadAllBytes(path);
  25. //
  26. // using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  27. // long fileLength = (long)file.GetLength();
  28. // byte[] buffer = new byte[fileLength];
  29. // buffer = file.GetBuffer(fileLength);
  30. // var tables = new cfg.Tables(file => new JsonElement(buffer));
  31. //
  32. // cfg.demo.item reward = tables.Tbitem.Get(1001);
  33. // GD.Print("== load succ ==:" + reward.Name);
  34. string path = "res://DataTables/Table/avatar_data.json";
  35. //byte[] buffer = File.ReadAllBytes(path);
  36. using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  37. string jsonText = file.GetAsText();
  38. using JsonDocument doc = JsonDocument.Parse(jsonText);
  39. // var tables = new cfg.Tables(file => doc.RootElement);
  40. // cfg.AvatarDoll.AvatarDollData_Test reward = tables.TbAvatarDollDataTest;
  41. GD.Print("== load succ ==:");
  42. }
  43. public void AvatarDataTest()
  44. {
  45. // 准备测试数据
  46. AvatarDollData data = new AvatarDollData();
  47. {
  48. data.FormatVersion = 1;
  49. data.Metadata.Author = "Author";
  50. data.Metadata.Introduction = "Introduction\nIntroduction\nIntroduction";
  51. data.Metadata.Version = "3.0.1";
  52. data.Metadata.Link = "https://www.google.com/";
  53. for (int i = 0; i < 10; ++i)
  54. {
  55. char a = (char)('a' + i);
  56. data.AssetMap[i] = "asset_" + a + a + a + ".png";
  57. }
  58. AvatarBody body = new AvatarBody();
  59. body.Guid = BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0);
  60. body.Name = "少女体型";
  61. body.Introduction = "默认少女体型";
  62. body.IconAssetId = 1;
  63. AvatarElement bodypart1 = new AvatarElement();
  64. bodypart1.AssetId = 1;
  65. bodypart1.PositionX = 111;
  66. bodypart1.PositionY = 111;
  67. bodypart1.Layer = 11;
  68. body.Elements["3000"] = new List<AvatarDoll.AvatarElement>();
  69. body.Elements["3000"].Add(bodypart1);
  70. data.Bodies.Add(body);
  71. AvatarHair hair = new AvatarHair();
  72. hair.Guid = BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0);
  73. hair.Name = "少女发型";
  74. hair.Introduction = "默认少女发型";
  75. hair.IconAssetId = 1;
  76. AvatarElement hairpart = new AvatarElement();
  77. hairpart.AssetId = 2;
  78. hairpart.PositionX = 222;
  79. hairpart.PositionY = 222;
  80. hairpart.Layer = 22;
  81. hair.Elements["3000"] = new List<AvatarDoll.AvatarElement>();
  82. hair.Elements["3000"].Add(hairpart);
  83. data.Hairs.Add(hair);
  84. AvatarFace face = new AvatarFace();
  85. face.Guid = BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0);
  86. face.Name = "少女脸型";
  87. face.Introduction = "默认少女脸型";
  88. face.IconAssetId = 1;
  89. AvatarElement facepart = new AvatarElement();
  90. facepart.AssetId = 3;
  91. facepart.PositionX = 333;
  92. facepart.PositionY = 333;
  93. facepart.Layer = 33;
  94. face.Elements["3000"] = new List<AvatarDoll.AvatarElement>();
  95. face.Elements["3000"].Add(facepart);
  96. data.Faces.Add(face);
  97. for (int i = 0; i < 3; ++i)
  98. {
  99. AvatarCloth cloth = new AvatarCloth();
  100. cloth.Guid = BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0);
  101. // cloth.SupportPose.Add("3000");
  102. // cloth.SupportPose.Add(EAvatarPose.Walk1_1);
  103. cloth.ClothClasses.SetType1(EClothClasses_Type1._衣服);
  104. cloth.ClothClasses.SetType2((short)EClothClasses_Type2_Cloth._套服);
  105. // cloth.SupportClothStatus.AdaptBody = (byte)EClothStatus_AdaptBody.Maid;
  106. // cloth.SupportClothStatus.Completeness = (byte)(EClothStatus_Completeness.Percent100 |
  107. // EClothStatus_Completeness.Percent50 |
  108. // EClothStatus_Completeness.Percent0);
  109. // cloth.SupportClothStatus.Style = (byte)EClothStatus_Style.Style0;
  110. cloth.BodySlotCost[cloth.ClothClasses.Hash] = 5;
  111. cloth.Name = "裙子" + i;
  112. cloth.Introduction = "裙子介绍";
  113. cloth.IconAssetId = i;
  114. PaletteParam setting1 = new PaletteParam();
  115. setting1.ElementIndex = -1;
  116. setting1.BasicColor = Colors.White;
  117. setting1.Tolerance = 30;
  118. setting1.ModulateColor = Colors.Red;
  119. setting1.BlendMode = 0;
  120. PaletteParam setting2 = new PaletteParam();
  121. setting2.ElementIndex = -1;
  122. setting2.BasicColor = new Color(1.0f, 1.0f, 0.0f, 1.0f);
  123. setting2.Tolerance = 30;
  124. setting2.ModulateColor = new Color(0.11f, 0.9f, 0.12f, 1.0f);
  125. setting2.BlendMode = 22;
  126. List<Palette> colorPalettes = new List<Palette>();
  127. Palette colorPalette = new Palette();
  128. colorPalette.Guid = BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0);
  129. colorPalette.Name = "原色";
  130. colorPalette.Introduction = "介绍";
  131. colorPalette.IconPaletteParams.Add(setting1);
  132. colorPalette.ElementPaletteParams.Add(setting1);
  133. colorPalette.ElementPaletteParams.Add(setting2);
  134. colorPalettes.Add(colorPalette);
  135. cloth.Palettes = colorPalettes;
  136. AvatarElement part = new AvatarElement();
  137. part.AssetId = i;
  138. part.Layer = i * 11;
  139. part.LayerOffset = -i;
  140. part.PositionX = i * 111;
  141. part.PositionY = -i * 111;
  142. part.Rotation = 103.5f;
  143. part.ScaleX = -1.0f;
  144. part.ScaleY = 2.5f;
  145. part.Skew = 1.0f;
  146. cloth.Elements["3000"] = new List<AvatarDoll.AvatarElement>();
  147. cloth.Elements["3000"].Add(part);
  148. // ClothStatus state = new ClothStatus(EClothStatus_AdaptBody.Maid, EClothStatus_Style.Style0,
  149. // EClothStatus_Completeness.Percent100);
  150. // cloth.PoseSet[state.Hash] = elementSet;
  151. data.Clothes.Add(cloth);
  152. }
  153. }
  154. // 序列化测试
  155. string jsonString = JsonSerializer.Serialize<AvatarDollData>(data);
  156. GD.Print("AvatarDollData json: \n" + jsonString);
  157. // 反序列化
  158. AvatarDollData deData = JsonSerializer.Deserialize<AvatarDollData>(jsonString);
  159. // 反序列化校验
  160. bool equal = true;
  161. {
  162. equal &= deData.FormatVersion == data.FormatVersion;
  163. equal &= deData.Metadata.Equals(data.Metadata);
  164. equal &= deData.AssetMap.Count == data.AssetMap.Count;
  165. equal &= deData.AssetMap.OrderBy(kv => kv.Key).SequenceEqual(data.AssetMap.OrderBy(kv => kv.Key));
  166. equal &= deData.Bodies.Count == data.Bodies.Count;
  167. for (int i = 0; i < data.Bodies.Count; ++i)
  168. equal &= deData.Bodies[i].Equals(data.Bodies[i]);
  169. equal &= deData.Faces.Count == data.Faces.Count;
  170. for (int i = 0; i < data.Faces.Count; ++i)
  171. equal &= deData.Faces[i].Equals(data.Faces[i]);
  172. equal &= deData.Hairs.Count == data.Hairs.Count;
  173. for (int i = 0; i < data.Hairs.Count; ++i)
  174. equal &= deData.Hairs[i].Equals(data.Hairs[i]);
  175. equal &= deData.Clothes.Count == data.Clothes.Count;
  176. for (int i = 0; i < data.Clothes.Count; ++i)
  177. equal &= deData.Clothes[i].Equals(data.Clothes[i]);
  178. }
  179. GD.Print("AvatarDollData Equal: " + equal);
  180. }
  181. public void AvatarProjectTest()
  182. {
  183. ProjectSettings settings = new ProjectSettings();
  184. PartState state1 = new PartState();
  185. state1.Name = "BodyType";
  186. state1.Scope = PartScope.All;
  187. state1.Default = 3;
  188. state1.Values = new List<string>() {"Chibi", "Kid", "Girl", "Maid", "Lady", "Miss" };
  189. state1.Required = new List<bool>() {false, false, false, true, false, false };
  190. settings.States.Add(state1);
  191. PartState state2 = new PartState();
  192. state2.Name = "Poses";
  193. state2.Scope = PartScope.All;
  194. state2.Default = 0;
  195. state2.Values = new List<string>() {"Stand_0", "Stand_1", "Stand_2" };
  196. state2.Required = new List<bool>() {true, true, true };
  197. settings.States.Add(state2);
  198. PartState state3 = new PartState();
  199. state3.Name = "ClothType";
  200. state3.Scope = PartScope.Cloth;
  201. state3.Default = 0;
  202. state3.Values = new List<string>() {"Normal", "LeftPart", "RightPart" };
  203. state3.Required = new List<bool>() {true, false, false };
  204. settings.States.Add(state3);
  205. PartState state4 = new PartState();
  206. state4.Name = "Completeness";
  207. state4.Scope = PartScope.Body | PartScope.Head | PartScope.Cloth;
  208. state4.Default = 0;
  209. state4.Values = new List<string>() {"DamageLevel0", "DamageLevel1", "DamageLevel2", "DamageLevel3" };
  210. state4.Required = new List<bool>() {true,false,false,false };
  211. settings.States.Add(state4);
  212. PartState state5 = new PartState();
  213. state5.Name = "Expression";
  214. state5.Scope = PartScope.Face;
  215. state5.Default = 0;
  216. state5.Values = new List<string>() {"Normal", "Happy", "Wink"};
  217. state5.Required = new List<bool>() {true,false,false };
  218. settings.States.Add(state5);
  219. // List<List<bool>> test_data = new()
  220. // {
  221. // new() { false, false, false, true, false, false },
  222. // new() { true, true, true },
  223. // new() { true, false, false },
  224. // new() { true, false, false, false },
  225. // new() { true, false, false }
  226. // };
  227. // int hash1 = AvatarDollProjectSettings.GetStatusesHashCode(settings);
  228. // int hash2 = AvatarDollProjectSettings.GetStatusesHashCode(test_data);
  229. //
  230. // GD.Print("States Hash Code Test" );
  231. //
  232. // // GD.Print(hash1);
  233. // GD.Print(hash2);
  234. // 序列化测试
  235. string jsonString = JsonSerializer.Serialize<ProjectSettings>(settings);
  236. GD.Print("AvatarDollProjectSettings json: \n" + jsonString);
  237. GD.Print(ProjectSettings.GetStatusesCode(settings));
  238. // 反序列化
  239. ProjectSettings deData = JsonSerializer.Deserialize<ProjectSettings>(jsonString);
  240. GD.Print(settings.Equals(deData));
  241. }
  242. }