diff --git a/Assets/Mirror/Editor/Weaver/Processors/SyncObjectInitializer.cs b/Assets/Mirror/Editor/Weaver/Processors/SyncObjectInitializer.cs index f2e43e048..b404fc336 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/SyncObjectInitializer.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/SyncObjectInitializer.cs @@ -1,4 +1,3 @@ -using System.Linq; using Mono.CecilX; using Mono.CecilX.Cil; @@ -8,56 +7,10 @@ public static class SyncObjectInitializer { public static void GenerateSyncObjectInitializer(ILProcessor worker, FieldDefinition fd) { - // call syncobject constructor - GenerateSyncObjectInstanceInitializer(worker, fd); - // register syncobject in network behaviour GenerateSyncObjectRegistration(worker, fd); } - // generates 'syncListInt = new SyncListInt()' if user didn't do that yet - static void GenerateSyncObjectInstanceInitializer(ILProcessor worker, FieldDefinition fd) - { - // check the ctor's instructions for an Stfld op-code for this specific sync list field. - foreach (Instruction ins in worker.Body.Instructions) - { - if (ins.OpCode.Code == Code.Stfld) - { - FieldDefinition field = (FieldDefinition)ins.Operand; - if (field.DeclaringType == fd.DeclaringType && field.Name == fd.Name) - { - // Already initialized by the user in the field definition, e.g: - // public SyncListInt Foo = new SyncListInt(); - return; - } - } - } - - // Not initialized by the user in the field definition, e.g: - // public SyncListInt Foo; - - TypeDefinition fieldType = fd.FieldType.Resolve(); - // find ctor with no parameters - MethodDefinition ctor = fieldType.Methods.FirstOrDefault(x => x.Name == ".ctor" && !x.HasParameters); - if (ctor == null) - { - Weaver.Error($"Can not initialize field {fd.Name} because no default constructor was found. Manually initialize the field (call the constructor) or add constructor without Parameter", fd); - return; - } - MethodReference objectConstructor = Weaver.CurrentAssembly.MainModule.ImportReference(ctor); - - // if is SyncList instead of SyncListInt then we need to make the ctor generic - if (fd.FieldType.IsGenericInstance) - { - GenericInstanceType genericInstance = (GenericInstanceType)fd.FieldType; - objectConstructor = objectConstructor.MakeHostInstanceGeneric(genericInstance); - } - - worker.Append(worker.Create(OpCodes.Ldarg_0)); - worker.Append(worker.Create(OpCodes.Newobj, objectConstructor)); - worker.Append(worker.Create(OpCodes.Stfld, fd)); - } - public static bool ImplementsSyncObject(TypeReference typeRef) { try diff --git a/Assets/Mirror/Runtime/NetworkBehaviour.cs b/Assets/Mirror/Runtime/NetworkBehaviour.cs index 2ad5586a4..424882e58 100644 --- a/Assets/Mirror/Runtime/NetworkBehaviour.cs +++ b/Assets/Mirror/Runtime/NetworkBehaviour.cs @@ -154,7 +154,10 @@ public int ComponentIndex // We collect all of them and we synchronize them with OnSerialize/OnDeserialize protected void InitSyncObject(SyncObject syncObject) { - syncObjects.Add(syncObject); + if (syncObject == null) + Debug.LogError("Uninitialized SyncObject. Manually call the constructor on your SyncList, SyncSet or SyncDictionary", this); + else + syncObjects.Add(syncObject); } #region Commands diff --git a/Assets/Mirror/Tests/Editor/Weaver/WeaverSyncListTests.cs b/Assets/Mirror/Tests/Editor/Weaver/WeaverSyncListTests.cs index 85d1b0824..00a9f2f5a 100644 --- a/Assets/Mirror/Tests/Editor/Weaver/WeaverSyncListTests.cs +++ b/Assets/Mirror/Tests/Editor/Weaver/WeaverSyncListTests.cs @@ -43,8 +43,7 @@ public void SyncListInheritance() [Test] public void SyncListMissingParamlessCtor() { - HasError("Can not initialize field Foo because no default constructor was found. Manually initialize the field (call the constructor) or add constructor without Parameter", - "WeaverSyncListTests.SyncListMissingParamlessCtor.SyncListMissingParamlessCtor/SyncListString2 WeaverSyncListTests.SyncListMissingParamlessCtor.SyncListMissingParamlessCtor::Foo"); + IsSuccess(); } [Test]