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.

116 lines
3.2KB

  1. using System;
  2. using System.Reflection;
  3. using RobocraftX.Common;
  4. using Svelto.ECS;
  5. using Svelto.ECS.Serialization;
  6. using TechbloxModdingAPI.Events;
  7. using TechbloxModdingAPI.Persistence;
  8. namespace TechbloxModdingAPI.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; } = "TechbloxModdingAPIVersionTrackingGameEngine";
  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("TechbloxModdingAPIVersionV0")]
  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. }