2020-04-29 01:56:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
using RobocraftX.Common;
|
|
|
|
|
using Svelto.ECS;
|
|
|
|
|
using Svelto.ECS.Serialization;
|
|
|
|
|
|
|
|
|
|
using GamecraftModdingAPI.Persistence;
|
|
|
|
|
using GamecraftModdingAPI.Events;
|
|
|
|
|
|
|
|
|
|
namespace GamecraftModdingAPI.Utility
|
|
|
|
|
{
|
2020-04-29 02:59:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tracks the API version the current game was built for.
|
|
|
|
|
/// For compatibility reasons, this must be enabled before it will work.
|
|
|
|
|
/// </summary>
|
2020-04-29 01:56:34 +00:00
|
|
|
|
public static class VersionTracking
|
|
|
|
|
{
|
|
|
|
|
private static readonly VersionTrackingEngine versionEngine = new VersionTrackingEngine();
|
|
|
|
|
|
|
|
|
|
private static bool isEnabled = false;
|
|
|
|
|
|
2020-04-29 02:59:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the API version saved in the current game.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The version.</returns>
|
2020-04-29 01:56:34 +00:00
|
|
|
|
public static uint GetVersion()
|
|
|
|
|
{
|
|
|
|
|
if (!isEnabled) return 0u;
|
|
|
|
|
return versionEngine.GetGameVersion();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 02:59:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enable API version tracking.
|
|
|
|
|
/// </summary>
|
2020-04-29 01:56:34 +00:00
|
|
|
|
public static void Enable()
|
|
|
|
|
{
|
2020-05-17 15:52:55 +00:00
|
|
|
|
if (!SerializerManager.ExistsSerializer(typeof(ModVersionStruct).FullName))
|
|
|
|
|
{
|
|
|
|
|
SerializerManager.AddSerializer<ModVersionDescriptor>(new SimpleEntitySerializer<ModVersionDescriptor>(
|
|
|
|
|
(_) => { return new EGID[1] { new EGID(0u, ApiExclusiveGroups.versionGroup) }; }
|
|
|
|
|
));
|
|
|
|
|
}
|
2020-04-29 01:56:34 +00:00
|
|
|
|
EventManager.AddEventEmitter(versionEngine);
|
|
|
|
|
isEnabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 02:59:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disable API version tracking.
|
|
|
|
|
/// </summary>
|
2020-04-29 01:56:34 +00:00
|
|
|
|
public static void Disable()
|
|
|
|
|
{
|
|
|
|
|
EventManager.AddEventEmitter(versionEngine);
|
|
|
|
|
isEnabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 15:52:55 +00:00
|
|
|
|
public static void Init() { }
|
|
|
|
|
|
2020-04-29 01:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class VersionTrackingEngine : IEventEmitterEngine
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; } = "GamecraftModdingAPIVersionTrackingGameEngine";
|
|
|
|
|
|
|
|
|
|
public EntitiesDB entitiesDB { set; private get; }
|
|
|
|
|
|
|
|
|
|
public int type => -1;
|
|
|
|
|
|
|
|
|
|
public bool isRemovable => false;
|
|
|
|
|
|
|
|
|
|
public IEntityFactory Factory { set; private get; }
|
|
|
|
|
|
|
|
|
|
public void Dispose() { }
|
|
|
|
|
|
|
|
|
|
public void Ready()
|
|
|
|
|
{
|
|
|
|
|
EGID egid = new EGID(0u, ApiExclusiveGroups.versionGroup);
|
|
|
|
|
if (!entitiesDB.Exists<ModVersionStruct>(egid))
|
|
|
|
|
{
|
|
|
|
|
Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
|
|
|
|
|
int v = (currentVersion.Major * 1000) + (currentVersion.Minor);
|
|
|
|
|
Factory.BuildEntity<ModVersionDescriptor>(egid).Init<ModVersionStruct>(new ModVersionStruct
|
|
|
|
|
{
|
|
|
|
|
version = (uint)v
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint GetGameVersion()
|
|
|
|
|
{
|
|
|
|
|
return entitiesDB.QueryUniqueEntity<ModVersionStruct>(ApiExclusiveGroups.versionGroup).version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Emit() { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct ModVersionStruct : IEntityComponent
|
|
|
|
|
{
|
|
|
|
|
public uint version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ModVersionDescriptor: SerializableEntityDescriptor<ModVersionDescriptor._ModVersionDescriptor>
|
|
|
|
|
{
|
|
|
|
|
[HashName("GamecraftModdingAPIVersionV0")]
|
|
|
|
|
public class _ModVersionDescriptor : IEntityDescriptor
|
|
|
|
|
{
|
|
|
|
|
public IComponentBuilder[] componentsToBuild { get; } = new IComponentBuilder[]{
|
|
|
|
|
new SerializableComponentBuilder<SerializationType, ModVersionStruct>(((int)SerializationType.Network, new DefaultSerializer<ModVersionStruct>()), ((int)SerializationType.Storage, new DefaultSerializer<ModVersionStruct>())),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|