diff --git a/GCDC/DiscordEngineInjectionPatch.cs b/GCDC/DiscordEngineInjectionPatch.cs index 1af4a60..1f7bfcd 100644 --- a/GCDC/DiscordEngineInjectionPatch.cs +++ b/GCDC/DiscordEngineInjectionPatch.cs @@ -1,9 +1,11 @@ using System; using System.Reflection; +using Gamecraft.Blocks.ConsoleBlock; using Harmony; using RobocraftX; using RobocraftX.GUI.CommandLine; using RobocraftX.Multiplayer; +using RobocraftX.StateSync; using Svelto.Context; using Svelto.ECS; using Unity.Entities; @@ -14,18 +16,24 @@ namespace GCDC [HarmonyPatch] public class DiscordEngineInjectionPatch { - static void Postfix(UnityContext contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters) + static void Postfix(EnginesRoot enginesRoot, ref StateSyncRegistrationHelper stateSyncReg, bool isAuthoritative) { - enginesRoot.AddEngine(new TextBlockUpdateEngine()); - Debug.Log($"Added text block update engine"); + if (isAuthoritative) + { + stateSyncReg.AddEngine(new TextBlockUpdateEngine()); + Debug.Log($"Added Discord text block update engine"); + } + else + Debug.Log("Not authoritative, not adding Discord engine"); } static MethodBase TargetMethod(HarmonyInstance instance) { - return _ComposeMethodInfo(CommandLineCompositionRoot.Compose>); + return _ComposeMethodInfo(ConsoleBlockCompositionRoot.Compose); } - private static MethodInfo _ComposeMethodInfo(Action, EnginesRoot, World, Action, MultiplayerInitParameters> a) + private delegate void ComposeAction(EnginesRoot er, ref StateSyncRegistrationHelper ssrh, bool isAuthoritative); + private static MethodInfo _ComposeMethodInfo(ComposeAction a) { return a.Method; } diff --git a/GCDC/GCDCPlugin.cs b/GCDC/GCDCPlugin.cs index 3697ebe..28808f1 100644 --- a/GCDC/GCDCPlugin.cs +++ b/GCDC/GCDCPlugin.cs @@ -1,4 +1,7 @@ -using IllusionPlugin; +using System.Reflection; +using Harmony; +using IllusionPlugin; +using UnityEngine; namespace GCDC { @@ -6,35 +9,39 @@ namespace GCDC { public string Name { get; } = "GCDC"; public string Version { get; } = "v0.0.1"; + public static HarmonyInstance harmony { get; protected set; } + public const string HarmonyID = "io.github.norbipeti.GCDC"; public void OnApplicationStart() { - throw new System.NotImplementedException(); + if (harmony == null) + { + harmony = HarmonyInstance.Create(HarmonyID); + harmony.PatchAll(Assembly.GetExecutingAssembly()); + } + + Debug.Log("GCDC loaded"); } public void OnApplicationQuit() { - throw new System.NotImplementedException(); + harmony?.UnpatchAll(HarmonyID); } public void OnLevelWasLoaded(int level) { - throw new System.NotImplementedException(); } public void OnLevelWasInitialized(int level) { - throw new System.NotImplementedException(); } public void OnUpdate() { - throw new System.NotImplementedException(); } public void OnFixedUpdate() { - throw new System.NotImplementedException(); } } } \ No newline at end of file diff --git a/GCDC/TextBlockUpdateEngine.cs b/GCDC/TextBlockUpdateEngine.cs index ac1bb0b..02bb931 100644 --- a/GCDC/TextBlockUpdateEngine.cs +++ b/GCDC/TextBlockUpdateEngine.cs @@ -1,7 +1,13 @@ +using System.Collections.Generic; +using System.Linq; +using RobocraftX.Blocks.GUI; +using RobocraftX.Common; using RobocraftX.Common.Input; using RobocraftX.Common.Utilities; +using RobocraftX.SimulationModeState; using RobocraftX.StateSync; using Svelto.ECS; +using Svelto.ECS.Experimental; using Unity.Jobs; using uREPL; @@ -14,12 +20,30 @@ namespace GCDC } public IEntitiesDB entitiesDB { get; set; } - public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility, in PlayerInput[] playerInputs) + public string name { get; } = "GCDC-TextUpdate"; + private volatile Queue messages = new Queue(); + private volatile bool updatedTextBlock; + + public JobHandle SimulatePhysicsStep( + in float deltaTime, + in PhysicsUtility utility, + in PlayerInput[] playerInputs) //Gamecraft.Blocks.ConsoleBlock.dll { - //TODO + if (updatedTextBlock) + return new JobHandle(); + var txt = messages.Count > 0 ? messages.Aggregate((current, msg) => current + "\n" + msg) : ""; + RuntimeCommands.Call("ChangeTextBlockCommand", "Discord", txt); + updatedTextBlock = true; + return new JobHandle(); } - public string name { get; } = "GCDC-TextUpdate"; + public void AddMessage(string message) + { + messages.Enqueue(message); + if (messages.Count > 10) + messages.Dequeue(); + updatedTextBlock = false; + } } } \ No newline at end of file