using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; namespace MetaCity.BundleKit.Editor { [Serializable] public class BuildCatalogCollection { public BuildCatalog[] catalogs; } [Serializable] public class BuildCatalog { //name of the bundle (used as the unique key to reference to the bundle regardless of its version) public string bundleName; //hash of the bundle (may be different for different version of the same bundle) public string bundleHash; //type of the bundle: prefab/scene/... (determines how the bundle should be loaded in the Launcher) public string bundleType; //Whether the scene is multiPlayer(only works on Scene Type) public bool multiPlayer; //the target platform of this bundle public string bundlePlatform; //the list of bundle names that this bundle dependents on public string[] bundleDependencies; //the full path of the built bundle file, can be used to validate bundle when incrementally adding new catalog public string bundleLocalPath; public override string ToString() { return $"[Name: {bundleName}({bundleType}:{bundleHash}), Dependencies: {string.Join(",", bundleDependencies)}, LocalPath: {bundleLocalPath}"; } } public static class CatalogUtilities { private static string CreateDependentBundleKey(string bundleName, string bundleHash) { return $"{bundleName}-{bundleHash}"; } public static BuildCatalog CreateMaterialCatalog(BuildTarget platform, string vrmModelName, AssetBundleManifest manifest) { var bundleName = manifest.GetAllAssetBundles()[0]; return new BuildCatalog { bundleName = bundleName, bundleHash = manifest.GetAssetBundleHash(bundleName).ToString(), bundleType = "Prefab", bundlePlatform = platform.ToString(), bundleDependencies = Array.Empty(), bundleLocalPath = Path.Combine(Constants.AvatarBundleFolderPath, bundleName) }; } public static BuildCatalog CreateVRMAvatarCatalog(BuildTarget platform, string vrmName, string vrmHash,string dependency,string vrmPath) { return new BuildCatalog { bundleName = vrmName, bundleHash = vrmHash, bundleType = "Avatar", bundlePlatform = platform.ToString(), bundleDependencies = string.IsNullOrEmpty(dependency)?Array.Empty():new string[1]{dependency}, bundleLocalPath = vrmPath }; } public static List CreateCatalog(PlayMode playMode,BuildTarget platform, AssetBundleManifest manifest, List bundles) { var catalogs = new List(); var allBundlesInManifest = new List(manifest.GetAllAssetBundles()); foreach (var bundle in bundles) { if (!bundle.NeedUpload) { continue; } var bundleName = bundle.BundleBuild.assetBundleName; if (!allBundlesInManifest.Contains(bundleName)) { continue; } var allDependencies = manifest.GetAllDependencies(bundleName); var dependentUniqueKeys = new string[allDependencies.Length]; for (var i = 0; i < allDependencies.Length; i++) { var dependentBundleName = allDependencies[i]; var bundleHash = manifest.GetAssetBundleHash(dependentBundleName).ToString(); dependentUniqueKeys[i] = CreateDependentBundleKey(dependentBundleName, bundleHash); } catalogs.Add(new BuildCatalog { bundleName = bundleName, bundleHash = manifest.GetAssetBundleHash(bundleName).ToString(), bundleType = bundle.BundleType.ToString(), bundlePlatform = platform.ToString(), multiPlayer=playMode==PlayMode.MultiPlayer, bundleDependencies = dependentUniqueKeys, bundleLocalPath = Path.Combine(Constants.BundleFolderPath, bundleName) }); } return catalogs; } } }