25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

206 lines
4.2 KiB

  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text;
  5. using SUISS.Core.Crypto;
  6. using UnityEngine;
  7. public class CachedWWW : CustomYieldInstruction
  8. {
  9. public CachedWWW(string url, int expiresInDays = 1)
  10. {
  11. this.Url = url;
  12. this._expiresInDays = expiresInDays;
  13. if (this.IsCached(url) && !this.IsExpired(url))
  14. {
  15. string str = "file://";
  16. url = str + this.ConvertToFilePath(url);
  17. this._loadFromCache = true;
  18. }
  19. this._download = new WWW(url);
  20. }
  21. public override bool keepWaiting
  22. {
  23. get
  24. {
  25. if (this._download.isDone)
  26. {
  27. if (!this._loadFromCache && this._download.error == null)
  28. {
  29. this.Save(this._download.url, this._download.bytes, this._expiresInDays);
  30. }
  31. return false;
  32. }
  33. return true;
  34. }
  35. }
  36. public string Url { get; private set; }
  37. public string Error
  38. {
  39. get
  40. {
  41. return this._download.error;
  42. }
  43. }
  44. public bool IsDone
  45. {
  46. get
  47. {
  48. return this._download.isDone;
  49. }
  50. }
  51. public Texture2D Texture
  52. {
  53. get
  54. {
  55. return this._download.texture;
  56. }
  57. }
  58. public Texture2D TextureNonReadable
  59. {
  60. get
  61. {
  62. return this._download.textureNonReadable;
  63. }
  64. }
  65. public string Text
  66. {
  67. get
  68. {
  69. return this._download.text;
  70. }
  71. }
  72. public AudioClip AudioClip
  73. {
  74. get
  75. {
  76. return this._download.GetAudioClip(false);
  77. }
  78. }
  79. private bool Save(string url, byte[] bytes, int expireInDays)
  80. {
  81. string text = this.ConvertToFilePath(url);
  82. try
  83. {
  84. Directory.CreateDirectory(Path.GetDirectoryName(text));
  85. File.WriteAllBytes(text, bytes);
  86. }
  87. catch (Exception ex)
  88. {
  89. UnityEngine.Debug.LogErrorFormat("Cannot save {0} to: {1}{2}{3}", new object[]
  90. {
  91. url,
  92. text,
  93. Environment.NewLine,
  94. ex.ToString()
  95. });
  96. return false;
  97. }
  98. this.SaveExpirationDate(this.GetExpirationKey(url), expireInDays);
  99. return true;
  100. }
  101. private bool IsCached(string url)
  102. {
  103. try
  104. {
  105. if (File.Exists(this.ConvertToFilePath(url)))
  106. {
  107. return true;
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. UnityEngine.Debug.LogErrorFormat("Error checking download exists in cache: {1}{2}{3}", new object[]
  113. {
  114. this.ConvertToFilePath(url),
  115. Environment.NewLine,
  116. ex.ToString()
  117. });
  118. return false;
  119. }
  120. return false;
  121. }
  122. private string GetExpirationKey(string url)
  123. {
  124. return string.Format("EXPIRATION_DATE_KEY_{0}", this.ConvertToFileName(url));
  125. }
  126. private bool IsExpired(string url)
  127. {
  128. string expirationKey = this.GetExpirationKey(url);
  129. if (PlayerPrefs.HasKey(expirationKey))
  130. {
  131. bool result = true;
  132. string @string = PlayerPrefs.GetString(expirationKey);
  133. DateTime dateTime;
  134. if (DateTime.TryParse(@string, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
  135. {
  136. result = (dateTime.Subtract(DateTime.UtcNow).Days < 0);
  137. }
  138. return result;
  139. }
  140. return true;
  141. }
  142. private void SaveExpirationDate(string expirationKey, int expireInDays)
  143. {
  144. PlayerPrefs.SetString(expirationKey, DateTime.UtcNow.AddDays((double)expireInDays).ToString(CultureInfo.InvariantCulture));
  145. }
  146. private string ConvertToFilePath(string url)
  147. {
  148. string fileName = this.ConvertToFileName(url);
  149. return this.GetFilePath(fileName);
  150. }
  151. private string GetFilePath(string fileName)
  152. {
  153. return string.Format("{0}/{1}/{2}", Application.temporaryCachePath, "Cache", fileName);
  154. }
  155. private string ConvertToFileName(string url)
  156. {
  157. StringBuilder stringBuilder = new StringBuilder();
  158. byte[] array = Sha256.HashToBytes(new object[]
  159. {
  160. Encoding.ASCII.GetBytes(url)
  161. });
  162. int num = array.Length;
  163. for (int i = 0; i < num; i++)
  164. {
  165. stringBuilder.Append(array[i].ToString("x2"));
  166. }
  167. if (stringBuilder.Length >= 6)
  168. {
  169. int length = Mathf.Max(stringBuilder.Length - 9, 0);
  170. stringBuilder.Remove(6, length);
  171. }
  172. return stringBuilder.ToString();
  173. }
  174. private const string CacheFolderName = "Cache";
  175. private const string ExpirationDateKey = "EXPIRATION_DATE_KEY_{0}";
  176. private const int URL_KEY_ID_SIZE_A = 6;
  177. private const int URL_KEY_ID_SIZE_B = 3;
  178. private WWW _download;
  179. private bool _loadFromCache;
  180. private int _expiresInDays = -1;
  181. }