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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. EventManager.AddEventEmitter(versionEngine);
  33. isEnabled = true;
  34. }
  35. /// <summary>
  36. /// Disable API version tracking.
  37. /// </summary>
  38. public static void Disable()
  39. {
  40. EventManager.AddEventEmitter(versionEngine);
  41. isEnabled = false;
  42. }
  43. public static void Init()
  44. {
  45. SerializerManager.AddSerializer<ModVersionDescriptor>(new SimpleEntitySerializer<ModVersionDescriptor>(
  46. (_) => { return new EGID[1] { new EGID(0u, ApiExclusiveGroups.versionGroup) }; }
  47. ));
  48. }
  49. }
  50. internal class VersionTrackingEngine : IEventEmitterEngine
  51. {
  52. public string Name { get; } = "GamecraftModdingAPIVersionTrackingGameEngine";
  53. public EntitiesDB entitiesDB { set; private get; }
  54. public int type => -1;
  55. public bool isRemovable => false;
  56. public IEntityFactory Factory { set; private get; }
  57. public void Dispose() { }
  58. public void Ready()
  59. {
  60. EGID egid = new EGID(0u, ApiExclusiveGroups.versionGroup);
  61. if (!entitiesDB.Exists<ModVersionStruct>(egid))
  62. {
  63. Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
  64. int v = (currentVersion.Major * 1000) + (currentVersion.Minor);
  65. Factory.BuildEntity<ModVersionDescriptor>(egid).Init<ModVersionStruct>(new ModVersionStruct
  66. {
  67. version = (uint)v
  68. });
  69. }
  70. }
  71. public uint GetGameVersion()
  72. {
  73. return entitiesDB.QueryUniqueEntity<ModVersionStruct>(ApiExclusiveGroups.versionGroup).version;
  74. }
  75. public void Emit() { }
  76. }
  77. public struct ModVersionStruct : IEntityComponent
  78. {
  79. public uint version;
  80. }
  81. public class ModVersionDescriptor: SerializableEntityDescriptor<ModVersionDescriptor._ModVersionDescriptor>
  82. {
  83. [HashName("GamecraftModdingAPIVersionV0")]
  84. public class _ModVersionDescriptor : IEntityDescriptor
  85. {
  86. public IComponentBuilder[] componentsToBuild { get; } = new IComponentBuilder[]{
  87. new SerializableComponentBuilder<SerializationType, ModVersionStruct>(((int)SerializationType.Network, new DefaultSerializer<ModVersionStruct>()), ((int)SerializationType.Storage, new DefaultSerializer<ModVersionStruct>())),
  88. };
  89. }
  90. }
  91. }