Browse Source

Merge pull request #986 from LumaDigital/MB_PortCPPToCS

CSComponent work and sender specific events
JoshEngebretson 9 years ago
parent
commit
6c24bfdee1

+ 14 - 1
Script/AtomicEditor/ui/frames/inspector/SelectionInspector.ts

@@ -168,6 +168,8 @@ class CSComponentSection extends ComponentSection {
 
 
         super(editType, inspector);
         super(editType, inspector);
 
 
+        this.updateTextFromClassAttr();
+
         this.hasDynamicAttr = true;
         this.hasDynamicAttr = true;
 
 
         this.subscribeToEvent(this, "AttributeEditResourceChanged", (ev) => this.handleAttributeEditResourceChanged(ev));
         this.subscribeToEvent(this, "AttributeEditResourceChanged", (ev) => this.handleAttributeEditResourceChanged(ev));
@@ -187,7 +189,7 @@ class CSComponentSection extends ComponentSection {
 
 
           var attrInfos = csc.getAttributes();
           var attrInfos = csc.getAttributes();
           this.updateDynamicAttrInfos(attrInfos);
           this.updateDynamicAttrInfos(attrInfos);
-
+          this.updateTextFromClassAttr();
         }
         }
 
 
     }
     }
@@ -205,6 +207,17 @@ class CSComponentSection extends ComponentSection {
 
 
     }
     }
 
 
+    private updateTextFromClassAttr() {
+        this.text = this.editType.typeName;
+
+        var object = this.editType.getFirstObject();
+        if (object) {
+            var value = object.getAttribute("Class");
+            if (value)
+                this.text = value;
+        }
+    }
+
 }
 }
 
 
 // Node Inspector + Component Inspectors
 // Node Inspector + Component Inspectors

+ 0 - 4
Script/AtomicNET/AtomicNET/Application/NETAtomicPlayer.cs

@@ -1,7 +1,3 @@
-using System;
-using System.Reflection;
-
-
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
     public partial class NETAtomicPlayer : PlayerApp
     public partial class NETAtomicPlayer : PlayerApp

+ 0 - 3
Script/AtomicNET/AtomicNET/Application/NETIPCPlayerApp.cs

@@ -1,6 +1,3 @@
-using System;
-using System.Reflection;
-
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
     public partial class NETIPCPlayerApp : IPCPlayerApp
     public partial class NETIPCPlayerApp : IPCPlayerApp

+ 0 - 4
Script/AtomicNET/AtomicNET/Application/NETServiceApplication.cs

@@ -1,7 +1,3 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
 
 

+ 99 - 2
Script/AtomicNET/AtomicNET/Core/AObject.cs

@@ -7,7 +7,32 @@ namespace AtomicEngine
 
 
     public partial class AObject : RefCounted
     public partial class AObject : RefCounted
     {
     {
-        internal Dictionary<uint, EventDelegate> eventHandlers = new Dictionary<uint, EventDelegate>();
+        private Dictionary<uint, EventDelegate> EventHandlers
+        {
+            get
+            {
+                if (eventHandlers == null)
+                {
+                    eventHandlers = new Dictionary<uint, EventDelegate>();
+                }
+                return eventHandlers;
+            }
+        }
+        private Dictionary<uint, EventDelegate> eventHandlers;
+
+        private Dictionary<SenderEventKey, SenderEventDelegate> SenderEventHandlers
+        {
+            get
+            {
+                if (senderEventHandlers == null)
+                {
+                    SenderEventKeyComparer comparer = SenderEventKeyComparer.Default;
+                    senderEventHandlers = new Dictionary<SenderEventKey, SenderEventDelegate>(comparer);
+                }
+                return senderEventHandlers;
+            }
+        }
+        private Dictionary<SenderEventKey, SenderEventDelegate> senderEventHandlers;
 
 
         public static T GetSubsystem<T>() where T : AObject
         public static T GetSubsystem<T>() where T : AObject
         {
         {
@@ -19,10 +44,16 @@ namespace AtomicEngine
             eventHandlers[eventType](eventType, eventData);
             eventHandlers[eventType](eventType, eventData);
         }
         }
 
 
+        internal void HandleEvent(AObject sender, uint eventType, ScriptVariantMap eventData)
+        {
+            var key = new SenderEventKey(eventType, sender.nativeInstance);
+            senderEventHandlers[key](sender, eventType, eventData);
+        }
+
         public void SubscribeToEvent(uint eventType, EventDelegate eventDelegate)
         public void SubscribeToEvent(uint eventType, EventDelegate eventDelegate)
         {
         {
             NETCore.RegisterNETEventType(eventType);
             NETCore.RegisterNETEventType(eventType);
-            eventHandlers[eventType] = eventDelegate;
+            EventHandlers[eventType] = eventDelegate;
             NativeCore.SubscribeToEvent(this, eventType);
             NativeCore.SubscribeToEvent(this, eventType);
         }
         }
 
 
@@ -31,6 +62,37 @@ namespace AtomicEngine
             SubscribeToEvent(AtomicNET.StringToStringHash(eventType), eventDelegate);
             SubscribeToEvent(AtomicNET.StringToStringHash(eventType), eventDelegate);
         }
         }
 
 
+        public void UnsubscribeFromEvent(uint eventType)
+        {
+            NativeCore.UnsubscribeFromEvent(this, eventType);
+            EventHandlers.Remove(eventType);
+        }
+
+        public void SubscribeToEvent(AObject sender, uint eventType, SenderEventDelegate eventDelegate)
+        {
+            if (sender == null)
+            {
+                throw new InvalidOperationException("AObject.SubscribeToEvent - trying to subscribe to events from a null object");
+            }
+
+            NETCore.RegisterNETEventType(eventType);
+            var key = new SenderEventKey(eventType, sender.nativeInstance);
+            SenderEventHandlers[key] = eventDelegate;
+            NativeCore.SubscribeToEvent(this, sender, eventType);
+        }
+
+        public void SubscribeToEvent(AObject sender, string eventType, SenderEventDelegate eventDelegate)
+        {
+            SubscribeToEvent(sender, AtomicNET.StringToStringHash(eventType), eventDelegate);
+        }
+
+        public void UnsubscribeFromEvent(AObject sender, uint eventType)
+        {
+            NativeCore.UnsubscribeFromEvent(this, sender, eventType);
+            var key = new SenderEventKey(eventType, sender.nativeInstance);
+            SenderEventHandlers.Remove(key);
+        }
+
         public void SendEvent(string eventType, ScriptVariantMap eventData = null)
         public void SendEvent(string eventType, ScriptVariantMap eventData = null)
         {
         {
 
 
@@ -41,7 +103,42 @@ namespace AtomicEngine
         [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
         [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
         private static extern void csi_Atomic_AObject_SendEvent(IntPtr self, string eventType, IntPtr variantMap);
         private static extern void csi_Atomic_AObject_SendEvent(IntPtr self, string eventType, IntPtr variantMap);
 
 
+        private struct SenderEventKey
+        {
+            public readonly uint EventType;
+            public readonly IntPtr Sender; 
 
 
+            public SenderEventKey(uint eventType, IntPtr sender)
+            {
+                EventType = eventType;
+                Sender = sender;
+            }
+        }
+
+        // Use a comparer to avoid boxing on struct .Equals override
+        private class SenderEventKeyComparer : IEqualityComparer<SenderEventKey>
+        {
+            public static readonly SenderEventKeyComparer Default = new SenderEventKeyComparer();
+
+            private SenderEventKeyComparer() { }
+
+            public bool Equals(SenderEventKey lhs, SenderEventKey rhs)
+            {
+                return (lhs.EventType == rhs.EventType &&
+                    lhs.Sender == rhs.Sender);
+            }
+
+            public int GetHashCode(SenderEventKey key)
+            {
+                unchecked
+                {
+                    int hash = 17;
+                    hash = hash * 23 + key.EventType.GetHashCode();
+                    hash = hash * 23 + key.Sender.GetHashCode();
+                    return hash;
+                }
+            }
+        }
     }
     }
 
 
 }
 }

+ 0 - 4
Script/AtomicNET/AtomicNET/Core/Constants.cs

@@ -1,7 +1,3 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
 
 

+ 118 - 35
Script/AtomicNET/AtomicNET/Core/NativeCore.cs

@@ -32,19 +32,22 @@ namespace AtomicEngine
 
 
         static internal void SubscribeToEvent(AObject receiver, uint eventType)
         static internal void SubscribeToEvent(AObject receiver, uint eventType)
         {
         {
-            List<WeakReference<AObject>> eventReceivers;
+            SubscribeToEvent(receiver, null, eventType);
+        }
+
+        static internal void SubscribeToEvent(AObject receiver, AObject sender, uint eventType)
+        {
+            List<EventSubscription> eventReceivers;
 
 
             if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
             if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
             {
             {
-                eventReceivers = eventReceiverLookup[eventType] = new List<WeakReference<AObject>>();
+                eventReceivers = eventReceiverLookup[eventType] = new List<EventSubscription>();
             }
             }
 
 
             AObject obj;
             AObject obj;
-
-            foreach (WeakReference<AObject> wr in eventReceivers)
+            foreach (EventSubscription er in eventReceivers)
             {
             {
-
-                if (!wr.TryGetTarget(out obj))
+                if (!er.Receiver.TryGetTarget(out obj))
                     continue; // GC'd
                     continue; // GC'd
 
 
                 // already on list?
                 // already on list?
@@ -53,20 +56,59 @@ namespace AtomicEngine
             }
             }
 
 
             WeakReference<RefCounted> w = null;
             WeakReference<RefCounted> w = null;
-
             if (!nativeLookup.TryGetValue(receiver.nativeInstance, out w))
             if (!nativeLookup.TryGetValue(receiver.nativeInstance, out w))
             {
             {
-                throw new System.InvalidOperationException("NativeCore.SubscribeToEvent - unable to find native instance");
+                throw new InvalidOperationException("NativeCore.SubscribeToEvent - unable to find native receiver instance");
             }
             }
 
 
             RefCounted refcounted;
             RefCounted refcounted;
-
             if (!w.TryGetTarget(out refcounted))
             if (!w.TryGetTarget(out refcounted))
             {
             {
-                throw new System.InvalidOperationException("NativeCore.SubscribeToEvent - attempting to subscribe a GC'd AObject");
+                throw new InvalidOperationException("NativeCore.SubscribeToEvent - attempting to subscribe a GC'd AObject");
+            }
+
+            IntPtr nativeSender = IntPtr.Zero;
+            if (sender != null)
+            {
+                nativeSender = sender.nativeInstance;
+                if (!nativeLookup.TryGetValue(sender.nativeInstance, out w))
+                {
+                    throw new InvalidOperationException("NativeCore.SubscribeToEvent - unable to find native sender instance");
+                }
+
+                if (!w.TryGetTarget(out refcounted))
+                {
+                    throw new InvalidOperationException("NativeCore.SubscribeToEvent - attempting to subscribe to a GC'd AObject");
+                }
             }
             }
 
 
-            eventReceivers.Add(new WeakReference<AObject>(receiver));
+            eventReceivers.Add(new EventSubscription(receiver, nativeSender));
+        }
+
+        static internal void UnsubscribeFromEvent(AObject receiver, uint eventType)
+        {
+            UnsubscribeFromEvent(receiver, null, eventType);
+        }
+
+        static internal void UnsubscribeFromEvent(AObject receiver, RefCounted sender, uint eventType)
+        {
+            List<EventSubscription> eventReceivers;
+            if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
+                return;
+
+            AObject obj;
+            foreach (EventSubscription er in eventReceivers)
+            {
+                if (!er.Receiver.TryGetTarget(out obj))
+                    continue; // GC'd
+
+                if (obj == receiver &&
+                    (sender == null || er.Sender == sender.nativeInstance))
+                {
+                    eventReceivers.Remove(er);
+                    return;
+                }
+            }
         }
         }
 
 
         static ScriptVariantMap[] svm;
         static ScriptVariantMap[] svm;
@@ -81,39 +123,57 @@ namespace AtomicEngine
                 svm[i] = new ScriptVariantMap();
                 svm[i] = new ScriptVariantMap();
         }
         }
 
 
-        public static void EventDispatch(uint eventType, IntPtr eventData)
+        public static void EventDispatch(IntPtr sender, uint eventType, IntPtr eventData)
         {
         {
-            List<WeakReference<AObject>> eventReceivers;
+            List<EventSubscription> eventReceivers;
 
 
             if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
             if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
             {
             {
                 // This should not happen, as event NET objects are subscribed to are filtered 
                 // This should not happen, as event NET objects are subscribed to are filtered 
-                throw new System.InvalidOperationException("NativeCore.EventDispatch - received unregistered event type");
+                throw new InvalidOperationException("NativeCore.EventDispatch - received unregistered event type");
 
 
             }
             }
 
 
-            ScriptVariantMap scriptMap = null;
-            AObject receiver;
+            AObject managedSender = null;
+            WeakReference<RefCounted> wr;
+            if (sender != IntPtr.Zero &&
+                nativeLookup.TryGetValue(sender, out wr))
+            {
+                RefCounted refCounted;
+                if (wr.TryGetTarget(out refCounted))
+                {
+                    managedSender = refCounted as AObject;
+                }
+            }
 
 
             // iterate over copy of list so we can modify it while running
             // iterate over copy of list so we can modify it while running
-            foreach (var w in eventReceivers.ToList())
+            ScriptVariantMap scriptMap = null;
+            AObject receiver;
+            foreach (EventSubscription er in eventReceivers.ToList())
             {
             {
                 // GC'd?
                 // GC'd?
-                if (!w.TryGetTarget(out receiver))
+                if (!er.Receiver.TryGetTarget(out receiver))
                     continue;
                     continue;
 
 
                 if (scriptMap == null)
                 if (scriptMap == null)
                 {
                 {
                     if (svmDepth == svmMax)
                     if (svmDepth == svmMax)
                     {
                     {
-                        throw new System.InvalidOperationException("NativeCore.EventDispatch - exceeded max svm");
+                        throw new InvalidOperationException("NativeCore.EventDispatch - exceeded max svm");
                     }
                     }
 
 
                     scriptMap = svm[svmDepth++];
                     scriptMap = svm[svmDepth++];
                     scriptMap.CopyVariantMap(eventData);
                     scriptMap.CopyVariantMap(eventData);
                 }
                 }
 
 
-                receiver.HandleEvent(eventType, scriptMap);
+                if (managedSender != null && er.Sender == sender)
+                {
+                    receiver.HandleEvent(managedSender, eventType, scriptMap);
+                }
+                else
+                {
+                    receiver.HandleEvent(eventType, scriptMap);
+                }
             }
             }
 
 
             if (scriptMap != null)
             if (scriptMap != null)
@@ -128,19 +188,19 @@ namespace AtomicEngine
 
 
             // expire event listeners
             // expire event listeners
 
 
-            int eventListenersRemoved = 0;
-            int nativesRemoved = 0;
+            //int eventListenersRemoved = 0;
+            //int nativesRemoved = 0;
 
 
             AObject obj;
             AObject obj;
 
 
-            foreach (List<WeakReference<AObject>> receiverList in eventReceiverLookup.Values)
+            foreach (List<EventSubscription> receiverList in eventReceiverLookup.Values)
             {
             {
-                foreach (WeakReference<AObject> receiver in receiverList.ToList())
+                foreach (EventSubscription er in receiverList.ToList())
                 {
                 {
-                    if (!receiver.TryGetTarget(out obj))
+                    if (!er.Receiver.TryGetTarget(out obj))
                     {
                     {
-                        receiverList.Remove(receiver);
-                        eventListenersRemoved++;
+                        receiverList.Remove(er);
+                        //eventListenersRemoved++;
                     }
                     }
 
 
                     if (watch.ElapsedMilliseconds > 16)
                     if (watch.ElapsedMilliseconds > 16)
@@ -163,7 +223,7 @@ namespace AtomicEngine
                     // expired
                     // expired
                     csi_AtomicEngine_ReleaseRef(native);
                     csi_AtomicEngine_ReleaseRef(native);
                     nativeLookup.Remove(native);
                     nativeLookup.Remove(native);
-                    nativesRemoved++;
+                    //nativesRemoved++;
                 }
                 }
 
 
                 if (watch.ElapsedMilliseconds > 16)
                 if (watch.ElapsedMilliseconds > 16)
@@ -197,7 +257,7 @@ namespace AtomicEngine
         {
         {
             if (nativeLookup.ContainsKey(native))
             if (nativeLookup.ContainsKey(native))
             {
             {
-                throw new System.InvalidOperationException("NativeCore.RegisterNative - Duplicate IntPtr key");
+                throw new InvalidOperationException("NativeCore.RegisterNative - Duplicate IntPtr key");
             }
             }
 
 
             r.nativeInstance = native;
             r.nativeInstance = native;
@@ -253,7 +313,7 @@ namespace AtomicEngine
 
 
             if (!nativeClassIDToNativeType.TryGetValue(classID, out nativeType))
             if (!nativeClassIDToNativeType.TryGetValue(classID, out nativeType))
             {
             {
-                throw new System.InvalidOperationException("NativeCore.WrapNative - Attempting to wrap unknown native class id");
+                throw new InvalidOperationException("NativeCore.WrapNative - Attempting to wrap unknown native class id");
             }
             }
 
 
             r = nativeType.managedConstructor(native);
             r = nativeType.managedConstructor(native);
@@ -277,11 +337,11 @@ namespace AtomicEngine
         {
         {
             if (nativeClassIDToNativeType.ContainsKey(nativeType.nativeClassID))
             if (nativeClassIDToNativeType.ContainsKey(nativeType.nativeClassID))
             {
             {
-                throw new System.InvalidOperationException("NativeCore.RegisterNativeType - Duplicate NativeType class id registered");
+                throw new InvalidOperationException("NativeCore.RegisterNativeType - Duplicate NativeType class id registered");
             }
             }
             if (typeToNativeType.ContainsKey(nativeType.type))
             if (typeToNativeType.ContainsKey(nativeType.type))
             {
             {
-                throw new System.InvalidOperationException("NativeCore.RegisterNativeType - Duplicate NativeType type registered");
+                throw new InvalidOperationException("NativeCore.RegisterNativeType - Duplicate NativeType type registered");
             }
             }
 
 
             nativeClassIDToNativeType[nativeType.nativeClassID] = nativeType;
             nativeClassIDToNativeType[nativeType.nativeClassID] = nativeType;
@@ -297,6 +357,17 @@ namespace AtomicEngine
             return false;
             return false;
         }
         }
 
 
+        public static Type GetNativeAncestorType(Type type)
+        {
+            Type ancestorType = type;
+            do
+            {
+                ancestorType = ancestorType.BaseType;
+            } while (ancestorType != null && !IsNativeType(ancestorType));
+
+            return ancestorType;
+        }
+
         static public IntPtr NativeContructorOverride
         static public IntPtr NativeContructorOverride
         {
         {
             get
             get
@@ -310,7 +381,7 @@ namespace AtomicEngine
             {
             {
                 if (nativeContructorOverride != IntPtr.Zero)
                 if (nativeContructorOverride != IntPtr.Zero)
                 {
                 {
-                    throw new System.InvalidOperationException("NativeCore.NativeContructorOverride - Previous nativeContructorOverride not consumed");
+                    throw new InvalidOperationException("NativeCore.NativeContructorOverride - Previous nativeContructorOverride not consumed");
                 }
                 }
 
 
                 nativeContructorOverride = value;
                 nativeContructorOverride = value;
@@ -321,7 +392,7 @@ namespace AtomicEngine
         {
         {
             if (nativeContructorOverride != IntPtr.Zero)
             if (nativeContructorOverride != IntPtr.Zero)
             {
             {
-                throw new System.InvalidOperationException("NativeCore.VerifyNativeContructorOverrideConsumed -  NativeContructorOverride not consumed");
+                throw new InvalidOperationException("NativeCore.VerifyNativeContructorOverrideConsumed -  NativeContructorOverride not consumed");
             }
             }
         }
         }
 
 
@@ -332,7 +403,7 @@ namespace AtomicEngine
         internal static Dictionary<IntPtr, WeakReference<RefCounted>> nativeLookup = new Dictionary<IntPtr, WeakReference<RefCounted>>();
         internal static Dictionary<IntPtr, WeakReference<RefCounted>> nativeLookup = new Dictionary<IntPtr, WeakReference<RefCounted>>();
 
 
         // weak references here, hold a ref native side
         // weak references here, hold a ref native side
-        internal static Dictionary<uint, List<WeakReference<AObject>>> eventReceiverLookup = new Dictionary<uint, List<WeakReference<AObject>>>();
+        internal static Dictionary<uint, List<EventSubscription>> eventReceiverLookup = new Dictionary<uint, List<EventSubscription>>();
 
 
 
 
         // Native ClassID to NativeType lookup
         // Native ClassID to NativeType lookup
@@ -345,6 +416,18 @@ namespace AtomicEngine
         [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
         [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
         private static extern void csi_AtomicEngine_ReleaseRef(IntPtr refCounted);
         private static extern void csi_AtomicEngine_ReleaseRef(IntPtr refCounted);
 
 
+        internal struct EventSubscription
+        {
+            public WeakReference<AObject> Receiver;
+            public IntPtr Sender;
+
+            public EventSubscription(AObject obj, IntPtr source)
+            {
+                Receiver = new WeakReference<AObject>(obj);
+                Sender = source;
+            }
+        }
+
     }
     }
 
 
 
 

+ 3 - 2
Script/AtomicNET/AtomicNET/Core/NativeEvents.cs

@@ -1,17 +1,18 @@
 using System;
 using System;
-using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-    public delegate void EventDispatchDelegate(uint eventType, IntPtr eventData);
+    public delegate void EventDispatchDelegate(IntPtr sender, uint eventType, IntPtr eventData);
 
 
     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     public delegate void UpdateDispatchDelegate(float timeStep);
     public delegate void UpdateDispatchDelegate(float timeStep);
 
 
     public delegate void EventDelegate(uint eventType, ScriptVariantMap eventData);
     public delegate void EventDelegate(uint eventType, ScriptVariantMap eventData);
 
 
+    public delegate void SenderEventDelegate(AObject sender, uint eventType, ScriptVariantMap eventData);
+
     public delegate void HandleUpdateDelegate(float timeStep);
     public delegate void HandleUpdateDelegate(float timeStep);
 
 
     [StructLayout(LayoutKind.Sequential)]
     [StructLayout(LayoutKind.Sequential)]

+ 0 - 1
Script/AtomicNET/AtomicNET/Core/RefCounted.cs

@@ -1,5 +1,4 @@
 using System;
 using System;
-using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine

+ 0 - 5
Script/AtomicNET/AtomicNET/Graphics/Viewport.cs

@@ -1,8 +1,3 @@
-
-
-using System;
-using System.Runtime.InteropServices;
-
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
     public partial class Viewport : AObject
     public partial class Viewport : AObject

+ 0 - 1
Script/AtomicNET/AtomicNET/IPC/IPC.cs

@@ -1,5 +1,4 @@
 using System;
 using System;
-using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine

+ 0 - 1
Script/AtomicNET/AtomicNET/Math/IntRect.cs

@@ -1,4 +1,3 @@
-using System;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine

+ 0 - 2
Script/AtomicNET/AtomicNET/Math/MathHelper.cs

@@ -9,8 +9,6 @@
 #endregion
 #endregion
 
 
 using System;
 using System;
-using System.Collections.Generic;
-using System.Text;
 
 
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {

+ 0 - 1
Script/AtomicNET/AtomicNET/Math/Rect.cs

@@ -1,4 +1,3 @@
-using System;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine

+ 0 - 4
Script/AtomicNET/AtomicNET/Resource/ResourceCache.cs

@@ -1,7 +1,3 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
 
 

+ 0 - 4
Script/AtomicNET/AtomicNET/Scene/CSComponent.cs

@@ -1,7 +1,3 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
 
 

+ 2 - 3
Script/AtomicNET/AtomicNET/Scene/InspectorAttribute.cs

@@ -1,17 +1,16 @@
 using System;
 using System;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
 
 
+    [AttributeUsage(AttributeTargets.Field)]
     public class InspectorAttribute : Attribute
     public class InspectorAttribute : Attribute
     {
     {
         public InspectorAttribute(string DefaultValue = "")
         public InspectorAttribute(string DefaultValue = "")
         {
         {
         }
         }
 
 
-        public string DefaultValue;
+        public readonly string DefaultValue;
     }
     }
 
 
 }
 }

+ 0 - 1
Script/AtomicNET/AtomicNET/Script/ScriptVariantMap.cs

@@ -1,7 +1,6 @@
 
 
 
 
 using System;
 using System;
-using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine

+ 161 - 136
Script/AtomicNET/AtomicNETService/AssemblyInspector.cs

@@ -17,210 +17,235 @@ using File = System.IO.File;
 namespace AtomicTools
 namespace AtomicTools
 {
 {
 
 
-	class AssemblyInspector
-	{
+    class AssemblyInspector
+    {
 
 
-		Dictionary<string, InspectorEnum> InspectorEnums = new Dictionary<string, InspectorEnum> ();
-		Dictionary<string, InspectorComponent> InspectorComponents = new Dictionary<string, InspectorComponent> ();
+        Dictionary<string, InspectorEnum> InspectorEnums = new Dictionary<string, InspectorEnum>();
+        Dictionary<string, InspectorComponent> InspectorComponents = new Dictionary<string, InspectorComponent>();
 
 
-		PEReader peFile;
-		MetadataReader metaReader;
+        PEReader peFile;
+        MetadataReader metaReader;
 
 
-		public AssemblyInspector ()
-		{
+        public AssemblyInspector()
+        {
 
 
-		}
+        }
 
 
-		public string DumpToJSON ()
-		{
-			var dict = new Dictionary<string, object> ();
+        public string DumpToJSON()
+        {
+            var dict = new Dictionary<string, object>();
 
 
-			var enumList = new List<object> ();
-			var componentList = new List<object> ();
+            var enumList = new List<object>();
+            var componentList = new List<object>();
 
 
-			foreach (var entry in InspectorEnums) {
-				enumList.Add (entry.Value.GetJSONDict ());
-			}
+            foreach (var entry in InspectorEnums)
+            {
+                enumList.Add(entry.Value.GetJSONDict());
+            }
 
 
-			foreach (var entry in InspectorComponents) {
-				componentList.Add (entry.Value.GetJSONDict ());
-			}
+            foreach (var entry in InspectorComponents)
+            {
+                componentList.Add(entry.Value.GetJSONDict());
+            }
 
 
-			dict ["enums"] = enumList;
-			dict ["components"] = componentList;
+            dict["enums"] = enumList;
+            dict["components"] = componentList;
 
 
-			return MiniJSON.Json.Serialize (dict);
+            return MiniJSON.Json.Serialize(dict);
 
 
-		}
+        }
 
 
-		public void Inspect (String pathToAssembly)
-		{
+        public void Inspect(String pathToAssembly)
+        {
 
 
-			using (var stream = File.OpenRead (pathToAssembly))
-			using (peFile = new PEReader (stream)) {
+            using (var stream = File.OpenRead(pathToAssembly))
+            using (peFile = new PEReader(stream))
+            {
 
 
-				metaReader = peFile.GetMetadataReader ();
+                metaReader = peFile.GetMetadataReader();
 
 
-				ParseEnums ();
-				ParseComponents ();
-			}
+                ParseEnums();
+                ParseComponents();
+            }
 
 
-		}
+        }
 
 
-		void ParseComponents ()
-		{
+        void ParseComponents()
+        {
 
 
-			foreach (var handle in metaReader.TypeDefinitions) {
+            foreach (var handle in metaReader.TypeDefinitions)
+            {
 
 
-				var typeDef = metaReader.GetTypeDefinition (handle);
+                var typeDef = metaReader.GetTypeDefinition(handle);
 
 
-				var baseTypeHandle = typeDef.BaseType;
+                var parentName = metaReader.GetString(typeDef.Name);
 
 
-				if (baseTypeHandle.Kind == HandleKind.TypeReference) {
+                var baseTypeHandle = typeDef.BaseType;
 
 
-					var typeRef = metaReader.GetTypeReference ((TypeReferenceHandle)baseTypeHandle);
+                // Work up to base reference (ie defined outside this assembly)
+                while (baseTypeHandle.Kind == HandleKind.TypeDefinition)
+                {
+                    var baseTypeDef = metaReader.GetTypeDefinition((TypeDefinitionHandle)baseTypeHandle);
+                    // No way to predetermine if .BaseType is valid
+                    try
+                    {
+                        baseTypeHandle = baseTypeDef.BaseType;
+                    }
+                    catch (Exception) { break; }
+                }
 
 
-                    var name = metaReader.GetString (typeRef.Name); 
+                if (baseTypeHandle.Kind == HandleKind.TypeReference)
+                {
 
 
-					if (name != "CSComponent")
-						continue;
+                    var typeRef = metaReader.GetTypeReference((TypeReferenceHandle)baseTypeHandle);
 
 
-					var inspector = new CSComponentInspector (typeDef, peFile, metaReader);
+                    var name = metaReader.GetString(typeRef.Name);
 
 
-					var icomponent = inspector.Inspect ();
+                    if (name != "CSComponent")
+                        continue;
 
 
-					if (icomponent != null)
-						InspectorComponents [icomponent.Name] = icomponent;
-				}
-			}
-		}
+                    var inspector = new CSComponentInspector(typeDef, peFile, metaReader);
 
 
+                    var icomponent = inspector.Inspect();
 
 
-		void ParseEnums ()
-		{
-			foreach (var handle in metaReader.TypeDefinitions) {
+                    if (icomponent != null)
+                        InspectorComponents[icomponent.Name] = icomponent;
+                }
+            }
+        }
 
 
-				var typeDef = metaReader.GetTypeDefinition (handle);
 
 
-				var baseTypeHandle = typeDef.BaseType;
+        void ParseEnums()
+        {
+            foreach (var handle in metaReader.TypeDefinitions)
+            {
 
 
-				if (baseTypeHandle.Kind == HandleKind.TypeReference) {
-					var typeRef = metaReader.GetTypeReference ((TypeReferenceHandle)baseTypeHandle);
+                var typeDef = metaReader.GetTypeDefinition(handle);
 
 
-					if (metaReader.GetString (typeRef.Name) == "Enum") {
-						ParseEnum (typeDef);
-					}
-				}
-			}
-		}
+                var baseTypeHandle = typeDef.BaseType;
 
 
-		void ParseEnum (TypeDefinition enumTypeDef)
-		{
+                if (baseTypeHandle.Kind == HandleKind.TypeReference)
+                {
+                    var typeRef = metaReader.GetTypeReference((TypeReferenceHandle)baseTypeHandle);
 
 
-			// TODO: verify that int32 is the enums storage type for constant read below
+                    if (metaReader.GetString(typeRef.Name) == "Enum")
+                    {
+                        ParseEnum(typeDef);
+                    }
+                }
+            }
+        }
 
 
-			InspectorEnum ienum = new InspectorEnum ();
+        void ParseEnum(TypeDefinition enumTypeDef)
+        {
 
 
-			ienum.Name = metaReader.GetString (enumTypeDef.Name);
+            // TODO: verify that int32 is the enums storage type for constant read below
 
 
-			InspectorEnums [ienum.Name] = ienum;
+            InspectorEnum ienum = new InspectorEnum();
 
 
-			var fields = enumTypeDef.GetFields ();
+            ienum.Name = metaReader.GetString(enumTypeDef.Name);
 
 
-			foreach (var fieldHandle in fields) {
+            InspectorEnums[ienum.Name] = ienum;
 
 
-				var inspectorField = new InspectorField ();
+            var fields = enumTypeDef.GetFields();
 
 
-				var fieldDef = metaReader.GetFieldDefinition (fieldHandle);
+            foreach (var fieldHandle in fields)
+            {
 
 
-				if ((fieldDef.Attributes & FieldAttributes.HasDefault) != 0) {
+                var inspectorField = new InspectorField();
 
 
-					var constantHandle = fieldDef.GetDefaultValue ();
-					var constant = metaReader.GetConstant (constantHandle);
+                var fieldDef = metaReader.GetFieldDefinition(fieldHandle);
 
 
-					BlobReader constantReader = metaReader.GetBlobReader (constant.Value);
+                if ((fieldDef.Attributes & FieldAttributes.HasDefault) != 0)
+                {
 
 
-					ienum.Values [metaReader.GetString (fieldDef.Name)] = constantReader.ReadInt32 ();
+                    var constantHandle = fieldDef.GetDefaultValue();
+                    var constant = metaReader.GetConstant(constantHandle);
 
 
-				}
-			}
+                    BlobReader constantReader = metaReader.GetBlobReader(constant.Value);
 
 
-			return;
+                    ienum.Values[metaReader.GetString(fieldDef.Name)] = constantReader.ReadInt32();
 
 
-		}
-	}
+                }
+            }
 
 
-	internal class InspectorEnum
-	{
-		public String Name;
-		public Dictionary<string, int> Values = new Dictionary<string, int> ();
+            return;
 
 
-		public Dictionary<string, object> GetJSONDict ()
-		{
-			var dict = new Dictionary<string,object> ();
-			dict ["name"] = Name;
-			dict ["values"] = Values;
-			return dict;
-		}
-	}
+        }
+    }
 
 
-	internal class InspectorComponent
-	{
-		public String Name;
-		public String Namespace;
-		public Dictionary<string, InspectorField> Fields = new Dictionary<string, InspectorField> ();
+    internal class InspectorEnum
+    {
+        public String Name;
+        public Dictionary<string, int> Values = new Dictionary<string, int>();
 
 
-		public Dictionary<string, object> GetJSONDict ()
-		{
-			var dict = new Dictionary<string,object> ();
+        public Dictionary<string, object> GetJSONDict()
+        {
+            var dict = new Dictionary<string, object>();
+            dict["name"] = Name;
+            dict["values"] = Values;
+            return dict;
+        }
+    }
 
 
-			dict ["name"] = Name;
-			dict ["namespace"] = Namespace;
+    internal class InspectorComponent
+    {
+        public String Name;
+        public String Namespace;
+        public Dictionary<string, InspectorField> Fields = new Dictionary<string, InspectorField>();
 
 
-			var fieldList = new List<object> ();
+        public Dictionary<string, object> GetJSONDict()
+        {
+            var dict = new Dictionary<string, object>();
 
 
-			foreach (var entry in Fields) {
-				fieldList.Add (entry.Value.GetJSONDict ());
-			}
+            dict["name"] = Name;
+            dict["namespace"] = Namespace;
 
 
-			dict ["fields"] = fieldList;
+            var fieldList = new List<object>();
 
 
-			return dict;
-		}
-	}
+            foreach (var entry in Fields)
+            {
+                fieldList.Add(entry.Value.GetJSONDict());
+            }
 
 
-	internal class InspectorField
-	{
+            dict["fields"] = fieldList;
 
 
-		public Dictionary<string, object> GetJSONDict ()
-		{
+            return dict;
+        }
+    }
 
 
-			var dict = new Dictionary<string,object> ();
+    internal class InspectorField
+    {
 
 
-			dict ["isEnum"] = IsEnum;
-			dict ["typeName"] = TypeName;
-			dict ["name"] = Name;
-			dict ["defaultValue"] = DefaultValue;
+        public Dictionary<string, object> GetJSONDict()
+        {
 
 
-			dict ["caPos"] = CustomAttrPositionalArgs;
-			dict ["caNamed"] = CustomAttrNamedArgs;
+            var dict = new Dictionary<string, object>();
 
 
-			return dict;
+            dict["isEnum"] = IsEnum;
+            dict["typeName"] = TypeName;
+            dict["name"] = Name;
+            dict["defaultValue"] = DefaultValue;
 
 
-		}
+            dict["caPos"] = CustomAttrPositionalArgs;
+            dict["caNamed"] = CustomAttrNamedArgs;
 
 
-		public bool IsEnum = false;
+            return dict;
 
 
-		public string TypeName;
+        }
 
 
-		// the Name of the InspectorField
-		public string Name;
-		// The DefaultValue if supplied
-		public string DefaultValue;
+        public bool IsEnum = false;
 
 
-		// custom attributes, positional and named
-		public List<string> CustomAttrPositionalArgs = new List<string> ();
-		public Dictionary<string, string> CustomAttrNamedArgs = new Dictionary<string, string> ();
-	}
+        public string TypeName;
+
+        // the Name of the InspectorField
+        public string Name;
+        // The DefaultValue if supplied
+        public string DefaultValue;
+
+        // custom attributes, positional and named
+        public List<string> CustomAttrPositionalArgs = new List<string>();
+        public Dictionary<string, string> CustomAttrNamedArgs = new Dictionary<string, string>();
+    }
 
 
 }
 }

File diff suppressed because it is too large
+ 702 - 627
Script/AtomicNET/AtomicNETService/CSComponentInspector.cs


+ 1 - 1
Script/AtomicNET/ComponentTest/TestComponent.cs

@@ -40,7 +40,7 @@ namespace ComponentTest
         [Inspector("Textures/chest.png")]
         [Inspector("Textures/chest.png")]
         public Sprite2D MySprite2DValue;
         public Sprite2D MySprite2DValue;
 
 
-        [Inspector(DefaultValue = "Textures/chest.png")]
+        [Inspector(DefaultValue : "Textures/chest.png")]
         public Sprite2D MyOtherSprite2DValue;
         public Sprite2D MyOtherSprite2DValue;
     }
     }
 
 

+ 3 - 3
Source/AtomicNET/NETNative/NETCore.h

@@ -28,8 +28,8 @@
 namespace Atomic
 namespace Atomic
 {
 {
 
 
-typedef void (*NETCoreEventDispatchFunction)(unsigned eventID, VariantMap* eventData);
-typedef void(*NETCoreUpdateDispatchFunction)(float timeStep);
+typedef void (*NETCoreEventDispatchFunction)(Object* refCounted, unsigned eventID, VariantMap* eventData);
+typedef void (*NETCoreUpdateDispatchFunction)(float timeStep);
 
 
 struct NETCoreDelegates
 struct NETCoreDelegates
 {
 {
@@ -54,7 +54,7 @@ public:
 
 
     static void RegisterNETEventType(unsigned eventType);
     static void RegisterNETEventType(unsigned eventType);
 
 
-    inline static void DispatchEvent(unsigned eventID, VariantMap* eventData = nullptr) { eventDispatch_(eventID, eventData); }
+    inline static void DispatchEvent(Object* refCounted, unsigned eventID, VariantMap* eventData = nullptr) { eventDispatch_(refCounted, eventID, eventData); }
     inline static void DispatchUpdateEvent(float timeStep) { if (updateDispatch_) updateDispatch_(timeStep); }
     inline static void DispatchUpdateEvent(float timeStep) { if (updateDispatch_) updateDispatch_(timeStep); }
 
 
     /// We access this directly in binding code, where there isn't a context
     /// We access this directly in binding code, where there isn't a context

+ 2 - 2
Source/AtomicNET/NETNative/NETEventDispatcher.cpp

@@ -63,13 +63,13 @@ namespace Atomic
 
 
             ncEventData[eventType] = ncEventData;
             ncEventData[eventType] = ncEventData;
 
 
-            NETCore::DispatchEvent(eventType.Value(), &ncEventData);
+            NETCore::DispatchEvent(sender, eventType.Value(), &ncEventData);
 
 
             return;
             return;
 
 
         }
         }
 
 
-        NETCore::DispatchEvent(eventType.Value(), &eventData);
+        NETCore::DispatchEvent(sender, eventType.Value(), &eventData);
 
 
     }
     }
 
 

+ 3 - 3
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.cpp

@@ -624,9 +624,9 @@ void CSFunctionWriter::WriteManagedConstructor(String& source)
 
 
     source += IndentLine(ToString("var classType = typeof(%s);\n", klass->GetName().CString()));
     source += IndentLine(ToString("var classType = typeof(%s);\n", klass->GetName().CString()));
     source += IndentLine("var thisType = this.GetType();\n");
     source += IndentLine("var thisType = this.GetType();\n");
-    source += IndentLine("var nativeThisType = NativeCore.IsNativeType(thisType);\n");    
-    source += IndentLine("var nativeBaseType = NativeCore.IsNativeType(thisType.BaseType);\n");
-    source += IndentLine("if ( (nativeThisType && (thisType == classType)) || (!nativeThisType && (nativeBaseType && (thisType.BaseType == classType))))\n");
+    source += IndentLine("var thisTypeIsNative = NativeCore.IsNativeType(thisType);\n");    
+    source += IndentLine("var nativeAncsestorType = NativeCore.GetNativeAncestorType(thisType);\n");
+    source += IndentLine("if ( (thisTypeIsNative && (thisType == classType)) || (!thisTypeIsNative && (nativeAncsestorType == classType)))\n");
     source += IndentLine("{\n");
     source += IndentLine("{\n");
 
 
     Indent();
     Indent();

Some files were not shown because too many files changed in this diff