using System; using System.Globalization; using System.IO; using System.Text; using SUISS.Core.Crypto; using UnityEngine; public class CachedWWW : CustomYieldInstruction { public CachedWWW(string url, int expiresInDays = 1) { this.Url = url; this._expiresInDays = expiresInDays; if (this.IsCached(url) && !this.IsExpired(url)) { string str = "file://"; url = str + this.ConvertToFilePath(url); this._loadFromCache = true; } this._download = new WWW(url); } public override bool keepWaiting { get { if (this._download.isDone) { if (!this._loadFromCache && this._download.error == null) { this.Save(this._download.url, this._download.bytes, this._expiresInDays); } return false; } return true; } } public string Url { get; private set; } public string Error { get { return this._download.error; } } public bool IsDone { get { return this._download.isDone; } } public Texture2D Texture { get { return this._download.texture; } } public Texture2D TextureNonReadable { get { return this._download.textureNonReadable; } } public string Text { get { return this._download.text; } } public AudioClip AudioClip { get { return this._download.GetAudioClip(false); } } private bool Save(string url, byte[] bytes, int expireInDays) { string text = this.ConvertToFilePath(url); try { Directory.CreateDirectory(Path.GetDirectoryName(text)); File.WriteAllBytes(text, bytes); } catch (Exception ex) { UnityEngine.Debug.LogErrorFormat("Cannot save {0} to: {1}{2}{3}", new object[] { url, text, Environment.NewLine, ex.ToString() }); return false; } this.SaveExpirationDate(this.GetExpirationKey(url), expireInDays); return true; } private bool IsCached(string url) { try { if (File.Exists(this.ConvertToFilePath(url))) { return true; } } catch (Exception ex) { UnityEngine.Debug.LogErrorFormat("Error checking download exists in cache: {1}{2}{3}", new object[] { this.ConvertToFilePath(url), Environment.NewLine, ex.ToString() }); return false; } return false; } private string GetExpirationKey(string url) { return string.Format("EXPIRATION_DATE_KEY_{0}", this.ConvertToFileName(url)); } private bool IsExpired(string url) { string expirationKey = this.GetExpirationKey(url); if (PlayerPrefs.HasKey(expirationKey)) { bool result = true; string @string = PlayerPrefs.GetString(expirationKey); DateTime dateTime; if (DateTime.TryParse(@string, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) { result = (dateTime.Subtract(DateTime.UtcNow).Days < 0); } return result; } return true; } private void SaveExpirationDate(string expirationKey, int expireInDays) { PlayerPrefs.SetString(expirationKey, DateTime.UtcNow.AddDays((double)expireInDays).ToString(CultureInfo.InvariantCulture)); } private string ConvertToFilePath(string url) { string fileName = this.ConvertToFileName(url); return this.GetFilePath(fileName); } private string GetFilePath(string fileName) { return string.Format("{0}/{1}/{2}", Application.temporaryCachePath, "Cache", fileName); } private string ConvertToFileName(string url) { StringBuilder stringBuilder = new StringBuilder(); byte[] array = Sha256.HashToBytes(new object[] { Encoding.ASCII.GetBytes(url) }); int num = array.Length; for (int i = 0; i < num; i++) { stringBuilder.Append(array[i].ToString("x2")); } if (stringBuilder.Length >= 6) { int length = Mathf.Max(stringBuilder.Length - 9, 0); stringBuilder.Remove(6, length); } return stringBuilder.ToString(); } private const string CacheFolderName = "Cache"; private const string ExpirationDateKey = "EXPIRATION_DATE_KEY_{0}"; private const int URL_KEY_ID_SIZE_A = 6; private const int URL_KEY_ID_SIZE_B = 3; private WWW _download; private bool _loadFromCache; private int _expiresInDays = -1; }