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.

140 lines
5.1KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using IllusionPlugin;
  8. using Newtonsoft.Json;
  9. using Svelto.Tasks.ExtraLean;
  10. using TechbloxModdingAPI;
  11. using TechbloxModdingAPI.App;
  12. using TechbloxModdingAPI.Blocks;
  13. using TechbloxModdingAPI.Commands;
  14. using TechbloxModdingAPI.Tasks;
  15. using Unity.Mathematics;
  16. using UnityEngine;
  17. using uREPL;
  18. namespace GCMC
  19. {
  20. public class GCMCPlugin : IEnhancedPlugin
  21. {
  22. public override string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
  23. public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  24. private readonly Dictionary<string, BlockType> mapping = new Dictionary<string, BlockType>(10);
  25. private JsonSerializer _serializer = JsonSerializer.Create();
  26. private JsonTraceWriter _traceWriter = new JsonTraceWriter();
  27. private async void ImportWorld(string name)
  28. {
  29. try
  30. {
  31. Log.Output("Reading block mappings...");
  32. var parser = new IniParser.FileIniDataParser();
  33. var ini = parser.ReadFile("BlockTypes.ini");
  34. mapping.Clear();
  35. foreach (var section in ini.Sections)
  36. {
  37. var mcblocks = section.SectionName.Split(',');
  38. BlockIDs type;
  39. if (section.Keys["type"] == null)
  40. {
  41. if (section.Keys["ignore"] != "true")
  42. {
  43. Log.Warn("Block type not specified for " + section.SectionName);
  44. continue;
  45. }
  46. type = BlockIDs.Invalid;
  47. }
  48. else if (!Enum.TryParse(section.Keys["type"], out type))
  49. {
  50. Log.Warn("Block type specified in ini not found: " + section.Keys["type"]);
  51. continue;
  52. }
  53. BlockColors color;
  54. if (section.Keys["color"] == null)
  55. color = BlockColors.Default;
  56. else if (!Enum.TryParse(section.Keys["color"], out color))
  57. {
  58. Log.Warn("Block color specified in ini not found: " + section.Keys["color"]);
  59. continue;
  60. }
  61. byte darkness;
  62. if (section.Keys["darkness"] == null)
  63. darkness = 0;
  64. else if (!byte.TryParse(section.Keys["darkness"], out darkness) || darkness > 9)
  65. {
  66. Log.Warn("Block darkness specified in ini isn't a number between 0 and 9: " +
  67. section.Keys["darkness"]);
  68. continue;
  69. }
  70. foreach (var mcblock in mcblocks)
  71. {
  72. mapping.Add(mcblock.ToUpper(), new BlockType
  73. {
  74. Material = mcblock.ToUpper(),
  75. Type = type,
  76. Color = new BlockColor(color, darkness)
  77. });
  78. }
  79. }
  80. Log.Output("Reading file...");
  81. Blocks[] blocksArray = null;
  82. await Task.Run(() =>
  83. {
  84. var fs = File.OpenText(name);
  85. _traceWriter.FileLength = ((FileStream) fs.BaseStream).Length;
  86. blocksArray = _serializer.Deserialize<Blocks[]>(new JsonTextReader(fs));
  87. });
  88. Log.Output("Placing blocks...");
  89. int i;
  90. for (i = 0; i < blocksArray.Length; i++)
  91. {
  92. var blocks = blocksArray[i];
  93. if (!mapping.TryGetValue(blocks.Material, out var type))
  94. {
  95. Console.WriteLine("Unknown block: " + blocks.Material);
  96. continue;
  97. }
  98. if (type.Type == BlockIDs.Invalid) continue;
  99. var block = new Block(type.Type, (blocks.Start + blocks.End) / 10 * 3 + new float3(5000, 0, 5000))
  100. {
  101. Color = type.Color,
  102. Scale = (blocks.End - blocks.Start + 1) * 3
  103. };
  104. }
  105. Log.Output(i + " blocks placed.");
  106. }
  107. catch (Exception e)
  108. {
  109. Console.WriteLine(e);
  110. Log.Error(e.Message);
  111. }
  112. }
  113. public override void OnApplicationStart()
  114. {
  115. TechbloxModdingAPI.Main.Init();
  116. CommandBuilder.Builder("importWorld", "Imports a Minecraft world.")
  117. .Action<string>(ImportWorld).Build();
  118. //_serializer.TraceWriter = _traceWriter;
  119. Debug.Log("GCMC loaded");
  120. }
  121. public override void OnApplicationQuit()
  122. {
  123. TechbloxModdingAPI.Main.Shutdown();
  124. }
  125. }
  126. }