ComponentCore.cs 2.2 KB

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