From 0bd348bd47c2a94a5149d267a908f1750991aa5d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 29 Oct 2020 00:37:47 +0100 Subject: [PATCH] Fix initial issues and add error on patch fail Fixed compilation and loading issues for 2020.10.27.17.13 --- Automation/gen_csproj.py | 0 GamecraftModdingAPI/Blocks/BlockColor.cs | 5 --- GamecraftModdingAPI/Blocks/BlockEngine.cs | 2 +- GamecraftModdingAPI/Blocks/PlacementEngine.cs | 4 +-- GamecraftModdingAPI/Blocks/RemovalEngine.cs | 5 ++- GamecraftModdingAPI/Input/FakeInput.cs | 4 +-- .../HotbarSlotSelectionHandlerEnginePatch.cs | 4 +-- GamecraftModdingAPI/Main.cs | 31 ++++++++++++++----- .../Tests/GamecraftModdingAPIPluginTest.cs | 5 --- 9 files changed, 33 insertions(+), 27 deletions(-) mode change 100644 => 100755 Automation/gen_csproj.py diff --git a/Automation/gen_csproj.py b/Automation/gen_csproj.py old mode 100644 new mode 100755 diff --git a/GamecraftModdingAPI/Blocks/BlockColor.cs b/GamecraftModdingAPI/Blocks/BlockColor.cs index 4461f91..bf22090 100644 --- a/GamecraftModdingAPI/Blocks/BlockColor.cs +++ b/GamecraftModdingAPI/Blocks/BlockColor.cs @@ -7,7 +7,6 @@ namespace GamecraftModdingAPI.Blocks { public BlockColors Color; public byte Darkness; - public byte Index; public byte Index => Color == BlockColors.Default ? byte.MaxValue @@ -27,8 +26,6 @@ namespace GamecraftModdingAPI.Blocks Color = (BlockColors) (index % 10); Darkness = (byte) (index / 10); } - - Index = index; } public BlockColor(BlockColors color, byte darkness) @@ -37,8 +34,6 @@ namespace GamecraftModdingAPI.Blocks throw new ArgumentOutOfRangeException(nameof(darkness), "Darkness must be 0-9 where 0 is default."); Color = color; Darkness = darkness; - if (color == BlockColors.Default) Index = byte.MaxValue; - else Index = (byte) (darkness * 10 + color); } public float4 RGBA => Block.BlockEngine.ConvertBlockColor(Index); diff --git a/GamecraftModdingAPI/Blocks/BlockEngine.cs b/GamecraftModdingAPI/Blocks/BlockEngine.cs index 929a65f..e39a3e8 100644 --- a/GamecraftModdingAPI/Blocks/BlockEngine.cs +++ b/GamecraftModdingAPI/Blocks/BlockEngine.cs @@ -2,10 +2,10 @@ using System; using System.Collections.Generic; using System.Linq; +using Gamecraft.ColourPalette; using Gamecraft.Wires; using RobocraftX.Blocks; using RobocraftX.Common; -using RobocraftX.GUI.Hotbar.Colours; using RobocraftX.Physics; using RobocraftX.Scene.Simulation; using Svelto.DataStructures; diff --git a/GamecraftModdingAPI/Blocks/PlacementEngine.cs b/GamecraftModdingAPI/Blocks/PlacementEngine.cs index e111598..cb1cdee 100644 --- a/GamecraftModdingAPI/Blocks/PlacementEngine.cs +++ b/GamecraftModdingAPI/Blocks/PlacementEngine.cs @@ -70,9 +70,7 @@ namespace GamecraftModdingAPI.Blocks DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid}; BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct { - blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale, - snapGridScale = uscale, - unitSnapOffset = 0, isUsingUnitSize = true + blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale }; EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color}; diff --git a/GamecraftModdingAPI/Blocks/RemovalEngine.cs b/GamecraftModdingAPI/Blocks/RemovalEngine.cs index 629fd7e..c27c339 100644 --- a/GamecraftModdingAPI/Blocks/RemovalEngine.cs +++ b/GamecraftModdingAPI/Blocks/RemovalEngine.cs @@ -20,8 +20,11 @@ namespace GamecraftModdingAPI.Blocks if (!entitiesDB.Exists(target)) return false; var connections = entitiesDB.QueryEntity(target); + var groups = entitiesDB.FindGroups(); + var connStructMapper = + entitiesDB.QueryNativeMappedEntities(groups); for (int i = connections.connections.Count() - 1; i >= 0; i--) - _connectionFactory.RemoveConnection(connections, i, entitiesDB); + _connectionFactory.RemoveConnection(connections, i, connStructMapper); _entityFunctions.RemoveEntity(target); return true; } diff --git a/GamecraftModdingAPI/Input/FakeInput.cs b/GamecraftModdingAPI/Input/FakeInput.cs index df6c3ce..8cab6f5 100644 --- a/GamecraftModdingAPI/Input/FakeInput.cs +++ b/GamecraftModdingAPI/Input/FakeInput.cs @@ -76,7 +76,7 @@ namespace GamecraftModdingAPI.Input case 9: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_9; break; default: break; } - if (commandLine) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.ToggleCommandLine; + //if (commandLine) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.ToggleCommandLine; - TODO if (escape) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Escape; if (enter) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Return; if (debug) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.ToggleDebugDisplay; @@ -125,7 +125,7 @@ namespace GamecraftModdingAPI.Input if (tertiary) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.TertiaryAction; if (primaryHeld) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.PrimaryActionHeld; if (secondaryHeld) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.SecondaryActionHeld; - if (toggleUnitGrid) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleUnitGrid; + //if (toggleUnitGrid) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleUnitGrid; if (ctrl) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CtrlAction; if (toggleColourMode) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleColourMode; if (scaleBlockUp) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ScaleBlockUp; diff --git a/GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs b/GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs index dd8a375..9a81f99 100644 --- a/GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs +++ b/GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs @@ -1,8 +1,6 @@ using System; using System.Reflection; -using RobocraftX.GUI; -using RobocraftX.GUI.Hotbar; using Svelto.ECS; using HarmonyLib; @@ -17,7 +15,7 @@ namespace GamecraftModdingAPI.Inventory public static BlockIDs EquippedPartID { get => (BlockIDs)selectedBlockInt; } - private static MethodInfo PatchedMethod { get; } = AccessTools.Method(AccessTools.TypeByName("RobocraftX.GUI.Hotbar.HotbarSlotSelectionHandlerEngine"), "ActivateSlotForCube", parameters: new Type[] { typeof(uint), typeof(int), typeof(ExclusiveGroupStruct) }); + private static MethodInfo PatchedMethod { get; } = AccessTools.Method("Gamecraft.GUI.Hotbar.Blocks.SyncHotbarSlotSelectedToEquipedPartEngine:ActivateSlotForCube", parameters: new Type[] { typeof(uint), typeof(int), typeof(ExclusiveGroupStruct) }); public static void Prefix(uint playerID, int selectedDBPartID, ExclusiveGroupStruct groupID) { diff --git a/GamecraftModdingAPI/Main.cs b/GamecraftModdingAPI/Main.cs index 4a8cf24..98fb6f6 100644 --- a/GamecraftModdingAPI/Main.cs +++ b/GamecraftModdingAPI/Main.cs @@ -1,17 +1,15 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Reflection; using GamecraftModdingAPI.Blocks; using HarmonyLib; +using RobocraftX; +using RobocraftX.Services; +using Svelto.Context; + using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Events; -using GamecraftModdingAPI.Players; using GamecraftModdingAPI.Tasks; -using uREPL; namespace GamecraftModdingAPI { @@ -46,7 +44,20 @@ namespace GamecraftModdingAPI Logging.MetaDebugLog($"Patching Gamecraft"); var currentAssembly = Assembly.GetExecutingAssembly(); harmony = new Harmony(currentAssembly.GetName().Name); - harmony.PatchAll(currentAssembly); + try + { + harmony.PatchAll(currentAssembly); + } + catch (Exception e) + { //Can't use ErrorBuilder or Logging.LogException (which eventually uses ErrorBuilder) yet + Logging.Log(e.ToString()); + Logging.LogWarning("Failed to patch Gamecraft. Attempting to patch to display error..."); + harmony.Patch(AccessTools.Method(typeof(FullGameCompositionRoot), "OnContextInitialized") + .MakeGenericMethod(typeof(UnityContext)), + new HarmonyMethod(((Action) OnPatchError).Method)); //Can't use lambdas here :( + return; + } + // init utility Logging.MetaDebugLog($"Initializing Utility"); #pragma warning disable 0612,0618 @@ -102,5 +113,11 @@ namespace GamecraftModdingAPI Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown"); } } + + private static void OnPatchError() + { + ErrorBuilder.DisplayMustQuitError("Failed to patch Gamecraft!\n" + + "Make sure you're using the latest version of GamecraftModdingAPI or disable mods if the API isn't released yet."); + } } } diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 0cba023..e3a9c50 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -173,11 +173,6 @@ namespace GamecraftModdingAPI.Tests Logging.CommandLog("Finished in " + sw.ElapsedMilliseconds + "ms"); }) .Build(); - //With Sync(): 1135ms - //Without Sync(): 134ms - //Async: 348 794ms, doesn't freeze game - //Without Sync() but wait for submission: 530ms - //With Sync() at the end: 380ms Block b = null; CommandBuilder.Builder("moveBlockInSim", "Run in build mode first while looking at a block, then in sim to move it up")