WeaverRunner.cs
5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor.Compilation;
using UnityEngine;
using Assembly = System.Reflection.Assembly;
namespace UnityEditor.Networking
{
internal class WeaverRunner
{
[InitializeOnLoadMethod]
static void OnInitializeOnLoad()
{
CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished;
}
static void OnCompilationFinished(string targetAssembly, CompilerMessage[] messages)
{
const string k_HlapiRuntimeAssemblyName = "com.unity.multiplayer-hlapi.Runtime";
// Do nothing if there were compile errors on the target
if (messages.Length > 0)
{
foreach (var msg in messages)
{
if (msg.type == CompilerMessageType.Error)
{
return;
}
}
}
// Should not run on the editor only assemblies
if (targetAssembly.Contains("-Editor") || targetAssembly.Contains(".Editor"))
{
return;
}
// Should not run on own assembly
if (targetAssembly.Contains(k_HlapiRuntimeAssemblyName))
{
return;
}
var scriptAssembliesPath = Application.dataPath + "/../" + Path.GetDirectoryName(targetAssembly);
string unityEngine = "";
string unetAssemblyPath = "";
var outputDirectory = scriptAssembliesPath;
var assemblyPath = targetAssembly;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
bool usesUnet = false;
bool foundThisAssembly = false;
HashSet<string> depenencyPaths = new HashSet<string>();
foreach (var assembly in assemblies)
{
// Find the assembly currently being compiled from domain assembly list and check if it's using unet
if (assembly.GetName().Name == Path.GetFileNameWithoutExtension(targetAssembly))
{
foundThisAssembly = true;
foreach (var dependency in assembly.GetReferencedAssemblies())
{
// Since this assembly is already loaded in the domain this is a no-op and retuns the
// already loaded assembly
var location = Assembly.Load(dependency).Location;
depenencyPaths.Add(Path.GetDirectoryName(location));
if (dependency.Name.Contains(k_HlapiRuntimeAssemblyName))
{
usesUnet = true;
}
}
}
try
{
if (assembly.Location.Contains("UnityEngine.CoreModule"))
{
unityEngine = assembly.Location;
}
if (assembly.Location.Contains(k_HlapiRuntimeAssemblyName))
{
unetAssemblyPath = assembly.Location;
}
}
catch (NotSupportedException)
{
// in memory assembly, can't get location
}
}
if (!foundThisAssembly)
{
// Target assembly not found in current domain, trying to load it to check references
// will lead to trouble in the build pipeline, so lets assume it should go to weaver.
// Add all assemblies in current domain to dependency list since there could be a
// dependency lurking there (there might be generated assemblies so ignore file not found exceptions).
// (can happen in runtime test framework on editor platform and when doing full library reimport)
foreach (var assembly in assemblies)
{
try
{
if (!(assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder))
depenencyPaths.Add(Path.GetDirectoryName(Assembly.Load(assembly.GetName().Name).Location));
}
catch (FileNotFoundException) { }
}
usesUnet = true;
}
if (!usesUnet)
{
return;
}
if (string.IsNullOrEmpty(unityEngine))
{
Debug.LogError("Failed to find UnityEngine assembly");
return;
}
if (string.IsNullOrEmpty(unetAssemblyPath))
{
Debug.LogError("Failed to find hlapi runtime assembly");
return;
}
Unity.UNetWeaver.Program.Process(unityEngine, unetAssemblyPath, outputDirectory, new[] { assemblyPath }, depenencyPaths.ToArray(), (value) => { Debug.LogWarning(value); }, (value) => { Debug.LogError(value); });
}
}
}