WIP unfinished

This commit is contained in:
XtraCube committed 2024-06-20 17:19:57 -04:00
1 parent 18853329f4
commit fd9adc62e7
13 files changed
+114 -110

No files matched your search

+2 -3
View File
@@ -15,8 +15,8 @@ namespace MiraAPI.Example;
public partial class ExamplePlugin : BasePlugin, IMiraConfig
{
public Harmony Harmony { get; } = new(Id);
public static ExamplePlugin Instance { get; private set; }
public ModdedOptionTabSettings TabSettings => new ModdedOptionTabSettings()
public ModdedOptionTabSettings TabSettings => new()
{
Title = "MiraAPI Example Mod",
TabIcon = MiraAssets.Empty
@@ -24,7 +24,6 @@ public partial class ExamplePlugin : BasePlugin, IMiraConfig
public override void Load()
{
Instance = this;
Harmony.PatchAll();
}
}
+7
View File
@@ -0,0 +1,7 @@
namespace MiraAPI.GameModes;
public struct ActiveMode
{
public string PluginId { get; set; }
public int ModeId { get; set; }
}
+1 -1
View File
@@ -4,6 +4,6 @@
public class DefaultMode : CustomGameMode
{
public override string Name => "Default";
public override string Description => "Default Among Us GameMode";
public override string Description => "Default Among Us Game Mode";
public override int Id => 0;
}
@@ -19,7 +19,7 @@ namespace MiraAPI.GameOptions.Attributes
{
foreach (var type in assembly.GetTypes())
{
foreach (PropertyInfo property in type.GetProperties())
foreach (var property in type.GetProperties())
{
foreach (var attribute in property.GetCustomAttributes())
{
@@ -18,21 +18,6 @@ namespace MiraAPI.GameOptions
private static Dictionary<Type, ModdedOptionGroup> OriginalTypes = new();
public static Dictionary<Assembly, IMiraConfig> RegisteredMods = new();
public static void Initialize()
{
IL2CPPChainloader.Instance.PluginLoad += (_, assembly, plugin) =>
{
if (plugin.GetType().GetInterfaces().Contains(typeof(IMiraConfig)))
{
IMiraConfig config = (IMiraConfig)Activator.CreateInstance(plugin.GetType());
RegisteredMods.Add(assembly, config);
}
RegisterOptionGroups(assembly);
ModdedOptionAttribute.Register(assembly);
};
}
private static void RegisterOptionGroups(Assembly assembly)
{
+1 -2
View File
@@ -1,5 +1,4 @@
using MiraAPI.Utilities;
using MiraAPI.Utilities.Assets;
using MiraAPI.Utilities.Assets;
using UnityEngine;
namespace MiraAPI
+25
View File
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using MiraAPI.GameModes;
using MiraAPI.GameOptions;
using MiraAPI.Roles;
namespace MiraAPI;
public class MiraPluginInfo
{
public string PluginId { get; set; }
public IMiraConfig Config { get; set; }
public readonly List<IModdedOptionGroup> OptionGroups = [];
public readonly List<IModdedOption> Options = [];
public readonly Dictionary<ushort, ICustomRole> CustomRoles = [];
public readonly Dictionary<int, CustomGameMode> GameModes = [];
public MiraPluginInfo(string id, IMiraConfig config)
{
PluginId = id;
Config = config;
}
}
+50
View File
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BepInEx;
using BepInEx.Unity.IL2CPP;
using MiraAPI.GameOptions.Attributes;
using MiraAPI.Roles;
using MiraAPI.Utilities;
namespace MiraAPI;
public class MiraPluginManager
{
public Dictionary<Assembly, MiraPluginInfo> RegisteredPlugins = [];
public void Initialize()
{
IL2CPPChainloader.Instance.PluginLoad += (_, assembly, plugin) =>
{
if (!plugin.GetType().GetInterfaces().Contains(typeof(IMiraConfig))) return;
var id = MetadataHelper.GetMetadata(plugin.GetType()).GUID;
var info = new MiraPluginInfo(id, plugin as IMiraConfig);
RegisterRoleAttribute(assembly, info);
ModdedOptionAttribute.Register(assembly);
RegisteredPlugins.Add(assembly, info);
};
}
private static void RegisterRoleAttribute(Assembly assembly, MiraPluginInfo pluginInfo)
{
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute<RegisterCustomRoleAttribute>();
if (attribute == null) continue;
Helpers.RegisterIl2CppType(type);
var role = CustomRoleManager.RegisterRole(type, attribute.RoleId);
pluginInfo.CustomRoles.Add((ushort)role.Role, role as ICustomRole);
}
}
}
+1
View File
@@ -6,6 +6,7 @@ using MiraAPI.Patches.Options;
using MiraAPI.Roles;
using Reactor.Utilities.Extensions;
using UnityEngine;
using UnityEngine.Events;
namespace MiraAPI.Patches
{
Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

+21 -54
View File
@@ -8,7 +8,6 @@ using Reactor.Utilities;
using Reactor.Utilities.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using MiraAPI.Networking;
using TMPro;
using UnityEngine;
@@ -18,46 +17,35 @@ namespace MiraAPI.Roles;
public static class CustomRoleManager
{
public static readonly Dictionary<ushort, RoleBehaviour> CustomRoles = [];
public static readonly Dictionary<PluginInfo, List<RoleBehaviour>> ModRoles = [];
public static void RegisterInRoleManager()
public static Dictionary<ushort, ICustomRole> CustomRoles = new();
public static int GetNextRoleId(ushort requestedId)
{
RoleManager.Instance.AllRoles = RoleManager.Instance.AllRoles.Concat(CustomRoles.Values).ToArray();
while (CustomRoles.ContainsKey(requestedId))
{
requestedId++;
}
return requestedId;
}
internal static void RegisterRole(Type roleType, ushort roleId, string modId)
internal static RoleBehaviour RegisterRole(Type roleType, ushort roleId)
{
if (!IL2CPPChainloader.Instance.Plugins.TryGetValue(modId, out PluginInfo info))
{
Debug.LogError($"{modId} is not a valid mod!");
return;
}
if (!(typeof(RoleBehaviour).IsAssignableFrom(roleType) && typeof(ICustomRole).IsAssignableFrom(roleType)))
{
return;
}
if (CustomRoles.Any(behaviour => behaviour.GetType() == roleType))
{
return;
Debug.LogError($"{roleType?.Name} does not inherit from RoleBehaviour or ICustomRole.");
return null;
}
var roleBehaviour = (RoleBehaviour)new GameObject(roleType.Name).DontDestroy().AddComponent(Il2CppType.From(roleType));
if (roleBehaviour is not ICustomRole customRole)
{
return;
roleBehaviour.gameObject.Destroy();
return null;
}
if (CustomRoles.ContainsKey(roleId))
{
Debug.LogError($"Role ID conflict for {customRole.RoleName}. Use a different ID.");
return;
}
roleBehaviour.Role = (RoleTypes)roleId;
roleBehaviour.Role = (RoleTypes)GetNextRoleId(roleId);
roleBehaviour.TeamType = customRole.Team == ModdedRoleTeams.Neutral ? RoleTeamTypes.Crewmate : (RoleTeamTypes)customRole.Team;
roleBehaviour.NameColor = customRole.RoleColor;
roleBehaviour.StringName = CustomStringName.CreateAndRegister(customRole.RoleName);
@@ -75,48 +63,27 @@ public static class CustomRoleManager
{
RoleManager.GhostRoles.Add(roleBehaviour.Role);
}
if (ModRoles.TryGetValue(info, out List<RoleBehaviour> roles))
{
roles.Add(roleBehaviour);
}
else
{
ModRoles.TryAdd(info, [roleBehaviour]);
}
CustomRoles.Add(roleId, roleBehaviour);
CustomRoles.Add(roleId, customRole);
if (customRole.HideSettings)
{
return;
return roleBehaviour;
}
var config = PluginSingleton<MiraAPIPlugin>.Instance.Config;
config.Bind(customRole.NumConfigDefinition, 1);
config.Bind(customRole.ChanceConfigDefinition, 100);
return roleBehaviour;
}
public static List<RoleBehaviour> GetRolesRegistered(string modId)
{
PluginInfo info = ModRoles.Keys.First(key => key.Metadata.GUID == modId);
if (ModRoles.TryGetValue(info, out List<RoleBehaviour> roles))
{
return roles;
}
else
{
Debug.LogError($"{modId} has no roles registered.");
return null;
}
}
public static bool GetCustomRoleBehaviour(RoleTypes roleType, out ICustomRole result)
{
CustomRoles.TryGetValue((ushort)roleType, out var temp);
if (temp is ICustomRole role)
if (temp != null)
{
result = role;
result = temp;
return true;
}
+4 -33
View File
@@ -7,40 +7,11 @@ using System.Reflection;
namespace MiraAPI.Roles;
[AttributeUsage(AttributeTargets.Class)]
public class RegisterCustomRoleAttribute : Attribute
{
private static readonly HashSet<Assembly> RegisteredAssemblies = [];
public ushort RoleId { get; }
public string ModId { get; }
public class RegisterCustomRoleAttribute(ushort roleId) : Attribute
{
public ushort RoleId { get; } = roleId;
public RegisterCustomRoleAttribute(string modId)
public RegisterCustomRoleAttribute() : this((ushort)(20 + CustomRoleManager.CustomRoles.Count))
{
ModId = modId;
RoleId = (ushort)(20 + CustomRoleManager.CustomRoles.Count);
}
public RegisterCustomRoleAttribute(string modId, ushort roleId)
{
ModId = modId;
RoleId = roleId;
}
public static void Initialize()
{
IL2CPPChainloader.Instance.PluginLoad += (_, assembly, plugin) => Register(assembly);
}
public static void Register(Assembly assembly)
{
if (!RegisteredAssemblies.Add(assembly)) return;
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute<RegisterCustomRoleAttribute>();
if (attribute != null)
{
Helpers.RegisterType(type);
CustomRoleManager.RegisterRole(type, attribute.RoleId, attribute.ModId);
}
}
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ namespace MiraAPI.Utilities
return ShipStatus.Instance.AllRooms.ToList().Find(room => room.roomArea.OverlapPoint(pos));
}
public static void RegisterType(Type type)
public static void RegisterIl2CppType(Type type)
{
if (ClassInjector.IsTypeRegisteredInIl2Cpp(type))
{