A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.5KB

  1. using System;
  2. using System.CodeDom;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using Gamecraft.Tweaks;
  8. using Svelto.ECS;
  9. namespace CodeGenerator
  10. {
  11. public class ECSAnalyzer
  12. {
  13. public static ECSClassInfo AnalyzeEntityDescriptor(Type entityDescriptorType)
  14. {
  15. // TODO: Add support for creating/deleting entities (getting an up to date server/client engines root)
  16. var templateType = typeof(EntityDescriptorTemplate<>).MakeGenericType(entityDescriptorType);
  17. var getTemplateClass = Expression.Constant(templateType);
  18. var getDescriptorExpr = Expression.PropertyOrField(getTemplateClass, "descriptor");
  19. var getTemplateDescriptorExpr =
  20. Expression.Lambda<Func<IEntityDescriptor>>(getDescriptorExpr);
  21. var getTemplateDescriptor = getTemplateDescriptorExpr.Compile();
  22. var builders = getTemplateDescriptor().componentsToBuild;
  23. return new ECSClassInfo
  24. {
  25. Name = entityDescriptorType.Name.Replace("EntityComponent", "").Replace("EntityStruct", ""),
  26. Properties = builders.Select(builder => builder.GetEntityComponentType()).SelectMany(AnalyzeFields).ToArray()
  27. };
  28. }
  29. private static ECSPropertyInfo[] AnalyzeFields(Type componentType)
  30. {
  31. bool useReflection = componentType.IsNotPublic;
  32. var result = new List<ECSPropertyInfo>();
  33. foreach (var field in componentType.GetFields())
  34. {
  35. var attr = field.GetCustomAttribute<TweakableStatAttribute>();
  36. string propName = field.Name;
  37. if (attr != null)
  38. propName = attr.propertyName;
  39. propName = char.ToUpper(propName[0]) + propName[1..];
  40. if (useReflection)
  41. {
  42. result.Add(new ECSReflectedPropertyInfo
  43. {
  44. Name = propName,
  45. Type = field.FieldType,
  46. OriginalClassName = componentType.FullName,
  47. ComponentType = componentType
  48. });
  49. }
  50. result.Add(new ECSPropertyInfo
  51. {
  52. Name = propName,
  53. Type = field.FieldType,
  54. ComponentType = componentType
  55. });
  56. }
  57. return result.ToArray();
  58. }
  59. }
  60. }