using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace UnityEngine.Build.Pipeline { /// /// Accesses information about all the asset bundles stored in a manifest file. /// [Serializable] public class CompatibilityAssetBundleManifest : ScriptableObject, ISerializationCallbackReceiver { Dictionary m_Details; [SerializeField] List m_Keys; [SerializeField] List m_Values; /// /// Stores the bundle information. /// /// The bundle information. public void SetResults(Dictionary results) { m_Details = new Dictionary(results); } /// /// Retrieves the names of all the asset bundles. /// /// Returns the names of all the asset bundles. public string[] GetAllAssetBundles() { string[] bundles = m_Details.Keys.ToArray(); Array.Sort(bundles); return bundles; } /// /// Oboslete method. /// /// Returns an empty array. public string[] GetAllAssetBundlesWithVariant() { return new string[0]; } /// /// Retrieves the hash of the asset bundle. /// /// The name of the bundle. /// Returns the hash. public Hash128 GetAssetBundleHash(string assetBundleName) { BundleDetails details; if (m_Details.TryGetValue(assetBundleName, out details)) return details.Hash; return new Hash128(); } /// /// Retrieves the cyclic redundancy check information for specified asset bundle. /// /// The bundle name. /// Returns the cyclic redundancy check information for specified asset bundle. public uint GetAssetBundleCrc(string assetBundleName) { BundleDetails details; if (m_Details.TryGetValue(assetBundleName, out details)) return details.Crc; return 0; } /// /// Retrieves all bundle dependencies based on the specified bundle name. /// /// The bundle name to lookup. /// Returns all the dependencies of the bundle. public string[] GetDirectDependencies(string assetBundleName) { return GetAllDependencies(assetBundleName); } /// /// Retrieves all bundle dependencies based on the specified bundle name. /// /// The bundle name to lookup. /// Returns all the dependencies of the bundle. public string[] GetAllDependencies(string assetBundleName) { BundleDetails details; if (m_Details.TryGetValue(assetBundleName, out details)) return details.Dependencies.ToArray(); return new string[0]; } /// /// Returns a formatted string with the contents of the manifest file. /// /// Returns a string suitable for saving into a manifest file. public override string ToString() { StringBuilder builder = new StringBuilder(); builder.Append("ManifestFileVersion: 1\n"); builder.Append("CompatibilityAssetBundleManifest:\n"); if (m_Details != null && m_Details.Count > 0) { builder.Append(" AssetBundleInfos:\n"); int infoCount = 0; foreach (var details in m_Details) { builder.AppendFormat(" Info_{0}:\n", infoCount++); builder.AppendFormat(" Name: {0}\n", details.Key); builder.AppendFormat(" Hash: {0}\n", details.Value.Hash); builder.AppendFormat(" CRC: {0}\n", details.Value.Crc); int dependencyCount = 0; if (details.Value.Dependencies != null && details.Value.Dependencies.Length > 0) { builder.Append(" Dependencies: {}\n"); foreach (var dependency in details.Value.Dependencies) builder.AppendFormat(" Dependency_{0}: {1}\n", dependencyCount++, dependency); } else builder.Append(" Dependencies: {}\n"); } } else builder.Append(" AssetBundleInfos: {}\n"); return builder.ToString(); } /// /// Converts our data to a serialized structure before a domain reload. /// public void OnBeforeSerialize() { m_Keys = new List(); m_Values = new List(); foreach (var pair in m_Details) { m_Keys.Add(pair.Key); m_Values.Add(pair.Value); } } /// /// Puts back the converted data into its original data structure after a domain reload. /// public void OnAfterDeserialize() { m_Details = new Dictionary(); for (int i = 0; i != Math.Min(m_Keys.Count, m_Values.Count); i++) m_Details.Add(m_Keys[i], m_Values[i]); } } }