private void GetBotActions(IMyBot bot, ActionCollection actions) { var methodInfos = bot.BotActions.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var methodInfo in methodInfos) { var attrs = methodInfo.GetCustomAttributes(true); for (int i = 0; i < attrs.Length; i++) { if (attrs[i] is MyBehaviorTreeActionAttribute) { MyBehaviorTreeActionAttribute btActionAttribute = (MyBehaviorTreeActionAttribute)attrs[i]; switch (btActionAttribute.ActionType) { case MyBehaviorTreeActionType.INIT: actions.AddInitAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null)); break; case MyBehaviorTreeActionType.BODY: actions.AddAction(btActionAttribute.ActionName, methodInfo, btActionAttribute.ReturnsRunning, (x, y) => (MyBehaviorTreeState)methodInfo.Invoke(x.BotActions, y)); break; case MyBehaviorTreeActionType.POST: actions.AddPostAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null)); break; } break; } } } }
private static void ExtractAction(ActionCollection actions, MethodInfo methodInfo) { MyBehaviorTreeActionAttribute customAttribute = methodInfo.GetCustomAttribute <MyBehaviorTreeActionAttribute>(); if (customAttribute != null) { switch (customAttribute.ActionType) { case MyBehaviorTreeActionType.INIT: actions.AddInitAction(customAttribute.ActionName, x => methodInfo.Invoke(x.BotActions, null)); return; case MyBehaviorTreeActionType.BODY: actions.AddAction(customAttribute.ActionName, methodInfo, customAttribute.ReturnsRunning, (x, y) => (MyBehaviorTreeState)methodInfo.Invoke(x.BotActions, y)); return; case MyBehaviorTreeActionType.POST: actions.AddPostAction(customAttribute.ActionName, x => methodInfo.Invoke(x.BotActions, null)); return; } } }
//private static ActionCollection CreateStaticActionCollection() //{ // var actions = new ActionCollection(); // var types = MyAIActionsParser.GetAllTypesFromAssemblies(); // foreach (var type in types) // { // var attr = type.GetCustomAttribute<MyBehaviorDescriptorAttribute>(); // if (!string.IsNullOrEmpty(attr.DescriptorCategory)) // continue; // var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); // foreach (var method in methods) // { // ExtractAction(actions, method); // } // } // return actions; //} private static void ExtractAction(ActionCollection actions, MethodInfo methodInfo) { var btActionAttribute = methodInfo.GetCustomAttribute<MyBehaviorTreeActionAttribute>(); if (btActionAttribute == null) return; switch (btActionAttribute.ActionType) { case MyBehaviorTreeActionType.INIT: actions.AddInitAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null)); break; case MyBehaviorTreeActionType.BODY: actions.AddAction(btActionAttribute.ActionName, methodInfo, btActionAttribute.ReturnsRunning, (x, y) => (MyBehaviorTreeState)methodInfo.Invoke(x.BotActions, y)); break; case MyBehaviorTreeActionType.POST: actions.AddPostAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null)); break; } }