Skip to content

Commit

Permalink
Add DisableServerAuthority to ModFlags (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak authored Mar 2, 2024
1 parent 710ca79 commit d82268b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Reactor.Networking.Shared/ModFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public enum ModFlags : ushort
/// Requires the host of the lobby to have the mod.
/// </summary>
RequireOnHost = 1 << 2,

/// <summary>
/// Notifies the game server that the host has authority over game logic.
/// </summary>
DisableServerAuthority = 1 << 3,
}
6 changes: 4 additions & 2 deletions Reactor/Networking/ModList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ internal static Mod GetByPluginType(Type pluginType)
return _modByPluginType[pluginType];
}

internal static bool IsAnyModIsRequiredOnAllClients { get; private set; }
internal static bool IsAnyModRequiredOnAllClients { get; private set; }
internal static bool IsAnyModDisableServerAuthority { get; private set; }

private static readonly Dictionary<uint, Mod> _modByNetId = new();
private static readonly Dictionary<Mod, uint> _netIdByMod = new();
Expand Down Expand Up @@ -115,7 +116,8 @@ private static void Refresh()
netId++;
}

IsAnyModIsRequiredOnAllClients = Current.Any(m => m.IsRequiredOnAllClients);
IsAnyModRequiredOnAllClients = Current.Any(m => m.IsRequiredOnAllClients);
IsAnyModDisableServerAuthority = Current.Any(m => (m.Flags & ModFlags.DisableServerAuthority) != 0);

var debug = new StringBuilder("Mod list:");
foreach (var mod in Current)
Expand Down
2 changes: 1 addition & 1 deletion Reactor/Networking/Patches/ClientPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static bool Prefix(InnerNetClient._HandleGameDataInner_d__39 __instance,

if (innerNetClient.AmHost)
{
if (reactorClientData == null && ModList.IsAnyModIsRequiredOnAllClients)
if (reactorClientData == null && ModList.IsAnyModRequiredOnAllClients)
{
Warning("Kicking " + clientData.PlayerName + " for not having Reactor installed");

Expand Down
2 changes: 1 addition & 1 deletion Reactor/Networking/Patches/HttpPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static class GameStartManagerPatch
public static void Postfix(GameStartManager __instance)
{
if (AmongUsClient.Instance.NetworkMode != NetworkModes.OnlineGame) return;
if (ModList.IsAnyModIsRequiredOnAllClients && !IsCurrentRegionModded())
if (ModList.IsAnyModRequiredOnAllClients && !IsCurrentRegionModded())
{
Warning("Vanilla region, locking public toggle");

Expand Down
41 changes: 41 additions & 0 deletions Reactor/Patches/Miscellaneous/DisableServerAuthorityPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using HarmonyLib;
using Reactor.Networking;

namespace Reactor.Patches.Miscellaneous;

[HarmonyPatch]
internal static class DisableServerAuthorityPatch
{
private const int DisableServerAuthorityFlag = 25;

public static bool Enabled => ModList.IsAnyModDisableServerAuthority || ReactorConfig.ForceDisableServerAuthority.Value;

[HarmonyPatch(typeof(Constants), nameof(Constants.GetBroadcastVersion))]
[HarmonyPostfix]
public static void GetBroadcastVersionPatch(ref int __result)
{
if (!Enabled) return;
if (AmongUsClient.Instance.NetworkMode != NetworkModes.OnlineGame) return;

Debug("Sending the DisableServerAuthority flag");

var revision = __result % 50;
if (revision < DisableServerAuthorityFlag)
{
__result += DisableServerAuthorityFlag;
}
}

[HarmonyPatch(typeof(Constants), nameof(Constants.IsVersionModded))]
[HarmonyPrefix]
public static bool IsVersionModdedPatch(ref bool __result)
{
if (Enabled)
{
__result = true;
return false;
}

return true;
}
}
3 changes: 3 additions & 0 deletions Reactor/ReactorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ internal static class ReactorConfig
{
public const string FeaturesSection = "Features";

public static ConfigEntry<bool> ForceDisableServerAuthority { get; private set; } = null!;

public static void Bind(ConfigFile config)
{
ForceDisableServerAuthority = config.Bind(FeaturesSection, nameof(ForceDisableServerAuthority), false, "Enables the DisableServerAuthority flag even when no mods declare it");
}
}

0 comments on commit d82268b

Please sign in to comment.