Minecraft world importer for Gamecraft.
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.

112 lines
4.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using GamecraftModdingAPI;
  5. using GamecraftModdingAPI.Blocks;
  6. using Newtonsoft.Json;
  7. using RobocraftX.Common.Input;
  8. using RobocraftX.Common.Utilities;
  9. using RobocraftX.StateSync;
  10. using Svelto.ECS;
  11. using Unity.Jobs;
  12. using Unity.Mathematics;
  13. using uREPL;
  14. namespace GCMC
  15. {
  16. public class CubePlacerEngine : IQueryingEntitiesEngine, IDeterministicTimeStopped
  17. {
  18. public void Ready()
  19. {
  20. RuntimeCommands.Register<string>("importWorld", ImportWorld, "Imports a Minecraft world.");
  21. }
  22. public EntitiesDB entitiesDB { get; set; }
  23. private void ImportWorld(string name)
  24. {
  25. try
  26. {
  27. Log.Output("Reading block mappings...");
  28. var parser = new IniParser.FileIniDataParser();
  29. var ini = parser.ReadFile("BlockTypes.ini");
  30. var mapping = new Dictionary<string, BlockType>(10);
  31. foreach (var section in ini.Sections)
  32. {
  33. var mcblocks = section.SectionName.Split(',');
  34. if (section.Keys["type"] == null)
  35. {
  36. if (section.Keys["ignore"] != "true")
  37. Log.Warn("Block type not specified for " + section.SectionName);
  38. continue;
  39. }
  40. if (!Enum.TryParse(section.Keys["type"], out BlockIDs type))
  41. {
  42. Log.Warn("Block type specified in ini not found: " + section.Keys["type"]);
  43. continue;
  44. }
  45. BlockColors color;
  46. if (section.Keys["color"] == null)
  47. color = BlockColors.Default;
  48. else if (!Enum.TryParse(section.Keys["color"], out color))
  49. {
  50. Log.Warn("Block color specified in ini not found: " + section.Keys["color"]);
  51. continue;
  52. }
  53. byte darkness;
  54. if (section.Keys["darkness"] == null)
  55. darkness = 0;
  56. else if (!byte.TryParse(section.Keys["darkness"], out darkness) || darkness > 9)
  57. {
  58. Log.Warn("Block darkness specified in ini isn't a number between 0 and 9: " +
  59. section.Keys["darkness"]);
  60. continue;
  61. }
  62. foreach (var mcblock in mcblocks)
  63. {
  64. mapping.Add(mcblock.ToUpper(), new BlockType
  65. {
  66. Material = mcblock.ToUpper(),
  67. Type = type,
  68. Color = new BlockColor {Color = color, Darkness = darkness}
  69. });
  70. }
  71. }
  72. Log.Output("Starting...");
  73. var blocksArray = JsonSerializer.Create()
  74. .Deserialize<Blocks[]>(new JsonTextReader(File.OpenText(name)));
  75. int C = 0;
  76. foreach (var blocks in blocksArray)
  77. {
  78. if (!mapping.TryGetValue(blocks.Material, out var type))
  79. {
  80. Console.WriteLine("Unknown block: " + blocks.Material);
  81. continue;
  82. }
  83. Block.PlaceNew(type.Type, (blocks.Start + blocks.End) / 10 * 3, color: type.Color.Color,
  84. darkness: type.Color.Darkness, scale: (blocks.End - blocks.Start + 1) * 3,
  85. rotation: float3.zero);
  86. C++;
  87. }
  88. Log.Output(C + " blocks placed.");
  89. }
  90. catch (Exception e)
  91. {
  92. Console.WriteLine(e);
  93. Log.Error(e.Message);
  94. }
  95. }
  96. public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility, in PlayerInput[] playerInputs)
  97. {
  98. return new JobHandle();
  99. }
  100. public string name { get; } = "Cube placer engine";
  101. }
  102. }