AtomicNET.cs 6.1 KB

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