Skip to content

Commit b8923f5

Browse files
Ryan Meierryanmeier
Ryan Meier
authored andcommitted
Add support for VersionHandler to enable/disable files based on build target by renaming them.
Currently, we disable certain files per-platform by using PluginHandler, but this only works for libraries (.dll and the like), and does not work on some earlier versions of unity. This change adds the ability for VersionHandler to disable files by modifying their file extension (from .zzz -> .zzz_DISABLED) based on the platform being targeted - using the same Asset Labels already being used for PluginHandler files. Change-Id: I658ff0d37f68508cff6956beb8aa7084138c0fa4
1 parent ff47ad3 commit b8923f5

File tree

2 files changed

+161
-2
lines changed

2 files changed

+161
-2
lines changed

source/VersionHandlerImpl/src/SettingsDialog.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ private class Settings {
5353
/// </summary>
5454
internal bool useProjectSettings;
5555

56+
/// <summary>
57+
/// Whether files can be disabled by renaming them.
58+
/// </summary>
59+
internal bool renameToDisableFilesEnabled;
60+
5661
/// <summary>
5762
/// Load settings into the dialog.
5863
/// </summary>
@@ -62,6 +67,7 @@ internal Settings() {
6267
renameToCanonicalFilenames = VersionHandlerImpl.RenameToCanonicalFilenames;
6368
verboseLoggingEnabled = VersionHandlerImpl.VerboseLoggingEnabled;
6469
useProjectSettings = VersionHandlerImpl.UseProjectSettings;
70+
renameToDisableFilesEnabled = VersionHandlerImpl.RenameToDisableFilesEnabled;
6571
}
6672

6773
/// <summary>
@@ -73,6 +79,9 @@ internal void Save() {
7379
VersionHandlerImpl.RenameToCanonicalFilenames = renameToCanonicalFilenames;
7480
VersionHandlerImpl.VerboseLoggingEnabled = verboseLoggingEnabled;
7581
VersionHandlerImpl.UseProjectSettings = useProjectSettings;
82+
VersionHandlerImpl.RenameToDisableFilesEnabled = renameToDisableFilesEnabled;
83+
84+
VersionHandlerImpl.BuildTargetChecker.HandleSettingsChanged();
7685
}
7786
}
7887

@@ -89,7 +98,7 @@ private void LoadSettings() {
8998
/// Setup the window's initial position and size.
9099
/// </summary>
91100
public void Initialize() {
92-
minSize = new Vector2(300, 200);
101+
minSize = new Vector2(300, 250);
93102
position = new Rect(UnityEngine.Screen.width / 3,
94103
UnityEngine.Screen.height / 3,
95104
minSize.x, minSize.y);
@@ -130,6 +139,12 @@ public void OnGUI() {
130139
settings.cleanUpPromptEnabled = EditorGUILayout.Toggle(settings.cleanUpPromptEnabled);
131140
GUILayout.EndHorizontal();
132141

142+
GUILayout.BeginHorizontal();
143+
GUILayout.Label("Allow disabling files via renaming", EditorStyles.boldLabel);
144+
settings.renameToDisableFilesEnabled =
145+
EditorGUILayout.Toggle(settings.renameToDisableFilesEnabled);
146+
GUILayout.EndHorizontal();
147+
133148
GUILayout.BeginHorizontal();
134149
GUILayout.Label("Verbose logging", EditorStyles.boldLabel);
135150
settings.verboseLoggingEnabled = EditorGUILayout.Toggle(settings.verboseLoggingEnabled);
@@ -140,6 +155,7 @@ public void OnGUI() {
140155
settings.useProjectSettings = EditorGUILayout.Toggle(settings.useProjectSettings);
141156
GUILayout.EndHorizontal();
142157

158+
143159
GUILayout.Space(10);
144160

145161
if (GUILayout.Button("Reset to Defaults")) {

source/VersionHandlerImpl/src/VersionHandlerImpl.cs

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ private static Dictionary<string, BuildTarget>
9999
/// </summary>
100100
public static string ASSET_LABEL = "gvh";
101101

102+
/// <summary>
103+
/// Label which flags whether an asset should be disabled by renaming the file
104+
/// (instead of using the PluginManager).
105+
/// </summary>
106+
public static string ASSET_LABEL_RENAME_TO_DISABLE = "gvh_rename_to_disable";
107+
102108
// Special build target name which enables all platforms / build targets.
103109
public static string BUILD_TARGET_NAME_ANY = "any";
104110

@@ -267,6 +273,11 @@ public static string ScriptingRuntimeDotNetVersion {
267273
/// </summary>
268274
public bool isManifest = false;
269275

276+
/// <summary>
277+
/// Set if this references an asset which is handled by PluginManager.
278+
/// </summary>
279+
public bool isHandledByPluginImporter = false;
280+
270281
/// <summary>
271282
/// List of compatible .NET versions parsed from this asset.
272283
/// </summary>
@@ -306,6 +317,8 @@ public FileMetadata(string filename) {
306317
foreach (string label in AssetDatabase.GetLabels(importer)) {
307318
ParseLabel(label);
308319
}
320+
321+
isHandledByPluginImporter = typeof(PluginImporter).IsInstanceOfType(importer);
309322
}
310323

311324
// On Windows the AssetDatabase converts native path separators
@@ -515,6 +528,10 @@ public void UpdateAssetLabels() {
515528
}
516529
if (targets != null && targets.Length > 0) {
517530
labels.Add(CreateLabel(TOKEN_TARGETS, targets));
531+
532+
if(!isHandledByPluginImporter) {
533+
labels.Add(ASSET_LABEL_RENAME_TO_DISABLE);
534+
}
518535
}
519536
if (dotNetTargets != null && dotNetTargets.Length > 0) {
520537
labels.Add(CreateLabel(TOKEN_DOTNET_TARGETS, dotNetTargets));
@@ -1429,12 +1446,15 @@ private string LibraryPrefix {
14291446
"Google.VersionHandler.RenameToCanonicalFilenames";
14301447
private const string PREFERENCE_VERBOSE_LOGGING_ENABLED =
14311448
"Google.VersionHandler.VerboseLoggingEnabled";
1449+
private const string PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED =
1450+
"Google.VersionHandler.RenameToDisableFilesEnabled";
14321451
// List of preference keys, used to restore default settings.
14331452
private static string[] PREFERENCE_KEYS = new [] {
14341453
PREFERENCE_ENABLED,
14351454
PREFERENCE_CLEANUP_PROMPT_ENABLED,
14361455
PREFERENCE_RENAME_TO_CANONICAL_FILENAMES,
1437-
PREFERENCE_VERBOSE_LOGGING_ENABLED
1456+
PREFERENCE_VERBOSE_LOGGING_ENABLED,
1457+
PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED
14381458
};
14391459

14401460
// Name of this plugin.
@@ -1466,6 +1486,7 @@ static VersionHandlerImpl() {
14661486
static void UpdateVersionedAssetsOnUpdate() {
14671487
UpdateVersionedAssets();
14681488
NotifyWhenCompliationComplete(false);
1489+
UpdateAssetsWithBuildTargets(EditorUserBuildSettings.activeBuildTarget);
14691490
}
14701491

14711492
/// <summary>
@@ -1606,6 +1627,16 @@ public static bool VerboseLoggingEnabled {
16061627
set { settings.SetBool(PREFERENCE_VERBOSE_LOGGING_ENABLED, value); }
16071628
}
16081629

1630+
/// <summary>
1631+
/// Enable / disable verbose logging.
1632+
/// </summary>
1633+
public static bool RenameToDisableFilesEnabled {
1634+
get {
1635+
return settings.GetBool(PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED, defaultValue: true);
1636+
}
1637+
set { settings.SetBool(PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED, value); }
1638+
}
1639+
16091640
/// <summary>
16101641
/// Set the methods to call when VersionHandler completes an update.
16111642
/// Each string in the specified list should have the format
@@ -1747,6 +1778,8 @@ public static void UpdateVersionedAssets(bool forceUpdate = false) {
17471778
// If this module is disabled do nothing.
17481779
if (!forceUpdate && !Enabled) return;
17491780

1781+
UpdateAssetsWithBuildTargets(EditorUserBuildSettings.activeBuildTarget);
1782+
17501783
var metadataSet = FileMetadataSet.ParseFromFilenames(FindAllAssets());
17511784
// Rename linux libraries, if any are being tracked.
17521785
var linuxLibraries = new LinuxLibraryRenamer(metadataSet);
@@ -1825,6 +1858,70 @@ public static void UpdateVersionedAssets(bool forceUpdate = false) {
18251858
}
18261859
}
18271860

1861+
/// <summary>
1862+
/// Go through all files with build targets that need to be enabled/disabled by renaming
1863+
/// (can't be handled through PluginHandler) and enable/disable them based on the whether they
1864+
/// should build on currentBuildTarget.
1865+
/// </summary>
1866+
/// <param name="currentBuildTarget">
1867+
/// The BuildTarget to use to determine which files should be enabled/disabled.
1868+
/// </param>
1869+
public static void UpdateAssetsWithBuildTargets(BuildTarget currentBuildTarget) {
1870+
string[] assets = SearchAssetDatabase(
1871+
assetsFilter: "l:" + FileMetadata.ASSET_LABEL_RENAME_TO_DISABLE);
1872+
1873+
var metadataSet = FileMetadataSet.ParseFromFilenames(assets);
1874+
foreach (var versionPair in metadataSet.Values) {
1875+
// Disable all but the most recent version of the file.
1876+
// Enable / disable most recent version based on whether its targets contain
1877+
// the currently selected build target.
1878+
foreach (var versionData in versionPair.Values) {
1879+
bool enabled = false;
1880+
if (versionData == versionPair.MostRecentVersion) {
1881+
if (versionData.GetBuildTargetsSpecified()) {
1882+
var buildTargets = versionData.GetBuildTargets();
1883+
enabled = buildTargets.Contains(currentBuildTarget);
1884+
} else {
1885+
enabled = true;
1886+
}
1887+
}
1888+
1889+
SetFileEnabledByRename(versionData, enabled);
1890+
}
1891+
}
1892+
1893+
}
1894+
1895+
/// <summary>
1896+
/// Enable or disable a file by renaming it (changing its extension to/from .xxx_DISABLED).
1897+
/// </summary>
1898+
/// <param name="metadata"> The metadata for the file which should be changed. </param>
1899+
/// <param name="enabled"> The new state of the file. </param>
1900+
private static void SetFileEnabledByRename(FileMetadata metadata, bool enabled) {
1901+
if (!RenameToDisableFilesEnabled)
1902+
return;
1903+
1904+
string disableToken = "_DISABLED";
1905+
1906+
var filename = metadata.filename;
1907+
var newFilename = filename;
1908+
if (enabled && filename.EndsWith(disableToken)) {
1909+
int tokenIndex = filename.LastIndexOf(disableToken);
1910+
if (tokenIndex > 0) {
1911+
newFilename = filename.Substring(0, tokenIndex);
1912+
}
1913+
} else if (!enabled && !filename.EndsWith(disableToken)) {
1914+
newFilename = filename + disableToken;
1915+
}
1916+
1917+
if (filename == newFilename || newFilename == "") {
1918+
return;
1919+
}
1920+
1921+
Log("Renaming file " + filename + " to " + newFilename, verbose: true);
1922+
AssetDatabase.MoveAsset(filename, newFilename);
1923+
}
1924+
18281925
// Returns the major/minor version of the unity environment we are running in
18291926
// as a float so it can be compared numerically.
18301927
public static float GetUnityVersionMajorMinor() {
@@ -1878,6 +1975,52 @@ internal static string[] OnWillSaveAssets(string[] paths) {
18781975
return paths;
18791976
}
18801977
}
1978+
1979+
/// <summary>
1980+
/// Update assets when BuildTarget changes.
1981+
/// </summary>
1982+
[InitializeOnLoad]
1983+
internal class BuildTargetChecker {
1984+
private static BuildTarget? lastKnownBuildTarget = null;
1985+
private static int ticksSinceLastCheck = 0;
1986+
private static int ticksBetweenChecks = 60;
1987+
1988+
1989+
static BuildTargetChecker() {
1990+
HandleSettingsChanged();
1991+
}
1992+
1993+
public static void HandleSettingsChanged() {
1994+
RunOnMainThread.OnUpdate -= CheckBuildTarget;
1995+
if (Enabled && RenameToDisableFilesEnabled) {
1996+
RunOnMainThread.OnUpdate += CheckBuildTarget;
1997+
}
1998+
}
1999+
2000+
private static void CheckBuildTarget() {
2001+
ticksSinceLastCheck++;
2002+
if (ticksSinceLastCheck < ticksBetweenChecks) {
2003+
return;
2004+
}
2005+
2006+
if (!Enabled || !RenameToDisableFilesEnabled) {
2007+
RunOnMainThread.OnUpdate -= CheckBuildTarget;
2008+
}
2009+
2010+
var newBuildTarget = EditorUserBuildSettings.activeBuildTarget;
2011+
if (lastKnownBuildTarget == null || newBuildTarget != lastKnownBuildTarget) {
2012+
lastKnownBuildTarget = newBuildTarget;
2013+
HandleBuildTargetChanged(newBuildTarget);
2014+
}
2015+
2016+
ticksSinceLastCheck = 0;
2017+
}
2018+
2019+
private static void HandleBuildTargetChanged(BuildTarget newBuildTarget) {
2020+
UpdateAssetsWithBuildTargets(newBuildTarget);
2021+
}
2022+
2023+
}
18812024
}
18822025

18832026
} // namespace Google

0 commit comments

Comments
 (0)