123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- namespace ET.Client
- {
- [FriendOf(typeof(ABInfo))]
- public static class ABInfoSystem
- {
- [ObjectSystem]
- public class ABInfoAwakeSystem: AwakeSystem<ABInfo, string, AssetBundle>
- {
- protected override void Awake(ABInfo self, string abName, AssetBundle a)
- {
- self.AssetBundle = a;
- self.Name = abName;
- self.RefCount = 1;
- self.AlreadyLoadAssets = false;
- }
- }
- [ObjectSystem]
- public class ABInfoDestroySystem: DestroySystem<ABInfo>
- {
- protected override void Destroy(ABInfo self)
- {
-
- self.RefCount = 0;
- self.Name = "";
- self.AlreadyLoadAssets = false;
- self.AssetBundle = null;
- }
- }
-
- public static void Destroy(this ABInfo self, bool unload = true)
- {
- if (self.AssetBundle != null)
- {
- self.AssetBundle.Unload(unload);
- }
- self.Dispose();
- }
- }
- [ChildOf(typeof(ResourcesComponent))]
- public class ABInfo: Entity, IAwake<string, AssetBundle>, IDestroy
- {
- public string Name { get; set; }
- public int RefCount { get; set; }
- public AssetBundle AssetBundle;
- public bool AlreadyLoadAssets;
- }
-
- [FriendOf(typeof(ResourcesComponent))]
- public static class AssetBundleHelper
- {
- public static string IntToString(this int value)
- {
- string result;
- if (ResourcesComponent.Instance.IntToStringDict.TryGetValue(value, out result))
- {
- return result;
- }
- result = value.ToString();
- ResourcesComponent.Instance.IntToStringDict[value] = result;
- return result;
- }
- public static string StringToAB(this string value)
- {
- string result;
- if (ResourcesComponent.Instance.StringToABDict.TryGetValue(value, out result))
- {
- return result;
- }
- result = value + ".unity3d";
- ResourcesComponent.Instance.StringToABDict[value] = result;
- return result;
- }
- public static string IntToAB(this int value)
- {
- return value.IntToString().StringToAB();
- }
- public static string BundleNameToLower(this string value)
- {
- string result;
- if (ResourcesComponent.Instance.BundleNameToLowerDict.TryGetValue(value, out result))
- {
- return result;
- }
- result = value.ToLower();
- ResourcesComponent.Instance.BundleNameToLowerDict[value] = result;
- return result;
- }
- }
- [FriendOf(typeof(ABInfo))]
- [FriendOf(typeof(ResourcesComponent))]
- public static class ResourcesComponentSystem
- {
- [ObjectSystem]
- public class ResourcesComponentAwakeSystem: AwakeSystem<ResourcesComponent>
- {
- protected override void Awake(ResourcesComponent self)
- {
- ResourcesComponent.Instance = self;
- if (Define.IsAsync)
- {
- self.LoadOneBundle("StreamingAssets");
- self.AssetBundleManifestObject = (AssetBundleManifest)self.GetAsset("StreamingAssets", "AssetBundleManifest");
- self.UnloadBundle("StreamingAssets", false);
- }
- }
- }
-
- [ObjectSystem]
- public class ResourcesComponentDestroySystem: DestroySystem<ResourcesComponent>
- {
- protected override void Destroy(ResourcesComponent self)
- {
- ResourcesComponent.Instance = null;
- foreach (var abInfo in self.bundles)
- {
- abInfo.Value.Destroy();
- }
- self.bundles.Clear();
- self.resourceCache.Clear();
- self.IntToStringDict.Clear();
- self.StringToABDict.Clear();
- self.BundleNameToLowerDict.Clear();
- if (self.AssetBundleManifestObject != null)
- {
- UnityEngine.Object.Destroy(self.AssetBundleManifestObject);
- self.AssetBundleManifestObject = null;
- }
- }
- }
- private static string[] GetDependencies(this ResourcesComponent self, string assetBundleName)
- {
- string[] dependencies = Array.Empty<string>();
- if (self.DependenciesCache.TryGetValue(assetBundleName, out dependencies))
- {
- return dependencies;
- }
- if (!Define.IsAsync)
- {
- if (Define.IsEditor)
- {
- dependencies = Define.GetAssetBundleDependencies(assetBundleName, true);
- }
- }
- else
- {
- dependencies = self.AssetBundleManifestObject.GetAllDependencies(assetBundleName);
- }
- self.DependenciesCache.Add(assetBundleName, dependencies);
- return dependencies;
- }
- private static string[] GetSortedDependencies(this ResourcesComponent self, string assetBundleName)
- {
- var info = new Dictionary<string, int>();
- var parents = new List<string>();
- self.CollectDependencies(parents, assetBundleName, info);
- string[] ss = info.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
- return ss;
- }
- private static void CollectDependencies(this ResourcesComponent self, List<string> parents, string assetBundleName, Dictionary<string, int> info)
- {
- parents.Add(assetBundleName);
- string[] deps = self.GetDependencies(assetBundleName);
- foreach (string parent in parents)
- {
- if (!info.ContainsKey(parent))
- {
- info[parent] = 0;
- }
- info[parent] += deps.Length;
- }
- foreach (string dep in deps)
- {
- if (parents.Contains(dep))
- {
- throw new Exception($"包有循环依赖,请重新标记: {assetBundleName} {dep}");
- }
- self.CollectDependencies(parents, dep, info);
- }
- parents.RemoveAt(parents.Count - 1);
- }
- public static bool Contains(this ResourcesComponent self, string bundleName)
- {
- return self.bundles.ContainsKey(bundleName);
- }
- public static UnityEngine.Object GetAsset(this ResourcesComponent self, string bundleName, string prefab)
- {
- Dictionary<string, UnityEngine.Object> dict;
- if (!self.resourceCache.TryGetValue(bundleName.BundleNameToLower(), out dict))
- {
- throw new Exception($"not found asset: {bundleName} {prefab}");
- }
- UnityEngine.Object resource = null;
- if (!dict.TryGetValue(prefab, out resource))
- {
- throw new Exception($"not found asset: {bundleName} {prefab}");
- }
- return resource;
- }
-
- public static async ETTask UnloadBundleAsync(this ResourcesComponent self, string assetBundleName, bool unload = true)
- {
- assetBundleName = assetBundleName.BundleNameToLower();
- string[] dependencies = self.GetSortedDependencies(assetBundleName);
-
- foreach (string dependency in dependencies)
- {
- using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, assetBundleName.GetHashCode()))
- {
- self.UnloadOneBundle(dependency, unload);
- await TimerComponent.Instance.WaitFrameAsync();
- }
- }
-
- }
-
- public static void UnloadBundle(this ResourcesComponent self, string assetBundleName, bool unload = true)
- {
- assetBundleName = assetBundleName.BundleNameToLower();
- string[] dependencies = self.GetSortedDependencies(assetBundleName);
-
- foreach (string dependency in dependencies)
- {
- self.UnloadOneBundle(dependency, unload);
- }
-
- }
- private static void UnloadOneBundle(this ResourcesComponent self, string assetBundleName, bool unload = true)
- {
- assetBundleName = assetBundleName.BundleNameToLower();
- ABInfo abInfo;
- if (!self.bundles.TryGetValue(assetBundleName, out abInfo))
- {
- return;
- }
-
- --abInfo.RefCount;
- if (abInfo.RefCount > 0)
- {
- return;
- }
-
- self.bundles.Remove(assetBundleName);
- self.resourceCache.Remove(assetBundleName);
- abInfo.Destroy(unload);
-
- }
-
-
-
-
-
- public static void LoadBundle(this ResourcesComponent self, string assetBundleName)
- {
- assetBundleName = assetBundleName.ToLower();
- string[] dependencies = self.GetSortedDependencies(assetBundleName);
-
- foreach (string dependency in dependencies)
- {
- if (string.IsNullOrEmpty(dependency))
- {
- continue;
- }
- self.LoadOneBundle(dependency);
- }
-
- }
- private static void AddResource(this ResourcesComponent self, string bundleName, string assetName, UnityEngine.Object resource)
- {
- Dictionary<string, UnityEngine.Object> dict;
- if (!self.resourceCache.TryGetValue(bundleName.BundleNameToLower(), out dict))
- {
- dict = new Dictionary<string, UnityEngine.Object>();
- self.resourceCache[bundleName] = dict;
- }
- dict[assetName] = resource;
- }
- private static void LoadOneBundle(this ResourcesComponent self, string assetBundleName)
- {
- assetBundleName = assetBundleName.BundleNameToLower();
- ABInfo abInfo;
- if (self.bundles.TryGetValue(assetBundleName, out abInfo))
- {
- ++abInfo.RefCount;
-
- return;
- }
- if (!Define.IsAsync)
- {
- if (Define.IsEditor)
- {
- string[] realPath = null;
- realPath = Define.GetAssetPathsFromAssetBundle(assetBundleName);
- foreach (string s in realPath)
- {
- string assetName = Path.GetFileNameWithoutExtension(s);
- UnityEngine.Object resource = Define.LoadAssetAtPath(s);
- self.AddResource(assetBundleName, assetName, resource);
- }
- if (realPath.Length > 0)
- {
- abInfo = self.AddChild<ABInfo, string, AssetBundle>(assetBundleName, null);
- self.bundles[assetBundleName] = abInfo;
-
- }
- else
- {
- Log.Error($"assets bundle not found: {assetBundleName}");
- }
- }
- return;
- }
- string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
- AssetBundle assetBundle = null;
- if (File.Exists(p))
- {
- assetBundle = AssetBundle.LoadFromFile(p);
- }
- else
- {
- p = Path.Combine(PathHelper.AppResPath, assetBundleName);
- assetBundle = AssetBundle.LoadFromFile(p);
- }
- if (assetBundle == null)
- {
-
- Log.Warning($"assets bundle not found: {assetBundleName}");
- return;
- }
- if (!assetBundle.isStreamedSceneAssetBundle)
- {
-
- var assets = assetBundle.LoadAllAssets();
- foreach (UnityEngine.Object asset in assets)
- {
- self.AddResource(assetBundleName, asset.name, asset);
- }
- }
- abInfo = self.AddChild<ABInfo, string, AssetBundle>(assetBundleName, assetBundle);
- self.bundles[assetBundleName] = abInfo;
-
- }
-
-
-
- public static async ETTask LoadBundleAsync(this ResourcesComponent self, string assetBundleName)
- {
- assetBundleName = assetBundleName.BundleNameToLower();
- string[] dependencies = self.GetSortedDependencies(assetBundleName);
-
- using (ListComponent<ABInfo> abInfos = ListComponent<ABInfo>.Create())
- {
- async ETTask LoadDependency(string dependency, List<ABInfo> abInfosList)
- {
- using CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, dependency.GetHashCode());
-
- ABInfo abInfo = await self.LoadOneBundleAsync(dependency);
- if (abInfo == null || abInfo.RefCount > 1)
- {
- return;
- }
- abInfosList.Add(abInfo);
- }
-
- using (ListComponent<ETTask> tasks = ListComponent<ETTask>.Create())
- {
- foreach (string dependency in dependencies)
- {
- tasks.Add(LoadDependency(dependency, abInfos));
- }
- await ETTaskHelper.WaitAll(tasks);
-
- tasks.Clear();
- foreach (ABInfo abInfo in abInfos)
- {
- tasks.Add(self.LoadOneBundleAllAssets(abInfo));
- }
- await ETTaskHelper.WaitAll(tasks);
- }
- }
- }
- private static async ETTask<ABInfo> LoadOneBundleAsync(this ResourcesComponent self, string assetBundleName)
- {
- assetBundleName = assetBundleName.BundleNameToLower();
- ABInfo abInfo;
- if (self.bundles.TryGetValue(assetBundleName, out abInfo))
- {
- ++abInfo.RefCount;
-
- return null;
- }
- string p = "";
- AssetBundle assetBundle = null;
- if (!Define.IsAsync)
- {
- if (Define.IsEditor)
- {
- string[] realPath = Define.GetAssetPathsFromAssetBundle(assetBundleName);
- foreach (string s in realPath)
- {
- string assetName = Path.GetFileNameWithoutExtension(s);
- UnityEngine.Object resource = Define.LoadAssetAtPath(s);
- self.AddResource(assetBundleName, assetName, resource);
- }
- if (realPath.Length > 0)
- {
- abInfo = self.AddChild<ABInfo, string, AssetBundle>(assetBundleName, null);
- self.bundles[assetBundleName] = abInfo;
-
- }
- else
- {
- Log.Error("Bundle not exist! BundleName: " + assetBundleName);
- }
-
- await TimerComponent.Instance.WaitAsync(100);
- return abInfo;
- }
- }
- p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
- if (!File.Exists(p))
- {
- p = Path.Combine(PathHelper.AppResPath, assetBundleName);
- }
- Log.Debug("Async load bundle BundleName : " + p);
-
-
-
-
-
- AssetBundleCreateRequest assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(p);
- await assetBundleCreateRequest;
- assetBundle = assetBundleCreateRequest.assetBundle;
- if (assetBundle == null)
- {
-
- Log.Warning($"assets bundle not found: {assetBundleName}");
- return null;
- }
- abInfo = self.AddChild<ABInfo, string, AssetBundle>(assetBundleName, assetBundle);
- self.bundles[assetBundleName] = abInfo;
- return abInfo;
-
- }
-
- private static async ETTask LoadOneBundleAllAssets(this ResourcesComponent self, ABInfo abInfo)
- {
- using CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode());
-
- if (abInfo.IsDisposed || abInfo.AlreadyLoadAssets)
- {
- return;
- }
- if (abInfo.AssetBundle != null && !abInfo.AssetBundle.isStreamedSceneAssetBundle)
- {
-
- AssetBundleRequest request = abInfo.AssetBundle.LoadAllAssetsAsync();
- await request;
- UnityEngine.Object[] assets = request.allAssets;
- foreach (UnityEngine.Object asset in assets)
- {
- self.AddResource(abInfo.Name, asset.name, asset);
- }
- }
- abInfo.AlreadyLoadAssets = true;
- }
- public static string DebugString(this ResourcesComponent self)
- {
- StringBuilder sb = new StringBuilder();
- foreach (ABInfo abInfo in self.bundles.Values)
- {
- sb.Append($"{abInfo.Name}:{abInfo.RefCount}\n");
- }
- return sb.ToString();
- }
- }
-
- [ComponentOf]
- public class ResourcesComponent: Entity, IAwake, IDestroy
- {
- public static ResourcesComponent Instance { get; set; }
- public AssetBundleManifest AssetBundleManifestObject { get; set; }
- public Dictionary<int, string> IntToStringDict = new Dictionary<int, string>();
- public Dictionary<string, string> StringToABDict = new Dictionary<string, string>();
- public Dictionary<string, string> BundleNameToLowerDict = new Dictionary<string, string>() { { "StreamingAssets", "StreamingAssets" } };
- public readonly Dictionary<string, Dictionary<string, UnityEngine.Object>> resourceCache =
- new Dictionary<string, Dictionary<string, UnityEngine.Object>>();
- public readonly Dictionary<string, ABInfo> bundles = new Dictionary<string, ABInfo>();
-
-
- public readonly Dictionary<string, string[]> DependenciesCache = new Dictionary<string, string[]>();
- }
- }
|