create completely empty default ctor if one doesnt already exist

This commit is contained in:
JohnCorby 2022-01-13 21:35:15 -08:00
parent 7fc240858e
commit 7064cbb793
2 changed files with 14 additions and 3 deletions

View File

@ -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);

View File

@ -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();