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.

67 lines
2.7KB

  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 HarmonyLib;
  9. using Svelto.ECS;
  10. namespace CodeGenerator
  11. {
  12. public static class ECSAnalyzer
  13. {
  14. public static ECSClassInfo AnalyzeEntityDescriptor(Type entityDescriptorType)
  15. {
  16. // TODO: Add support for creating/deleting entities (getting an up to date server/client engines root)
  17. var templateType = typeof(EntityDescriptorTemplate<>).MakeGenericType(entityDescriptorType);
  18. var templateDescriptor = AccessTools.Property(templateType, "descriptor");
  19. var getDescriptorExpr = Expression.MakeMemberAccess(null, templateDescriptor ?? throw new InvalidOperationException());
  20. var getTemplateDescriptorExpr =
  21. Expression.Lambda<Func<IEntityDescriptor>>(getDescriptorExpr);
  22. var getTemplateDescriptor = getTemplateDescriptorExpr.Compile();
  23. // TODO: Crashes on constructing the descriptor type. Maybe move the analysis part to a mod.
  24. var builders = getTemplateDescriptor().componentsToBuild;
  25. return new ECSClassInfo
  26. {
  27. Name = entityDescriptorType.Name.Replace("EntityComponent", "").Replace("EntityStruct", ""),
  28. Properties = builders.Select(builder => builder.GetEntityComponentType()).SelectMany(AnalyzeFields).ToArray()
  29. };
  30. }
  31. private static ECSPropertyInfo[] AnalyzeFields(Type componentType)
  32. {
  33. bool useReflection = componentType.IsNotPublic;
  34. var result = new List<ECSPropertyInfo>();
  35. foreach (var field in componentType.GetFields())
  36. {
  37. var attr = field.GetCustomAttribute<TweakableStatAttribute>();
  38. string propName = field.Name;
  39. if (attr != null)
  40. propName = attr.propertyName;
  41. propName = char.ToUpper(propName[0]) + propName[1..];
  42. if (useReflection)
  43. {
  44. result.Add(new ECSReflectedPropertyInfo
  45. {
  46. Name = propName,
  47. Type = field.FieldType,
  48. OriginalClassName = componentType.FullName,
  49. ComponentType = componentType
  50. });
  51. }
  52. result.Add(new ECSPropertyInfo
  53. {
  54. Name = propName,
  55. Type = field.FieldType,
  56. ComponentType = componentType
  57. });
  58. }
  59. return result.ToArray();
  60. }
  61. }
  62. }