Browse Source

Attempting to wrap a native of unknown type no longer throws an exception, instead returns null with optional logging

Josh Engebretson 9 years ago
parent
commit
bfc67f807c

+ 14 - 6
Script/AtomicNET/AtomicNET/Core/NativeCore.cs

@@ -348,20 +348,25 @@ namespace AtomicEngine
 
             IntPtr classID = RefCounted.csi_Atomic_RefCounted_GetClassID(native);
 
-            // and store, with downcast support for instance Component -> StaticModel
-            // we never want to hit this path for script inherited natives
-
+            // Check whether this is a valid native class to wrap
             NativeType nativeType;
-
             if (!nativeClassIDToNativeType.TryGetValue(classID, out nativeType))
             {
-                throw new InvalidOperationException("NativeCore.WrapNative - Attempting to wrap unknown native class id");
+                if (logWrapUnknownNative)
+                {
+                    Log.Info("WrapNative returning null for unknown class: " + csi_Atomic_RefCounted_GetTypeName(native));
+                }
+
+                return null;
             }
 
             // TODO: make CSComponent abstract and have general abstract logic here?
             if (nativeType.Type == typeof(CSComponent))
                 return null;
 
+            // and store, with downcast support for instance Component -> StaticModel
+            // we never want to hit this path for script inherited natives
+
             r = nativeType.managedConstructor(native);
 
             w = new WeakReference<RefCounted>(r);
@@ -383,7 +388,7 @@ namespace AtomicEngine
         }
 
         [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
-        private static extern IntPtr csi_Atomic_AObject_GetTypeName(IntPtr self);
+        private static extern string csi_Atomic_RefCounted_GetTypeName(IntPtr self);
 
         [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
         private static extern int csi_Atomic_RefCounted_Refs(IntPtr self);
@@ -489,6 +494,9 @@ namespace AtomicEngine
             }
         }
 
+        // If true, we will log any attempted access to instances of unknown native classes
+        static bool logWrapUnknownNative = false;
+
     }
 
 

+ 6 - 0
Script/AtomicNET/AtomicNET/IO/Log.cs

@@ -7,6 +7,12 @@ namespace AtomicEngine
     public partial class Log : AObject
     {
 
+        public static void Debug(string message)
+        {
+            Log.Write(Constants.LOG_DEBUG, message);
+        }
+
+
         public static void Warn(string message)
         {
             Log.Write(Constants.LOG_WARNING, message);

+ 6 - 2
Source/AtomicNET/NETNative/NETCInterop.cpp

@@ -68,11 +68,15 @@ namespace Atomic
             refCounted->ReleaseRef();
         }
 
-        ATOMIC_EXPORT_API const char* csi_Atomic_AObject_GetTypeName(Object* self)
+        ATOMIC_EXPORT_API const char* csi_Atomic_RefCounted_GetTypeName(RefCounted* self)
         {
+            if (!self->IsObject())
+            {
+                return "RefCounted";
+            }
 
             static String returnValue;
-            returnValue = self->GetTypeName();
+            returnValue = ((Object*)self)->GetTypeName();
             return returnValue.CString();
         }