123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace YooAsset
- {
-
-
-
- [Serializable]
- internal class PatchManifest
- {
-
-
-
- public string FileVersion;
-
-
-
- public int ResourceVersion;
-
-
-
- public bool EnableAddressable;
-
-
-
- public int OutputNameStyle;
-
-
-
- public string BuildinTags;
-
-
-
- public List<PatchAsset> AssetList = new List<PatchAsset>();
-
-
-
- public List<PatchBundle> BundleList = new List<PatchBundle>();
-
-
-
- [NonSerialized]
- public readonly Dictionary<string, PatchBundle> BundleDic = new Dictionary<string, PatchBundle>();
-
-
-
- [NonSerialized]
- public readonly Dictionary<string, PatchAsset> AssetDic = new Dictionary<string, PatchAsset>();
-
-
-
- [NonSerialized]
- public readonly Dictionary<string, string> AssetPathMapping = new Dictionary<string, string>();
-
- private bool _isInitAssetPathMapping = false;
- private bool _locationToLower = false;
-
-
-
- public void InitAssetPathMapping(bool locationToLower)
- {
- if (_isInitAssetPathMapping)
- return;
- _isInitAssetPathMapping = true;
- if (EnableAddressable)
- {
- if (locationToLower)
- YooLogger.Error("Addressable not support location to lower !");
- foreach (var patchAsset in AssetList)
- {
- string location = patchAsset.Address;
- if (AssetPathMapping.ContainsKey(location))
- throw new Exception($"Address have existed : {location}");
- else
- AssetPathMapping.Add(location, patchAsset.AssetPath);
- }
- }
- else
- {
- _locationToLower = locationToLower;
- foreach (var patchAsset in AssetList)
- {
- string location = patchAsset.AssetPath;
- if (locationToLower)
- location = location.ToLower();
-
- if (AssetPathMapping.ContainsKey(location))
- throw new Exception($"AssetPath have existed : {location}");
- else
- AssetPathMapping.Add(location, patchAsset.AssetPath);
-
- if (Path.HasExtension(location))
- {
- string locationWithoutExtension = StringUtility.RemoveExtension(location);
- if (AssetPathMapping.ContainsKey(locationWithoutExtension))
- YooLogger.Warning($"AssetPath have existed : {locationWithoutExtension}");
- else
- AssetPathMapping.Add(locationWithoutExtension, patchAsset.AssetPath);
- }
- }
- }
- }
-
-
-
- public string MappingToAssetPath(string location)
- {
- if (string.IsNullOrEmpty(location))
- {
- YooLogger.Error("Failed to mapping location to asset path, The location is null or empty.");
- return string.Empty;
- }
- if (_locationToLower)
- location = location.ToLower();
- if (AssetPathMapping.TryGetValue(location, out string assetPath))
- {
- return assetPath;
- }
- else
- {
- YooLogger.Warning($"Failed to mapping location to asset path : {location}");
- return string.Empty;
- }
- }
-
-
-
-
- public PatchBundle GetMainPatchBundle(string assetPath)
- {
- if (AssetDic.TryGetValue(assetPath, out PatchAsset patchAsset))
- {
- int bundleID = patchAsset.BundleID;
- if (bundleID >= 0 && bundleID < BundleList.Count)
- {
- var patchBundle = BundleList[bundleID];
- return patchBundle;
- }
- else
- {
- throw new Exception($"Invalid bundle id : {bundleID} Asset path : {assetPath}");
- }
- }
- else
- {
- throw new Exception("Should never get here !");
- }
- }
-
-
-
-
- public PatchBundle[] GetAllDependencies(string assetPath)
- {
- if (AssetDic.TryGetValue(assetPath, out PatchAsset patchAsset))
- {
- List<PatchBundle> result = new List<PatchBundle>(patchAsset.DependIDs.Length);
- foreach (var dependID in patchAsset.DependIDs)
- {
- if (dependID >= 0 && dependID < BundleList.Count)
- {
- var dependPatchBundle = BundleList[dependID];
- result.Add(dependPatchBundle);
- }
- else
- {
- throw new Exception($"Invalid bundle id : {dependID} Asset path : {assetPath}");
- }
- }
- return result.ToArray();
- }
- else
- {
- throw new Exception("Should never get here !");
- }
- }
-
-
-
- public bool TryGetPatchAsset(string assetPath, out PatchAsset result)
- {
- return AssetDic.TryGetValue(assetPath, out result);
- }
-
-
-
- public bool TryGetPatchBundle(string bundleName, out PatchBundle result)
- {
- return BundleDic.TryGetValue(bundleName, out result);
- }
-
-
-
- public static void Serialize(string savePath, PatchManifest patchManifest)
- {
- string json = JsonUtility.ToJson(patchManifest);
- FileUtility.CreateFile(savePath, json);
- }
-
-
-
- public static PatchManifest Deserialize(string jsonData)
- {
- PatchManifest patchManifest = JsonUtility.FromJson<PatchManifest>(jsonData);
-
- if (patchManifest.FileVersion != YooAssetSettings.PatchManifestFileVersion)
- throw new Exception($"The manifest file version are not compatible : {patchManifest.FileVersion} != {YooAssetSettings.PatchManifestFileVersion}");
-
- foreach (var patchBundle in patchManifest.BundleList)
- {
- patchBundle.ParseFlagsValue();
- patchBundle.ParseFileName(patchManifest.OutputNameStyle);
- patchManifest.BundleDic.Add(patchBundle.BundleName, patchBundle);
- }
-
- foreach (var patchAsset in patchManifest.AssetList)
- {
-
- string assetPath = patchAsset.AssetPath;
- if (patchManifest.AssetDic.ContainsKey(assetPath))
- throw new Exception($"AssetPath have existed : {assetPath}");
- else
- patchManifest.AssetDic.Add(assetPath, patchAsset);
- }
- return patchManifest;
- }
- }
- }
|