Browse Source

[C#] Adding managed version of ResourceCache::ReleaseAllResources and public NativeCore.RunGC method which can be run to force a GC collection + native expire

Josh Engebretson 8 years ago
parent
commit
afec5ff3d6

+ 11 - 1
Script/AtomicNET/AtomicNET/Core/NativeCore.cs

@@ -226,7 +226,17 @@ namespace AtomicEngine
 
         }
 
-        internal static void ExpireNatives()
+        /// <summary>
+        ///  Runs a GC collection, waits for any finalizers, and then expires any natives collected 
+        /// </summary>
+        public static void RunGC()
+        {
+            GC.Collect();
+            GC.WaitForPendingFinalizers();
+            ExpireNatives();
+        }
+
+        private static void ExpireNatives()
         {
             var watch = new Stopwatch();
             watch.Start();

+ 0 - 5
Script/AtomicNET/AtomicNET/Player/Player.cs

@@ -28,11 +28,6 @@ namespace AtomicPlayer
             {
                 if (loadedScenes.Contains(e.Scene))
                     loadedScenes.Remove(e.Scene);
-
-                GC.Collect();
-                GC.WaitForPendingFinalizers();
-                NativeCore.ExpireNatives();                
-
             });
 
         }

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

@@ -1,3 +1,6 @@
+using System;
+using System.Runtime.InteropServices;
+
 namespace AtomicEngine
 {
 
@@ -25,5 +28,21 @@ namespace AtomicEngine
 
             return null;
         }
+
+        /// <summary>
+        ///  Release all resources. When called with the force flag false, releases all currently unused resources.
+        /// </summary>
+        public void ReleaseAllResources(bool force = false)
+        {
+            // We need to GC before calling native ResourceCache::ReleaseAllResources, to ensure all managed resource references are down
+            // otherwise, the cache will hold onto the resource
+            NativeCore.RunGC();
+
+            csi_Atomic_ResourceCache_ReleaseAllResources(nativeInstance, force);
+        }
+
+        [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
+        private static extern void csi_Atomic_ResourceCache_ReleaseAllResources(IntPtr self, bool force);
+
     }
 }

+ 7 - 0
Script/Packages/Atomic/Resource.json

@@ -13,6 +13,13 @@
 			"RemovePackageFile": ["String", "bool", "bool"]
 		}
 	},
+	"excludes" : {
+		"CSharp" : {
+			"ResourceCache" : {
+				"ReleaseAllResources" : ["bool"]
+			}
+		}
+	},
 	"typescript_decl" : {
 		"ResourceCache" : [
 			"getResource<T extends Resource>(type: string, name: string, sendEventOnFailure?: boolean): T;",

+ 12 - 0
Source/AtomicNET/NETNative/NETCInterop.cpp

@@ -1,4 +1,6 @@
 
+
+#include <Atomic/Resource/ResourceCache.h>
 #include <Atomic/Script/ScriptVariant.h>
 #include <Atomic/Script/ScriptVariantMap.h>
 #include <Atomic/IPC/IPC.h>
@@ -671,6 +673,16 @@ namespace Atomic
             return self->Distance(*point);
         }
 
+        // Native method is suppressed from automated bindings, for additional GC logic in C# version of method
+        ATOMIC_EXPORT_API void csi_Atomic_ResourceCache_ReleaseAllResources(ResourceCache* self, bool force)
+        {
+            if (!self)
+            {
+                return;
+            }
+
+            self->ReleaseAllResources(force);
+        }
 
 #ifdef ATOMIC_PLATFORM_IOS
         ATOMIC_EXPORT_API void SDL_IOS_Init(const char *resourceDir, const char *documentsDir)