Skip to content

Commit

Permalink
fix: invalid reference to UnityEditor in standalone build
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Sep 30, 2020
1 parent 0ac5767 commit 69f5be7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Assets/Mirror/Editor/Weaver/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static MethodReference MakeHostInstanceGeneric(this MethodReference self,
/// <para> Note that calling ArraySegment`T.get_Count directly gives an invalid IL error </para>
/// </summary>
/// <param name="self"></param>
/// <param name="instanceType"></param>
/// <param name="instanceType">Generic Instance eg Writer<int></param>
/// <returns></returns>
public static FieldReference SpecializeField(this FieldReference self, GenericInstanceType instanceType)
{
Expand Down
28 changes: 22 additions & 6 deletions Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// finds all readers and writers and register them
using System.IO;
using System;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
using UnityEditor.Compilation;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;

namespace Mirror.Weaver
{
Expand All @@ -18,8 +20,6 @@ public static void Process(AssemblyDefinition CurrentAssembly, Assembly unityAss
Writers.Init();
foreach (Assembly unityAsm in unityAssembly.assemblyReferences)
{
// cute optimization, None of the unity libraries have readers and writers
// saves about .3 seconds in every weaver test
if (unityAsm.name == "Mirror")
{
using (var asmResolver = new DefaultAssemblyResolver())
Expand Down Expand Up @@ -217,6 +217,13 @@ static void LoadDeclaredReaders(AssemblyDefinition currentAssembly, TypeDefiniti
}
}

private static bool IsEditorAssembly(AssemblyDefinition currentAssembly)
{
return currentAssembly.MainModule.AssemblyReferences.Any(assemblyReference =>
assemblyReference.Name == nameof(UnityEditor)
) ;
}

/// <summary>
/// Creates a method that will store all the readers and writers into
/// <see cref="Writer{T}.write"/> and <see cref="Reader{T}.read"/>
Expand All @@ -231,11 +238,20 @@ public static void InitializeReaderAndWriters(AssemblyDefinition currentAssembly
MethodAttributes.Static,
WeaverTypes.Import(typeof(void)));

System.Reflection.ConstructorInfo attributeconstructor = typeof(InitializeOnLoadMethodAttribute).GetConstructors()[0];
System.Reflection.ConstructorInfo attributeconstructor = typeof(RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new [] { typeof(RuntimeInitializeLoadType)});

var customAttributeRef = new CustomAttribute(currentAssembly.MainModule.ImportReference(attributeconstructor));
CustomAttribute customAttributeRef = new CustomAttribute(currentAssembly.MainModule.ImportReference(attributeconstructor));
customAttributeRef.ConstructorArguments.Add(new CustomAttributeArgument(WeaverTypes.Import<RuntimeInitializeLoadType>(), RuntimeInitializeLoadType.BeforeSceneLoad));
rwInitializer.CustomAttributes.Add(customAttributeRef);

if (IsEditorAssembly(currentAssembly))
{
// editor assembly, add InitializeOnLoadMethod too. Useful for the editor tests
System.Reflection.ConstructorInfo initializeOnLoadConstructor = typeof(InitializeOnLoadMethodAttribute).GetConstructor(new Type[0]);
CustomAttribute initializeCustomConstructorRef = new CustomAttribute(currentAssembly.MainModule.ImportReference(initializeOnLoadConstructor));
rwInitializer.CustomAttributes.Add(initializeCustomConstructorRef);
}

ILProcessor worker = rwInitializer.Body.GetILProcessor();

Writers.InitializeWriters(worker);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Editor/Weaver/Weaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void ConfirmGeneratedCodeClass()
if (generateContainerClass == null)
{
generateContainerClass = new TypeDefinition("Mirror", "GeneratedNetworkCode",
TypeAttributes.BeforeFieldInit | TypeAttributes.Class | TypeAttributes.AnsiClass | TypeAttributes.Public | TypeAttributes.AutoClass,
TypeAttributes.BeforeFieldInit | TypeAttributes.Class | TypeAttributes.AnsiClass | TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.Abstract | TypeAttributes.Sealed,
WeaverTypes.Import<object>());
}
}
Expand Down

0 comments on commit 69f5be7

Please sign in to comment.