AtomicNET.cs 5.9 KB

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