Moar Gamecraft commands!
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.

58 lines
2.6KB

  1. using System;
  2. using System.Reflection;
  3. using Harmony;
  4. using UnityEngine;
  5. using Unity.Entities;
  6. using RobocraftX;
  7. using RobocraftX.GUI.CommandLine;
  8. using RobocraftX.Multiplayer;
  9. using RobocraftX.StateSync;
  10. using Svelto.ECS;
  11. using Svelto.Context;
  12. namespace ExtraCommands
  13. {
  14. [HarmonyPatch]
  15. class CommandLineCompositionRootSaveCommandPatch
  16. {
  17. static void Postfix(UnityContext<FullGameCompositionRoot> contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters)
  18. {
  19. int engineCount = 0;
  20. MethodInfo commandHelp = Harmony.AccessTools.Method(Harmony.AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility"), "SaveCommandHelp", new Type[] { typeof(string), typeof(string) });
  21. foreach (Type t in typeof(CustomCommandEngine).Assembly.GetTypes())
  22. {
  23. CustomCommandAttribute[] attributes = (CustomCommandAttribute[])t.GetCustomAttributes(typeof(CustomCommandAttribute), false);
  24. CustomCommandEngine inst = null;
  25. foreach (CustomCommandAttribute attr in attributes)
  26. {
  27. if (attr != null && t.IsSubclassOf(typeof(CustomCommandEngine)))
  28. {
  29. if (inst == null)
  30. {
  31. // create instance by getting the constructor through reflection
  32. inst = (CustomCommandEngine)t.GetConstructor(new Type[] { typeof(UnityContext<FullGameCompositionRoot>), typeof(EnginesRoot), typeof(World), typeof(Action), typeof(MultiplayerInitParameters) })
  33. .Invoke(new object[] { contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters });
  34. // add to engineRoot
  35. enginesRoot.AddEngine(inst);
  36. engineCount++;
  37. }
  38. }
  39. }
  40. }
  41. Debug.Log($"Added {engineCount} custom command engines");
  42. }
  43. static MethodBase TargetMethod(HarmonyInstance instance)
  44. {
  45. return _ComposeMethodInfo(CommandLineCompositionRoot.Compose);
  46. }
  47. private delegate void ComposeAction(UnityContext<FullGameCompositionRoot> c, EnginesRoot r, World w, Action a,
  48. MultiplayerInitParameters mip, ref StateSyncRegistrationHelper ssrh); //Ref params can't be used with Actions
  49. private static MethodInfo _ComposeMethodInfo(ComposeAction a)
  50. {
  51. return a.Method;
  52. }
  53. }
  54. }