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.

60 lines
2.8KB

  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 commandCount = 0;
  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. // add to Gamecraft help command
  39. commandHelp.Invoke(null, new string[] { attr.Name, attr.Description });
  40. commandCount++;
  41. }
  42. }
  43. }
  44. enginesRoot.AddEngine(new UnregisterCommandEngine(contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters));
  45. Debug.Log($"Added {commandCount} custom commands in {engineCount} engines");
  46. }
  47. static MethodBase TargetMethod(HarmonyInstance instance)
  48. {
  49. return _ComposeMethodInfo(CommandLineCompositionRoot.Compose<CommandLineContext>);
  50. }
  51. private static MethodInfo _ComposeMethodInfo(Action<CommandLineContext, EnginesRoot, World, Action, MultiplayerInitParameters> a)
  52. {
  53. return a.Method;
  54. }
  55. }
  56. }