NativeCore.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. namespace AtomicEngine
  5. {
  6. // Want to be able to inherit from CSComponent, ScriptObject, and the various UI classes
  7. // This means that the managed side needs to be kept alive as will have object state
  8. // and we might get these back from native code in a variety of ways
  9. // a inherited instance must have a GCHandle to keep it alive
  10. // it must also at some point along constructor chain create the backing native
  11. // can we detect a native class?
  12. // we shouldn't go off IntPtr here, as a pointer could be reused native before recycles
  13. static class NativeCore
  14. {
  15. // given an existing instance classID, construct the managed instance, with downcast support (ask for Component, get StaticModel for example)
  16. public static Dictionary<IntPtr, Func<IntPtr, RefCounted>> nativeClassIDToManagedConstructor = new Dictionary<IntPtr, Func<IntPtr, RefCounted>>();
  17. // native engine types, instances of these types can be trivially recreated managed side
  18. private static Dictionary<Type, Type> nativeTypes = new Dictionary<Type, Type> ();
  19. // RefID -> WeakReference of managed instance
  20. public static Dictionary<uint, WeakReference> nativeLookup = new Dictionary<uint, WeakReference> ();
  21. public static bool GetNativeType (Type type)
  22. {
  23. return nativeTypes.ContainsKey (type);
  24. }
  25. public static void RegisterNativeType (Type type)
  26. {
  27. nativeTypes.Add (type, type);
  28. }
  29. public static IntPtr RegisterNative (IntPtr native, RefCounted r)
  30. {
  31. r.nativeInstance = native;
  32. var w = new WeakReference (r);
  33. NativeCore.nativeLookup [r.RefID] = w;
  34. r.AddRef();
  35. return native;
  36. }
  37. public static void ReleaseExpiredNativeReferences()
  38. {
  39. List<uint> released = new List<uint> ();
  40. foreach(KeyValuePair<uint, WeakReference> entry in nativeLookup)
  41. {
  42. if (entry.Value.Target == null || !entry.Value.IsAlive) {
  43. released.Add (entry.Key);
  44. } else {
  45. }
  46. }
  47. foreach (uint id in released) {
  48. // use safe release
  49. RefCounted.SafeReleaseRef (id);
  50. nativeLookup.Remove (id);
  51. }
  52. }
  53. // wraps an existing native instance, with downcast support
  54. public static T WrapNative<T> (IntPtr native) where T:RefCounted
  55. {
  56. if (native == IntPtr.Zero)
  57. return null;
  58. // instance id
  59. uint id = csb_Atomic_RefCounted_GetRefID (native);
  60. WeakReference w;
  61. // first see if we're already available
  62. if (nativeLookup.TryGetValue (id, out w)) {
  63. if (w.IsAlive) {
  64. // we're alive!
  65. return (T)w.Target;
  66. } else {
  67. // we were seen before, but have since been GC'd, remove!
  68. nativeLookup.Remove (id);
  69. }
  70. }
  71. IntPtr classID = RefCounted.csb_Atomic_RefCounted_GetClassID (native);
  72. // and store, with downcast support for instance Component -> StaticModel
  73. // we never want to hit this path for script inherited natives
  74. RefCounted r = nativeClassIDToManagedConstructor[classID](native);
  75. w = new WeakReference (r);
  76. NativeCore.nativeLookup [id] = w;
  77. // store a ref, so native side will not be released while we still have a reference in managed code
  78. r.AddRef();
  79. return (T) r;
  80. }
  81. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  82. private static extern uint csb_Atomic_RefCounted_GetRefID(IntPtr self);
  83. }
  84. }