Skip to content

Commit 34caf12

Browse files
author
Stewart Miles
committed
Fixed AAR processing dialog getting stuck in Unity 5.6.
In some versions of Unity (e.g 5.6), progress dialogs are not displayed correctly when they're updated multiple times per editor update. This changes the ProcessAars() method to incrementally process AARs each editor tick to update the editor dialog and correctly display the update dialog. Bug: 113127364 Change-Id: I6bfd175d506d93cc554b362482f8846da12d2481
1 parent 16c9753 commit 34caf12

File tree

2 files changed

+75
-48
lines changed

2 files changed

+75
-48
lines changed

source/PlayServicesResolver/src/ResolverVer1_1.cs

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -667,35 +667,40 @@ private void GradleResolution(
667667

668668
}
669669
// Process / explode copied AARs.
670-
ProcessAars(destinationDirectory, new HashSet<string>(copiedArtifacts));
671-
672-
// Look up the original Dependency structure for each missing artifact.
673-
var missingArtifactsAsDependencies = new List<Dependency>();
674-
foreach (var artifact in missingArtifacts) {
675-
Dependency dep;
676-
if (!allDependencies.TryGetValue(artifact, out dep)) {
677-
// If this fails, something may have gone wrong with the Gradle script.
678-
// Rather than failing hard, fallback to recreating the Dependency
679-
// class with the partial data we have now.
680-
var components = new List<string>(artifact.Split(new char[] { ':' }));
681-
if (components.Count < 2) {
682-
PlayServicesResolver.Log(
683-
String.Format(
684-
"Found invalid missing artifact {0}\n" +
685-
"Something went wrong with the gradle artifact download " +
686-
"script\n." +
687-
"Please report a bug", artifact),
688-
level: LogLevel.Warning);
689-
continue;
690-
} else if (components.Count < 3 || components[2] == "+") {
691-
components.Add("LATEST");
670+
ProcessAars(
671+
destinationDirectory, new HashSet<string>(copiedArtifacts),
672+
() => {
673+
// Look up the original Dependency structure for each missing artifact.
674+
var missingArtifactsAsDependencies = new List<Dependency>();
675+
foreach (var artifact in missingArtifacts) {
676+
Dependency dep;
677+
if (!allDependencies.TryGetValue(artifact, out dep)) {
678+
// If this fails, something may have gone wrong with the Gradle
679+
// script. Rather than failing hard, fallback to recreating the
680+
// Dependency class with the partial data we have now.
681+
var components = new List<string>(
682+
artifact.Split(new char[] { ':' }));
683+
if (components.Count < 2) {
684+
PlayServicesResolver.Log(
685+
String.Format(
686+
"Found invalid missing artifact {0}\n" +
687+
"Something went wrong with the gradle artifact " +
688+
"download script\n." +
689+
"Please report a bug", artifact),
690+
level: LogLevel.Warning);
691+
continue;
692+
} else if (components.Count < 3 || components[2] == "+") {
693+
components.Add("LATEST");
694+
}
695+
dep = new Dependency(components[0], components[1], components[2]);
696+
}
697+
missingArtifactsAsDependencies.Add(dep);
692698
}
693-
dep = new Dependency(components[0], components[1], components[2]);
694-
}
695-
missingArtifactsAsDependencies.Add(dep);
696-
}
697-
if (logErrorOnMissingArtifacts) LogMissingDependenciesError(missingArtifacts);
698-
resolutionComplete(missingArtifactsAsDependencies);
699+
if (logErrorOnMissingArtifacts) {
700+
LogMissingDependenciesError(missingArtifacts);
701+
}
702+
resolutionComplete(missingArtifactsAsDependencies);
703+
});
699704
};
700705

701706
// Executes gradleComplete on the main thread.
@@ -1328,23 +1333,37 @@ private string FindAarInTargetPath(string aarPath) {
13281333
/// <param name="dir">The directory to process.</param>
13291334
/// <param name="updatedFiles">Set of files that were recently updated and should be
13301335
/// processed.</param>
1331-
/// <param name="displayProgress">Display a progress bar while processing.</param>
1332-
private void ProcessAars(string dir, HashSet<string> updatedFiles,
1333-
bool displayProgress = true) {
1336+
/// <param name="complete">Executed when this process is complete.</param>
1337+
private void ProcessAars(string dir, HashSet<string> updatedFiles, Action complete) {
13341338
// Get set of AAR files and directories we're managing.
1335-
var aars = new HashSet<string>(PlayServicesResolver.FindLabeledAssets());
1336-
foreach (var aarData in aarExplodeData.Values) aars.Add(aarData.path);
1339+
var uniqueAars = new HashSet<string>(PlayServicesResolver.FindLabeledAssets());
1340+
foreach (var aarData in aarExplodeData.Values) uniqueAars.Add(aarData.path);
1341+
var aars = new Queue<string>(uniqueAars);
13371342

13381343
const string progressBarTitle = "Processing AARs...";
1339-
float numberOfAars = (float)aars.Count;
1340-
int aarIndex = 0;
1341-
displayProgress &= (numberOfAars > 0 && !ExecutionEnvironment.InBatchMode);
1342-
try {
1343-
foreach (string aarPath in aars) {
1344+
int numberOfAars = aars.Count;
1345+
bool displayProgress = (numberOfAars > 0 && !ExecutionEnvironment.InBatchMode);
1346+
1347+
if (numberOfAars == 0) {
1348+
complete();
1349+
return;
1350+
}
1351+
// EditorUtility.DisplayProgressBar can't be called multiple times per UI thread tick
1352+
// in some versions of Unity so perform increment processing on each UI thread tick.
1353+
RunOnMainThread.PollOnUpdateUntilComplete(() => {
1354+
int remainingAars = aars.Count;
1355+
bool allAarsProcessed = remainingAars == 0;
1356+
// Since the completion callback can trigger an update, remove this closure from
1357+
// the polling job list if complete.
1358+
if (allAarsProcessed) return true;
1359+
int processedAars = numberOfAars - remainingAars;
1360+
string aarPath = aars.Dequeue();
1361+
remainingAars--;
1362+
allAarsProcessed = remainingAars == 0;
1363+
float progress = (float)processedAars / (float)numberOfAars;
1364+
try {
13441365
if (displayProgress) {
1345-
EditorUtility.DisplayProgressBar(progressBarTitle, aarPath,
1346-
(float)aarIndex / numberOfAars);
1347-
aarIndex++;
1366+
EditorUtility.DisplayProgressBar(progressBarTitle, aarPath, progress);
13481367
}
13491368
bool explode = ShouldExplode(aarPath);
13501369
var aarData = FindAarExplodeDataEntry(aarPath);
@@ -1365,7 +1384,8 @@ private void ProcessAars(string dir, HashSet<string> updatedFiles,
13651384
} else if (aarPath != aarData.path) {
13661385
// Clean up previously expanded / exploded versions of the AAR.
13671386
PlayServicesResolver.Log(
1368-
String.Format("Cleaning up previously exploded AAR {0}", aarPath),
1387+
String.Format("Cleaning up previously exploded AAR {0}",
1388+
aarPath),
13691389
level: LogLevel.Verbose);
13701390
FileUtils.DeleteExistingFileOrDirectory(
13711391
DetermineExplodedAarPath(aarPath));
@@ -1374,11 +1394,15 @@ private void ProcessAars(string dir, HashSet<string> updatedFiles,
13741394
aarData.gradleExport = PlayServicesResolver.GradleProjectExportEnabled;
13751395
aarData.TargetAbis = AndroidAbis.Current;
13761396
}
1397+
SaveAarExplodeCache();
1398+
} finally {
1399+
if (allAarsProcessed) {
1400+
if (displayProgress) EditorUtility.ClearProgressBar();
1401+
complete();
1402+
}
13771403
}
1378-
SaveAarExplodeCache();
1379-
} finally {
1380-
if (displayProgress) EditorUtility.ClearProgressBar();
1381-
}
1404+
return allAarsProcessed;
1405+
});
13821406
}
13831407

13841408
/// <summary>

source/VersionHandlerImpl/src/RunOnMainThread.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public static void PollOnUpdateUntilComplete(Func<bool> condition) {
253253
while (true) {
254254
ExecuteAll();
255255
lock (pollingJobs) {
256-
if (pollingJobs.IndexOf(condition) < 0) break;
256+
if (pollingJobs.Count == 0) break;
257257
}
258258
// Wait 100ms.
259259
Thread.Sleep(100);
@@ -395,7 +395,10 @@ private static void ExecuteAll() {
395395
}
396396

397397
// Execute polling jobs.
398-
ExecutePollingJobs();
398+
int remainingJobs;
399+
do {
400+
remainingJobs = ExecutePollingJobs();
401+
} while (remainingJobs > 0 && ExecutionEnvironment.InBatchMode);
399402
});
400403
}
401404
}

0 commit comments

Comments
 (0)