Skip to content

Commit aecc90e

Browse files
committed
feat: make it able to pre-cache litenetlib behaviour sync elements and rpcs
1 parent 321bb78 commit aecc90e

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

Scripts/GameApi/LiteNetLibBehaviour.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public partial class LiteNetLibBehaviour : MonoBehaviour
1313
{
1414
public const string TAG_NULL = "<NULL_B>";
1515

16-
private class CacheFunctions
16+
public class CacheFunctions
1717
{
1818
public readonly List<MethodInfo> Functions = new List<MethodInfo>();
1919
public readonly List<MethodInfo> FunctionsCanCallByEveryone = new List<MethodInfo>();
@@ -180,13 +180,21 @@ public void Setup(byte behaviourIndex)
180180
CacheRpcs<TargetRpcAttribute>(_targetRpcIds, s_CacheTargetRpcs);
181181
}
182182

183-
private void CacheElements(Dictionary<string, List<FieldInfo>> cacheDict)
183+
public static void CacheElementsAndRpcs(Type baseType)
184+
{
185+
GetCachedElements(baseType, s_CacheSyncElements, out _);
186+
GetCachedRpcs<ElasticRpcAttribute>(baseType, string.Empty, s_CacheElasticRpcs, out _);
187+
GetCachedRpcs<ServerRpcAttribute>(baseType, string.Empty, s_CacheServerRpcs, out _);
188+
GetCachedRpcs<AllRpcAttribute>(baseType, string.Empty, s_CacheAllRpcs, out _);
189+
GetCachedRpcs<TargetRpcAttribute>(baseType, string.Empty, s_CacheTargetRpcs, out _);
190+
}
191+
192+
public static void GetCachedElements(Type baseType, Dictionary<string, List<FieldInfo>> cacheDict, out List<FieldInfo> syncElementFieldInfos)
184193
{
185-
Type baseType = GetType();
186194
string typeName = baseType.FullName;
187195

188196
// Find sync elements
189-
if (!cacheDict.TryGetValue(typeName, out List<FieldInfo> syncElementFieldInfos))
197+
if (!cacheDict.TryGetValue(typeName, out syncElementFieldInfos))
190198
{
191199
syncElementFieldInfos = new List<FieldInfo>();
192200
HashSet<string> tempLookupNames = new HashSet<string>();
@@ -213,10 +221,13 @@ private void CacheElements(Dictionary<string, List<FieldInfo>> cacheDict)
213221
tempLookupType = null;
214222
cacheDict.Add(typeName, syncElementFieldInfos);
215223
}
224+
}
216225

226+
private void CacheElements(Dictionary<string, List<FieldInfo>> cacheDict)
227+
{
228+
GetCachedElements(GetType(), cacheDict, out List<FieldInfo> syncElementFieldInfos);
217229
if (syncElementFieldInfos.Count == 0)
218230
return;
219-
220231
// Setup sync elements
221232
foreach (FieldInfo syncElementFieldInfo in syncElementFieldInfos)
222233
{
@@ -229,20 +240,19 @@ private void CacheElements(Dictionary<string, List<FieldInfo>> cacheDict)
229240
}
230241
}
231242

232-
private void CacheRpcs<RpcType>(Dictionary<string, int> ids, Dictionary<string, CacheFunctions> cacheDict)
243+
public static void GetCachedRpcs<RpcType>(Type baseType, string logTag, Dictionary<string, CacheFunctions> cacheDict, out CacheFunctions cacheFunctions)
233244
where RpcType : RpcAttribute
234245
{
235-
Type baseType = GetType();
236246
string typeName = baseType.FullName;
237247

238-
if (!cacheDict.TryGetValue(typeName, out CacheFunctions cacheFunctions))
248+
if (!cacheDict.TryGetValue(typeName, out cacheFunctions))
239249
{
240250
cacheFunctions = new CacheFunctions();
241251
HashSet<string> tempLookupNames = new HashSet<string>();
242252
MethodInfo[] tempLookupMethods;
243253
Type tempLookupType = baseType;
244254
RpcType tempAttribute;
245-
255+
bool writeErrorLog = !string.IsNullOrWhiteSpace(logTag);
246256
while (tempLookupType != null && tempLookupType != typeof(LiteNetLibBehaviour))
247257
{
248258
tempLookupMethods = tempLookupType.GetMethods(
@@ -259,8 +269,8 @@ private void CacheRpcs<RpcType>(Dictionary<string, int> ids, Dictionary<string,
259269

260270
if (lookupMethod.ReturnType != typeof(void))
261271
{
262-
if (Manager.LogError)
263-
Logging.LogError(LogTag, $"Cannot register RPC [{lookupMethod.Name}] return type must be void.");
272+
if (writeErrorLog)
273+
Logging.LogError(logTag, $"Cannot register RPC [{lookupMethod.Name}] return type must be void.");
264274
continue;
265275
}
266276

@@ -277,7 +287,13 @@ private void CacheRpcs<RpcType>(Dictionary<string, int> ids, Dictionary<string,
277287

278288
cacheDict.Add(typeName, cacheFunctions);
279289
}
290+
}
280291

292+
private void CacheRpcs<RpcType>(Dictionary<string, int> ids, Dictionary<string, CacheFunctions> cacheDict)
293+
where RpcType : RpcAttribute
294+
{
295+
Type baseType = GetType();
296+
GetCachedRpcs<RpcType>(baseType, Manager.LogError ? LogTag : string.Empty, cacheDict, out CacheFunctions cacheFunctions);
281297
SetupRpcs(ids, cacheFunctions.Functions, false);
282298
SetupRpcs(ids, cacheFunctions.FunctionsCanCallByEveryone, true);
283299
}

0 commit comments

Comments
 (0)