PlayerLauncher.cs
10.9 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using NUnit.Framework.Internal.Filters;
using UnityEditor;
using UnityEditor.TestRunner.TestLaunchers;
using UnityEditor.TestTools.TestRunner.Api;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestRunner.Utils;
using UnityEngine.TestTools.TestRunner;
using UnityEngine.TestTools.TestRunner.Callbacks;
namespace UnityEditor.TestTools.TestRunner
{
internal class TestLaunchFailedException : Exception
{
public TestLaunchFailedException() {}
public TestLaunchFailedException(string message) : base(message) {}
}
[Serializable]
internal class PlayerLauncher : RuntimeTestLauncherBase
{
private readonly PlaymodeTestsControllerSettings m_Settings;
private readonly BuildTarget m_TargetPlatform;
private ITestRunSettings m_OverloadTestRunSettings;
private string m_SceneName;
private int m_HeartbeatTimeout;
public PlayerLauncher(PlaymodeTestsControllerSettings settings, BuildTarget? targetPlatform, ITestRunSettings overloadTestRunSettings, int heartbeatTimeout)
{
m_Settings = settings;
m_TargetPlatform = targetPlatform ?? EditorUserBuildSettings.activeBuildTarget;
m_OverloadTestRunSettings = overloadTestRunSettings;
m_HeartbeatTimeout = heartbeatTimeout;
}
protected override RuntimePlatform? TestTargetPlatform
{
get { return BuildTargetConverter.TryConvertToRuntimePlatform(m_TargetPlatform); }
}
public override void Run()
{
var editorConnectionTestCollector = RemoteTestRunController.instance;
editorConnectionTestCollector.hideFlags = HideFlags.HideAndDontSave;
editorConnectionTestCollector.Init(m_TargetPlatform, m_HeartbeatTimeout);
var remotePlayerLogController = RemotePlayerLogController.instance;
remotePlayerLogController.hideFlags = HideFlags.HideAndDontSave;
using (var settings = new PlayerLauncherContextSettings(m_OverloadTestRunSettings))
{
m_SceneName = CreateSceneName();
var scene = PrepareScene(m_SceneName);
string scenePath = scene.path;
var filter = m_Settings.BuildNUnitFilter();
var runner = LoadTests(filter);
var exceptionThrown = ExecutePreBuildSetupMethods(runner.LoadedTest, filter);
if (exceptionThrown)
{
ReopenOriginalScene(m_Settings.originalScene);
AssetDatabase.DeleteAsset(m_SceneName);
CallbacksDelegator.instance.RunFailed("Run Failed: One or more errors in a prebuild setup. See the editor log for details.");
return;
}
var playerBuildOptions = GetBuildOptions(scenePath);
var success = BuildAndRunPlayer(playerBuildOptions);
editorConnectionTestCollector.PostBuildAction();
ExecutePostBuildCleanupMethods(runner.LoadedTest, filter);
ReopenOriginalScene(m_Settings.originalScene);
AssetDatabase.DeleteAsset(m_SceneName);
if (!success)
{
editorConnectionTestCollector.CleanUp();
ScriptableObject.DestroyImmediate(editorConnectionTestCollector);
Debug.LogError("Player build failed");
throw new TestLaunchFailedException("Player build failed");
}
if ((playerBuildOptions.BuildPlayerOptions.options & BuildOptions.AutoRunPlayer) != 0)
{
editorConnectionTestCollector.PostSuccessfulBuildAction();
editorConnectionTestCollector.PostSuccessfulLaunchAction();
}
var runSettings = m_OverloadTestRunSettings as PlayerLauncherTestRunSettings;
if (success && runSettings != null && runSettings.buildOnly)
{
EditorUtility.RevealInFinder(playerBuildOptions.BuildPlayerOptions.locationPathName);
}
}
}
public Scene PrepareScene(string sceneName)
{
var scene = CreateBootstrapScene(sceneName, runner =>
{
runner.AddEventHandlerMonoBehaviour<PlayModeRunnerCallback>();
runner.settings = m_Settings;
var commandLineArgs = Environment.GetCommandLineArgs();
if (!commandLineArgs.Contains("-doNotReportTestResultsBackToEditor"))
{
runner.AddEventHandlerMonoBehaviour<RemoteTestResultSender>();
}
runner.AddEventHandlerMonoBehaviour<PlayerQuitHandler>();
runner.AddEventHandlerScriptableObject<TestRunCallbackListener>();
});
return scene;
}
private static bool BuildAndRunPlayer(PlayerLauncherBuildOptions buildOptions)
{
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Building player with following options:\n{0}", buildOptions);
// Android has to be in listen mode to establish player connection
if (buildOptions.BuildPlayerOptions.target == BuildTarget.Android)
{
buildOptions.BuildPlayerOptions.options &= ~BuildOptions.ConnectToHost;
}
// For now, so does Lumin
if (buildOptions.BuildPlayerOptions.target == BuildTarget.Lumin)
{
buildOptions.BuildPlayerOptions.options &= ~BuildOptions.ConnectToHost;
}
var result = BuildPipeline.BuildPlayer(buildOptions.BuildPlayerOptions);
if (result.summary.result != Build.Reporting.BuildResult.Succeeded)
Debug.LogError(result.SummarizeErrors());
return result.summary.result == Build.Reporting.BuildResult.Succeeded;
}
internal PlayerLauncherBuildOptions GetBuildOptions(string scenePath)
{
var buildOnly = false;
var runSettings = m_OverloadTestRunSettings as PlayerLauncherTestRunSettings;
if (runSettings != null)
{
buildOnly = runSettings.buildOnly;
}
var buildOptions = new BuildPlayerOptions();
var scenes = new List<string>() { scenePath };
scenes.AddRange(EditorBuildSettings.scenes.Select(x => x.path));
buildOptions.scenes = scenes.ToArray();
buildOptions.options |= BuildOptions.Development | BuildOptions.ConnectToHost | BuildOptions.IncludeTestAssemblies | BuildOptions.StrictMode;
buildOptions.target = m_TargetPlatform;
if (EditorUserBuildSettings.waitForPlayerConnection)
buildOptions.options |= BuildOptions.WaitForPlayerConnection;
if (EditorUserBuildSettings.allowDebugging)
buildOptions.options |= BuildOptions.AllowDebugging;
if (EditorUserBuildSettings.installInBuildFolder)
buildOptions.options |= BuildOptions.InstallInBuildFolder;
else if (!buildOnly)
buildOptions.options |= BuildOptions.AutoRunPlayer;
var buildTargetGroup = EditorUserBuildSettings.activeBuildTargetGroup;
//Check if Lz4 is supported for the current buildtargetgroup and enable it if need be
if (PostprocessBuildPlayer.SupportsLz4Compression(buildTargetGroup, m_TargetPlatform))
{
if (EditorUserBuildSettings.GetCompressionType(buildTargetGroup) == Compression.Lz4)
buildOptions.options |= BuildOptions.CompressWithLz4;
else if (EditorUserBuildSettings.GetCompressionType(buildTargetGroup) == Compression.Lz4HC)
buildOptions.options |= BuildOptions.CompressWithLz4HC;
}
string buildLocation;
if (buildOnly)
{
buildLocation = buildOptions.locationPathName = runSettings.buildOnlyLocationPath;
}
else
{
var reduceBuildLocationPathLength = false;
//Some platforms hit MAX_PATH limits during the build process, in these cases minimize the path length
if ((m_TargetPlatform == BuildTarget.WSAPlayer) || (m_TargetPlatform == BuildTarget.XboxOne))
{
reduceBuildLocationPathLength = true;
}
var uniqueTempPathInProject = FileUtil.GetUniqueTempPathInProject();
var playerDirectoryName = reduceBuildLocationPathLength ? "PwT" : "PlayerWithTests";
if (reduceBuildLocationPathLength)
{
uniqueTempPathInProject = Path.GetTempFileName();
File.Delete(uniqueTempPathInProject);
Directory.CreateDirectory(uniqueTempPathInProject);
}
var tempPath = Path.GetFullPath(uniqueTempPathInProject);
buildLocation = Path.Combine(tempPath, playerDirectoryName);
// iOS builds create a folder with Xcode project instead of an executable, therefore no executable name is added
if (m_TargetPlatform == BuildTarget.iOS)
{
buildOptions.locationPathName = buildLocation;
}
else
{
string extensionForBuildTarget =
PostprocessBuildPlayer.GetExtensionForBuildTarget(buildTargetGroup, buildOptions.target,
buildOptions.options);
var playerExecutableName = "PlayerWithTests";
playerExecutableName += string.Format(".{0}", extensionForBuildTarget);
buildOptions.locationPathName = Path.Combine(buildLocation, playerExecutableName);
}
}
return new PlayerLauncherBuildOptions
{
BuildPlayerOptions = ModifyBuildOptions(buildOptions),
PlayerDirectory = buildLocation,
};
}
private BuildPlayerOptions ModifyBuildOptions(BuildPlayerOptions buildOptions)
{
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => x.GetReferencedAssemblies().Any(z => z.Name == "UnityEditor.TestRunner")).ToArray();
var attributes = allAssemblies.SelectMany(assembly => assembly.GetCustomAttributes(typeof(TestPlayerBuildModifierAttribute), true).OfType<TestPlayerBuildModifierAttribute>()).ToArray();
var modifiers = attributes.Select(attribute => attribute.ConstructModifier()).ToArray();
foreach (var modifier in modifiers)
{
buildOptions = modifier.ModifyOptions(buildOptions);
}
return buildOptions;
}
}
}