AtomicNET.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. namespace AtomicEngine
  5. {
  6. public class AtomicNETRuntime
  7. {
  8. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  9. public static extern int csb_Atomic_Test (int id);
  10. public static void Startup()
  11. {
  12. //Console.WriteLine("AtomicNETRuntime Startup:" + csb_Atomic_Test(42));
  13. Atomic.Initialize();
  14. }
  15. }
  16. public static class Atomic
  17. {
  18. static public void Initialize()
  19. {
  20. ContainerModule.Initialize ();
  21. CoreModule.Initialize ();
  22. MathModule.Initialize ();
  23. EngineModule.Initialize ();
  24. InputModule.Initialize ();
  25. IOModule.Initialize ();
  26. ResourceModule.Initialize ();
  27. AudioModule.Initialize ();
  28. GraphicsModule.Initialize ();
  29. SceneModule.Initialize ();
  30. Atomic2DModule.Initialize ();
  31. Atomic3DModule.Initialize ();
  32. NavigationModule.Initialize ();
  33. NetworkModule.Initialize ();
  34. PhysicsModule.Initialize ();
  35. EnvironmentModule.Initialize ();
  36. UIModule.Initialize ();
  37. AtomicPlayer.PlayerModule.Initialize ();
  38. initSubsystems();
  39. }
  40. static Dictionary<Type, RefCounted> subSystems = new Dictionary<Type, RefCounted>();
  41. static private void registerSubsystem (RefCounted subsystem)
  42. {
  43. subSystems[subsystem.GetType()] = subsystem;
  44. }
  45. static public T GetSubsystem<T>() where T : RefCounted
  46. {
  47. return (T) subSystems [typeof(T)];
  48. }
  49. static private void initSubsystems()
  50. {
  51. registerSubsystem (NativeCore.WrapNative<Graphics> (csb_AtomicEngine_GetSubsystem("Graphics")));
  52. registerSubsystem (NativeCore.WrapNative<Renderer> (csb_AtomicEngine_GetSubsystem("Renderer")));
  53. registerSubsystem (NativeCore.WrapNative<ResourceCache> (csb_AtomicEngine_GetSubsystem("ResourceCache")));
  54. var zone = GetSubsystem<Renderer>().GetDefaultZone();
  55. Console.WriteLine("Allocated: " + zone.nativeInstance);
  56. }
  57. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  58. private static extern IntPtr csb_AtomicEngine_GetSubsystem(string name);
  59. }
  60. public static partial class Constants
  61. {
  62. public const string LIBNAME = "__Internal";
  63. }
  64. public partial class RefCounted
  65. {
  66. public RefCounted()
  67. {
  68. }
  69. protected RefCounted (IntPtr native)
  70. {
  71. nativeInstance = native;
  72. }
  73. public IntPtr nativeInstance;
  74. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  75. public static extern IntPtr csb_Atomic_RefCounted_GetClassID (IntPtr self);
  76. }
  77. static class NativeCore
  78. {
  79. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  80. private static extern void csb_AtomicEngine_ReleaseRef (IntPtr refCounted);
  81. // given an existing instance classID, construct the managed instance, with downcast support (ask for Component, get StaticModel for example)
  82. public static Dictionary<IntPtr, Func<IntPtr, RefCounted>> nativeClassIDToManagedConstructor = new Dictionary<IntPtr, Func<IntPtr, RefCounted>>();
  83. // weak references here, hold a ref native side
  84. public static Dictionary<IntPtr, WeakReference> nativeLookup = new Dictionary<IntPtr, WeakReference> ();
  85. // native engine types, instances of these types can be trivially recreated managed side
  86. private static Dictionary<Type, Type> nativeTypes = new Dictionary<Type, Type> ();
  87. public static bool GetNativeType (Type type)
  88. {
  89. return nativeTypes.ContainsKey (type);
  90. }
  91. public static void RegisterNativeType (Type type)
  92. {
  93. nativeTypes.Add (type, type);
  94. }
  95. public static void ReleaseExpiredNativeReferences()
  96. {
  97. List<IntPtr> released = new List<IntPtr> ();
  98. foreach(KeyValuePair<IntPtr, WeakReference> entry in nativeLookup)
  99. {
  100. if (entry.Value.Target == null || !entry.Value.IsAlive)
  101. {
  102. released.Add (entry.Key);
  103. }
  104. }
  105. foreach (IntPtr native in released) {
  106. nativeLookup.Remove (native);
  107. csb_AtomicEngine_ReleaseRef(native);
  108. }
  109. }
  110. // called by native code
  111. public static void NETUpdate (float timeStep)
  112. {
  113. GC.Collect();
  114. GC.WaitForPendingFinalizers();
  115. GC.Collect();
  116. ReleaseExpiredNativeReferences();
  117. }
  118. // called by native code for every refcounted deleted
  119. public static void RefCountedDeleted (IntPtr native)
  120. {
  121. nativeLookup.Remove(native);
  122. }
  123. // register a newly created native
  124. public static IntPtr RegisterNative (IntPtr native, RefCounted r)
  125. {
  126. r.nativeInstance = native;
  127. var w = new WeakReference (r);
  128. NativeCore.nativeLookup [native] = w;
  129. // keep native side alive
  130. r.AddRef();
  131. return native;
  132. }
  133. // wraps an existing native instance, with downcast support
  134. public static T WrapNative<T> (IntPtr native) where T:RefCounted
  135. {
  136. if (native == IntPtr.Zero)
  137. return null;
  138. WeakReference w;
  139. // first see if we're already available
  140. if (nativeLookup.TryGetValue (native, out w)) {
  141. if (w.IsAlive) {
  142. // we're alive!
  143. return (T)w.Target;
  144. } else {
  145. // we were seen before, but have since been GC'd, remove!
  146. nativeLookup.Remove (native);
  147. csb_AtomicEngine_ReleaseRef(native);
  148. }
  149. }
  150. IntPtr classID = RefCounted.csb_Atomic_RefCounted_GetClassID (native);
  151. // and store, with downcast support for instance Component -> StaticModel
  152. // we never want to hit this path for script inherited natives
  153. RefCounted r = nativeClassIDToManagedConstructor[classID](native);
  154. w = new WeakReference (r);
  155. NativeCore.nativeLookup [native] = w;
  156. // store a ref, so native side will not be released while we still have a reference in managed code
  157. r.AddRef();
  158. return (T) r;
  159. }
  160. }
  161. }