Google.VersionHandler.FileMetadataByVersion.EnableMostRecentPlugins C# (CSharp) Method

EnableMostRecentPlugins() public method

If this instance references a set of plugins, enable the most recent versions.
public EnableMostRecentPlugins ( ) : bool
return bool
        public bool EnableMostRecentPlugins() {
            bool modified = false;
            int versionIndex = 0;
            int numberOfVersions = metadataByVersion.Count;
            var disabledVersions = new List<string>();
            string enabledVersion = null;

            // If the canonical file is out of date, update it.
            if (numberOfVersions > 0) {
                FileMetadata mostRecentVersion =
                    (FileMetadata)((new ArrayList(metadataByVersion.Values))
                                   [numberOfVersions - 1]);
                if (mostRecentVersion.filename != filenameCanonical) {
                    FileMetadata canonicalMetadata = null;
                    foreach (var metadata in metadataByVersion.Values) {
                        if (metadata.filename == filenameCanonical) {
                            canonicalMetadata = metadata;
                            break;
                        }
                    }
                    if (!mostRecentVersion.RenameAsset(filenameCanonical)) {
                        return false;
                    }
                    if (canonicalMetadata != null) {
                        // Overwrote the current version with the rename
                        // operation.
                        metadataByVersion.Remove(
                            canonicalMetadata.CalculateVersion());
                        numberOfVersions = metadataByVersion.Count;
                    }
                    modified = true;
                }
            }

            // Configure targeting for each revision of the plugin.
            foreach (FileMetadata metadata in metadataByVersion.Values) {
                versionIndex++;
                PluginImporter pluginImporter = null;
                try {
                    pluginImporter =
                        (PluginImporter)metadata.GetAssetImporter();
                } catch (InvalidCastException) {
                    continue;
                }
                bool editorEnabled = metadata.GetEditorEnabled();
                var selectedTargets = metadata.GetBuildTargets();
                bool modifiedThisVersion = false;
                // Only enable the most recent plugin - SortedDictionary
                // orders keys in ascending order.
                bool obsoleteVersion = (numberOfVersions > 1 &&
                                        versionIndex < numberOfVersions);
                // If this is an obsolete version.
                if (obsoleteVersion) {
                    // Disable for all platforms and the editor.
                    editorEnabled = false;
                    selectedTargets = new HashSet<BuildTarget>();
                } else {
                    // Track the current version.
                    enabledVersion = metadata.versionString;
                }
                // Enable / disable editor and platform settings.
                if (pluginImporter.GetCompatibleWithEditor() !=
                    editorEnabled) {
                    pluginImporter.SetCompatibleWithEditor(editorEnabled);
                    modifiedThisVersion = true;
                }
                foreach (BuildTarget target in
                         FileMetadata.BUILD_TARGET_NAME_TO_ENUM.Values) {
                    if (FileMetadata.IsTargetSupportedByUnity(target)) {
                        bool enabled = selectedTargets != null &&
                            selectedTargets.Contains(target);
                        try {
                            if (pluginImporter.GetCompatibleWithPlatform(target) !=
                                enabled) {
                                pluginImporter.SetCompatibleWithPlatform(
                                    target, enabled);
                                modifiedThisVersion = true;
                            }
                        }
                        catch(Exception e) {
                          UnityEngine.Debug.LogWarning(
                            "Unexpected error enumerating targets: " + e.Message);
                        }
                    }
                }
                if (modifiedThisVersion) {
                    pluginImporter.SaveAndReimport();
                }
                // If the version was modified and it's obsolete keep track of
                // it to log it later.
                if (obsoleteVersion && modifiedThisVersion) {
                    disabledVersions.Add(metadata.versionString);
                }
                modified |= modifiedThisVersion;
            }
            // Log the versions that have been disabled and the version that
            // has been enabled.
            if (modified && enabledVersion != null &&
                VersionHandler.VerboseLoggingEnabled) {
                string message = (filenameCanonical + ": enabled version " +
                                  enabledVersion);
                if (disabledVersions.Count > 0) {
                    message += ("  obsolete versions disabled (" +
                                String.Join(", ", disabledVersions.ToArray()) +
                                ")");
                }
                UnityEngine.Debug.Log(message);
            }
            return modified;
        }