Minecraft world importer for Gamecraft.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

178 lignes
6.5KB

  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;
  10. using Svelto.Tasks.ExtraLean;
  11. using TechbloxModdingAPI;
  12. using TechbloxModdingAPI.App;
  13. using TechbloxModdingAPI.Blocks;
  14. using TechbloxModdingAPI.Commands;
  15. using TechbloxModdingAPI.Tasks;
  16. using TechbloxModdingAPI.Utility;
  17. using Unity.Mathematics;
  18. using UnityEngine;
  19. using uREPL;
  20. namespace GCMC
  21. {
  22. public class GCMCPlugin : IEnhancedPlugin
  23. {
  24. public override string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
  25. public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  26. private readonly Dictionary<string, BlockType> _mapping = new Dictionary<string, BlockType>(10);
  27. private readonly List<(Block, SimBody, float3)> _simulationBlocks = new List<(Block, SimBody, float3)>();
  28. private readonly JsonSerializer _serializer = JsonSerializer.Create();
  29. private readonly JsonTraceWriter _traceWriter = new JsonTraceWriter();
  30. private async void ImportWorld(string name)
  31. {
  32. try
  33. {
  34. Log.Output("Reading block mappings...");
  35. var parser = new IniParser.FileIniDataParser();
  36. var ini = parser.ReadFile("BlockTypes.ini");
  37. _mapping.Clear();
  38. foreach (var section in ini.Sections)
  39. {
  40. var mcblocks = section.SectionName.Split(',');
  41. BlockIDs type;
  42. if (section.Keys["type"] == null)
  43. {
  44. if (section.Keys["ignore"] != "true")
  45. {
  46. Log.Warn("Block type not specified for " + section.SectionName);
  47. continue;
  48. }
  49. type = BlockIDs.Invalid;
  50. }
  51. else if (!Enum.TryParse(section.Keys["type"], out type))
  52. {
  53. Log.Warn("Block type specified in ini not found: " + section.Keys["type"]);
  54. continue;
  55. }
  56. BlockColors color;
  57. if (section.Keys["color"] == null)
  58. color = BlockColors.Default;
  59. else if (!Enum.TryParse(section.Keys["color"], out color))
  60. {
  61. Log.Warn("Block color specified in ini not found: " + section.Keys["color"]);
  62. continue;
  63. }
  64. byte darkness;
  65. if (section.Keys["darkness"] == null)
  66. darkness = 0;
  67. else if (!byte.TryParse(section.Keys["darkness"], out darkness) || darkness > 9)
  68. {
  69. Log.Warn("Block darkness specified in ini isn't a number between 0 and 9: " +
  70. section.Keys["darkness"]);
  71. continue;
  72. }
  73. foreach (var mcblock in mcblocks)
  74. {
  75. _mapping.Add(mcblock.ToUpper(), new BlockType
  76. {
  77. Material = mcblock.ToUpper(),
  78. Type = type,
  79. Color = new BlockColor(color, darkness)
  80. });
  81. }
  82. }
  83. Log.Output("Reading file...");
  84. Blocks[] blocksArray = null;
  85. await Task.Run(() =>
  86. {
  87. var fs = File.OpenText(name);
  88. _traceWriter.FileLength = ((FileStream) fs.BaseStream).Length;
  89. blocksArray = _serializer.Deserialize<Blocks[]>(new JsonTextReader(fs));
  90. });
  91. Log.Output("Placing blocks...");
  92. int i;
  93. for (i = 0; i < blocksArray.Length; i++)
  94. {
  95. var blocks = blocksArray[i];
  96. if (!_mapping.TryGetValue(blocks.Material, out var type))
  97. {
  98. Console.WriteLine("Unknown block: " + blocks.Material);
  99. continue;
  100. }
  101. if (type.Type == BlockIDs.Invalid) continue;
  102. var block = new Block(type.Type, (blocks.Start + blocks.End) / 10 * 3 + new float3(5000, 0, 5000))
  103. {
  104. Color = type.Color,
  105. Scale = (blocks.End - blocks.Start + 1) * 3,
  106. //Static = true - Doesn't seem to work
  107. };
  108. _simulationBlocks.Add((block, null, default));
  109. }
  110. Game.Simulate += GameOnSimulate;
  111. Log.Output(i + " blocks placed.");
  112. }
  113. catch (Exception e)
  114. {
  115. Console.WriteLine(e);
  116. Log.Error(e.Message);
  117. }
  118. }
  119. private void GameOnSimulate(object sender, GameEventArgs e)
  120. {
  121. Game.Simulate -= GameOnSimulate;
  122. GameWhileSimulating().RunOn(Scheduler.extraLeanRunner);
  123. }
  124. private IEnumerator GameWhileSimulating()
  125. {
  126. while (!GameState.IsSimulationMode())
  127. yield return Yield.It;
  128. for (var i = 0; i < _simulationBlocks.Count; i++)
  129. {
  130. var (block, body, _) = _simulationBlocks[i];
  131. if (body is null) body = block.GetSimBody();
  132. _simulationBlocks[i] = (block, body, body.Position);
  133. }
  134. while (GameState.IsSimulationMode())
  135. {
  136. foreach (var (_, body, pos) in _simulationBlocks)
  137. {
  138. body.Velocity = 0;
  139. body.AngularVelocity = 0;
  140. body.Position = pos;
  141. body.Rotation = float3.zero;
  142. }
  143. yield return Yield.It;
  144. }
  145. }
  146. public override void OnApplicationStart()
  147. {
  148. TechbloxModdingAPI.Main.Init();
  149. CommandBuilder.Builder("importWorld", "Imports a Minecraft world.")
  150. .Action<string>(ImportWorld).Build();
  151. //_serializer.TraceWriter = _traceWriter;
  152. Debug.Log("GCMC loaded");
  153. }
  154. public override void OnApplicationQuit()
  155. {
  156. TechbloxModdingAPI.Main.Shutdown();
  157. }
  158. }
  159. }