ComponentCore.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Reflection;
  5. namespace AtomicEngine
  6. {
  7. internal enum CSComponentMethod
  8. {
  9. Start,
  10. DelayedStart,
  11. Update,
  12. PostUpdate,
  13. FixedUpdate,
  14. PostFixedUpdate
  15. };
  16. static internal class ComponentCore
  17. {
  18. static Dictionary<String, Type> componentTypes = new Dictionary<String, Type>();
  19. static Dictionary<uint, GCHandle> liveComponents = new Dictionary<uint, GCHandle>();
  20. static uint idGen = 1;
  21. public static IntPtr CurrentCSComponentNativeInstance = default(IntPtr);
  22. public static void CallComponentMethod(uint componentID, CSComponentMethod method, float value)
  23. {
  24. GCHandle handle;
  25. if (liveComponents.TryGetValue (componentID, out handle)) {
  26. var component = (CSComponent)handle.Target;
  27. switch (method) {
  28. case CSComponentMethod.Start:
  29. component.Start ();
  30. break;
  31. case CSComponentMethod.Update:
  32. component.Update (value);
  33. break;
  34. }
  35. }
  36. }
  37. public static void RegisterCSComponent(CSComponent component)
  38. {
  39. // keep the instance from being GC'd as it may be referenced solely in native code
  40. // attached to a node
  41. GCHandle handle = GCHandle.Alloc (component);
  42. // register
  43. liveComponents [idGen] = handle;
  44. component.SetManagedID (idGen);
  45. idGen++;
  46. }
  47. // native create CSComponent
  48. public static void CreateCSComponent(string name, IntPtr nativeCSComponent)
  49. {
  50. Type type;
  51. if (componentTypes.TryGetValue (name, out type)) {
  52. // create an instance of the component type
  53. CurrentCSComponentNativeInstance = nativeCSComponent;
  54. var component = (CSComponent) Activator.CreateInstance(type);
  55. CurrentCSComponentNativeInstance = IntPtr.Zero;
  56. RegisterCSComponent (component);
  57. }
  58. }
  59. static void RegisterComponentType(Type componentType)
  60. {
  61. // TODO: Check for name clash, we don't want to use assembly qualified names, etc to keep it simple
  62. componentTypes [componentType.Name] = componentType;
  63. }
  64. public static void RegisterAssemblyComponents(Assembly assembly)
  65. {
  66. Type csComponentType = typeof(CSComponent);
  67. Type[] types = assembly.GetTypes ();
  68. List<Type> componentTypes = new List<Type> ();
  69. foreach (Type type in types)
  70. {
  71. for (var current = type.BaseType; current != null; current = current.BaseType) {
  72. if (current == csComponentType) {
  73. componentTypes.Add(type);
  74. break;
  75. }
  76. }
  77. }
  78. foreach (Type type in componentTypes) {
  79. RegisterComponentType (type);
  80. }
  81. }
  82. }
  83. }