public static void SubscribeToFailedCommands(CommandParent command, Type type) { Type commandEventType = typeof(CommandEvent); foreach (MethodInfo method in type.GetMethods()) { CommandFallbackAttribute?attr = method.GetCustomAttribute <CommandFallbackAttribute>(); if (attr is null || method.IsStatic) { continue; } ParameterInfo[] parameters = method.GetParameters(); // (CommandEvent invokation) if (parameters.Length != 1 || !(parameters.First().ParameterType == commandEventType || parameters.First().ParameterType.IsSubclassOf(commandEventType))) { continue; } command .FailedCommand .Where(failedCommand => failedCommand.FailType == attr.Type ) .Subscribe(failedCommand => method.Invoke(command, new object[] { failedCommand }) ); } }
// Use this for initialization void Start() { if (UILinkHandler != null) { UIHandler = UILinkHandler.GetComponent<UIScript>(); if (!UIHandler.hasStarted) { UIHandler.Start(); } } if (blueButtonItem != null) { blueButtonCommand = blueButtonItem.GetComponent<CommandParent>(); blueButtonCommand.LinkInput(Color.blue); if (UILinkHandler != null) UIHandler.linkButton(UIScript.UI_BUTTON.BUTTON_BLUE, blueButtonCommand); } if (redButtonItem != null) { redButtonCommand = redButtonItem.GetComponent<CommandParent>(); redButtonCommand.LinkInput(Color.red); if (UILinkHandler != null) UIHandler.linkButton(UIScript.UI_BUTTON.BUTTON_RED, redButtonCommand); } if (yellowButtonItem != null) { yellowButtonCommand = yellowButtonItem.GetComponent<CommandParent>(); yellowButtonCommand.LinkInput(Color.yellow); if (UILinkHandler != null) UIHandler.linkButton(UIScript.UI_BUTTON.BUTTON_YELLOW, yellowButtonCommand); } if (greenButtonItem != null) { greenButtonCommand = greenButtonItem.GetComponent<CommandParent>(); greenButtonCommand.LinkInput(Color.green); if (UILinkHandler != null) UIHandler.linkButton(UIScript.UI_BUTTON.BUTTON_GREEN, greenButtonCommand); } }
//ignoring the second parameter unlinks the given button public void linkButton(UI_BUTTON linkedButton, CommandParent linkedObject = null) { if (linkedObject == null) { switch (linkedButton) { case UI_BUTTON.BUTTON_RED: redLink = null; redButtonText.text = defaultText; break; case UI_BUTTON.BUTTON_GREEN: greenLink = null; greenButtonText.text = defaultText; break; case UI_BUTTON.BUTTON_BLUE: blueLink = null; blueButtonText.text = defaultText; break; case UI_BUTTON.BUTTON_YELLOW: yellowLink = null; yellowButtonText.text = defaultText; break; } } else { switch (linkedButton) { case UI_BUTTON.BUTTON_RED: redLink = linkedObject; redButtonText.text = linkedObject.actionDescription; break; case UI_BUTTON.BUTTON_GREEN: greenLink = linkedObject; greenButtonText.text = linkedObject.actionDescription; break; case UI_BUTTON.BUTTON_BLUE: blueLink = linkedObject; blueButtonText.text = linkedObject.actionDescription; break; case UI_BUTTON.BUTTON_YELLOW: yellowLink = linkedObject; yellowButtonText.text = linkedObject.actionDescription; break; } } }
// (CommandEvent invokation, string arg0, int arg1) //Arguments.Select((arg, argIndex) => arg.GetValueFrom(arguments, argIndex)); /// <summary> /// Invokes the command. /// </summary> /// <param name="parent">The parent command of this command</param> /// <param name="commandEvent">The command event that invoked the command</param> /// <param name="arguments">The arguments that have been used to invoke the command</param> /// <returns>Whether the command was properly invoked</returns> public Task InvokeAsync(CommandParent parent, CommandEvent commandEvent, IEnumerable <object?> arguments) => Task.Run(() => Member.Invoke(parent, new object[] { commandEvent }.Concat(arguments.ToArray()).ToArray()));