Browse Source

Support having components in namespaces

JoshEngebretson 10 years ago
parent
commit
813dc95659

+ 116 - 89
Script/AtomicNET/AtomicNET/AtomicNETEngine/ComponentCore.cs

@@ -9,40 +9,40 @@ namespace AtomicEngine
 {
 {
 
 
 
 
-public partial class CSComponent : ScriptComponent
-{
-
-  public void StartInternal()
+  public partial class CSComponent : ScriptComponent
   {
   {
-    ApplyFieldValues();
-    Start();
-  }
 
 
-  public virtual void Start()
-  {
+    public void StartInternal()
+    {
+      ApplyFieldValues();
+      Start();
+    }
 
 
-  }
+    public virtual void Start()
+    {
 
 
+    }
 
 
-  public virtual void Update(float timeStep)
-  {
 
 
-  }
+    public virtual void Update(float timeStep)
+    {
+
+    }
 
 
 
 
 
 
-}
+  }
 
 
-public static class ComponentCore
-{
+  public static class ComponentCore
+  {
 
 
-  static List<CSComponent> startQueue = new List<CSComponent>();
+    static List<CSComponent> startQueue = new List<CSComponent>();
 
 
-  // holds a reference
-  static Dictionary<IntPtr, CSComponent> liveComponents = new Dictionary<IntPtr, CSComponent>();
+    // holds a reference
+    static Dictionary<IntPtr, CSComponent> liveComponents = new Dictionary<IntPtr, CSComponent>();
 
 
-  public static void Update (float timeStep)
-  {
+    public static void Update (float timeStep)
+    {
 
 
       List<CSComponent> _startQueue = new List<CSComponent>(startQueue);
       List<CSComponent> _startQueue = new List<CSComponent>(startQueue);
       startQueue.Clear();
       startQueue.Clear();
@@ -54,23 +54,23 @@ public static class ComponentCore
 
 
       foreach (var c in liveComponents.Values)
       foreach (var c in liveComponents.Values)
       {
       {
-            c.Update(timeStep);
+        c.Update(timeStep);
       }
       }
-  }
+    }
 
 
-  // This will need to be optimized
-  public static void CSComponentApplyFields(IntPtr componentPtr, IntPtr fieldMapPtr)
-  {
-    NETVariantMap fieldMap = NativeCore.WrapNative<NETVariantMap>(fieldMapPtr);;
-    CSComponent component = NativeCore.WrapNative<CSComponent>(componentPtr);;
+    // This will need to be optimized
+    public static void CSComponentApplyFields(IntPtr componentPtr, IntPtr fieldMapPtr)
+    {
+      NETVariantMap fieldMap = NativeCore.WrapNative<NETVariantMap>(fieldMapPtr);;
+      CSComponent component = NativeCore.WrapNative<CSComponent>(componentPtr);;
 
 
-    if (fieldMap == null || component == null)
-        return;
+      if (fieldMap == null || component == null)
+      return;
 
 
-    FieldInfo[] fields = componentClassFields[component.GetType()];
+      FieldInfo[] fields = componentClassFields[component.GetType()];
 
 
-    foreach (var field in fields)
-    {
+      foreach (var field in fields)
+      {
         if (fieldMap.Contains(field.Name))
         if (fieldMap.Contains(field.Name))
         {
         {
           //Console.WriteLine("Applying: {0} {1}", field.Name, field.FieldType.Name);
           //Console.WriteLine("Applying: {0} {1}", field.Name, field.FieldType.Name);
@@ -79,83 +79,110 @@ public static class ComponentCore
 
 
           if (fieldType.IsEnum)
           if (fieldType.IsEnum)
           {
           {
-              field.SetValue(component, fieldMap.GetInt(field.Name));
-              continue;
+            field.SetValue(component, fieldMap.GetInt(field.Name));
+            continue;
           }
           }
 
 
           switch (Type.GetTypeCode(fieldType))
           switch (Type.GetTypeCode(fieldType))
           {
           {
             case TypeCode.Boolean:
             case TypeCode.Boolean:
-                field.SetValue(component, fieldMap.GetBool(field.Name));
-                break;
-
-              case TypeCode.Int32:
-                  field.SetValue(component, fieldMap.GetInt(field.Name));
-                  break;
-
-              case TypeCode.Single:
-                  field.SetValue(component, fieldMap.GetFloat(field.Name));
-                  break;
-
-              case TypeCode.String:
-                  field.SetValue(component, fieldMap.GetString(field.Name));
-                  break;
-
-              default:
-
-                  if (fieldType == typeof(Vector3))
-                  {
-                    field.SetValue(component, fieldMap.GetVector3(field.Name));
-                  }
-                  else if (fieldType == typeof(Quaternion))
-                  {
-                    field.SetValue(component, fieldMap.GetQuaternion(field.Name));
-                  }
-                  else if (fieldType.IsSubclassOf(typeof(Resource)))
-                  {
-                      field.SetValue(component, fieldMap.GetResourceFromRef(field.Name));
-                  }
-                  else if (fieldType.IsSubclassOf(typeof(RefCounted)))
-                  {
-                      field.SetValue(component, fieldMap.GetPtr(field.Name));
-                  }
-
-                  break;
+            field.SetValue(component, fieldMap.GetBool(field.Name));
+            break;
+
+            case TypeCode.Int32:
+            field.SetValue(component, fieldMap.GetInt(field.Name));
+            break;
+
+            case TypeCode.Single:
+            field.SetValue(component, fieldMap.GetFloat(field.Name));
+            break;
+
+            case TypeCode.String:
+            field.SetValue(component, fieldMap.GetString(field.Name));
+            break;
+
+            default:
+
+            if (fieldType == typeof(Vector3))
+            {
+              field.SetValue(component, fieldMap.GetVector3(field.Name));
+            }
+            else if (fieldType == typeof(Quaternion))
+            {
+              field.SetValue(component, fieldMap.GetQuaternion(field.Name));
+            }
+            else if (fieldType.IsSubclassOf(typeof(Resource)))
+            {
+              field.SetValue(component, fieldMap.GetResourceFromRef(field.Name));
+            }
+            else if (fieldType.IsSubclassOf(typeof(RefCounted)))
+            {
+              field.SetValue(component, fieldMap.GetPtr(field.Name));
+            }
+
+            break;
           }
           }
         }
         }
+      }
     }
     }
-  }
 
 
-  static Dictionary<Type, FieldInfo[] > componentClassFields = new Dictionary<Type, FieldInfo[]>();
+    static Dictionary<Type, FieldInfo[] > componentClassFields = new Dictionary<Type, FieldInfo[]>();
 
 
-  public static IntPtr CSComponentCreate(string assemblyName, string classTypeName)
-  {
+    static Dictionary<string, Dictionary<string, Type>> componentTypeLookup = new Dictionary<string, Dictionary<string, Type>>();
 
 
-    Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
+    public static IntPtr CSComponentCreate(string assemblyName, string classTypeName)
+    {
 
 
-    Type type = assembly.GetType("AtomicNETTest."  + classTypeName);
+      Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
 
 
-    // TODO: check type is derived from CSComponent
+      string classNamespace = "";
+      if (!componentTypeLookup.ContainsKey(assemblyName))
+      {
+        componentTypeLookup[assemblyName] = new Dictionary<string, Type>();
+      }
 
 
-    var component = (CSComponent) Activator.CreateInstance(type);
+      var lookup = componentTypeLookup[assemblyName];
 
 
-    if (!componentClassFields.ContainsKey(type))
-    {
-      var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
-          .Where(field => field.IsDefined(typeof(InspectorAttribute), true));
+      Type type = null;
 
 
-      componentClassFields[type] = fields.ToArray<FieldInfo>();
+      if (!lookup.TryGetValue(classTypeName, out type))
+      {
+        var types = assembly.GetTypes();
+        foreach (var atype in types)
+        {
+          if (atype.Name == classTypeName)
+          {
+            lookup[classTypeName] = atype;
+            type = atype;
+            break;
+          }
+        }
+      }
 
 
-    }
+      if (type == null)
+      return IntPtr.Zero;
 
 
-    startQueue.Add(component);
+      // TODO: check type is derived from CSComponent
 
 
-    liveComponents[component.nativeInstance] = component;
+      var component = (CSComponent) Activator.CreateInstance(type);
 
 
-    return component.nativeInstance;
+      if (!componentClassFields.ContainsKey(type))
+      {
+        var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
+        .Where(field => field.IsDefined(typeof(InspectorAttribute), true));
 
 
-  }
+        componentClassFields[type] = fields.ToArray<FieldInfo>();
 
 
-}
+      }
+
+      startQueue.Add(component);
+
+      liveComponents[component.nativeInstance] = component;
+
+      return component.nativeInstance;
+
+    }
+
+  }
 
 
 }
 }

+ 3 - 0
Script/AtomicNET/AtomicNET/AtomicNETTools/AssemblyInspector.cs

@@ -163,6 +163,7 @@ namespace AtomicTools
 	internal class InspectorComponent
 	internal class InspectorComponent
 	{
 	{
 		public String Name;
 		public String Name;
+		public String Namespace;
 		public Dictionary<string, InspectorField> Fields = new Dictionary<string, InspectorField> ();
 		public Dictionary<string, InspectorField> Fields = new Dictionary<string, InspectorField> ();
 
 
 		public Dictionary<string, object> GetJSONDict ()
 		public Dictionary<string, object> GetJSONDict ()
@@ -170,6 +171,8 @@ namespace AtomicTools
 			var dict = new Dictionary<string,object> ();
 			var dict = new Dictionary<string,object> ();
 
 
 			dict ["name"] = Name;
 			dict ["name"] = Name;
+			dict ["namespace"] = Namespace;
+
 			var fieldList = new List<object> ();
 			var fieldList = new List<object> ();
 
 
 			foreach (var entry in Fields) {
 			foreach (var entry in Fields) {

+ 1 - 0
Script/AtomicNET/AtomicNET/AtomicNETTools/CSComponentInspector.cs

@@ -47,6 +47,7 @@ namespace AtomicTools
 
 
 			this._inspectorComponent = new InspectorComponent ();
 			this._inspectorComponent = new InspectorComponent ();
 			this._inspectorComponent.Name = metaReader.GetString (typeDef.Name);
 			this._inspectorComponent.Name = metaReader.GetString (typeDef.Name);
+			this._inspectorComponent.Namespace = metaReader.GetString (typeDef.Namespace);
 		}
 		}
 
 
 		// Inspect a CSComponent derived class
 		// Inspect a CSComponent derived class