Versions.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace VEngine
  6. {
  7. public static class Versions
  8. {
  9. public const string APIVersion = "7.0";
  10. public static Manifest Manifest;
  11. private static readonly Dictionary<string, string> BundleWithPathOrUrLs = new Dictionary<string, string>();
  12. public static bool SimulationMode;
  13. public static readonly List<string> builtinAssets = new List<string>();
  14. public static bool OfflineMode { get; set; }
  15. public static Func<string, Type, Asset> FuncCreateAsset { get; set; }
  16. public static Func<string, bool, Scene> FuncCreateScene { get; set; }
  17. public static Func<string, bool, ManifestAsset> FuncCreateManifest { get; set; }
  18. public static string ManifestsVersion => Manifest == null ? string.Empty : Manifest.version.ToString();
  19. public static string PlayerDataPath { get; set; }
  20. public static string DownloadURL { get; set; }
  21. public static string DownloadDataPath { get; set; }
  22. internal static string LocalProtocol { get; set; }
  23. public static string PlatformName { get; set; }
  24. public static Asset CreateAsset(string path, Type type)
  25. {
  26. if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));
  27. return FuncCreateAsset(path, type);
  28. }
  29. public static Scene CreateScene(string path, bool additive)
  30. {
  31. if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));
  32. return FuncCreateScene(path, additive);
  33. }
  34. public static ManifestAsset CreateManifest(string name, bool builtin)
  35. {
  36. if (string.IsNullOrEmpty(name)) throw new ArgumentException(nameof(name));
  37. return FuncCreateManifest(name, builtin);
  38. }
  39. public static void Override(Manifest value)
  40. {
  41. Manifest = value;
  42. }
  43. public static string GetDownloadDataPath(string file)
  44. {
  45. return $"{DownloadDataPath}/{file}";
  46. }
  47. public static string GetPlayerDataURL(string file)
  48. {
  49. return $"{LocalProtocol}{PlayerDataPath}/{file}";
  50. }
  51. public static string GetPlayerDataPath(string file)
  52. {
  53. return $"{PlayerDataPath}/{file}";
  54. }
  55. public static string GetDownloadURL(string file)
  56. {
  57. return $"{DownloadURL}{PlatformName}/{file}";
  58. }
  59. public static string GetTemporaryPath(string file)
  60. {
  61. var ret = $"{Application.temporaryCachePath}/{file}";
  62. var dir = Path.GetDirectoryName(ret);
  63. if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
  64. return ret;
  65. }
  66. public static ClearHistory ClearAsync()
  67. {
  68. var clearAsync = new ClearHistory();
  69. clearAsync.Start();
  70. return clearAsync;
  71. }
  72. public static void InitializeOnLoad()
  73. {
  74. if (FuncCreateAsset == null) FuncCreateAsset = BundledAsset.Create;
  75. if (FuncCreateScene == null) FuncCreateScene = BundledScene.Create;
  76. if (FuncCreateManifest == null) FuncCreateManifest = ManifestAsset.Create;
  77. if (Application.platform != RuntimePlatform.OSXEditor &&
  78. Application.platform != RuntimePlatform.OSXPlayer &&
  79. Application.platform != RuntimePlatform.IPhonePlayer)
  80. {
  81. if (Application.platform == RuntimePlatform.WindowsEditor ||
  82. Application.platform == RuntimePlatform.WindowsPlayer)
  83. LocalProtocol = "file:///";
  84. else
  85. LocalProtocol = string.Empty;
  86. }
  87. else
  88. {
  89. LocalProtocol = "file://";
  90. }
  91. if (string.IsNullOrEmpty(PlatformName)) PlatformName = Utility.GetPlatformName();
  92. if (string.IsNullOrEmpty(PlayerDataPath))
  93. PlayerDataPath = $"{Application.streamingAssetsPath}/{Utility.buildPath}";
  94. if (string.IsNullOrEmpty(DownloadDataPath))
  95. DownloadDataPath = $"{Application.persistentDataPath}/{Utility.buildPath}";
  96. if (!Directory.Exists(DownloadDataPath)) Directory.CreateDirectory(DownloadDataPath);
  97. }
  98. public static InitializeVersions InitializeAsync(string downloadUrl)
  99. {
  100. DownloadURL = downloadUrl;
  101. InitializeOnLoad();
  102. var operation = new InitializeVersions();
  103. operation.Start();
  104. return operation;
  105. }
  106. public static UpdateVersions UpdateAsync(string file)
  107. {
  108. var operation = new UpdateVersions
  109. {
  110. file = file
  111. };
  112. operation.Start();
  113. return operation;
  114. }
  115. public static GetDownloadSize GetDownloadSizeAsync()
  116. {
  117. var getDownloadSize = new GetDownloadSize();
  118. getDownloadSize.bundles.AddRange(Manifest.bundles);
  119. getDownloadSize.Start();
  120. return getDownloadSize;
  121. }
  122. public static GetDownloadSize GetDownloadSizeAsync(UpdateVersions updateVersion)
  123. {
  124. var getDownloadSize = new GetDownloadSize();
  125. if (updateVersion.asset != null) getDownloadSize.bundles.AddRange(updateVersion.asset.asset.bundles);
  126. getDownloadSize.Start();
  127. return getDownloadSize;
  128. }
  129. public static DownloadVersions DownloadAsync(IEnumerable<DownloadInfo> groups)
  130. {
  131. var download = new DownloadVersions();
  132. download.items.AddRange(groups);
  133. download.Start();
  134. return download;
  135. }
  136. public static bool IsDownloaded(ManifestBundle bundle)
  137. {
  138. if (OfflineMode || builtinAssets.Contains(bundle.nameWithAppendHash)) return true;
  139. //modified by guodong start
  140. if(Manifest.Contains(bundle.nameWithAppendHash))
  141. {
  142. return true;
  143. }
  144. //modified by guodong end
  145. var path = GetDownloadDataPath(bundle.nameWithAppendHash);
  146. var file = new FileInfo(path);
  147. return file.Exists && file.Length == bundle.size && Utility.ComputeCRC32(path) == bundle.crc;
  148. }
  149. internal static void SetBundlePathOrURl(string assetBundleName, string url)
  150. {
  151. BundleWithPathOrUrLs[assetBundleName] = url;
  152. }
  153. internal static string GetBundlePathOrURL(ManifestBundle bundle)
  154. {
  155. var assetBundleName = bundle.nameWithAppendHash;
  156. if (BundleWithPathOrUrLs.TryGetValue(assetBundleName, out var path)) return path;
  157. if (OfflineMode || builtinAssets.Contains(assetBundleName))
  158. {
  159. path = GetPlayerDataPath(assetBundleName);
  160. BundleWithPathOrUrLs[assetBundleName] = path;
  161. return path;
  162. }
  163. if (IsDownloaded(bundle))
  164. {
  165. path = GetDownloadDataPath(assetBundleName);
  166. BundleWithPathOrUrLs[assetBundleName] = path;
  167. return path;
  168. }
  169. path = GetDownloadURL(assetBundleName);
  170. BundleWithPathOrUrLs[assetBundleName] = path;
  171. return path;
  172. }
  173. public static bool GetDependencies(string assetPath, out ManifestBundle bundle, out ManifestBundle[] bundles)
  174. {
  175. if (Manifest.Contains(assetPath))
  176. {
  177. bundle = Manifest.GetBundle(assetPath);
  178. bundles = Manifest.GetDependencies(bundle);
  179. return true;
  180. }
  181. bundle = null;
  182. bundles = null;
  183. return false;
  184. }
  185. public static bool Contains(string assetPath)
  186. {
  187. return Manifest.Contains(assetPath);
  188. }
  189. public static ManifestBundle GetBundle(string bundle)
  190. {
  191. return Manifest.GetBundle(bundle);
  192. }
  193. }
  194. }