ComponentCore.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, CSComponent> liveComponents = new Dictionary<uint, CSComponent>();
  20. public static IntPtr CurrentCSComponentNativeInstance = default(IntPtr);
  21. public static void CallComponentMethod(uint componentID, CSComponentMethod method, float value)
  22. {
  23. CSComponent component;
  24. if (liveComponents.TryGetValue (componentID, out component)) {
  25. switch (method) {
  26. case CSComponentMethod.Start:
  27. component.Start ();
  28. break;
  29. case CSComponentMethod.Update:
  30. component.Update (value);
  31. break;
  32. }
  33. }
  34. }
  35. public static void RegisterCSComponent(CSComponent component)
  36. {
  37. // register
  38. liveComponents [component.RefID] = component;
  39. }
  40. // native create CSComponent
  41. public static IntPtr CreateCSComponent(string name)
  42. {
  43. Type type;
  44. if (componentTypes.TryGetValue (name, out type)) {
  45. // create an instance of the component type
  46. var component = (CSComponent) Activator.CreateInstance(type);
  47. RegisterCSComponent (component);
  48. return component.nativeInstance;
  49. }
  50. return IntPtr.Zero;
  51. }
  52. static void RegisterComponentType(Type componentType)
  53. {
  54. // TODO: Check for name clash, we don't want to use assembly qualified names, etc to keep it simple
  55. componentTypes [componentType.Name] = componentType;
  56. }
  57. public static void RegisterAssemblyComponents(Assembly assembly)
  58. {
  59. Type csComponentType = typeof(CSComponent);
  60. Type[] types = assembly.GetTypes ();
  61. List<Type> componentTypes = new List<Type> ();
  62. foreach (Type type in types)
  63. {
  64. for (var current = type.BaseType; current != null; current = current.BaseType) {
  65. if (current == csComponentType) {
  66. componentTypes.Add(type);
  67. break;
  68. }
  69. }
  70. }
  71. foreach (Type type in componentTypes) {
  72. RegisterComponentType (type);
  73. }
  74. }
  75. }
  76. }