Magically import images and more into Gamecraft as blocks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Newtonsoft.Json;
  5. using GamecraftModdingAPI;
  6. using GamecraftModdingAPI.Blocks;
  7. namespace Pixi.Common
  8. {
  9. public static class VoxelObjectNotationUtility
  10. {
  11. private static readonly float[] origin_base = new float[3] { 0, 0, 0 };
  12. private static Dictionary<string, BlockIDs> enumMap = null;
  13. public static string SerializeBlocks(Block[] blocks, float[] origin = null)
  14. {
  15. BlockJsonInfo[] blockJsons = new BlockJsonInfo[blocks.Length];
  16. for (int i = 0; i < blocks.Length; i++)
  17. {
  18. blockJsons[i] = JsonObject(blocks[i], origin);
  19. }
  20. return JsonConvert.SerializeObject(blockJsons);
  21. }
  22. public static byte[] SerializeBlocksToBytes(Block[] blocks)
  23. {
  24. return Encoding.UTF8.GetBytes(SerializeBlocks(blocks));
  25. }
  26. public static BlockJsonInfo[] DeserializeBlocks(byte[] data)
  27. {
  28. return DeserializeBlocks(Encoding.UTF8.GetString(data));
  29. }
  30. public static BlockJsonInfo[] DeserializeBlocks(string data)
  31. {
  32. return JsonConvert.DeserializeObject<BlockJsonInfo[]>(data);
  33. }
  34. public static BlockJsonInfo JsonObject(Block block, float[] origin = null)
  35. {
  36. if (origin == null) origin = origin_base;
  37. BlockJsonInfo jsonInfo = new BlockJsonInfo
  38. {
  39. name = block.Type.ToString(),
  40. position = new float[3] { block.Position.x - origin[0], block.Position.y - origin[1], block.Position.z - origin[2]},
  41. rotation = new float[3] { block.Rotation.x, block.Rotation.y, block.Rotation.z },
  42. color = ColorSpaceUtility.UnquantizeToArray(block.Color),
  43. scale = new float[3] {block.Scale.x, block.Scale.y, block.Scale.z},
  44. };
  45. // custom stats for special blocks
  46. switch (block.Type)
  47. {
  48. case BlockIDs.TextBlock:
  49. TextBlock t = block.Specialise<TextBlock>();
  50. jsonInfo.name += "\t" + t.Text + "\t" + t.TextBlockId;
  51. break;
  52. case BlockIDs.ConsoleBlock:
  53. ConsoleBlock c = block.Specialise<ConsoleBlock>();
  54. jsonInfo.name += "\t" + c.Command + "\t" + c.Arg1 + "\t" + c.Arg2 + "\t" + c.Arg3;
  55. break;
  56. case BlockIDs.DampedSpring:
  57. DampedSpring d = block.Specialise<DampedSpring>();
  58. jsonInfo.name += "\t" + d.Stiffness + "\t" + d.Damping;
  59. break;
  60. case BlockIDs.ServoAxle:
  61. case BlockIDs.ServoHinge:
  62. case BlockIDs.PneumaticAxle:
  63. case BlockIDs.PneumaticHinge:
  64. Servo s = block.Specialise<Servo>();
  65. jsonInfo.name += "\t" + s.MinimumAngle + "\t" + s.MaximumAngle + "\t" + s.MaximumForce + "\t" +
  66. s.Reverse;
  67. break;
  68. case BlockIDs.MotorM:
  69. case BlockIDs.MotorS:
  70. Motor m = block.Specialise<Motor>();
  71. jsonInfo.name += "\t" + m.TopSpeed + "\t" + m.Torque + "\t" + m.Reverse;
  72. break;
  73. default: break;
  74. }
  75. return jsonInfo;
  76. }
  77. public static BlockIDs NameToEnum(BlockJsonInfo block)
  78. {
  79. return NameToEnum(block.name);
  80. }
  81. public static BlockIDs NameToEnum(string name)
  82. {
  83. if (enumMap == null) GenerateEnumMap();
  84. return enumMap[name];
  85. }
  86. private static void GenerateEnumMap()
  87. {
  88. enumMap = new Dictionary<string, BlockIDs>();
  89. foreach(BlockIDs e in Enum.GetValues(typeof(BlockIDs)))
  90. {
  91. enumMap[e.ToString()] = e;
  92. }
  93. }
  94. }
  95. }