NativeCore.cs 2.8 KB

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