Skip to content

Commit 3970bcd

Browse files
author
Stewart Miles
committed
Fixed some Version Handler issues in 2019.3+
If VersionHandler.UpdateNow() is called from a static constructor in Unity 2019.3 (which is TBH undefined behavior) Unity does not save asset metadata changes to disk leaving all newly _enabled_ plugins in a disabled state. This changes all entry points to UpdateVersionedAssets() to run the core logic on the editor thread if possible so that metadata changes are correctly applied when the asset database is refreshed. Other minor clean ups: - Moved log level loading into a shared method that is used by all public entry points. - Changed calls to UpdateVersionedAssets() from the AssetPostProcessor method to excecute after 2s so that batch asset changes don't result in multiple calls to UpdateVersionedAssets(). - Improved logging of loaded manifests. - Minor speed up of loaded assembly polling. Change-Id: I0effaa84c21a13ac5c615520a1a2eb1751d071c5
1 parent 0d86b89 commit 3970bcd

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

source/VersionHandlerImpl/src/VersionHandlerImpl.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,10 +1774,17 @@ public bool ParseManifests(FileMetadataByVersion metadataByVersion,
17741774
var obsoleteFilesSorted = new List<string>(obsoleteFiles);
17751775
currentFilesSorted.Sort();
17761776
obsoleteFilesSorted.Sort();
1777-
Log(String.Format("'{0}' Manifest:\n\nCurrent files:\n{1}\n\nObsolete files:\n{2}",
1778-
filenameCanonical,
1779-
String.Join("\n", currentFilesSorted.ToArray()),
1780-
String.Join("\n", obsoleteFilesSorted.ToArray())),
1777+
var components = new List<string>();
1778+
if (currentFilesSorted.Count > 0) {
1779+
components.Add(String.Format("Current files:\n{0}",
1780+
String.Join("\n", currentFilesSorted.ToArray())));
1781+
}
1782+
if (obsoleteFilesSorted.Count > 0) {
1783+
components.Add(String.Format("Obsolete files:\n{0}",
1784+
String.Join("\n", obsoleteFilesSorted.ToArray())));
1785+
}
1786+
Log(String.Format("'{0}' Manifest:\n{1}",
1787+
filenameCanonical, String.Join("\n", components.ToArray())),
17811788
verbose: true);
17821789
return true;
17831790
}
@@ -2148,6 +2155,13 @@ public static Logger Logger {
21482155
InstallSourceFilename = Assembly.GetAssembly(typeof(VersionHandlerImpl)).Location
21492156
};
21502157

2158+
/// <summary>
2159+
/// Load log preferences.
2160+
/// </summary>
2161+
private static void LoadLogPreferences() {
2162+
VerboseLoggingEnabled = VerboseLoggingEnabled;
2163+
}
2164+
21512165
/// <summary>
21522166
/// Enables / disables assets imported at multiple revisions / versions.
21532167
/// In addition, this module will read text files matching _manifest_
@@ -2156,8 +2170,7 @@ public static Logger Logger {
21562170
static VersionHandlerImpl() {
21572171
Log("Loaded VersionHandlerImpl", verbose: true);
21582172
RunOnMainThread.Run(() => {
2159-
// Load log preferences.
2160-
VerboseLoggingEnabled = VerboseLoggingEnabled;
2173+
LoadLogPreferences();
21612174
UpdateVersionedAssetsOnUpdate();
21622175
}, runNow: false);
21632176
}
@@ -2196,7 +2209,10 @@ private static bool EnabledEditorDllsLoaded {
21962209
var loadedAssemblyPaths = new HashSet<string>();
21972210
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
21982211
try {
2199-
loadedAssemblyPaths.Add(Path.GetFullPath(assembly.Location));
2212+
var path = Path.GetFullPath(assembly.Location);
2213+
if (enabledEditorDlls.Contains(path)) {
2214+
loadedAssemblyPaths.Add(path);
2215+
}
22002216
} catch (NotSupportedException) {
22012217
// Dynamic assemblies do not have a file location so ignore.
22022218
}
@@ -2432,6 +2448,7 @@ public static void ShowSettings() {
24322448
/// </summary>
24332449
[MenuItem("Assets/External Dependency Manager/Version Handler/Update")]
24342450
public static void UpdateNow() {
2451+
LoadLogPreferences();
24352452
UpdateVersionedAssets(true, () => {
24362453
Dialog.Display(PLUGIN_NAME, "Update complete.", 0, "OK");
24372454
});
@@ -2589,6 +2606,20 @@ public static void UpdateVersionedAssets(bool forceUpdate) {
25892606
/// <param name="forceUpdate">Whether to force an update.</param>
25902607
/// <param name="complete">Called when this is method is complete.</param>
25912608
public static void UpdateVersionedAssets(bool forceUpdate, Action complete) {
2609+
CancelUpdateVersionedAssets();
2610+
RunOnMainThread.Run(() => { UpdateVersionedAssetsOnMainThread(forceUpdate, complete); },
2611+
runNow: false);
2612+
}
2613+
2614+
/// <summary>
2615+
/// Find all files in the asset database with multiple version numbers
2616+
/// encoded in their filename, select the most recent revisions and
2617+
/// delete obsolete versions and files referenced by old manifests that
2618+
/// are not present in the most recent manifests.
2619+
/// </summary>
2620+
/// <param name="forceUpdate">Whether to force an update.</param>
2621+
/// <param name="complete">Called when this is method is complete.</param>
2622+
private static void UpdateVersionedAssetsOnMainThread(bool forceUpdate, Action complete) {
25922623
// If this module is disabled do nothing.
25932624
if (!forceUpdate && !Enabled) {
25942625
complete();
@@ -2784,14 +2815,35 @@ public static float GetUnityVersionMajorMinor() {
27842815
return ExecutionEnvironment.VersionMajorMinor;
27852816
}
27862817

2818+
// ID of the scheduled job which performs an update.
2819+
private static int updateVersionedAssetsJob = 0;
2820+
2821+
/// <summary>
2822+
/// Cancel the update versioned assets job.
2823+
/// </summary>
2824+
private static void CancelUpdateVersionedAssets() {
2825+
if (updateVersionedAssetsJob > 0) {
2826+
RunOnMainThread.Cancel(updateVersionedAssetsJob);
2827+
updateVersionedAssetsJob = 0;
2828+
}
2829+
}
2830+
27872831
/// <summary>
27882832
/// Scanned for versioned assets and apply modifications if required.
27892833
/// </summary>
27902834
private static void OnPostprocessAllAssets(
27912835
string[] importedAssets, string[] deletedAssets,
27922836
string[] movedAssets, string[] movedFromPath) {
27932837
ManifestReferences.FlushCaches();
2794-
UpdateVersionedAssets();
2838+
if (Enabled) {
2839+
const double UpdateDelayInMiliseconds = 2000;
2840+
CancelUpdateVersionedAssets();
2841+
updateVersionedAssetsJob =
2842+
RunOnMainThread.Schedule(() => {
2843+
UpdateVersionedAssets();
2844+
},
2845+
UpdateDelayInMiliseconds);
2846+
}
27952847
}
27962848

27972849
/// <summary>

0 commit comments

Comments
 (0)