ComponentCore.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using System.Runtime.Loader;
  5. using System.Runtime.InteropServices;
  6. using System.Linq;
  7. namespace AtomicEngine
  8. {
  9. public partial class CSComponent : ScriptComponent
  10. {
  11. public void StartInternal()
  12. {
  13. ApplyFieldValues();
  14. Start();
  15. }
  16. public virtual void Start()
  17. {
  18. }
  19. public virtual void Update(float timeStep)
  20. {
  21. }
  22. }
  23. public static class ComponentCore
  24. {
  25. static List<CSComponent> startQueue = new List<CSComponent>();
  26. // holds a reference
  27. static Dictionary<IntPtr, CSComponent> liveComponents = new Dictionary<IntPtr, CSComponent>();
  28. public static void Update (float timeStep)
  29. {
  30. List<CSComponent> _startQueue = new List<CSComponent>(startQueue);
  31. startQueue.Clear();
  32. foreach (var c in _startQueue)
  33. {
  34. c.StartInternal();
  35. }
  36. foreach (var c in liveComponents.Values)
  37. {
  38. c.Update(timeStep);
  39. }
  40. }
  41. // This will need to be optimized
  42. public static void CSComponentApplyFields(IntPtr componentPtr, IntPtr fieldMapPtr)
  43. {
  44. NETVariantMap fieldMap = NativeCore.WrapNative<NETVariantMap>(fieldMapPtr);;
  45. CSComponent component = NativeCore.WrapNative<CSComponent>(componentPtr);;
  46. if (fieldMap == null || component == null)
  47. return;
  48. FieldInfo[] fields = componentClassFields[component.GetType()];
  49. foreach (var field in fields)
  50. {
  51. if (fieldMap.Contains(field.Name))
  52. {
  53. //Console.WriteLine("Applying: {0} {1}", field.Name, field.FieldType.Name);
  54. var fieldType = field.FieldType;
  55. if (fieldType.IsEnum)
  56. {
  57. field.SetValue(component, fieldMap.GetInt(field.Name));
  58. continue;
  59. }
  60. switch (Type.GetTypeCode(fieldType))
  61. {
  62. case TypeCode.Boolean:
  63. field.SetValue(component, fieldMap.GetBool(field.Name));
  64. break;
  65. case TypeCode.Int32:
  66. field.SetValue(component, fieldMap.GetInt(field.Name));
  67. break;
  68. case TypeCode.Single:
  69. field.SetValue(component, fieldMap.GetFloat(field.Name));
  70. break;
  71. case TypeCode.String:
  72. field.SetValue(component, fieldMap.GetString(field.Name));
  73. break;
  74. default:
  75. if (fieldType == typeof(Vector3))
  76. {
  77. field.SetValue(component, fieldMap.GetVector3(field.Name));
  78. }
  79. else if (fieldType == typeof(Quaternion))
  80. {
  81. field.SetValue(component, fieldMap.GetQuaternion(field.Name));
  82. }
  83. else if (fieldType.IsSubclassOf(typeof(Resource)))
  84. {
  85. field.SetValue(component, fieldMap.GetResourceFromRef(field.Name));
  86. }
  87. else if (fieldType.IsSubclassOf(typeof(RefCounted)))
  88. {
  89. field.SetValue(component, fieldMap.GetPtr(field.Name));
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. static Dictionary<Type, FieldInfo[] > componentClassFields = new Dictionary<Type, FieldInfo[]>();
  97. public static IntPtr CSComponentCreate(string assemblyName, string classTypeName)
  98. {
  99. Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
  100. Type type = assembly.GetType("AtomicNETTest." + classTypeName);
  101. // TODO: check type is derived from CSComponent
  102. var component = (CSComponent) Activator.CreateInstance(type);
  103. if (!componentClassFields.ContainsKey(type))
  104. {
  105. var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  106. .Where(field => field.IsDefined(typeof(InspectorAttribute), true));
  107. componentClassFields[type] = fields.ToArray<FieldInfo>();
  108. }
  109. startQueue.Add(component);
  110. liveComponents[component.nativeInstance] = component;
  111. return component.nativeInstance;
  112. }
  113. }
  114. }