Skip to content

Commit f2b44a4

Browse files
Merge branch 'main' into main
2 parents 1e5f521 + b4b00df commit f2b44a4

File tree

450 files changed

+9813
-35800
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

450 files changed

+9813
-35800
lines changed

Assets/FishNet/CodeGenerating/Helpers/Extension/TypeDefinitionExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using UnityEngine;
8+
using UnityEngine.SceneManagement;
89

910
namespace FishNet.CodeGenerating.Helping.Extension
1011
{
@@ -100,6 +101,9 @@ public static IEnumerable<PropertyDefinition> FindAllPublicProperties(this TypeD
100101
/// </summary>
101102
private static bool IsExcluded(TypeDefinition typeDef, System.Type[] excludedBaseTypes = null, string[] excludedAssemblyPrefixes = null)
102103
{
104+
if (typeDef.FullName == typeof(LoadSceneParameters).FullName)
105+
return false;
106+
103107
if (excludedBaseTypes != null)
104108
{
105109
foreach (System.Type t in excludedBaseTypes)

Assets/FishNet/CodeGenerating/Helpers/GeneralHelper.cs

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,58 @@ void CreateGenericInstanceComparer()
11881188
void CreateClassOrStructComparer()
11891189
{
11901190
//Class or struct.
1191-
Instruction exitMethodInst = processor.Create(OpCodes.Ldc_I4_0);
1191+
Instruction falseLdcInst = processor.Create(OpCodes.Ldc_I4_0);
1192+
1193+
//Non-value type null check.
1194+
if (!dataTr.IsValueType)
1195+
{
1196+
GeneralHelper gh = base.GetClass<GeneralHelper>();
1197+
1198+
Instruction checkNullAndNotNullInst = Instruction.Create(OpCodes.Nop);
1199+
1200+
VariableDefinition isNullV0 = gh.CreateVariable(comparerMd, typeof(bool));
1201+
VariableDefinition isNullV1 = gh.CreateVariable(comparerMd, typeof(bool));
1202+
1203+
//isNull0 = (value0 == null);
1204+
processor.Emit(OpCodes.Ldarg, v0Pd);
1205+
processor.Emit(OpCodes.Ldnull);
1206+
processor.Emit(OpCodes.Ceq);
1207+
processor.Emit(OpCodes.Stloc, isNullV0);
1208+
//isNull1 = (value1 == null);
1209+
processor.Emit(OpCodes.Ldarg, v1Pd);
1210+
processor.Emit(OpCodes.Ldnull);
1211+
processor.Emit(OpCodes.Ceq);
1212+
processor.Emit(OpCodes.Stloc, isNullV1);
1213+
1214+
//If (isNull0 && isNull1) return true;
1215+
processor.Emit(OpCodes.Ldloc, isNullV0);
1216+
processor.Emit(OpCodes.Ldloc, isNullV1);
1217+
processor.Emit(OpCodes.And);
1218+
processor.Emit(OpCodes.Brfalse, checkNullAndNotNullInst);
1219+
processor.Emit(OpCodes.Ldc_I4_1);
1220+
processor.Emit(OpCodes.Ret);
1221+
//Skip past ret here.
1222+
processor.Append(checkNullAndNotNullInst);
1223+
1224+
//bool isNullOpposing = (isNull0 != isNull1);
1225+
VariableDefinition isNullOpposingVd = gh.CreateVariable(comparerMd, typeof(bool));
1226+
processor.Emit(OpCodes.Ldloc, isNullV0);
1227+
processor.Emit(OpCodes.Ldloc, isNullV1);
1228+
processor.Emit(OpCodes.Ceq);
1229+
processor.Emit(OpCodes.Ldc_I4_0);
1230+
processor.Emit(OpCodes.Ceq);
1231+
processor.Emit(OpCodes.Stloc, isNullOpposingVd);
1232+
1233+
1234+
Instruction checkPropertiesInst = Instruction.Create(OpCodes.Nop);
1235+
//if (isNullOpposing) return false;
1236+
processor.Emit(OpCodes.Ldloc, isNullOpposingVd);
1237+
processor.Emit(OpCodes.Brfalse, checkPropertiesInst);
1238+
processor.Emit(OpCodes.Ldc_I4_0);
1239+
processor.Emit(OpCodes.Ret);
1240+
//Skip past ret here.
1241+
processor.Append(checkPropertiesInst);
1242+
}
11921243

11931244
//Fields.
11941245
foreach (FieldDefinition fieldDef in dataTr.FindAllSerializableFields(base.Session, null, WriterProcessor.EXCLUDED_ASSEMBLY_PREFIXES))
@@ -1203,7 +1254,7 @@ void CreateClassOrStructComparer()
12031254
processor.Emit(OpCodes.Ldfld, fr);
12041255
FinishTypeReferenceCompare(fieldDef.FieldType);
12051256
}
1206-
1257+
12071258
//Properties.
12081259
foreach (PropertyDefinition propertyDef in dataTr.FindAllSerializableProperties(base.Session, null, WriterProcessor.EXCLUDED_ASSEMBLY_PREFIXES))
12091260
{
@@ -1217,14 +1268,14 @@ void CreateClassOrStructComparer()
12171268
processor.Emit(OpCodes.Call, getMr);
12181269
FinishTypeReferenceCompare(propertyDef.PropertyType);
12191270
}
1220-
1271+
12211272
//Return true;
12221273
processor.Emit(OpCodes.Ldc_I4_1);
12231274
processor.Emit(OpCodes.Ret);
1224-
processor.Append(exitMethodInst);
1275+
processor.Append(falseLdcInst);
12251276
processor.Emit(OpCodes.Ret);
12261277

1227-
1278+
12281279
void FinishTypeReferenceCompare(TypeReference tr)
12291280
{
12301281
/* If a class or struct see if it already has a comparer
@@ -1240,7 +1291,7 @@ void FinishTypeReferenceCompare(TypeReference tr)
12401291
{
12411292
MethodDefinition cMd = CreateEqualityComparer(tr);
12421293
processor.Emit(OpCodes.Call, cMd);
1243-
processor.Emit(OpCodes.Brfalse, exitMethodInst);
1294+
processor.Emit(OpCodes.Brfalse, falseLdcInst);
12441295
}
12451296
//Call existing.
12461297
else
@@ -1255,14 +1306,14 @@ void FinishTypeReferenceCompare(TypeReference tr)
12551306
{
12561307
MethodReference mr = base.ImportReference(cMd);
12571308
processor.Emit(OpCodes.Call, mr);
1258-
processor.Emit(OpCodes.Brfalse, exitMethodInst);
1309+
processor.Emit(OpCodes.Brfalse, falseLdcInst);
12591310
}
12601311
}
12611312
}
12621313
//Value types do not need to check custom comparers.
12631314
else
12641315
{
1265-
processor.Emit(OpCodes.Bne_Un, exitMethodInst);
1316+
processor.Emit(OpCodes.Bne_Un, falseLdcInst);
12661317
}
12671318
}
12681319
}

Assets/FishNet/CodeGenerating/Helpers/NetworkBehaviourHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal class NetworkBehaviourHelper : CodegenBase
3333
public MethodReference Replicate_Replay_Start_MethodRef;
3434
public MethodReference Replicate_Current_MethodRef;
3535
public MethodReference Reconcile_Client_MethodRef;
36-
public MethodReference Reconcile_Client_Local_MethodRef;
36+
public MethodReference Reconcile_Current_MethodRef;
3737
public MethodReference ClearReplicateCache_Internal_MethodRef;
3838
public MethodReference Replicate_Replay_MethodRef;
3939
public MethodReference Reconcile_Reader_MethodRef;
@@ -136,10 +136,10 @@ public override bool ImportReferences()
136136
Reconcile_Reader_MethodRef = base.ImportReference(mi);
137137
else if (mi.Name == nameof(NetworkBehaviour.Reconcile_Server))
138138
Reconcile_Server_MethodRef = base.ImportReference(mi);
139-
else if (mi.Name == nameof(NetworkBehaviour.Reconcile_Client))
139+
else if (mi.Name == nameof(NetworkBehaviour.Reconcile_Client))
140140
Reconcile_Client_MethodRef = base.ImportReference(mi);
141-
else if (mi.Name == nameof(NetworkBehaviour.Reconcile_Client_Local))
142-
Reconcile_Client_Local_MethodRef = base.ImportReference(mi);
141+
else if (mi.Name == nameof(NetworkBehaviour.Reconcile_Current))
142+
Reconcile_Current_MethodRef = base.ImportReference(mi);
143143
else if (mi.Name == nameof(NetworkBehaviour.ClearReplicateCache_Internal))
144144
ClearReplicateCache_Internal_MethodRef = base.ImportReference(mi);
145145
}

Assets/FishNet/CodeGenerating/ILCore/FishNetILPP.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using FishNet.CodeGenerating.Processing;
66
using FishNet.CodeGenerating.Processing.Rpc;
77
using FishNet.Configuring;
8-
using FishNet.Editing.Upgrading;
9-
using FishNet.Serializing.Helping;
108
using MonoFN.Cecil;
119
using MonoFN.Cecil.Cil;
1210
using System;
@@ -122,8 +120,6 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
122120
}
123121
else
124122
{
125-
TryLogV3ToV4Helpers(session);
126-
127123
MemoryStream pe = new();
128124
MemoryStream pdb = new();
129125
WriterParameters writerParameters = new()
@@ -136,24 +132,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
136132
return new(new(pe.ToArray(), pdb.ToArray()), session.Diagnostics);
137133
}
138134
}
139-
140-
/// <summary>
141-
/// Logs warning if v3 to v4 helpers are enabled.
142-
/// </summary>
143-
private void TryLogV3ToV4Helpers(CodegenSession session)
144-
{
145-
#if !FISHNET_DISABLE_V3TOV4_HELPERS
146-
/* There is no way to check if this has run already once per codegen
147-
* so the only option is to print per session, which means
148-
* a print will occur every time an assembly compiles. This means
149-
* several prints will potentially occur per script change.
150-
*
151-
* However, these warnings typically only print when all errors are gone.
152-
* When this is true the user may go ahead and disable this warning
153-
* as instructed. */
154-
session.LogWarning(UpgradeFromV3ToV4Menu.EnabledWarning);
155-
#endif
156-
}
135+
157136

158137
/// <summary>
159138
/// Makees methods public scope which use CodegenMakePublic attribute.

Assets/FishNet/CodeGenerating/Processing/NetworkBehaviourProcessor.cs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ internal class NetworkBehaviourProcessor : CodegenBase
2222
#endregion
2323

2424
#region Const.
25-
internal const string EARLY_INITIALIZED_NAME = "NetworkInitializeEarly_";
26-
internal const string LATE_INITIALIZED_NAME = "NetworkInitializeLate_";
2725
internal const string NETWORKINITIALIZE_EARLY_INTERNAL_NAME = "NetworkInitialize___Early";
28-
internal const string NETWORKINITIALIZE_LATE_INTERNAL_NAME = "NetworkInitialize__Late";
26+
internal const string NETWORKINITIALIZE_LATE_INTERNAL_NAME = "NetworkInitialize___Late";
27+
private readonly string[] _networkInitializeMethodNames = new string[] { NETWORKINITIALIZE_EARLY_INTERNAL_NAME, NETWORKINITIALIZE_LATE_INTERNAL_NAME };
2928
#endregion
3029

3130
internal bool ProcessLocal(TypeDefinition typeDef)
@@ -65,10 +64,10 @@ internal bool ProcessLocal(TypeDefinition typeDef)
6564
* multiple times. EG: with this solution if parent had 1 sync type
6665
* and childA had 2 the parent would be index 0, and childA would have 1 and 2.
6766
* But also, if childB inherited childA it would have 3+.
68-
*
67+
*
6968
* Going in reverse also gaurantees the awake method will already be created
70-
* or modified in any class a child inherits. This lets us call it appropriately
71-
* as well error if the awake does not exist, such as could not be created. */
69+
* or modified in any class a child inherits. This lets us call it appropriately
70+
* as well error if the awake does not exist, such as could not be created. */
7271
typeDefs.Reverse();
7372

7473
foreach (TypeDefinition td in typeDefs)
@@ -77,7 +76,7 @@ internal bool ProcessLocal(TypeDefinition typeDef)
7776
* can use it. */
7877
MethodDefinition networkInitializeIfDisabledMd;
7978
CreateNetworkInitializeMethods(td, out networkInitializeIfDisabledMd);
80-
CallNetworkInitializesFromNetworkInitializeIfDisabled(networkInitializeIfDisabledMd);
79+
CallNetworkInitialize(networkInitializeIfDisabledMd);
8180

8281

8382

@@ -111,7 +110,7 @@ internal bool ProcessLocal(TypeDefinition typeDef)
111110
//Copy user logic from awake into a new method.
112111
CopyAwakeUserLogic(td);
113112
/* Create awake method or if already exist make
114-
* it public virtual. */
113+
* it public virtual. */
115114
if (!ModifyAwakeMethod(td, out bool awakeCreated))
116115
{
117116
//This is a hard fail and will break the solution so throw here.
@@ -146,7 +145,6 @@ internal bool ProcessLocal(TypeDefinition typeDef)
146145
/// </summary>
147146
internal string GetAwakeUserLogicMethodDefinition(TypeDefinition td) => $"Awake_UserLogic_{td.FullName}_{base.Module.Name}";
148147

149-
150148
/// <summary>
151149
/// Returns if a class has been processed.
152150
/// </summary>
@@ -207,7 +205,7 @@ internal bool NonNetworkBehaviourHasInvalidAttributes(Collection<TypeDefinition>
207205
private void CallBaseAwake(TypeDefinition td)
208206
{
209207
/* If base is not a class which can be processed then there
210-
* is no need to continue. */
208+
* is no need to continue. */
211209
if (!td.CanProcessBaseType(base.Session))
212210
return;
213211

@@ -221,7 +219,6 @@ private void CallBaseAwake(TypeDefinition td)
221219
processor.Emit(OpCodes.Call, baseAwakeMr);
222220
}
223221

224-
225222
/// <summary>
226223
/// Calls the next awake method if the nested awake was created by codegen.
227224
/// </summary>
@@ -241,7 +238,6 @@ private void CallAwakeUserLogic(TypeDefinition td)
241238
base.GetClass<GeneralHelper>().CallCopiedMethod(awakeMd, userLogicMd);
242239
}
243240

244-
245241
/// <summary>
246242
/// Adds a check to NetworkInitialize to see if it has already run.
247243
/// </summary>
@@ -287,23 +283,22 @@ void AddCheck(string methodName)
287283
/// </summary>
288284
private void CallBaseOnNetworkInitializeMethods(TypeDefinition typeDef)
289285
{
290-
//If base class cannot have a networkinitialize no reason to continue.
291-
if (!typeDef.CanProcessBaseType(base.Session))
286+
//If base is null cannot call base.
287+
if (typeDef.BaseType == null)
292288
return;
293289

294-
string[] initializeMethodNames = new string[] { NETWORKINITIALIZE_EARLY_INTERNAL_NAME, NETWORKINITIALIZE_LATE_INTERNAL_NAME };
295-
foreach (string mdName in initializeMethodNames)
290+
foreach (string mdName in _networkInitializeMethodNames)
296291
{
297-
/* Awake will always exist because it was added previously.
298-
* Get awake for copy and base of copy. */
299-
MethodDefinition thisMd = typeDef.GetMethod(mdName);
300-
ILProcessor processor = thisMd.Body.GetILProcessor();
301-
302-
/* Awake will always exist because it was added previously.
303-
* Get awake for copy and base of copy. */
304292
MethodReference baseMr = typeDef.GetMethodReferenceInBase(base.Session, mdName);
293+
//Not found in base.
294+
if (baseMr == null)
295+
continue;
296+
305297
MethodDefinition baseMd = baseMr.CachedResolve(base.Session);
306298

299+
MethodDefinition thisMd = typeDef.GetMethod(mdName);
300+
ILProcessor processor = thisMd.Body.GetILProcessor();
301+
307302
bool alreadyHasBaseCall = false;
308303
//Check if already calls baseAwake.
309304
foreach (Instruction item in thisMd.Body.Instructions)
@@ -328,11 +323,12 @@ private void CallBaseOnNetworkInitializeMethods(TypeDefinition typeDef)
328323
//Create instructions for base call.
329324
List<Instruction> instructions = new()
330325
{
331-
processor.Create(OpCodes.Ldarg_0), //this.
332-
processor.Create(OpCodes.Call, baseMr)
333-
};
326+
processor.Create(OpCodes.Ldarg_0), //this.
327+
processor.Create(OpCodes.Call, baseMr)
328+
};
334329
processor.InsertFirst(instructions);
335330
}
331+
336332
}
337333
}
338334

@@ -345,8 +341,7 @@ private void AddReturnToAwake(TypeDefinition td)
345341
MethodDefinition awakeMd = td.GetMethod(NetworkBehaviourHelper.AWAKE_METHOD_NAME);
346342
ILProcessor processor = awakeMd.Body.GetILProcessor();
347343
//If no instructions or the last instruction isnt ret.
348-
if (processor.Body.Instructions.Count == 0
349-
|| processor.Body.Instructions[processor.Body.Instructions.Count - 1].OpCode != OpCodes.Ret)
344+
if (processor.Body.Instructions.Count == 0 || processor.Body.Instructions[processor.Body.Instructions.Count - 1].OpCode != OpCodes.Ret)
350345
{
351346
processor.Emit(OpCodes.Ret);
352347
}
@@ -392,22 +387,21 @@ MethodDefinition CreateMethod(string name, MethodDefinition copied = null)
392387
}
393388
}
394389

395-
396390
/// <summary>
397391
/// Creates an 'NetworkInitialize' method which is called by the childmost class to initialize scripts on Awake.
398392
/// </summary>
399-
private void CallNetworkInitializesFromNetworkInitializeIfDisabled(MethodDefinition networkInitializeIfDisabledMd)
393+
private void CallNetworkInitialize(MethodDefinition networkIntializeMd)
400394
{
401-
ILProcessor processor = networkInitializeIfDisabledMd.Body.GetILProcessor();
395+
ILProcessor processor = networkIntializeMd.Body.GetILProcessor();
402396

403-
networkInitializeIfDisabledMd.Body.Instructions.Clear();
397+
networkIntializeMd.Body.Instructions.Clear();
404398
CallMethod(NETWORKINITIALIZE_EARLY_INTERNAL_NAME);
405399
CallMethod(NETWORKINITIALIZE_LATE_INTERNAL_NAME);
406400
processor.Emit(OpCodes.Ret);
407401

408402
void CallMethod(string name)
409403
{
410-
MethodReference initIfDisabledMr = networkInitializeIfDisabledMd.DeclaringType.GetMethodReference(base.Session, name);
404+
MethodReference initIfDisabledMr = networkIntializeMd.DeclaringType.GetMethodReference(base.Session, name);
411405
processor.Emit(OpCodes.Ldarg_0);
412406
processor.Emit(initIfDisabledMr.GetCallOpCode(base.Session), initIfDisabledMr);
413407
}
@@ -473,8 +467,7 @@ internal void CreateFirstNetworkInitializeCall(TypeDefinition typeDef, MethodDef
473467
{
474468
created = true;
475469

476-
thisAwakeMethodDef = new(NetworkBehaviourHelper.AWAKE_METHOD_NAME, MethodDefinitionExtensions.PUBLIC_VIRTUAL_ATTRIBUTES,
477-
typeDef.Module.TypeSystem.Void);
470+
thisAwakeMethodDef = new(NetworkBehaviourHelper.AWAKE_METHOD_NAME, MethodDefinitionExtensions.PUBLIC_VIRTUAL_ATTRIBUTES, typeDef.Module.TypeSystem.Void);
478471
thisAwakeMethodDef.Body.InitLocals = true;
479472
typeDef.Methods.Add(thisAwakeMethodDef);
480473

@@ -500,14 +493,11 @@ internal void CreateFirstNetworkInitializeCall(TypeDefinition typeDef, MethodDef
500493
if (created && firstUserAwakeMethodDef != null)
501494
{
502495
MethodReference baseAwakeMethodRef = typeDef.Module.ImportReference(firstUserAwakeMethodDef);
503-
instructions.Add(processor.Create(OpCodes.Ldarg_0));//this.
496+
instructions.Add(processor.Create(OpCodes.Ldarg_0)); //this.
504497
instructions.Add(processor.Create(OpCodes.Call, baseAwakeMethodRef));
505498
}
506499

507500
processor.InsertFirst(instructions);
508501
}
509-
510-
511-
512502
}
513503
}

0 commit comments

Comments
 (0)