sequential role IDs

This commit is contained in:
XtraCube committed 2024-08-22 18:48:38 -04:00
1 parent 62ec462e79
commit a0a2e9a6ab
5 files changed
+39 -32

No files matched your search

+2 -2
View File
@@ -6,9 +6,9 @@ using System.Collections.Generic;
namespace MiraAPI.PluginLoading;
public class MiraPluginInfo(string id, IMiraPlugin miraPlugin, PluginInfo info)
public class MiraPluginInfo(IMiraPlugin miraPlugin, PluginInfo info)
{
public string PluginId { get; set; } = id;
public string PluginId { get; set; } = info.Metadata.GUID;
public IMiraPlugin MiraPlugin { get; set; } = miraPlugin;
public PluginInfo PluginInfo { get; set; } = info;
public ConfigFile PluginConfig { get; set; } = miraPlugin.GetConfigFile();
+11 -5
View File
@@ -10,6 +10,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MiraAPI.Hud;
using Reactor.Networking;
namespace MiraAPI.PluginLoading;
@@ -27,15 +28,14 @@ internal class MiraPluginManager
internal void Initialize()
{
Instance = this;
IL2CPPChainloader.Instance.PluginLoad += (_, assembly, plugin) =>
IL2CPPChainloader.Instance.PluginLoad += (pluginInfo, assembly, plugin) =>
{
if (!plugin.GetType().GetInterfaces().Contains(typeof(IMiraPlugin)))
{
return;
}
var id = MetadataHelper.GetMetadata(plugin.GetType()).GUID;
var info = new MiraPluginInfo(id, plugin as IMiraPlugin, IL2CPPChainloader.Instance.Plugins[id]);
var info = new MiraPluginInfo(plugin as IMiraPlugin, pluginInfo);
RegisterAllOptions(assembly, info);
@@ -44,7 +44,7 @@ internal class MiraPluginManager
RegisteredPlugins.Add(assembly, info);
Logger<MiraApiPlugin>.Info($"Registering mod {id} with Mira API.");
Logger<MiraApiPlugin>.Info($"Registering mod {pluginInfo.Metadata.GUID} with Mira API.");
};
}
@@ -92,10 +92,16 @@ internal class MiraPluginManager
{
continue;
}
if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients)
{
Logger<MiraApiPlugin>.Error("Custom roles are only supported on all clients.");
return;
}
ClassInjector.RegisterTypeInIl2Cpp(type);
var role = CustomRoleManager.RegisterRole(type, attribute.RoleId, pluginInfo);
var role = CustomRoleManager.RegisterRole(type, pluginInfo);
try
{
+8 -12
View File
@@ -20,23 +20,19 @@ public static class CustomRoleManager
{
public static readonly Dictionary<ushort, RoleBehaviour> CustomRoles = new();
private static int GetNextRoleId(ushort requestedId)
private static ushort _roleId = 100;
private static ushort GetNextRoleId()
{
while (CustomRoles.ContainsKey(requestedId) || requestedId <= Enum.GetNames<RoleTypes>().Length)
{
Logger<MiraApiPlugin>.Error(requestedId + ", " + (requestedId+1));
requestedId++;
}
return requestedId;
return _roleId++;
}
public static void RegisterInRoleManager()
internal static void RegisterInRoleManager()
{
RoleManager.Instance.AllRoles = RoleManager.Instance.AllRoles.Concat(CustomRoles.Values).ToArray();
}
internal static RoleBehaviour RegisterRole(Type roleType, ushort roleId, MiraPluginInfo parentMod)
internal static RoleBehaviour RegisterRole(Type roleType, MiraPluginInfo parentMod)
{
if (!(typeof(RoleBehaviour).IsAssignableFrom(roleType) && typeof(ICustomRole).IsAssignableFrom(roleType)))
{
@@ -52,7 +48,7 @@ public static class CustomRoleManager
return null;
}
roleBehaviour.Role = (RoleTypes)GetNextRoleId(roleId);
roleBehaviour.Role = (RoleTypes)GetNextRoleId();
roleBehaviour.TeamType = customRole.Team == ModdedRoleTeams.Neutral ? RoleTeamTypes.Crewmate : (RoleTeamTypes)customRole.Team;
roleBehaviour.NameColor = customRole.RoleColor;
roleBehaviour.StringName = CustomStringName.CreateAndRegister(customRole.RoleName);
@@ -72,7 +68,7 @@ public static class CustomRoleManager
RoleManager.GhostRoles.Add(roleBehaviour.Role);
}
CustomRoles.Add(roleId, roleBehaviour);
CustomRoles.Add(_roleId, roleBehaviour);
if (customRole.HideSettings)
{
+1 -13
View File
@@ -3,16 +3,4 @@
namespace MiraAPI.Roles;
[AttributeUsage(AttributeTargets.Class)]
public class RegisterCustomRoleAttribute : Attribute
{
public ushort RoleId { get; }
public RegisterCustomRoleAttribute()
{
RoleId = (ushort)(20 + CustomRoleManager.CustomRoles.Count);
}
public RegisterCustomRoleAttribute(ushort roleId)
{
RoleId = roleId;
}
}
public class RegisterCustomRoleAttribute : Attribute;
+17
View File
@@ -0,0 +1,17 @@
using System;
using System.Linq;
namespace MiraAPI.Roles;
public class RoleId
{
public static ushort Get<T>() where T : RoleBehaviour
{
if (!CustomRoleManager.CustomRoles.Values.OfType<T>().Any())
{
throw new InvalidOperationException($"Role {typeof(T)} is not registered");
}
return (ushort)CustomRoleManager.CustomRoles.Values.OfType<T>().Single().Role;
}
}