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.

VersionTracking.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }