Skip to content

Commit 6e4b257

Browse files
author
Stewart Miles
committed
Workaround flakey EditorApplication.isCompiling property.
Version Handler relies upon EditorApplication.isCompiling to determine whether an asset database refresh caused any editor assemblies (DLLs) to be reloaded into the app domain. However, deterministic behavior of EditorApplication.isCompiling varies across Unity versions. This change works around EditorApplication.isCompiling non-determinism by storing the set of editor assemblies that should be enabled when the asset database has been refreshed and then polls for them be loaded into the application domain before sending a notification that the Version Handler has updated the project. This _could_ have the side-effect of never notifying that the project is ready if an assembly fails to load but that's better than sending a notification before the app domain has been reloaded. Bug: 150471207 Change-Id: If481784a7eb1256cc345464a274ec9cf5637830b
1 parent d0003c8 commit 6e4b257

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

source/VersionHandlerImpl/src/VersionHandlerImpl.cs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,16 @@ public FileMetadata MostRecentVersion {
920920
}
921921
}
922922

923+
/// <summary>
924+
/// Backing store for EnabledEditorDlls.
925+
/// </summary>
926+
private HashSet<string> enabledEditorDlls = new HashSet<string>();
927+
928+
/// <summary>
929+
/// Full path of DLLs that should be loaded into the editor application domain.
930+
/// </summary>
931+
public ICollection<string> EnabledEditorDlls { get { return enabledEditorDlls; } }
932+
923933
/// <summary>
924934
/// Determine whether the PluginImporter class is available in
925935
/// UnityEditor. Unity 4 does not have the PluginImporter class so
@@ -975,6 +985,7 @@ public bool EnableMostRecentPlugins(HashSet<string> disableFiles) {
975985
int numberOfVersions = metadataByVersion.Count;
976986
var disabledVersions = new List<string>();
977987
string enabledVersion = null;
988+
enabledEditorDlls = new HashSet<string>();
978989

979990
// If the canonical file is out of date, update it.
980991
if (numberOfVersions > 0) {
@@ -1071,6 +1082,9 @@ public bool EnableMostRecentPlugins(HashSet<string> disableFiles) {
10711082
dotNetVersionMessage),
10721083
verbose: true);
10731084
pluginImporter.SetCompatibleWithEditor(editorEnabled);
1085+
if (metadata.filename.ToLower().EndsWith(".dll")) {
1086+
enabledEditorDlls.Add(Path.GetFullPath(metadata.filename));
1087+
}
10741088
modifiedThisVersion = true;
10751089
}
10761090
bool compatibleWithAnyPlatform = pluginImporter.GetCompatibleWithAnyPlatform();
@@ -1200,6 +1214,16 @@ public Dictionary<string, FileMetadata> MetadataByFilename {
12001214
get { return metadataByFilename; }
12011215
}
12021216

1217+
/// <summary>
1218+
/// Backing store for EnabledEditorDlls.
1219+
/// </summary>
1220+
private HashSet<string> enabledEditorDlls = new HashSet<string>();
1221+
1222+
/// <summary>
1223+
/// Full path of DLLs that should be loaded into the editor application domain.
1224+
/// </summary>
1225+
public ICollection<string> EnabledEditorDlls { get { return enabledEditorDlls; } }
1226+
12031227
/// <summary>
12041228
/// Construct an instance.
12051229
/// </summary>
@@ -1226,6 +1250,7 @@ public static FileMetadataSet FilterOutReadOnlyFiles(FileMetadataSet metadataSet
12261250
public void Clear() {
12271251
metadataByCanonicalFilename = new Dictionary<string, FileMetadataByVersion>();
12281252
metadataByFilename = new Dictionary<string, FileMetadata>();
1253+
enabledEditorDlls = new HashSet<string>();
12291254
}
12301255

12311256
/// <summary>
@@ -1545,6 +1570,7 @@ public bool EnableMostRecentPlugins(bool forceUpdate,
15451570

15461571
foreach (var metadataByVersion in Values) {
15471572
modified |= metadataByVersion.EnableMostRecentPlugins(disableFiles);
1573+
enabledEditorDlls.UnionWith(metadataByVersion.EnabledEditorDlls);
15481574
}
15491575
return modified;
15501576
}
@@ -2162,6 +2188,23 @@ private static bool Refreshing {
21622188
}
21632189
}
21642190

2191+
/// <summary>
2192+
/// Whether all editor DLLs have been loaded into the app domain.
2193+
/// </summary>
2194+
private static bool EnabledEditorDllsLoaded {
2195+
get {
2196+
var loadedAssemblyPaths = new HashSet<string>();
2197+
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
2198+
try {
2199+
loadedAssemblyPaths.Add(Path.GetFullPath(assembly.Location));
2200+
} catch (NotSupportedException) {
2201+
// Dynamic assemblies do not have a file location so ignore.
2202+
}
2203+
}
2204+
return loadedAssemblyPaths.IsSupersetOf(enabledEditorDlls);
2205+
}
2206+
}
2207+
21652208
/// <summary>
21662209
/// Polls on main thread until compilation is finished prior to calling
21672210
/// NotifyUpdateCompleteMethods() if an asset database refresh was in progress or
@@ -2183,15 +2226,15 @@ private static bool Refreshing {
21832226
/// refreshing.</param>
21842227
private static void NotifyWhenCompliationComplete(bool forceNotification) {
21852228
RunOnMainThread.PollOnUpdateUntilComplete(() => {
2186-
if (EditorApplication.isCompiling) {
2229+
if (EditorApplication.isCompiling || !EnabledEditorDllsLoaded) {
21872230
if (!compiling) {
21882231
Log("Compiling...", verbose: true);
21892232
}
21902233
compiling = true;
2191-
// In batch mode this can get stuck forever as PollOnUpdateUntilComplete()
2192-
// will block the main thread which prevents the EditorApplication.isCompiling
2193-
// flag from being updated by the editor.
2194-
return ExecutionEnvironment.InBatchMode;
2234+
// When running a single method this can get stuck forever as
2235+
// PollOnUpdateUntilComplete() will block the main thread which prevents the
2236+
// EditorApplication.isCompiling flag from being updated by the editor.
2237+
return ExecutionEnvironment.ExecuteMethodEnabled;
21952238
}
21962239
if (compiling) {
21972240
Log("Compilation complete.", verbose: true);
@@ -2391,9 +2434,7 @@ public static void ShowSettings() {
23912434
[MenuItem("Assets/External Dependency Manager/Version Handler/Update")]
23922435
public static void UpdateNow() {
23932436
UpdateVersionedAssets(true, () => {
2394-
if (!ExecutionEnvironment.InBatchMode) {
2395-
Dialog.Display(PLUGIN_NAME, "Update complete.", 0, "OK");
2396-
}
2437+
Dialog.Display(PLUGIN_NAME, "Update complete.", 0, "OK");
23972438
});
23982439
}
23992440

@@ -2535,6 +2576,10 @@ public static void UpdateVersionedAssets(bool forceUpdate) {
25352576
UpdateVersionedAssets(forceUpdate, () => {});
25362577
}
25372578

2579+
/// <summary>
2580+
/// All editor DLLs enabled by the last pass of UpdateVersionedAssets().
2581+
/// </summary>
2582+
private static HashSet<string> enabledEditorDlls = new HashSet<string>();
25382583

25392584
/// <summary>
25402585
/// Find all files in the asset database with multiple version numbers
@@ -2565,6 +2610,7 @@ public static void UpdateVersionedAssets(bool forceUpdate, Action complete) {
25652610
var obsoleteFiles = new ObsoleteFiles(
25662611
ManifestReferences.FindAndReadManifests(allMetadataSet), allMetadataSet);
25672612
if (metadataSet.EnableMostRecentPlugins(forceUpdate, obsoleteFiles.All)) {
2613+
enabledEditorDlls.UnionWith(metadataSet.EnabledEditorDlls);
25682614
analytics.Report("enablemostrecentplugins", "Enable Most Recent Plugins");
25692615
AssetDatabase.Refresh();
25702616
Refreshing = true;

0 commit comments

Comments
 (0)