Skip to content

Commit cec3b8e

Browse files
committed
Fixed LabAPI hanging the entire server startup due to a plugin having a null RequiredApiVersion property
1 parent f8a7789 commit cec3b8e

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

LabExtended/Core/ApiLoader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ private static void InvokeApi(bool isPreload)
322322
{
323323
if (isPreload)
324324
{
325+
LabApiNullPluginVersionFix.Internal_Init();
325326
SwitchContainer.Internal_Init();
326327
LogPatch.Internal_Init();
327328
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using HarmonyLib;
2+
3+
using LabApi.Loader;
4+
using LabApi.Loader.Features.Plugins;
5+
6+
using LabExtended.Core;
7+
using LabExtended.Extensions;
8+
9+
using System.Reflection;
10+
11+
namespace LabExtended.Patches.Fixes
12+
{
13+
/// <summary>
14+
/// Fixes the server hanging on startup when a plugin with a null RequiredApiVersion is present.
15+
/// </summary>
16+
public static class LabApiNullPluginVersionFix
17+
{
18+
private static MethodInfo targetMethod = typeof(PluginLoader).FindMethod(nameof(PluginLoader.ValidateVersion));
19+
private static MethodInfo patchMethod = typeof(LabApiNullPluginVersionFix).FindMethod(nameof(Prefix));
20+
21+
private static bool Prefix(Plugin plugin, ref bool __result)
22+
{
23+
if (plugin.RequiredApiVersion is null
24+
|| plugin.Properties is null)
25+
{
26+
__result = true;
27+
return false;
28+
}
29+
30+
return false;
31+
}
32+
33+
internal static void Internal_Init()
34+
{
35+
try
36+
{
37+
if (ApiPatcher.Harmony.Patch(targetMethod, new HarmonyMethod(patchMethod)) != null)
38+
{
39+
ApiPatcher.labExPatchCountOffset++;
40+
ApiLog.Debug("LabExtended", $"Applied &6{nameof(LabApiNullPluginVersionFix)}&r");
41+
}
42+
else
43+
{
44+
ApiLog.Error("LabExtended", $"Failed to apply &3{nameof(LabApiNullPluginVersionFix)}&r!");
45+
}
46+
}
47+
catch (Exception ex)
48+
{
49+
ApiLog.Error("LabExtended", ex);
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)