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 2.5KB

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