Example #1
0
        /// <summary>
        /// Save the content update information for a set of AddressableAssetEntry objects.
        /// </summary>
        /// <param name="locations">The ContentCatalogDataEntry locations that were built into the Content Catalog.</param>
        /// <param name="path">File to write content stat info to.  If file already exists, it will be deleted before the new file is created.</param>
        /// <param name="entries">The entries to save.</param>
        /// <param name="dependencyData">The raw dependency information generated from the build.</param>
        /// <param name="playerVersion">The player version to save. This is usually set to AddressableAssetSettings.PlayerBuildVersion.</param>
        /// <param name="remoteCatalogPath">The server path (if any) that contains an updateable content catalog.  If this is empty, updates cannot occur.</param>
        /// <param name="carryOverCacheState">Cached state that needs to carry over from the previous build.  This mainly affects Content Update.</param>
        /// <returns>True if the file is saved, false otherwise.</returns>
        public static bool SaveContentState(List <ContentCatalogDataEntry> locations, string path, List <AddressableAssetEntry> entries, IDependencyData dependencyData, string playerVersion, string remoteCatalogPath, List <CachedAssetState> carryOverCacheState)
        {
            try
            {
                var cachedInfos = GetCachedAssetStates(locations, entries, dependencyData);

                var cachedBundleInfos = new List <CachedBundleState>();
                foreach (ContentCatalogDataEntry ccEntry in locations)
                {
                    if (typeof(IAssetBundleResource).IsAssignableFrom(ccEntry.ResourceType))
                    {
                        cachedBundleInfos.Add(new CachedBundleState()
                        {
                            bundleFileId = ccEntry.InternalId, data = ccEntry.Data
                        });
                    }
                }

                if (carryOverCacheState != null)
                {
                    foreach (var cs in carryOverCacheState)
                    {
                        cachedInfos.Add(cs);
                    }
                }

                var cacheData = new AddressablesContentState
                {
                    cachedInfos           = cachedInfos.ToArray(),
                    playerVersion         = playerVersion,
                    editorVersion         = Application.unityVersion,
                    remoteCatalogLoadPath = remoteCatalogPath,
                    cachedBundles         = cachedBundleInfos.ToArray()
                };
                var formatter = new BinaryFormatter();
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                var dir = Path.GetDirectoryName(path);
                if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                var stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write);
                formatter.Serialize(stream, cacheData);
                stream.Flush();
                stream.Close();
                stream.Dispose();
                return(true);
            }
            catch (UnauthorizedAccessException uae)
            {
                if (!AddressableAssetUtility.IsVCAssetOpenForEdit(path))
                {
                    Debug.LogErrorFormat("Cannot access the file {0}. It may be locked by version control.", path);
                }
                else
                {
                    Debug.LogException(uae);
                }
                return(false);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                return(false);
            }
        }