From f08280776ebf98381ce413f2e42173130a68354f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 29 May 2020 16:57:52 +0200 Subject: [PATCH] Who needs engines --- GCMC/CubePlacerEngine.cs | 131 --------------------------------------- GCMC/GCMCPlugin.cs | 123 ++++++++++++++++++++++++++++++++---- GCMC/PlaceBlockPatch.cs | 35 ----------- 3 files changed, 111 insertions(+), 178 deletions(-) delete mode 100644 GCMC/CubePlacerEngine.cs delete mode 100644 GCMC/PlaceBlockPatch.cs diff --git a/GCMC/CubePlacerEngine.cs b/GCMC/CubePlacerEngine.cs deleted file mode 100644 index e01b69b..0000000 --- a/GCMC/CubePlacerEngine.cs +++ /dev/null @@ -1,131 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading.Tasks; -using GamecraftModdingAPI; -using GamecraftModdingAPI.Blocks; -using GamecraftModdingAPI.Engines; -using GamecraftModdingAPI.Utility; -using Newtonsoft.Json; -using RobocraftX.Common; -using Svelto.ECS; -using Unity.Mathematics; -using uREPL; - -namespace GCMC -{ - public class CubePlacerEngine : IApiEngine - { - public void Ready() - { - RuntimeCommands.Register("importWorld", ImportWorld, "Imports a Minecraft world."); - _serializer.TraceWriter = _traceWriter; - } - - public EntitiesDB entitiesDB { get; set; } - - private readonly Dictionary mapping = new Dictionary(10); - private JsonSerializer _serializer = JsonSerializer.Create(); - private JsonTraceWriter _traceWriter = new JsonTraceWriter(); - - private async void ImportWorld(string name) - { - try - { - Log.Output("Reading block mappings..."); - var parser = new IniParser.FileIniDataParser(); - var ini = parser.ReadFile("BlockTypes.ini"); - mapping.Clear(); - foreach (var section in ini.Sections) - { - var mcblocks = section.SectionName.Split(','); - BlockIDs type; - if (section.Keys["type"] == null) - { - if (section.Keys["ignore"] != "true") - { - Log.Warn("Block type not specified for " + section.SectionName); - continue; - } - - type = BlockIDs.Invalid; - } - else if (!Enum.TryParse(section.Keys["type"], out type)) - { - Log.Warn("Block type specified in ini not found: " + section.Keys["type"]); - continue; - } - - BlockColors color; - if (section.Keys["color"] == null) - color = BlockColors.Default; - else if (!Enum.TryParse(section.Keys["color"], out color)) - { - Log.Warn("Block color specified in ini not found: " + section.Keys["color"]); - continue; - } - - byte darkness; - if (section.Keys["darkness"] == null) - darkness = 0; - else if (!byte.TryParse(section.Keys["darkness"], out darkness) || darkness > 9) - { - Log.Warn("Block darkness specified in ini isn't a number between 0 and 9: " + - section.Keys["darkness"]); - continue; - } - - foreach (var mcblock in mcblocks) - { - mapping.Add(mcblock.ToUpper(), new BlockType - { - Material = mcblock.ToUpper(), - Type = type, - Color = new BlockColor {Color = color, Darkness = darkness} - }); - } - } - - Log.Output("Reading file..."); - Blocks[] blocksArray = null; - await Task.Run(() => - { - var fs = File.OpenText(name); - _traceWriter.FileLength = ((FileStream) fs.BaseStream).Length; - blocksArray = _serializer.Deserialize(new JsonTextReader(fs)); - }); - Log.Output("Placing blocks..."); - int i; - for (i = 0; i < blocksArray.Length; i++) - { - var blocks = blocksArray[i]; - if (!mapping.TryGetValue(blocks.Material, out var type)) - { - Console.WriteLine("Unknown block: " + blocks.Material); - continue; - } - - if (type.Type == BlockIDs.Invalid) continue; - - Block.PlaceNew(type.Type, (blocks.Start + blocks.End) / 10 * 3, color: type.Color.Color, - darkness: type.Color.Darkness, scale: (blocks.End - blocks.Start + 1) * 3, - rotation: float3.zero); - } - - Log.Output(i + " blocks placed."); - } - catch (Exception e) - { - Console.WriteLine(e); - Log.Error(e.Message); - } - } - - public void Dispose() - { - } - - public string Name { get; } = "GCMCCubePlacerEngine"; - public bool isRemovable { get; } = false; - } -} \ No newline at end of file diff --git a/GCMC/GCMCPlugin.cs b/GCMC/GCMCPlugin.cs index 28547ec..cc76000 100644 --- a/GCMC/GCMCPlugin.cs +++ b/GCMC/GCMCPlugin.cs @@ -1,8 +1,15 @@ -using System.Reflection; -using GamecraftModdingAPI.Utility; -using HarmonyLib; +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using GamecraftModdingAPI; +using GamecraftModdingAPI.Blocks; +using GamecraftModdingAPI.Commands; using IllusionPlugin; +using Newtonsoft.Json; +using Unity.Mathematics; using UnityEngine; +using uREPL; namespace GCMC { @@ -10,24 +17,116 @@ namespace GCMC { public string Name { get; } = "GCMC"; public string Version { get; } = "v0.0.1"; - public static Harmony harmony { get; protected set; } - public const string HarmonyID = "io.github.norbipeti.GCMC"; + + private readonly Dictionary mapping = new Dictionary(10); + private JsonSerializer _serializer = JsonSerializer.Create(); + private JsonTraceWriter _traceWriter = new JsonTraceWriter(); + + private async void ImportWorld(string name) + { + try + { + Log.Output("Reading block mappings..."); + var parser = new IniParser.FileIniDataParser(); + var ini = parser.ReadFile("BlockTypes.ini"); + mapping.Clear(); + foreach (var section in ini.Sections) + { + var mcblocks = section.SectionName.Split(','); + BlockIDs type; + if (section.Keys["type"] == null) + { + if (section.Keys["ignore"] != "true") + { + Log.Warn("Block type not specified for " + section.SectionName); + continue; + } + + type = BlockIDs.Invalid; + } + else if (!Enum.TryParse(section.Keys["type"], out type)) + { + Log.Warn("Block type specified in ini not found: " + section.Keys["type"]); + continue; + } + + BlockColors color; + if (section.Keys["color"] == null) + color = BlockColors.Default; + else if (!Enum.TryParse(section.Keys["color"], out color)) + { + Log.Warn("Block color specified in ini not found: " + section.Keys["color"]); + continue; + } + + byte darkness; + if (section.Keys["darkness"] == null) + darkness = 0; + else if (!byte.TryParse(section.Keys["darkness"], out darkness) || darkness > 9) + { + Log.Warn("Block darkness specified in ini isn't a number between 0 and 9: " + + section.Keys["darkness"]); + continue; + } + + foreach (var mcblock in mcblocks) + { + mapping.Add(mcblock.ToUpper(), new BlockType + { + Material = mcblock.ToUpper(), + Type = type, + Color = new BlockColor {Color = color, Darkness = darkness} + }); + } + } + + Log.Output("Reading file..."); + Blocks[] blocksArray = null; + await Task.Run(() => + { + var fs = File.OpenText(name); + _traceWriter.FileLength = ((FileStream) fs.BaseStream).Length; + blocksArray = _serializer.Deserialize(new JsonTextReader(fs)); + }); + Log.Output("Placing blocks..."); + int i; + for (i = 0; i < blocksArray.Length; i++) + { + var blocks = blocksArray[i]; + if (!mapping.TryGetValue(blocks.Material, out var type)) + { + Console.WriteLine("Unknown block: " + blocks.Material); + continue; + } + + if (type.Type == BlockIDs.Invalid) continue; + + Block.PlaceNew(type.Type, (blocks.Start + blocks.End) / 10 * 3, color: type.Color.Color, + darkness: type.Color.Darkness, scale: (blocks.End - blocks.Start + 1) * 3, + rotation: float3.zero); + } + + Log.Output(i + " blocks placed."); + } + catch (Exception e) + { + Console.WriteLine(e); + Log.Error(e.Message); + } + } public void OnApplicationStart() { - /*if (harmony == null) - { - harmony = new Harmony(HarmonyID); - harmony.PatchAll(Assembly.GetExecutingAssembly()); - }*/ - GameEngineManager.AddGameEngine(new CubePlacerEngine()); + GamecraftModdingAPI.Main.Init(); + CommandBuilder.Builder("importWorld", "Imports a Minecraft world.") + .Action(ImportWorld).Build(); + _serializer.TraceWriter = _traceWriter; Debug.Log("GCMC loaded"); } public void OnApplicationQuit() { - harmony?.UnpatchAll(HarmonyID); } public void OnLevelWasLoaded(int level) diff --git a/GCMC/PlaceBlockPatch.cs b/GCMC/PlaceBlockPatch.cs deleted file mode 100644 index f8c7813..0000000 --- a/GCMC/PlaceBlockPatch.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Reflection; -using DataLoader; -using HarmonyLib; -using JetBrains.Annotations; -using RobocraftX.Common; -using RobocraftX.CR.MachineEditing; -using RobocraftX.StateSync; -using Svelto.ECS; -using Unity.Entities; -using UnityEngine; - -namespace GCMC -{ - /*[HarmonyPatch] - [UsedImplicitly] - public class PlaceBlockPatch - { - static void Postfix(EnginesRoot enginesRoot, ref StateSyncRegistrationHelper stateSyncReg, bool isAuthoritative) - { - if (isAuthoritative) - { - stateSyncReg.AddDeterministicEngine(new CubePlacerEngine()); - Debug.Log($"Added Minecraft world import engine"); - } - else - Debug.Log("Not authoritative, not adding MC engine"); - } - - static MethodBase TargetMethod() - { - return typeof(MainEditingCompositionRoot).GetMethod("Compose", - BindingFlags.Public | BindingFlags.Static); - } - }*/ -} \ No newline at end of file