Skip to content

Commit 962185f

Browse files
committed
Final Fix
1 parent ca56992 commit 962185f

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

Reactor/Networking/Attributes/InnerNetObjectAttribute.cs

+31-20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Reactor.Networking.Attributes;
1616
public sealed class InnerNetObjectAttribute : Attribute
1717
{
1818
private static readonly HashSet<Assembly> _registeredAssemblies = new();
19+
private static readonly HashSet<MethodInfo> _registeredMethods = new();
1920

2021
/// <summary>
2122
/// Registers all <see cref="InnerNetObject"/>s annotated with <see cref="InnerNetObjectAttribute"/> in the specified <paramref name="assembly"/>.
@@ -49,20 +50,7 @@ public static void Register(Assembly assembly)
4950

5051
if (prefabMethod != null)
5152
{
52-
var prefab = prefabMethod.Invoke(null, null);
53-
54-
if (prefab == null)
55-
{
56-
Warning($"Failed to register InnerNetObject, prefab return null.");
57-
}
58-
else if (prefab is InnerNetObject netObj)
59-
{
60-
AddInnerNetObject(netObj);
61-
}
62-
else if (prefab is GameObject gameObj)
63-
{
64-
AddInnerNetObject(gameObj);
65-
}
53+
_registeredMethods.Add(prefabMethod);
6654
}
6755
else
6856
{
@@ -77,6 +65,35 @@ public static void Register(Assembly assembly)
7765
}
7866
}
7967

68+
internal static void LoadRegistered()
69+
{
70+
if (_registeredMethods.Count > 0) // Increase array length by one because of beginning if check in InnerNetClient.CoHandleSpawn()
71+
{
72+
var innerNetClient = AmongUsClient.Instance;
73+
var list2 = innerNetClient.SpawnableObjects.ToList();
74+
list2.Add(new());
75+
innerNetClient.SpawnableObjects = list2.ToArray();
76+
}
77+
78+
foreach (var prefabMethod in _registeredMethods)
79+
{
80+
var prefab = prefabMethod.Invoke(null, null);
81+
82+
if (prefab == null)
83+
{
84+
Warning($"Failed to register InnerNetObject, prefab return null.");
85+
}
86+
else if (prefab is InnerNetObject netObj)
87+
{
88+
AddInnerNetObject(netObj);
89+
}
90+
else if (prefab is GameObject gameObj)
91+
{
92+
AddInnerNetObject(gameObj);
93+
}
94+
}
95+
}
96+
8097
private static void AddInnerNetObject(InnerNetObject prefab)
8198
{
8299
var innerNetClient = AmongUsClient.Instance;
@@ -110,12 +127,6 @@ private static void AddInnerNetObject(GameObject prefab)
110127

111128
internal static void Initialize()
112129
{
113-
// Increase array length by one because of beginning if check in InnerNetClient.CoHandleSpawn()
114-
var innerNetClient = AmongUsClient.Instance;
115-
var list2 = innerNetClient.SpawnableObjects.ToList();
116-
list2.Add(new());
117-
innerNetClient.SpawnableObjects = list2.ToArray();
118-
119130
IL2CPPChainloader.Instance.PluginLoad += (_, assembly, _) => Register(assembly);
120131
}
121132
}

Reactor/ReactorPlugin.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public ReactorPlugin()
5151
RegisterCustomRpcAttribute.Initialize();
5252
MessageConverterAttribute.Initialize();
5353
MethodRpcAttribute.Initialize();
54+
InnerNetObjectAttribute.Initialize();
5455

5556
LocalizationManager.Register(new HardCodedLocalizationProvider());
5657
}
@@ -82,7 +83,7 @@ public override void Load()
8283
/// <inheritdoc />
8384
public void LateLoad()
8485
{
85-
InnerNetObjectAttribute.Initialize();
86+
InnerNetObjectAttribute.LoadRegistered();
8687
}
8788

8889
/// <inheritdoc />

0 commit comments

Comments
 (0)