oops role ID had issues

This commit is contained in:
XtraCube committed 2024-08-27 14:29:06 -04:00
1 parent 157c5af280
commit 74592b2558
5 files changed
+46 -32

No files matched your search

@@ -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 = $"<size=70%>(Click to open)</size>";
newText.transform.localPosition = new Vector3(2.6249f, -0.165f, 0f);
newText.gameObject.GetComponent<TextTranslatorTMP>().Destroy();
List<OptionBehaviour> newOpts = new();
List<OptionBehaviour> newOpts = [];
foreach (var opt in group.Options)
{
OptionBehaviour newOpt = opt.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.settingsContainer);
@@ -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);
}
+11 -20
View File
@@ -89,6 +89,7 @@ internal class MiraPluginManager
private static void RegisterRoleAttribute(Assembly assembly, MiraPluginInfo pluginInfo)
{
List<Type> roles = [];
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute<RegisterCustomRoleAttribute>();
@@ -97,32 +98,22 @@ internal class MiraPluginManager
continue;
}
if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type)))
{
Logger<MiraApiPlugin>.Error($"{type.Name} does not inherit from RoleBehaviour or ICustomRole.");
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, pluginInfo);
try
{
pluginInfo.CustomRoles.Add((ushort)role.Role, role);
}
catch (Exception e)
{
Logger<MiraApiPlugin>.Error("Failed to register role: " + type.Name);
foreach (var (k, v) in pluginInfo.CustomRoles)
{
Logger<MiraApiPlugin>.Error($"{k}: {v}");
}
Logger<MiraApiPlugin>.Error(e);
}
roles.Add(type);
}
CustomRoleManager.RegisterRoleTypes(roles, pluginInfo);
}
private static void RegisterColorClasses(Assembly assembly)
+28 -5
View File
@@ -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<ushort, RoleBehaviour> CustomRoles = new();
public static readonly Dictionary<Type, ushort> 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<Type> 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;
}
+3 -3
View File
@@ -7,11 +7,11 @@ public class RoleId
{
public static ushort Get<T>() where T : RoleBehaviour
{
if (!CustomRoleManager.CustomRoles.Values.OfType<T>().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<T>().Single().Role;
return roleId;
}
}