From 7064cbb7938525cf87e511fe51022bb23f7a1238 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Jan 2022 21:35:15 -0800 Subject: [PATCH] create completely empty default ctor if one doesnt already exist --- MirrorWeaver/Weaver/Readers.cs | 13 +++++++++++-- QSBTests/MessageTests.cs | 4 +++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/MirrorWeaver/Weaver/Readers.cs b/MirrorWeaver/Weaver/Readers.cs index 789fa3b9..ec3f8fa7 100644 --- a/MirrorWeaver/Weaver/Readers.cs +++ b/MirrorWeaver/Weaver/Readers.cs @@ -6,6 +6,8 @@ using Mono.Cecil.Cil; // Unity.Mirror.CodeGen assembly definition file in the Editor, and add CecilX.Rocks. // otherwise we get an unknown import exception. using Mono.Cecil.Rocks; +using Mono.Collections.Generic; +using System.Linq; namespace Mirror.Weaver { @@ -313,8 +315,15 @@ namespace Mirror.Weaver if (ctor == null) { Log.Error($"{variable.Name} can't be deserialized because it has no default constructor. Don't use {variable.Name} in [SyncVar]s, Rpcs, Cmds, etc.", variable); - WeavingFailed = true; - return; + // WeavingFailed = true; + // return; + var resolvedVariable = variable.Resolve(); + var anyCtor = resolvedVariable.Methods.First(m => m.IsConstructor); + ctor = new MethodDefinition(anyCtor.Name, anyCtor.Attributes, anyCtor.ReturnType); + var ctorWorker = ctor.Body.GetILProcessor(); + ctorWorker.Emit(OpCodes.Ret); + resolvedVariable.Methods.Add(ctor); + Log.Warning("created empty default ctor", variable); } MethodReference ctorRef = assembly.MainModule.ImportReference(ctor); diff --git a/QSBTests/MessageTests.cs b/QSBTests/MessageTests.cs index 4eefe65f..f29595e4 100644 --- a/QSBTests/MessageTests.cs +++ b/QSBTests/MessageTests.cs @@ -28,7 +28,9 @@ namespace QSBTests var fields = type.GetFields(Util.Flags) .Select(x => module.ImportReference(x)); - var constructor = module.ImportReference(type.GetConstructors(Util.Flags).Single()).Resolve(); + var constructors = type.GetConstructors(Util.Flags); + var constructor_ = constructors.Length > 1 ? constructors.Single(x => x.GetParameters().Length != 0) : constructors[0]; + var constructor = module.ImportReference(constructor_).Resolve(); var serialize = module.ImportReference(type.GetMethod("Serialize", Util.Flags)).Resolve(); var deserialize = module.ImportReference(type.GetMethod("Deserialize", Util.Flags)).Resolve();