From 74592b2558cebd00a92c0c238eb1a013fe1f19bb Mon Sep 17 00:00:00 2001
From: XtraCube <72575280+XtraCube@users.noreply.github.com>
Date: Tue, 27 Aug 2024 14:29:06 -0400
Subject: [PATCH] oops role ID had issues
---
.../Patches/Options/GameOptionsMenuPatch.cs | 4 +--
.../Patches/Options/GameSettingMenuPatches.cs | 4 +--
MiraAPI/PluginLoading/MiraPluginManager.cs | 31 +++++++----------
MiraAPI/Roles/CustomRoleManager.cs | 33 ++++++++++++++++---
MiraAPI/Roles/RoleId.cs | 6 ++--
5 files changed, 46 insertions(+), 32 deletions(-)
diff --git a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs
index 7f30b85..0a488ff 100644
--- a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs
+++ b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs
@@ -96,12 +96,12 @@ public static class GameOptionsMenuPatch
categoryHeaderMasked.gameObject.SetActive(false);
group.Header = categoryHeaderMasked;
- TextMeshPro newText = GameObject.Instantiate(categoryHeaderMasked.Title, categoryHeaderMasked.transform);
+ TextMeshPro newText = Object.Instantiate(categoryHeaderMasked.Title, categoryHeaderMasked.transform);
newText.text = $"(Click to open)";
newText.transform.localPosition = new Vector3(2.6249f, -0.165f, 0f);
newText.gameObject.GetComponent().Destroy();
- List newOpts = new();
+ List newOpts = [];
foreach (var opt in group.Options)
{
OptionBehaviour newOpt = opt.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.settingsContainer);
diff --git a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs
index aa479c1..6da7827 100644
--- a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs
+++ b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs
@@ -95,7 +95,7 @@ public static class GameSettingMenuPatches
UpdateText(menu, settings, roles);
}
- string name = SelectedMod?.MiraPlugin.OptionsTitleText;
+ var name = SelectedMod?.MiraPlugin.OptionsTitleText;
_text.text = name?[..Math.Min(name.Length, 25)];
}
@@ -105,7 +105,7 @@ public static class GameSettingMenuPatches
if (CurrentSelectedMod != 0)
{
- if (SelectedMod.OptionGroups.Count == 0)
+ if (SelectedMod.Options.Where(x=>x.AdvancedRole==null).ToList().Count == 0)
{
menu.GameSettingsButton.gameObject.SetActive(false);
}
diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs
index 8a48e38..13c9c37 100644
--- a/MiraAPI/PluginLoading/MiraPluginManager.cs
+++ b/MiraAPI/PluginLoading/MiraPluginManager.cs
@@ -89,6 +89,7 @@ internal class MiraPluginManager
private static void RegisterRoleAttribute(Assembly assembly, MiraPluginInfo pluginInfo)
{
+ List roles = [];
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute();
@@ -97,32 +98,22 @@ internal class MiraPluginManager
continue;
}
+ if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type)))
+ {
+ Logger.Error($"{type.Name} does not inherit from RoleBehaviour or ICustomRole.");
+ continue;
+ }
+
if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients)
{
Logger.Error("Custom roles are only supported on all clients.");
return;
}
-
- ClassInjector.RegisterTypeInIl2Cpp(type);
-
- var role = CustomRoleManager.RegisterRole(type, pluginInfo);
-
- try
- {
- pluginInfo.CustomRoles.Add((ushort)role.Role, role);
- }
- catch (Exception e)
- {
- Logger.Error("Failed to register role: " + type.Name);
-
- foreach (var (k, v) in pluginInfo.CustomRoles)
- {
- Logger.Error($"{k}: {v}");
- }
-
- Logger.Error(e);
- }
+
+ roles.Add(type);
}
+
+ CustomRoleManager.RegisterRoleTypes(roles, pluginInfo);
}
private static void RegisterColorClasses(Assembly assembly)
diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs
index 0cbd453..74e0bcf 100644
--- a/MiraAPI/Roles/CustomRoleManager.cs
+++ b/MiraAPI/Roles/CustomRoleManager.cs
@@ -9,6 +9,7 @@ using Reactor.Utilities.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
+using Il2CppInterop.Runtime.Injection;
using MiraAPI.Utilities;
using TMPro;
using UnityEngine;
@@ -19,7 +20,9 @@ namespace MiraAPI.Roles;
public static class CustomRoleManager
{
public static readonly Dictionary CustomRoles = new();
-
+
+ public static readonly Dictionary RoleIds = new();
+
private static ushort _roleId = 100;
private static ushort GetNextRoleId()
@@ -32,7 +35,25 @@ public static class CustomRoleManager
RoleManager.Instance.AllRoles = RoleManager.Instance.AllRoles.Concat(CustomRoles.Values).ToArray();
}
- internal static RoleBehaviour RegisterRole(Type roleType, MiraPluginInfo parentMod)
+ public static void RegisterRoleTypes(List roles, MiraPluginInfo pluginInfo)
+ {
+ roles.ForEach(x=>RoleIds.Add(x, GetNextRoleId()));
+
+ foreach (var roleType in roles)
+ {
+ ClassInjector.RegisterTypeInIl2Cpp(roleType);
+
+ var role = RegisterRole(roleType, pluginInfo);
+ if (role is null)
+ {
+ continue;
+ }
+
+ pluginInfo.CustomRoles.Add((ushort)role.Role, role);
+ }
+ }
+
+ private static RoleBehaviour RegisterRole(Type roleType, MiraPluginInfo parentMod)
{
if (!(typeof(RoleBehaviour).IsAssignableFrom(roleType) && typeof(ICustomRole).IsAssignableFrom(roleType)))
{
@@ -47,8 +68,10 @@ public static class CustomRoleManager
roleBehaviour.gameObject.Destroy();
return null;
}
+
+ var roleId = RoleIds[roleType];
- roleBehaviour.Role = (RoleTypes)GetNextRoleId();
+ roleBehaviour.Role = (RoleTypes)roleId;
roleBehaviour.TeamType = customRole.Team == ModdedRoleTeams.Neutral ? RoleTeamTypes.Crewmate : (RoleTeamTypes)customRole.Team;
roleBehaviour.NameColor = customRole.RoleColor;
roleBehaviour.StringName = CustomStringName.CreateAndRegister(customRole.RoleName);
@@ -68,7 +91,7 @@ public static class CustomRoleManager
RoleManager.GhostRoles.Add(roleBehaviour.Role);
}
- CustomRoles.Add(_roleId, roleBehaviour);
+ CustomRoles.Add(roleId, roleBehaviour);
if (customRole.HideSettings)
{
@@ -78,7 +101,7 @@ public static class CustomRoleManager
var config = parentMod.PluginConfig;
config.Bind(customRole.NumConfigDefinition, 1);
config.Bind(customRole.ChanceConfigDefinition, 100);
-
+
return roleBehaviour;
}
diff --git a/MiraAPI/Roles/RoleId.cs b/MiraAPI/Roles/RoleId.cs
index bb7b714..f9d30c0 100644
--- a/MiraAPI/Roles/RoleId.cs
+++ b/MiraAPI/Roles/RoleId.cs
@@ -7,11 +7,11 @@ public class RoleId
{
public static ushort Get() where T : RoleBehaviour
{
- if (!CustomRoleManager.CustomRoles.Values.OfType().Any())
+ if (!CustomRoleManager.RoleIds.TryGetValue(typeof(T), out var roleId))
{
throw new InvalidOperationException($"Role {typeof(T)} is not registered");
}
-
- return (ushort)CustomRoleManager.CustomRoles.Values.OfType().Single().Role;
+
+ return roleId;
}
}
\ No newline at end of file