Skip to content

Commit b86ee70

Browse files
committed
Added a fix for LocalAdmin not responding to commands after entering Idle Mode
1 parent 9a5929d commit b86ee70

5 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎LabExtended/Core/ApiLoader.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
using Version = System.Version;
5353
using LabExtended.Patches.Events.Mirror;
54+
using Mirror;
5455

5556
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
5657
#pragma warning disable CS8764 // Nullability of return type doesn't match overridden member (possibly because of nullability attributes).
@@ -372,6 +373,7 @@ private static void InvokeApi(bool isPreload)
372373

373374
Scp049CancellingResurrectionPatch.Internal_Init();
374375
MirrorSetSyncVarPatch.Internal_Init();
376+
PrompterQueueIdleModeFix.Internal_Init();
375377

376378
FirearmModuleCache.Internal_Init();
377379

‎LabExtended/Events/MirrorEvents.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,7 @@ public static void OnSentRpc(NetworkBehaviour behaviour, string rpcName, int rpc
402402
ApiLog.Error("OnSentRpc", ex);
403403
}
404404
}
405+
406+
internal static bool Internal_AnySyncVarSubsribers()
407+
=> UpdatingSyncVar is not null || UpdatedSyncVar is not null;
405408
}

‎LabExtended/Patches/Events/Mirror/MirrorRemoveObserverPatch.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static bool RemoveObserverPrefix(NetworkIdentity __instance, NetworkConn
3333
[HarmonyPatch(typeof(NetworkIdentity), nameof(NetworkIdentity.ClearObservers))]
3434
private static bool ClearObserversPrefix(NetworkIdentity __instance)
3535
{
36-
foreach (var conn in __instance.observers.Values)
36+
foreach (var conn in __instance.observers.Values.ToList())
3737
{
3838
if (conn is null)
3939
continue;

‎LabExtended/Patches/Events/Mirror/MirrorSetSyncVarPatch.cs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@
1010

1111
using NorthwoodLib.Pools;
1212

13+
using System.Reflection;
14+
1315
namespace LabExtended.Patches.Events.Mirror
1416
{
1517
/// <summary>
1618
/// Implements the <see cref="MirrorEvents.UpdatingSyncVar"/> and <see cref="MirrorEvents.UpdatedSyncVar"/> events.
1719
/// </summary>
1820
public static class MirrorSetSyncVarPatch
1921
{
20-
private static readonly HarmonyMethod patchMethod = new(
21-
typeof(MirrorSetSyncVarPatch).FindMethod(x => x.Name == "GeneratedSyncVarSetterPrefix"));
22+
private static readonly MethodInfo patchMethod = typeof(MirrorSetSyncVarPatch).FindMethod(x => x.Name == "GeneratedSyncVarSetterPrefix");
2223

2324
private static bool GeneratedSyncVarSetterPrefix<T>(NetworkBehaviour __instance, T value, ref T field, ulong dirtyBit, Action<T, T> OnChanged)
2425
{
26+
if (!MirrorEvents.Internal_AnySyncVarSubsribers())
27+
return true;
28+
2529
if (NetworkBehaviour.SyncVarEqual(value, ref field))
2630
return false;
2731

@@ -83,10 +87,11 @@ internal static void Internal_Init()
8387
continue;
8488

8589
var genericMethod = targetMethod.MakeGenericMethod(property.PropertyType);
90+
var genericPatchMethod = patchMethod.MakeGenericMethod(property.PropertyType);
8691

8792
if (genericMethod != null)
8893
{
89-
if (ApiPatcher.Harmony.Patch(genericMethod, patchMethod) != null)
94+
if (ApiPatcher.Harmony.Patch(genericMethod, new(genericPatchMethod)) != null)
9095
{
9196
patchedTypes.Add(property.PropertyType);
9297
}
@@ -113,7 +118,7 @@ internal static void Internal_Init()
113118
ApiLog.Error("MirrorSetSyncVarPatch", ex);
114119
}
115120

116-
ApiLog.Debug("MirrorSetSyncVarPatch", $"Patched &3{patchedTypes.Count}&r SyncVar setters");
121+
ApiLog.Debug("MirrorSetSyncVarPatch", $"Patched &3{patchedTypes.Count}&r SyncVar setter types");
117122

118123
ListPool<Type>.Shared.Return(patchedTypes);
119124
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using LabExtended.Core;
2+
using LabExtended.Utilities.Update;
3+
4+
namespace LabExtended.Patches.Fixes
5+
{
6+
/// <summary>
7+
/// Fixes the issue with the prompter queue not being processed while in idle mode.
8+
/// </summary>
9+
public static class PrompterQueueIdleModeFix
10+
{
11+
private static void Internal_Update()
12+
{
13+
if (IdleMode.IdleModeActive)
14+
{
15+
while (ServerConsole.PrompterQueue.TryDequeue(out var command))
16+
{
17+
try
18+
{
19+
ServerConsole.EnterCommand(command, ServerConsole.Scs);
20+
}
21+
catch (Exception ex)
22+
{
23+
ApiLog.Error(nameof(PrompterQueueIdleModeFix), ex);
24+
}
25+
}
26+
}
27+
}
28+
29+
internal static void Internal_Init()
30+
{
31+
PlayerUpdateHelper.OnUpdate += Internal_Update;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)