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.

56 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 Svelto.ECS;
  10. using Svelto.Context;
  11. namespace ExtraCommands
  12. {
  13. [HarmonyPatch]
  14. class CommandLineCompositionRootSaveCommandPatch
  15. {
  16. static void Postfix(UnityContext<FullGameCompositionRoot> contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters)
  17. {
  18. int engineCount = 0;
  19. MethodInfo commandHelp = Harmony.AccessTools.Method(Harmony.AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility"), "SaveCommandHelp", new Type[] { typeof(string), typeof(string) });
  20. foreach (Type t in typeof(CustomCommandEngine).Assembly.GetTypes())
  21. {
  22. CustomCommandAttribute[] attributes = (CustomCommandAttribute[])t.GetCustomAttributes(typeof(CustomCommandAttribute), false);
  23. CustomCommandEngine inst = null;
  24. foreach (CustomCommandAttribute attr in attributes)
  25. {
  26. if (attr != null && t.IsSubclassOf(typeof(CustomCommandEngine)))
  27. {
  28. if (inst == null)
  29. {
  30. // create instance by getting the constructor through reflection
  31. inst = (CustomCommandEngine)t.GetConstructor(new Type[] { typeof(UnityContext<FullGameCompositionRoot>), typeof(EnginesRoot), typeof(World), typeof(Action), typeof(MultiplayerInitParameters) })
  32. .Invoke(new object[] { contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters });
  33. // add to engineRoot
  34. enginesRoot.AddEngine(inst);
  35. engineCount++;
  36. }
  37. }
  38. }
  39. }
  40. // enginesRoot.AddEngine(new UnregisterCommandEngine(contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters));
  41. Debug.Log($"Added {engineCount} custom command engines");
  42. }
  43. static MethodBase TargetMethod(HarmonyInstance instance)
  44. {
  45. return _ComposeMethodInfo(CommandLineCompositionRoot.Compose<UnityContext<FullGameCompositionRoot>>);
  46. }
  47. private static MethodInfo _ComposeMethodInfo(Action<UnityContext<FullGameCompositionRoot>, EnginesRoot, World, Action, MultiplayerInitParameters> a)
  48. {
  49. return a.Method;
  50. }
  51. }
  52. }