game mode stuff, missing implementation of 4 methods coming soon™️

This commit is contained in:
AlchlcDvl committed 2024-12-07 10:46:26 -05:00
1 parent bc756d5c1b
commit dbacd044eb
18 files changed
+216 -165

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@
<ItemGroup>
<PackageReference Include="Reactor" Version="2.3.1" />
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.697" Private="false" ExcludeAssets="runtime;native" />
<PackageReference Include="AmongUs.GameLibs.Steam" Version="2024.9.4" PrivateAssets="all" />
<PackageReference Include="AmongUs.GameLibs.Steam" Version="2024.10.29" PrivateAssets="all" />
<PackageReference Include="BepInEx.AutoPlugin" Version="1.1.0" PrivateAssets="all" />
<PackageReference Include="BepInEx.IL2CPP.MSBuild" Version="2.0.1" PrivateAssets="all" ExcludeAssets="runtime" />
@@ -1,11 +1,12 @@
using System.Collections.Generic;
using UnityEngine;
namespace MiraAPI.GameModes;
/// <summary>
/// Base class for custom gamemodes.
/// </summary>
public abstract class CustomGameMode
public abstract class AbstractGameMode
{
/// <summary>
/// Gets the game mode name.
@@ -18,9 +19,14 @@ public abstract class CustomGameMode
public abstract string Description { get; }
/// <summary>
/// Gets the game mode ID.
/// Gets the game mode description.
/// </summary>
public abstract int Id { get; }
public virtual Color Color { get; } = Color.white;
/// <summary>
/// Gets or sets the game mode id.
/// </summary>
public uint ID { get; set; }
/// <summary>
/// Called when Intro Cutscene is destroyed.
+62 -38
View File
@@ -1,7 +1,9 @@
using Reactor.Utilities;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Il2CppSystem.Threading.Tasks;
using MiraAPI.GameOptions;
using MiraAPI.PluginLoading;
using Reactor.Utilities;
namespace MiraAPI.GameModes;
@@ -10,62 +12,84 @@ namespace MiraAPI.GameModes;
/// </summary>
public static class CustomGameModeManager
{
/// <summary>
/// List of registered gamemodes.
/// </summary>
internal static readonly Dictionary<int, CustomGameMode> GameModes = [];
private static readonly Dictionary<uint, AbstractGameMode> IdToModeMap = [];
public static bool IsDefault()
private static uint _nextId;
private static uint GetNextId()
{
return ActiveMode?.Id == 0;
_nextId++;
return _nextId;
}
/// <summary>
/// Current gamemode.
/// </summary>
public static CustomGameMode? ActiveMode { get; internal set; } = new DefaultMode();
/// <summary>
/// Set current gamemode.
/// </summary>
/// <param name="id">gamemode ID.</param>
public static void SetGameMode(int id)
{
if (GameModes.TryGetValue(id, out var gameMode))
{
ActiveMode = gameMode;
return;
}
Logger<MiraApiPlugin>.Error($"No gamemode with id {id} found!");
}
internal static uint LastId => _nextId;
/// <summary>
/// Register gamemode from type.
/// </summary>
/// <param name="gameModeType">Type of gamemode class, should inherit from <see cref="CustomGameMode"/>.</param>
internal static void RegisterGameMode(Type gameModeType)
/// <param name="gameModeType">Type of gamemode class, should inherit from <see cref="AbstractGameMode"/>.</param>
/// <param name="pluginInfo">The custom plugin info of the mod.</param>
internal static void RegisterGameMode(Type gameModeType, MiraPluginInfo pluginInfo)
{
if (!typeof(CustomGameMode).IsAssignableFrom(gameModeType))
if (!typeof(AbstractGameMode).IsAssignableFrom(gameModeType))
{
Logger<MiraApiPlugin>.Warning($"{gameModeType.Name} does not inherit CustomGameMode!");
return;
}
var modeObj = Activator.CreateInstance(gameModeType);
var instance = Activator.CreateInstance(gameModeType);
if (modeObj is not CustomGameMode gameMode)
if (instance is not AbstractGameMode mode)
{
Logger<MiraApiPlugin>.Error($"Failed to create instance of {gameModeType.Name}");
return;
}
if (GameModes.Any(x => x.Key == gameMode.Id))
IdToModeMap.Add(GetNextId(), mode);
pluginInfo.GameModes.Add(_nextId, mode);
mode.ID = _nextId;
}
/// <summary>
/// Checks to see if the default game mode is on.
/// </summary>
/// <returns>True if the default mode is one.</returns>
public static bool IsDefault() => (uint)OptionGroupSingleton<GameModeOption>.Instance.CurrentMode.Value == 0;
/// <summary>
/// Gets the current gamemode.
/// </summary>
public static AbstractGameMode? ActiveMode { get; internal set; }
internal static void RegisterDefaultMode()
{
var defaultMode = new DefaultMode();
IdToModeMap.Add(0, defaultMode);
defaultMode.ID = 0;
}
internal static void SetGameMode(uint id)
{
if (IdToModeMap.TryGetValue(id, out var mode))
{
Logger<MiraApiPlugin>.Error($"ID for gamemode {gameMode.Name} already exists!");
ActiveMode = mode;
return;
}
GameModes.Add(gameMode.Id, gameMode);
ActiveMode = IdToModeMap[0];
Logger<MiraApiPlugin>.Warning($"Unable to find game mode of id {id}!");
}
internal static void GetAndSetGameMode()
{
var id = (uint)OptionGroupSingleton<GameModeOption>.Instance.CurrentMode.Value;
if (IdToModeMap.TryGetValue(id, out var mode))
{
ActiveMode = mode;
return;
}
ActiveMode = IdToModeMap[0];
Logger<MiraApiPlugin>.Warning($"Unable to find game mode of id {id}!");
}
}
+7 -3
View File
@@ -1,9 +1,13 @@
namespace MiraAPI.GameModes;
[RegisterGameMode]
public class DefaultMode : CustomGameMode
/// <summary>
/// The default game mode.
/// </summary>
public class DefaultMode : AbstractGameMode
{
/// <inheritdoc/>
public override string Name => "Default";
/// <inheritdoc/>
public override string Description => "Default Among Us GameMode";
public override int Id => 0;
}
+18
View File
@@ -0,0 +1,18 @@
using MiraAPI.GameOptions;
using MiraAPI.GameOptions.OptionTypes;
namespace MiraAPI.GameModes;
/// <summary>
/// The game mode option.
/// </summary>
public class GameModeOption : AbstractOptionGroup
{
/// <inheritdoc/>
public override string GroupName => "Game Mode";
public ModdedNumberOption CurrentMode { get; set; } = new("Current Game Mode", 0, 0, CustomGameModeManager.LastId, 1, Utilities.MiraNumberSuffixes.None)
{
ChangedEvent = x => CustomGameModeManager.SetGameMode((uint)x),
};
}
+4 -23
View File
@@ -1,28 +1,9 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace MiraAPI.GameModes;
/// <summary>
/// Registers a game mode to MiraAPI.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class RegisterGameModeAttribute : Attribute
{
private static readonly HashSet<Assembly> RegisteredAssemblies = [];
internal static void Register(Assembly assembly)
{
if (!RegisteredAssemblies.Add(assembly))
{
return;
}
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute<RegisterGameModeAttribute>();
if (attribute != null)
{
CustomGameModeManager.RegisterGameMode(type);
}
}
}
}
public class RegisterGameModeAttribute : Attribute;
+11 -1
View File
@@ -1,4 +1,5 @@
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using MiraAPI.PluginLoading;
@@ -14,15 +15,22 @@ namespace MiraAPI;
[BepInProcess("Among Us.exe")]
[BepInDependency(ReactorPlugin.Id)]
[ReactorModFlags(ModFlags.RequireOnAllClients)]
public partial class MiraApiPlugin : BasePlugin
public partial class MiraApiPlugin : BasePlugin, IMiraPlugin
{
private static MiraPluginManager? PluginManager { get; set; }
public Harmony Harmony { get; } = new(Id);
public static Color MiraColor { get; } = new Color32(238, 154, 112, 255);
public static MiraApiPlugin Instance { get; private set; }
public string OptionsTitleText => "Mira API";
public override void Load()
{
Instance = this;
Harmony.PatchAll();
ReactorCredits.Register("Mira API", Version, true, ReactorCredits.AlwaysShow);
@@ -30,4 +38,6 @@ public partial class MiraApiPlugin : BasePlugin
PluginManager = new MiraPluginManager();
PluginManager.Initialize();
}
public ConfigFile GetConfigFile() => Config;
}
+9 -9
View File
@@ -1,14 +1,16 @@
/*
using HarmonyLib;
using InnerNet;
using MiraAPI.GameModes;
[HarmonyPatch]
public static class ConsolePatches
{
[HarmonyPrefix]
[HarmonyPatch(typeof(Console), nameof(Console.CanUse))]
[HarmonyPrefix, HarmonyPatch(typeof(Console), nameof(Console.CanUse))]
public static bool CanUsePatch(Console __instance, [HarmonyArgument(0)] NetworkedPlayerInfo pc, [HarmonyArgument(1)] out bool canUse, [HarmonyArgument(2)] out bool couldUse)
{
if (AmongUsClient.Instance.GameState == InnerNetClient.GameStates.Started && ShipStatus.Instance)
{
if (CustomGameModeManager.ActiveMode != null && CustomGameModeManager.ActiveMode.CanUseConsole(__instance))
if (CustomGameModeManager.ActiveMode != null && CustomGameModeManager.ActiveMode.CanUseTasks(__instance))
{
var task = __instance.FindTask(pc.Object);
@@ -31,8 +33,7 @@ public static class ConsolePatches
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(SystemConsole), nameof(SystemConsole.CanUse))]
[HarmonyPrefix, HarmonyPatch(typeof(SystemConsole), nameof(SystemConsole.CanUse))]
public static bool SystemCanUsePatch(SystemConsole __instance, [HarmonyArgument(0)] NetworkedPlayerInfo pc, [HarmonyArgument(1)] out bool canUse, [HarmonyArgument(2)] out bool couldUse)
{
if (AmongUsClient.Instance.GameState == InnerNetClient.GameStates.Started && ShipStatus.Instance)
@@ -50,8 +51,7 @@ public static class ConsolePatches
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(MapConsole), nameof(MapConsole.CanUse))]
[HarmonyPrefix, HarmonyPatch(typeof(MapConsole), nameof(MapConsole.CanUse))]
public static bool MapCanUsePatch(MapConsole __instance, [HarmonyArgument(0)] NetworkedPlayerInfo pc, [HarmonyArgument(1)] out bool canUse, [HarmonyArgument(2)] out bool couldUse)
{
if (AmongUsClient.Instance.GameState == InnerNetClient.GameStates.Started && ShipStatus.Instance)
@@ -68,4 +68,4 @@ public static class ConsolePatches
canUse = couldUse = true;
return true;
}
}*/
}
+6 -5
View File
@@ -1,11 +1,12 @@
/*
using HarmonyLib;
using MiraAPI.GameModes;
[HarmonyPatch(typeof(DeadBody))]
public static class DeadBodyPatch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(DeadBody.OnClick))]
[HarmonyPrefix, HarmonyPatch(nameof(DeadBody.OnClick))]
public static bool OnClickPatch(DeadBody __instance)
{
return CustomGameModeManager.ActiveMode == null || CustomGameModeManager.ActiveMode.CanReport(__instance);
return CustomGameModeManager.ActiveMode?.CanReport(__instance) == true;
}
}*/
}
@@ -1,4 +1,6 @@
/*
using HarmonyLib;
using MiraAPI.GameModes;
[HarmonyPatch]
public static class GameLogicPatches
{
@@ -17,4 +19,4 @@ public static class GameLogicPatches
CustomGameModeManager.ActiveMode?.AssignRoles(out runOriginal, __instance);
return runOriginal;
}
}*/
}
+12
View File
@@ -0,0 +1,12 @@
using HarmonyLib;
using MiraAPI.GameModes;
[HarmonyPatch]
public static class HudPatches
{
[HarmonyPostfix, HarmonyPatch(typeof(HudManager), nameof(HudManager.Start))]
public static void HudStartPatch(HudManager __instance) => CustomGameModeManager.ActiveMode?.HudStart(__instance);
[HarmonyPostfix, HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))]
public static void HudUpdatePatch(HudManager __instance) => CustomGameModeManager.ActiveMode?.HudUpdate(__instance);
}
@@ -1,4 +1,6 @@
/*
using HarmonyLib;
using MiraAPI.GameModes;
/// <summary>
/// Allow Impostors to kill each other if can kill is enabled in gamemode or friendly fire is toggled on
/// </summary>
@@ -16,9 +18,8 @@ public static class ImpostorTargetPatch
}
__result = target is { Disconnected: false, IsDead: false } &&
target.PlayerId != __instance.Player.PlayerId && !(target.Role == null) &&
!(target.Object == null) && !target.Object.inVent && !target.Object.inMovingPlat;
target.PlayerId != __instance.Player.PlayerId && target.Role != null &&
target.Object != null && !target.Object.inVent && !target.Object.inMovingPlat;
return false;
}
}*/
}
@@ -1,9 +1,10 @@
/*
using HarmonyLib;
using MiraAPI.GameModes;
[HarmonyPatch(typeof(MapBehaviour))]
public static class MapBehaviourPatch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(MapBehaviour.ShowSabotageMap))]
[HarmonyPrefix, HarmonyPatch(nameof(MapBehaviour.ShowSabotageMap))]
public static bool ShowSabotagePatch(MapBehaviour __instance)
{
var shouldShow = CustomGameModeManager.ActiveMode?.ShouldShowSabotageMap(__instance);
@@ -15,4 +16,4 @@ public static class MapBehaviourPatch
return true;
}
}*/
}
+5 -4
View File
@@ -1,11 +1,12 @@
/*
using HarmonyLib;
using MiraAPI.GameModes;
[HarmonyPatch(typeof(KillAnimation))]
public static class OnDeathPatch
{
[HarmonyPostfix]
[HarmonyPatch(nameof(KillAnimation.CoPerformKill))]
[HarmonyPostfix, HarmonyPatch(nameof(KillAnimation.CoPerformKill))]
public static void OnDeathPostfix([HarmonyArgument(0)] PlayerControl source, [HarmonyArgument(1)] PlayerControl target)
{
CustomGameModeManager.ActiveMode?.OnDeath(target);
}
}*/
}
-47
View File
@@ -1,47 +0,0 @@
/*
[HarmonyPatch(typeof(Vent))]
public static class VentPatches
{
[HarmonyPrefix]
[HarmonyPatch(nameof(Vent.CanUse))]
public static bool CanUseVentPatch(Vent __instance, ref float __result, [HarmonyArgument(0)] NetworkedPlayerInfo pc, [HarmonyArgument(1)] ref bool canUse, [HarmonyArgument(2)] ref bool couldUse)
{
if (CustomGameModeManager.ActiveMode?.CanVent(__instance, pc) == false)
{
return couldUse = canUse = false;
}
if (pc.Role is not ICustomRole role)
{
return couldUse = canUse = true;
}
var num = float.MaxValue;
var @object = pc.Object;
var customRoleUsable = role.CanUseVent;
canUse = couldUse = customRoleUsable && !pc.IsDead && (@object.CanMove || @object.inVent);
if (canUse)
{
var center = @object.Collider.bounds.center;
var position = __instance.transform.position;
num = Vector2.Distance(center, position);
canUse &= num <= __instance.UsableDistance && !PhysicsHelpers.AnythingBetween(@object.Collider, center, position, Constants.ShipOnlyMask, false);
}
__result = num;
return false;
}
[HarmonyPrefix]
[HarmonyPatch(nameof(Vent.SetOutline))]
public static bool SetOutlinePatch(Vent __instance, [HarmonyArgument(0)] bool on, [HarmonyArgument(1)] bool mainTarget)
{
var color = PlayerControl.LocalPlayer.Data.Role is ICustomRole role
? role.RoleColor : PlayerControl.LocalPlayer.Data.Role.IsImpostor ? Palette.ImpostorRed : Palette.CrewmateBlue;
__instance.myRend.material.SetFloat(ShaderID.Outline, on ? 1 : 0);
__instance.myRend.material.SetColor(ShaderID.OutlineColor, color);
__instance.myRend.material.SetColor(ShaderID.AddColor, mainTarget ? color : Color.clear);
return false;
}
}*/
-11
View File
@@ -16,17 +16,6 @@ public static class HudManagerPatches
// Custom buttons parent.
private static GameObject? _bottomLeft;
/*
/// <summary>
/// Trigger hudstart on current custom gamemode
/// </summary>
[HarmonyPostfix]
[HarmonyPatch(nameof(HudManager.OnGameStart))]
public static void GameStartPatch(HudManager __instance)
{
CustomGameModeManager.ActiveMode?.HudStart(__instance);
}*/
/// <summary>
/// Create custom buttons and arrange them on the hud.
/// </summary>
+10 -5
View File
@@ -3,12 +3,8 @@ using BepInEx.Configuration;
using MiraAPI.GameModes;
using MiraAPI.GameOptions;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Linq;
using MiraAPI.GameOptions.OptionTypes;
using MiraAPI.Hud;
using MiraAPI.Roles;
namespace MiraAPI.PluginLoading;
@@ -52,6 +48,15 @@ public class MiraPluginInfo
return new ReadOnlyDictionary<ushort, RoleBehaviour>(CustomRoles);
}
/// <summary>
/// Gets a read only dictionary of Game Mode IDs and the AbstractGameMode object they are associated with.
/// </summary>
/// <returns>Read only dictionary of IDs and Game Modes.</returns>
public ReadOnlyDictionary<uint, AbstractGameMode> GetModes()
{
return new ReadOnlyDictionary<uint, AbstractGameMode>(GameModes);
}
/// <summary>
/// Gets a read only collection of this plugin's custom buttons.
/// </summary>
@@ -67,7 +72,7 @@ public class MiraPluginInfo
internal Dictionary<ushort, RoleBehaviour> CustomRoles { get; } = [];
internal Dictionary<int, CustomGameMode> GameModes { get; } = [];
internal Dictionary<uint, AbstractGameMode> GameModes { get; } = [];
internal List<CustomActionButton> Buttons { get; } = [];
+47 -4
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection;
using BepInEx.Unity.IL2CPP;
using MiraAPI.Colors;
using MiraAPI.GameModes;
using MiraAPI.GameOptions;
using MiraAPI.GameOptions.Attributes;
using MiraAPI.Hud;
@@ -29,6 +30,7 @@ public sealed class MiraPluginManager
internal void Initialize()
{
Instance = this;
IL2CPPChainloader.Instance.PluginLoad += (pluginInfo, assembly, plugin) =>
{
if (plugin is not IMiraPlugin miraPlugin)
@@ -46,21 +48,49 @@ public sealed class MiraPluginManager
RegisterColorClasses(assembly);
RegisterGameModeAttribute(assembly, info);
_registeredPlugins.Add(assembly, info);
Logger<MiraApiPlugin>.Info($"Registering mod {pluginInfo.Metadata.GUID} with Mira API.");
};
IL2CPPChainloader.Instance.Finished += PaletteManager.RegisterAllColors;
IL2CPPChainloader.Instance.Finished += FinishInitialisation;
}
private void FinishInitialisation()
{
var plugin = IL2CPPChainloader.Instance.Plugins["mira.api"];
var info = new MiraPluginInfo(MiraApiPlugin.Instance, plugin);
var assembly = typeof(MiraPluginManager).Assembly;
RegisterModifierAttribute(assembly);
RegisterAllOptions(assembly, info);
RegisterRoleAttribute(assembly, info);
RegisterButtonAttribute(assembly, info);
RegisterColorClasses(assembly);
RegisterGameModeAttribute(assembly, info);
_registeredPlugins.Add(assembly, info);
PaletteManager.RegisterAllColors();
CustomGameModeManager.RegisterDefaultMode();
CustomGameModeManager.GetAndSetGameMode();
Logger<MiraApiPlugin>.Info("Initialised mira stuff");
}
/// <summary>
/// Get a mira plugin by its GUID.
/// </summary>
/// <param name="guid">The plugin GUID.</param>
/// <param name="pluginGUID">The plugin GUID.</param>
/// <returns>A MiraPluginInfo.</returns>
public static MiraPluginInfo GetPluginByGuid(string guid)
public static MiraPluginInfo GetPluginByGuid(string pluginGUID)
{
return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == guid);
return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == pluginGUID);
}
private static void RegisterAllOptions(Assembly assembly, MiraPluginInfo pluginInfo)
@@ -180,4 +210,17 @@ public sealed class MiraPluginManager
}
}
}
private static void RegisterGameModeAttribute(Assembly assembly, MiraPluginInfo pluginInfo)
{
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute<RegisterGameModeAttribute>();
if (attribute != null)
{
CustomGameModeManager.RegisterGameMode(type, pluginInfo);
}
}
}
}