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.

113 lines
3.1KB

  1. using System;
  2. using System.Reflection;
  3. using RobocraftX.Common;
  4. using Svelto.ECS;
  5. using Svelto.ECS.Serialization;
  6. using GamecraftModdingAPI.Persistence;
  7. using GamecraftModdingAPI.Events;
  8. namespace GamecraftModdingAPI.Utility
  9. {
  10. /// <summary>
  11. /// Tracks the API version the current game was built for.
  12. /// For compatibility reasons, this must be enabled before it will work.
  13. /// </summary>
  14. public static class VersionTracking
  15. {
  16. private static readonly VersionTrackingEngine versionEngine = new VersionTrackingEngine();
  17. private static bool isEnabled = false;
  18. /// <summary>
  19. /// Gets the API version saved in the current game.
  20. /// </summary>
  21. /// <returns>The version.</returns>
  22. public static uint GetVersion()
  23. {
  24. if (!isEnabled) return 0u;
  25. return versionEngine.GetGameVersion();
  26. }
  27. /// <summary>
  28. /// Enable API version tracking.
  29. /// </summary>
  30. public static void Enable()
  31. {
  32. if (!SerializerManager.ExistsSerializer(typeof(ModVersionStruct).FullName))
  33. {
  34. SerializerManager.AddSerializer<ModVersionDescriptor>(new SimpleEntitySerializer<ModVersionDescriptor>(
  35. (_) => { return new EGID[1] { new EGID(0u, ApiExclusiveGroups.versionGroup) }; }
  36. ));
  37. }
  38. EventManager.AddEventEmitter(versionEngine);
  39. isEnabled = true;
  40. }
  41. /// <summary>
  42. /// Disable API version tracking.
  43. /// </summary>
  44. public static void Disable()
  45. {
  46. EventManager.AddEventEmitter(versionEngine);
  47. isEnabled = false;
  48. }
  49. public static void Init() { }
  50. }
  51. internal class VersionTrackingEngine : IEventEmitterEngine
  52. {
  53. public string Name { get; } = "GamecraftModdingAPIVersionTrackingGameEngine";
  54. public EntitiesDB entitiesDB { set; private get; }
  55. public int type => -1;
  56. public bool isRemovable => false;
  57. public IEntityFactory Factory { set; private get; }
  58. public void Dispose() { }
  59. public void Ready()
  60. {
  61. EGID egid = new EGID(0u, ApiExclusiveGroups.versionGroup);
  62. if (!entitiesDB.Exists<ModVersionStruct>(egid))
  63. {
  64. Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
  65. int v = (currentVersion.Major * 1000) + (currentVersion.Minor);
  66. Factory.BuildEntity<ModVersionDescriptor>(egid).Init<ModVersionStruct>(new ModVersionStruct
  67. {
  68. version = (uint)v
  69. });
  70. }
  71. }
  72. public uint GetGameVersion()
  73. {
  74. return entitiesDB.QueryUniqueEntity<ModVersionStruct>(ApiExclusiveGroups.versionGroup).version;
  75. }
  76. public void Emit() { }
  77. }
  78. public struct ModVersionStruct : IEntityComponent
  79. {
  80. public uint version;
  81. }
  82. public class ModVersionDescriptor: SerializableEntityDescriptor<ModVersionDescriptor._ModVersionDescriptor>
  83. {
  84. [HashName("GamecraftModdingAPIVersionV0")]
  85. public class _ModVersionDescriptor : IEntityDescriptor
  86. {
  87. public IComponentBuilder[] componentsToBuild { get; } = new IComponentBuilder[]{
  88. new SerializableComponentBuilder<SerializationType, ModVersionStruct>(((int)SerializationType.Network, new DefaultSerializer<ModVersionStruct>()), ((int)SerializationType.Storage, new DefaultSerializer<ModVersionStruct>())),
  89. };
  90. }
  91. }
  92. }