Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using Sirenix.Utilities.Editor;
  8. using Sirenix.OdinInspector.Editor;
  9. using BehaviorDesigner.Editor;
  10. using BehaviorDesigner.Runtime;
  11. using MyGame.Gameplay.AI.BehaviorTree;
  12.  
  13. namespace MyGame.Gameplay.Agents.Controllers.Editor {
  14.     [CustomEditor(typeof(NPCController))]
  15.     public class NPCControllerEditor : OdinEditor {
  16.         public override void OnInspectorGUI() {
  17.             base.OnInspectorGUI();
  18.  
  19.             if (!GUILayout.Button("Update Behavior Tree Mapped Properties", SirenixGUIStyles.Button)) { return; }
  20.  
  21.             NPCController npcController = (NPCController)target;
  22.             BehaviorTree[] behaviorTrees = npcController.GetComponents<BehaviorTree>();
  23.  
  24.             for (int i = 0; i < behaviorTrees.Length; i++) {
  25.                 Debug.LogWarningFormat("Updating Behavior Tree: {0}", i);
  26.  
  27.                 UpdateBehaviorTreeMappedProperties<NPCController>(npcController.gameObject, behaviorTrees[i]);
  28.             }
  29.  
  30.             EditorUtility.SetDirty(npcController.gameObject);
  31.         }
  32.  
  33.         static void UpdateBehaviorTreeMappedProperties<T>(GameObject gameObject, BehaviorTree behaviorTree) {
  34.             Type type = typeof(T);
  35.             PropertyInfo[] properties = type.GetProperties();
  36.             string propertyFormat = type.FullName + "/{0}";
  37.  
  38.             List<SharedVariable> btVariables = behaviorTree.GetAllVariables();
  39.             List<PropertyInfo> mappedProperties = new List<PropertyInfo>();
  40.             List<string> newPropertyNames = new List<string>();
  41.  
  42.             foreach (PropertyInfo property in properties) {
  43.                 MappedPropertyAttribute attribute = (MappedPropertyAttribute)property.GetCustomAttributes(typeof(MappedPropertyAttribute), false).FirstOrDefault();
  44.  
  45.                 if (attribute == null) { continue; }
  46.  
  47.                 SharedVariable sharedVariable = btVariables.FirstOrDefault(variable => variable.Name == property.Name);
  48.                 bool isNew = sharedVariable == null;
  49.  
  50.                 if (isNew) {
  51.                     if (!CreateSharedVariable(property, ref sharedVariable)) { continue; }
  52.  
  53.                     btVariables.Add(sharedVariable);
  54.                     newPropertyNames.Add(property.Name);
  55.                 } else {
  56.                     if (!IsSharedVariableForPropertyType(sharedVariable.GetType(), property.PropertyType)) {
  57.                         Debug.LogErrorFormat("Type mismatch on property {0}: MappedProperty type: {1}, SharedVariable type: {2}", property.Name, property.PropertyType, sharedVariable.GetType());
  58.  
  59.                         continue;
  60.                     }
  61.                 }
  62.  
  63.                 sharedVariable.PropertyMapping = string.Format(propertyFormat, property.Name);
  64.                 sharedVariable.PropertyMappingOwner = gameObject;
  65.  
  66.                 mappedProperties.Add(property);
  67.             }
  68.  
  69.             string[] btVariablesUnmapped = btVariables.Where(variable => variable.Name != "NPCController" && mappedProperties.All(mappedProperty => mappedProperty.Name != variable.Name)).Select(variable => variable.Name).ToArray();
  70.  
  71.             btVariables = btVariables.OrderBy(variable => variable.Name != "NPCController")
  72.                                      .ThenBy(variable => btVariablesUnmapped.Contains(variable.Name))
  73.                                      .ThenBy(variable => mappedProperties.FindIndex(mappedProperty => mappedProperty.Name == variable.Name))
  74.                                      .ToList();
  75.  
  76.             SerializeBehaviorTree(behaviorTree, btVariables);
  77.  
  78.             if (newPropertyNames.Count > 0) { Debug.LogFormat("{0} new properties created: {1}", newPropertyNames.Count, string.Join(", ", newPropertyNames.ToArray())); }
  79.             if (btVariablesUnmapped.Length > 0) { Debug.LogFormat("Unmapped variables: {0}", string.Join(", ", btVariablesUnmapped)); }
  80.         }
  81.  
  82.         static bool CreateSharedVariable(PropertyInfo property, ref SharedVariable sharedVariable) {
  83.             Type sharedVariableType = GetSharedVariableForPropertyType(property.PropertyType);
  84.  
  85.             if (sharedVariableType == null) {
  86.                 Debug.LogErrorFormat("Cannot find SharedVariable class for {0} (property type: {1})", property.Name, property.PropertyType);
  87.  
  88.                 return false;
  89.             }
  90.  
  91.             sharedVariable = (SharedVariable)Activator.CreateInstance(sharedVariableType);
  92.             sharedVariable.IsShared = true;
  93.             sharedVariable.IsGlobal = false;
  94.             sharedVariable.IsDynamic = false;
  95.             sharedVariable.Name = property.Name;
  96.  
  97.             return true;
  98.         }
  99.  
  100.         static Type GetSharedVariableForPropertyType(Type propertyType) {
  101.             Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  102.  
  103.             return assemblies.Select(assembly => assembly.GetTypes().FirstOrDefault(type => IsSharedVariableForPropertyType(type, propertyType)))
  104.                              .FirstOrDefault(sharedType => sharedType != null);
  105.         }
  106.  
  107.         static bool IsSharedVariableForPropertyType(Type type, Type propertyType) {
  108.             Type baseType = type.BaseType;
  109.  
  110.             return baseType != null &&
  111.                    baseType.IsGenericType &&
  112.                    baseType.GetGenericTypeDefinition() == typeof(SharedVariable<>) &&
  113.                    baseType.GetGenericArguments()[0] == propertyType;
  114.         }
  115.  
  116.         static void SerializeBehaviorTree(BehaviorTree behaviorTree, List<SharedVariable> variables) {
  117.             BehaviorSource behaviorSource = behaviorTree.GetBehaviorSource();
  118.             behaviorSource.SetAllVariables(variables);
  119.  
  120.             if (BehaviorDesignerPreferences.GetBool(BDPreferences.BinarySerialization)) {
  121.                 BinarySerialization.Save(behaviorSource);
  122.             } else {
  123.                 JSONSerialization.Save(behaviorSource);
  124.             }
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement