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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. [Obsolete]
  15. public static class VersionTracking
  16. {
  17. private static readonly VersionTrackingEngine versionEngine = new VersionTrackingEngine();
  18. private static bool isEnabled = false;
  19. /// <summary>
  20. /// Gets the API version saved in the current game.
  21. /// </summary>
  22. /// <returns>The version.</returns>
  23. public static uint GetVersion()
  24. {
  25. if (!isEnabled) return 0u;
  26. return versionEngine.GetGameVersion();
  27. }
  28. /// <summary>
  29. /// Enable API version tracking.
  30. /// </summary>
  31. public static void Enable()
  32. {
  33. if (!SerializerManager.ExistsSerializer(typeof(ModVersionStruct).FullName))
  34. {
  35. SerializerManager.AddSerializer<ModVersionDescriptor>(new SimpleEntitySerializer<ModVersionDescriptor>(
  36. (_) => { return new EGID[1] { new EGID(0u, ApiExclusiveGroups.versionGroup) }; }
  37. ));
  38. }
  39. EventManager.AddEventEmitter(versionEngine);
  40. isEnabled = true;
  41. }
  42. /// <summary>
  43. /// Disable API version tracking.
  44. /// </summary>
  45. public static void Disable()
  46. {
  47. EventManager.AddEventEmitter(versionEngine);
  48. isEnabled = false;
  49. }
  50. public static void Init() { }
  51. }
  52. [Obsolete]
  53. internal class VersionTrackingEngine : IEventEmitterEngine
  54. {
  55. public string Name { get; } = "GamecraftModdingAPIVersionTrackingGameEngine";
  56. public EntitiesDB entitiesDB { set; private get; }
  57. public int type => -1;
  58. public bool isRemovable => false;
  59. public IEntityFactory Factory { set; private get; }
  60. public void Dispose() { }
  61. public void Ready()
  62. {
  63. EGID egid = new EGID(0u, ApiExclusiveGroups.versionGroup);
  64. if (!entitiesDB.Exists<ModVersionStruct>(egid))
  65. {
  66. Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
  67. int v = (currentVersion.Major * 1000) + (currentVersion.Minor);
  68. Factory.BuildEntity<ModVersionDescriptor>(egid).Init<ModVersionStruct>(new ModVersionStruct
  69. {
  70. version = (uint)v
  71. });
  72. }
  73. }
  74. public uint GetGameVersion()
  75. {
  76. return entitiesDB.QueryUniqueEntity<ModVersionStruct>(ApiExclusiveGroups.versionGroup).version;
  77. }
  78. public void Emit() { }
  79. }
  80. [Obsolete]
  81. public struct ModVersionStruct : IEntityComponent
  82. {
  83. public uint version;
  84. }
  85. [Obsolete]
  86. public class ModVersionDescriptor: SerializableEntityDescriptor<ModVersionDescriptor._ModVersionDescriptor>
  87. {
  88. [HashName("GamecraftModdingAPIVersionV0")]
  89. public class _ModVersionDescriptor : IEntityDescriptor
  90. {
  91. public IComponentBuilder[] componentsToBuild { get; } = new IComponentBuilder[]{
  92. new SerializableComponentBuilder<SerializationType, ModVersionStruct>(((int)SerializationType.Network, new DefaultSerializer<ModVersionStruct>()), ((int)SerializationType.Storage, new DefaultSerializer<ModVersionStruct>())),
  93. };
  94. }
  95. }
  96. }