using System; using System.Collections; using System.IO; using UnityEngine.ResourceManagement; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.ResourceProviders; using UnityEngine.ResourceManagement.Util; using UnityEngine.Serialization; namespace UnityEngine.AddressableAssets.Initialization { /// /// IInitializableObject that sets up the Caching system. /// [Serializable] public class CacheInitialization : IInitializableObject { /// /// Sets properties of the Caching system. /// /// The id of thei object. /// The JSON serialized CacheInitializationData object. /// True if the initialization succeeded. public bool Initialize(string id, string dataStr) { #if ENABLE_CACHING var data = JsonUtility.FromJson(dataStr); if (data != null) { Caching.compressionEnabled = data.CompressionEnabled; var activeCache = Caching.currentCacheForWriting; if (!string.IsNullOrEmpty(data.CacheDirectoryOverride)) { var dir = Addressables.ResolveInternalId(data.CacheDirectoryOverride); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); activeCache = Caching.GetCacheByPath(dir); if (!activeCache.valid) activeCache = Caching.AddCache(dir); Caching.currentCacheForWriting = activeCache; } if (data.LimitCacheSize) activeCache.maximumAvailableStorageSpace = data.MaximumCacheSize; else activeCache.maximumAvailableStorageSpace = long.MaxValue; #pragma warning disable 618 activeCache.expirationDelay = data.ExpirationDelay; #pragma warning restore 618 } #endif //ENABLE_CACHING return true; } /// public virtual AsyncOperationHandle InitializeAsync(ResourceManager rm, string id, string data) { CacheInitOp op = new CacheInitOp(); op.Init(() => { return Initialize(id, data); }); return rm.StartOperation(op, default); } #if ENABLE_CACHING /// /// The root path of the cache. /// public static string RootPath { get { return Path.GetDirectoryName(Caching.defaultCache.path); } } #endif //ENABLE_CACHING class CacheInitOp : AsyncOperationBase, IUpdateReceiver { private Func m_Callback; #if ENABLE_CACHING private bool m_UpdateRequired = true; #endif //ENABLE_CACHING public void Init(Func callback) { m_Callback = callback; } /// protected override bool InvokeWaitForCompletion() { #if ENABLE_CACHING m_RM?.Update(Time.unscaledDeltaTime); if (!IsDone) InvokeExecute(); return IsDone; #else return true; #endif } public void Update(float unscaledDeltaTime) { #if ENABLE_CACHING if (Caching.ready && m_UpdateRequired) { m_UpdateRequired = false; if (m_Callback != null) Complete(m_Callback(), true, ""); else Complete(true, true, ""); Addressables.Log("CacheInitialization Complete"); } #else Complete(true, true, ""); Addressables.Log("UnityEngine.Caching not supported on this platform but a CacheInitialization object has been found in AddressableAssetSettings. No action has been taken."); #endif } protected override void Execute() { ((IUpdateReceiver)this).Update(0.0f); } } } /// /// Contains settings for the Caching system. /// [Serializable] public class CacheInitializationData { [FormerlySerializedAs("m_compressionEnabled")] [SerializeField] bool m_CompressionEnabled = true; /// /// Enable recompression of asset bundles into LZ4 format as they are saved to the cache. This sets the Caching.compressionEnabled value. /// public bool CompressionEnabled { get { return m_CompressionEnabled; } set { m_CompressionEnabled = value; } } [FormerlySerializedAs("m_cacheDirectoryOverride")] [SerializeField] string m_CacheDirectoryOverride = ""; /// /// If not null or empty a new cache is created using Caching.AddCache and it is set active by assigning it to Caching.currentCacheForWriting. /// public string CacheDirectoryOverride { get { return m_CacheDirectoryOverride; } set { m_CacheDirectoryOverride = value; } } [FormerlySerializedAs("m_expirationDelay")] [SerializeField] int m_ExpirationDelay = 12960000; //this value taken from the docs and is 150 days /// /// Controls how long bundles are kept in the cache. This value is applied to Caching.currentCacheForWriting.expirationDelay. The value is in seconds and has a limit of 12960000 (150 days). /// [Obsolete("Functionality remains unchanged. However, due to issues with Caching this property is being marked obsolete. See Caching API documentation for more details.")] public int ExpirationDelay { get { return m_ExpirationDelay; } set { m_ExpirationDelay = value; } } [FormerlySerializedAs("m_limitCacheSize")] [SerializeField] bool m_LimitCacheSize; /// /// If true, the maximum cache size will be set to MaximumCacheSize. /// public bool LimitCacheSize { get { return m_LimitCacheSize; } set { m_LimitCacheSize = value; } } [FormerlySerializedAs("m_maximumCacheSize")] [SerializeField] long m_MaximumCacheSize = long.MaxValue; /// /// The maximum size of the cache in bytes. This value is applied to Caching.currentCacheForWriting.maximumAvailableStorageSpace. This will only be set if LimitCacheSize is true. /// public long MaximumCacheSize { get { return m_MaximumCacheSize; } set { m_MaximumCacheSize = value; } } } }