123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- namespace VEngine
- {
- public static class Versions
- {
- public const string APIVersion = "7.0";
- public static Manifest Manifest;
- private static readonly Dictionary<string, string> BundleWithPathOrUrLs = new Dictionary<string, string>();
- public static bool SimulationMode;
- public static readonly List<string> builtinAssets = new List<string>();
- public static bool OfflineMode { get; set; }
- public static Func<string, Type, Asset> FuncCreateAsset { get; set; }
- public static Func<string, bool, Scene> FuncCreateScene { get; set; }
- public static Func<string, bool, ManifestAsset> FuncCreateManifest { get; set; }
- public static string ManifestsVersion => Manifest == null ? string.Empty : Manifest.version.ToString();
- public static string PlayerDataPath { get; set; }
- public static string DownloadURL { get; set; }
- public static string DownloadDataPath { get; set; }
- internal static string LocalProtocol { get; set; }
- public static string PlatformName { get; set; }
- public static Asset CreateAsset(string path, Type type)
- {
- if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));
- return FuncCreateAsset(path, type);
- }
- public static Scene CreateScene(string path, bool additive)
- {
- if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));
- return FuncCreateScene(path, additive);
- }
- public static ManifestAsset CreateManifest(string name, bool builtin)
- {
- if (string.IsNullOrEmpty(name)) throw new ArgumentException(nameof(name));
- return FuncCreateManifest(name, builtin);
- }
- public static void Override(Manifest value)
- {
- Manifest = value;
- }
- public static string GetDownloadDataPath(string file)
- {
- return $"{DownloadDataPath}/{file}";
- }
- public static string GetPlayerDataURL(string file)
- {
- return $"{LocalProtocol}{PlayerDataPath}/{file}";
- }
- public static string GetPlayerDataPath(string file)
- {
- return $"{PlayerDataPath}/{file}";
- }
- public static string GetDownloadURL(string file)
- {
- return $"{DownloadURL}{PlatformName}/{file}";
- }
- public static string GetTemporaryPath(string file)
- {
- var ret = $"{Application.temporaryCachePath}/{file}";
- var dir = Path.GetDirectoryName(ret);
- if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
- return ret;
- }
- public static ClearHistory ClearAsync()
- {
- var clearAsync = new ClearHistory();
- clearAsync.Start();
- return clearAsync;
- }
- public static void InitializeOnLoad()
- {
- if (FuncCreateAsset == null) FuncCreateAsset = BundledAsset.Create;
- if (FuncCreateScene == null) FuncCreateScene = BundledScene.Create;
- if (FuncCreateManifest == null) FuncCreateManifest = ManifestAsset.Create;
- if (Application.platform != RuntimePlatform.OSXEditor &&
- Application.platform != RuntimePlatform.OSXPlayer &&
- Application.platform != RuntimePlatform.IPhonePlayer)
- {
- if (Application.platform == RuntimePlatform.WindowsEditor ||
- Application.platform == RuntimePlatform.WindowsPlayer)
- LocalProtocol = "file:///";
- else
- LocalProtocol = string.Empty;
- }
- else
- {
- LocalProtocol = "file://";
- }
- if (string.IsNullOrEmpty(PlatformName)) PlatformName = Utility.GetPlatformName();
- if (string.IsNullOrEmpty(PlayerDataPath))
- PlayerDataPath = $"{Application.streamingAssetsPath}/{Utility.buildPath}";
- if (string.IsNullOrEmpty(DownloadDataPath))
- DownloadDataPath = $"{Application.persistentDataPath}/{Utility.buildPath}";
- if (!Directory.Exists(DownloadDataPath)) Directory.CreateDirectory(DownloadDataPath);
- }
- public static InitializeVersions InitializeAsync(string downloadUrl)
- {
- DownloadURL = downloadUrl;
- InitializeOnLoad();
- var operation = new InitializeVersions();
- operation.Start();
- return operation;
- }
- public static UpdateVersions UpdateAsync(string file)
- {
- var operation = new UpdateVersions
- {
- file = file
- };
- operation.Start();
- return operation;
- }
- public static GetDownloadSize GetDownloadSizeAsync()
- {
- var getDownloadSize = new GetDownloadSize();
- getDownloadSize.bundles.AddRange(Manifest.bundles);
- getDownloadSize.Start();
- return getDownloadSize;
- }
- public static GetDownloadSize GetDownloadSizeAsync(UpdateVersions updateVersion)
- {
- var getDownloadSize = new GetDownloadSize();
- if (updateVersion.asset != null) getDownloadSize.bundles.AddRange(updateVersion.asset.asset.bundles);
- getDownloadSize.Start();
- return getDownloadSize;
- }
- public static DownloadVersions DownloadAsync(IEnumerable<DownloadInfo> groups)
- {
- var download = new DownloadVersions();
- download.items.AddRange(groups);
- download.Start();
- return download;
- }
- public static bool IsDownloaded(ManifestBundle bundle)
- {
- if (OfflineMode || builtinAssets.Contains(bundle.nameWithAppendHash)) return true;
- //modified by guodong start
- if(Manifest.Contains(bundle.nameWithAppendHash))
- {
- return true;
- }
- //modified by guodong end
- var path = GetDownloadDataPath(bundle.nameWithAppendHash);
- var file = new FileInfo(path);
- return file.Exists && file.Length == bundle.size && Utility.ComputeCRC32(path) == bundle.crc;
- }
- internal static void SetBundlePathOrURl(string assetBundleName, string url)
- {
- BundleWithPathOrUrLs[assetBundleName] = url;
- }
- internal static string GetBundlePathOrURL(ManifestBundle bundle)
- {
- var assetBundleName = bundle.nameWithAppendHash;
- if (BundleWithPathOrUrLs.TryGetValue(assetBundleName, out var path)) return path;
- if (OfflineMode || builtinAssets.Contains(assetBundleName))
- {
- path = GetPlayerDataPath(assetBundleName);
- BundleWithPathOrUrLs[assetBundleName] = path;
- return path;
- }
- if (IsDownloaded(bundle))
- {
- path = GetDownloadDataPath(assetBundleName);
- BundleWithPathOrUrLs[assetBundleName] = path;
- return path;
- }
- path = GetDownloadURL(assetBundleName);
- BundleWithPathOrUrLs[assetBundleName] = path;
- return path;
- }
- public static bool GetDependencies(string assetPath, out ManifestBundle bundle, out ManifestBundle[] bundles)
- {
- if (Manifest.Contains(assetPath))
- {
- bundle = Manifest.GetBundle(assetPath);
- bundles = Manifest.GetDependencies(bundle);
- return true;
- }
- bundle = null;
- bundles = null;
- return false;
- }
- public static bool Contains(string assetPath)
- {
- return Manifest.Contains(assetPath);
- }
- public static ManifestBundle GetBundle(string bundle)
- {
- return Manifest.GetBundle(bundle);
- }
- }
- }
|