ManifestAsset.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. namespace VEngine
  7. {
  8. public class ManifestAsset : Loadable
  9. {
  10. private static readonly List<ManifestAsset> Unused = new List<ManifestAsset>();
  11. private bool builtin;
  12. private UnityWebRequest request;
  13. public Manifest asset { get; protected set; }
  14. public ManifestVersion assetVersion { get; protected set; }
  15. protected string name { get; set; }
  16. public bool changed
  17. {
  18. get
  19. {
  20. if (assetVersion == null) return false;
  21. var find = Versions.Manifest;
  22. if (find != null) return find.version < assetVersion.version;
  23. return true;
  24. }
  25. }
  26. protected override void OnLoad()
  27. {
  28. asset = ScriptableObject.CreateInstance<Manifest>();
  29. asset.name = name;
  30. pathOrURL = builtin ? Versions.GetPlayerDataURL(name) : Versions.GetDownloadURL(name);
  31. var file = Manifest.GetVersionFile(name);
  32. var url = builtin ? Versions.GetPlayerDataURL(file) : Versions.GetDownloadURL(file);
  33. DownloadAsync(url, GetTemporaryPath(file));
  34. status = LoadableStatus.CheckVersion;
  35. }
  36. protected override void OnUnused()
  37. {
  38. Unused.Add(this);
  39. }
  40. public virtual void Override()
  41. {
  42. if (assetVersion == null) return;
  43. if (builtin)
  44. {
  45. var path = Versions.GetDownloadDataPath(Manifest.GetVersionFile(asset.name));
  46. var file = ManifestVersion.Load(path);
  47. if (file.version > assetVersion.version)
  48. {
  49. path = Versions.GetDownloadDataPath(asset.name);
  50. if (File.Exists(path))
  51. using (var stream = File.OpenRead(path))
  52. {
  53. if (Utility.ComputeCRC32(stream) == file.crc)
  54. {
  55. asset.Load(path);
  56. Versions.Override(asset);
  57. return;
  58. }
  59. }
  60. }
  61. asset.Load(GetTemporaryPath(name));
  62. Versions.Override(asset);
  63. }
  64. else
  65. {
  66. if (!changed) return;
  67. var split = name.Split(new[]
  68. {
  69. '_'
  70. }, StringSplitOptions.RemoveEmptyEntries);
  71. if (split.Length > 1)
  72. {
  73. var newName = split[0];
  74. asset.name = newName;
  75. }
  76. var from = GetTemporaryPath(name);
  77. var dest = Versions.GetDownloadDataPath(name).Replace(name, asset.name);
  78. if (File.Exists(from))
  79. {
  80. Logger.I("Copy {0} to {1}.", from, dest);
  81. File.Copy(from, dest, true);
  82. }
  83. var versionName = Manifest.GetVersionFile(name);
  84. from = GetTemporaryPath(versionName);
  85. if (File.Exists(from))
  86. {
  87. var path = Versions.GetDownloadDataPath(versionName).Replace(name, asset.name);
  88. Logger.I("Copy {0} to {1}.", from, path);
  89. File.Copy(from, path, true);
  90. }
  91. }
  92. Versions.Override(asset);
  93. }
  94. public static ManifestAsset LoadAsync(string name, bool builtin = false)
  95. {
  96. var manifestAsset = Versions.CreateManifest(name, builtin);
  97. manifestAsset.Load();
  98. return manifestAsset;
  99. }
  100. internal static ManifestAsset Create(string name, bool builtin)
  101. {
  102. return new ManifestAsset
  103. {
  104. name = name,
  105. builtin = builtin
  106. };
  107. }
  108. public static void UpdateManifestAssets()
  109. {
  110. for (var index = 0; index < Unused.Count; index++)
  111. {
  112. var item = Unused[index];
  113. if (Updater.Instance.busy) break;
  114. if (!item.isDone) continue;
  115. Unused.RemoveAt(index);
  116. index--;
  117. item.Unload();
  118. }
  119. }
  120. private void DownloadAsync(string url, string savePath)
  121. {
  122. if (File.Exists(savePath)) File.Delete(savePath);
  123. Logger.I("Load {0}", url);
  124. request = UnityWebRequest.Get(url);
  125. request.downloadHandler = new DownloadHandlerFile(savePath);
  126. request.SendWebRequest();
  127. }
  128. private string GetTemporaryPath(string filename)
  129. {
  130. return Versions.GetTemporaryPath(string.Format(builtin ? "Builtin/{0}" : "{0}", filename));
  131. }
  132. protected override void OnUpdate()
  133. {
  134. if (status == LoadableStatus.CheckVersion)
  135. {
  136. UpdateVersion();
  137. }
  138. else if (status == LoadableStatus.Downloading)
  139. {
  140. UpdateDownloading();
  141. }
  142. else if (status == LoadableStatus.Loading)
  143. {
  144. if (changed && !builtin)
  145. {
  146. var assetPath = GetTemporaryPath(name);
  147. asset.Load(assetPath);
  148. }
  149. Finish();
  150. }
  151. }
  152. private void UpdateDownloading()
  153. {
  154. if (request == null)
  155. {
  156. Finish("request == nul with " + status);
  157. return;
  158. }
  159. progress = 0.2f + request.downloadProgress;
  160. if (!request.isDone) return;
  161. if (!string.IsNullOrEmpty(request.error))
  162. {
  163. Finish(request.error);
  164. return;
  165. }
  166. request.Dispose();
  167. request = null;
  168. status = LoadableStatus.Loading;
  169. }
  170. private void UpdateVersion()
  171. {
  172. if (request == null)
  173. {
  174. Finish("request == null with " + status);
  175. return;
  176. }
  177. progress = 0.2f * request.downloadProgress;
  178. if (!request.isDone) return;
  179. if (!string.IsNullOrEmpty(request.error))
  180. {
  181. Finish(request.error);
  182. return;
  183. }
  184. request.Dispose();
  185. request = null;
  186. var savePath = GetTemporaryPath(Manifest.GetVersionFile(name));
  187. if (!File.Exists(savePath))
  188. {
  189. Finish("version not exist.");
  190. return;
  191. }
  192. assetVersion = ManifestVersion.Load(savePath);
  193. Logger.I("Read {0} with version {1} crc {2}", name, assetVersion.version, assetVersion.crc);
  194. var path = GetTemporaryPath(name);
  195. if (File.Exists(path))
  196. using (var stream = File.OpenRead(path))
  197. {
  198. if (Utility.ComputeCRC32(stream) == assetVersion.crc)
  199. {
  200. Logger.I("Skip to download {0}, because nothing to update.", name);
  201. status = LoadableStatus.Loading;
  202. return;
  203. }
  204. }
  205. if (File.Exists(path)) File.Delete(path);
  206. DownloadAsync(pathOrURL, path);
  207. status = LoadableStatus.Downloading;
  208. }
  209. }
  210. }