// write embedded manifest to streamingassets private static void OutputEmbeddedManifest(EmbeddedManifest embeddedManifest, string outputPath) { if (embeddedManifest.bundles.Count > 0) { var json = JsonUtility.ToJson(embeddedManifest); var manifestPath = Path.Combine(outputPath, "streamingassets-manifest.json"); File.WriteAllText(manifestPath, json); } }
// write embedded manifest to streamingassets private static void WriteEmbeddedManifest(PackageBuildInfo buildInfo, EmbeddedManifest embeddedManifest) { if (embeddedManifest.bundles.Count > 0) { var json = JsonUtility.ToJson(embeddedManifest); var manifestPath = Path.Combine(buildInfo.packagePath, Manifest.EmbeddedManifestFileName); File.WriteAllText(manifestPath, json); buildInfo.filelist.Add(Manifest.EmbeddedManifestFileName); } }
private static void Cleanup(PackageBuildInfo buildInfo, AssetBundleManifest assetBundleManifest, ZipArchiveManifest zipArchiveManifest, FileListManifest fileListManifest, RawFileManifest rawFileManifest, EmbeddedManifest embeddedManifest) { foreach (var dir in Directory.GetDirectories(buildInfo.packagePath)) { CleanupRecursively(dir, "Assets", fileListManifest, false); } foreach (var file in Directory.GetFiles(buildInfo.packagePath)) { var match = false; var fi = new FileInfo(file); var filename = fi.Name; if (builtinFiles.Contains(filename)) { match = true; } if (!match && buildInfo.filelist.Contains(filename)) { match = true; } if (!match && filename == Manifest.EmbeddedManifestFileName && embeddedManifest.bundles.Count > 0) { match = true; } if (!match) { Debug.LogWarning("delete unused file: " + filename); try { fi.Delete(); } catch (Exception exception) { Debug.LogError(exception); } } } }
private static bool HasEmbeddedManifestInsideExecutableAssemblies( Manifest application, out string assemblyNames) { bool hasAssemblies = false; var assemblies = new List <string>(); foreach (AssemblyReference assembly in application.AssemblyReferences) { if (Path.GetExtension(assembly.SourcePath) == $".{Constants.ExecutableFileExtension}" && EmbeddedManifest.Read(assembly.SourcePath) != null) { assemblies.Add(Path.GetFileName(assembly.SourcePath)); hasAssemblies = true; } } assemblyNames = string.Join(", ", assemblies); return(hasAssemblies); }
// 生成清单 public static void BuildManifest(BundleBuilderData data, string outputPath, AssetBundleManifest assetBundleManifest, ZipArchiveManifest zipArchiveManifest, out EmbeddedManifest embeddedManifest) { var manifest = new Manifest(); embeddedManifest = new EmbeddedManifest(); if (assetBundleManifest != null) { var assetBundles = assetBundleManifest.GetAllAssetBundles(); foreach (var assetBundle in assetBundles) { BundleBuilderData.BundleInfo bundleInfo; BundleBuilderData.BundleSplit bundleSplit; if (TryGetBundleSplit(data, assetBundle, out bundleInfo, out bundleSplit)) { // Debug.Log(bundleInfo.name); var assetBundlePath = Path.Combine(outputPath, assetBundle); using (var stream = File.OpenRead(assetBundlePath)) { var fileInfo = new FileInfo(assetBundlePath); var checksum = new Utils.Crc16(); checksum.Update(stream); var bundle = new Manifest.BundleInfo(); bundle.type = Manifest.BundleType.AssetBundle; bundle.name = bundleSplit.name; bundle.checksum = checksum.hex; bundle.size = (int)fileInfo.Length; bundle.load = bundleInfo.load; bundle.priority = bundleInfo.priority; foreach (var asset in bundleSplit.assets) { var assetPath = AssetDatabase.GetAssetPath(asset); bundle.assets.Add(assetPath); } bundle.dependencies = assetBundleManifest.GetAllDependencies(assetBundle); manifest.bundles.Add(bundle); if (bundleInfo.streamingAssets) { embeddedManifest.bundles.Add(new EmbeddedManifest.BundleInfo() { name = bundle.name, checksum = bundle.checksum, size = bundle.size, }); } } } } } if (zipArchiveManifest != null) { foreach (var zipArchive in zipArchiveManifest.archives) { var bundleInfo = GetBundleInfo(data, zipArchive.name); var zipArchivePath = Path.Combine(outputPath, zipArchive.name); using (var stream = File.OpenRead(zipArchivePath)) { var fileInfo = new FileInfo(zipArchivePath); var checksum = new Utils.Crc16(); checksum.Update(stream); var bundle = new Manifest.BundleInfo(); bundle.type = Manifest.BundleType.ZipArchive; bundle.name = zipArchive.name; bundle.checksum = checksum.hex; bundle.size = (int)fileInfo.Length; bundle.load = bundleInfo.load; bundle.priority = bundleInfo.priority; foreach (var assetPath in zipArchive.assets) { // var assetPath = AssetDatabase.GetAssetPath(asset.target); bundle.assets.Add(assetPath); } // bundle.dependencies = null; manifest.bundles.Add(bundle); if (bundleInfo.streamingAssets) { embeddedManifest.bundles.Add(new EmbeddedManifest.BundleInfo() { name = bundle.name, checksum = bundle.checksum, size = bundle.size, }); } } } } if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } OutputManifest(manifest, outputPath); OutputEmbeddedManifest(embeddedManifest, outputPath); }
private static void Cleanup(string outputPath, AssetBundleManifest assetBundleManifest, ZipArchiveManifest zipArchiveManifest, EmbeddedManifest embeddedManifest) { foreach (var file in Directory.GetFiles(outputPath)) { var fi = new FileInfo(file); var match = false; if ( fi.Name == "AssetBundles" || fi.Name == "AssetBundles.manifest" || fi.Name == "checksum.txt" || fi.Name == "manifest.json" ) { match = true; } if (fi.Name == EmbeddedManifest.FileName) { if (embeddedManifest.bundles.Count > 0) { match = true; } } if (!match) { foreach (var assetBundle in assetBundleManifest.GetAllAssetBundles()) { if (fi.Name == assetBundle || fi.Name == assetBundle + ".manifest") { match = true; break; } } } if (!match) { foreach (var zipArchive in zipArchiveManifest.archives) { if (fi.Name == zipArchive.name) { match = true; break; } } } if (!match) { // Debug.LogWarning("delete unused file: " + fi.Name); try { fi.Delete(); } catch (Exception exception) { Debug.LogError(exception); } } } }
private static void PrepareStreamingAssets(BundleBuilderData data, string outputPath, EmbeddedManifest embeddedManifest) { if (embeddedManifest.bundles.Count > 0) { if (!Directory.Exists(EmbeddedManifest.BundlesPath)) { Directory.CreateDirectory(EmbeddedManifest.BundlesPath); } File.Copy(Path.Combine(outputPath, EmbeddedManifest.FileName), Path.Combine(EmbeddedManifest.BundlesPath, EmbeddedManifest.FileName), true); foreach (var bundleInfo in embeddedManifest.bundles) { File.Copy(Path.Combine(outputPath, bundleInfo.name), Path.Combine(EmbeddedManifest.BundlesPath, bundleInfo.name), true); } AssetDatabase.Refresh(); // cleanup foreach (var file in Directory.GetFiles(EmbeddedManifest.BundlesPath)) { var fi = new FileInfo(file); var match = false; if (fi.Name == EmbeddedManifest.FileName || fi.Name == EmbeddedManifest.FileName + ".meta") { continue; } foreach (var bundleInfo in embeddedManifest.bundles) { if (fi.Name == bundleInfo.name || fi.Name == bundleInfo.name + ".meta") { match = true; break; } } if (!match) { fi.Delete(); } } AssetDatabase.Refresh(); } else { if (Directory.Exists(EmbeddedManifest.BundlesPath)) { Directory.Delete(EmbeddedManifest.BundlesPath); } } }
// 生成最终包文件, 生成最终清单 public static EmbeddedManifest BuildFinalPackages(PackageBuildInfo buildInfo, AssetBundleManifest assetBundleManifest, ZipArchiveManifest zipArchiveManifest, FileListManifest fileListManifest, RawFileManifest rawFileManifest) { var data = buildInfo.data; var manifest = new Manifest(); manifest.chunkSize = data.chunkSize; manifest.build = buildInfo.data.build; manifest.timestamp = (int)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds; manifest.tag = buildInfo.sharedBuildInfo.tag; var embeddedManifest = new EmbeddedManifest(); if (assetBundleManifest != null) { var assetBundles = assetBundleManifest.GetAllAssetBundles(); foreach (var assetBundle in assetBundles) { BundleBuilderData.BundleInfo bundleInfo; BundleBuilderData.BundleSplit bundleSplit; BundleBuilderData.BundleSlice bundleSlice; if (TryGetBundleSlice(data, assetBundle, out bundleInfo, out bundleSplit, out bundleSlice)) { // Debug.Log(bundleInfo.name); var fileEntry = EncryptFileEntry(data, buildInfo, bundleSplit.encrypted, bundleSlice.name, buildInfo.assetBundlePath); var bundle = new Manifest.BundleInfo(); bundle.comment = bundleInfo.note; bundle.tag = bundleInfo.tag; bundle.encrypted = bundleSplit.encrypted; bundle.rsize = fileEntry.rsize; bundle.type = Manifest.BundleType.AssetBundle; bundle.name = fileEntry.name; bundle.checksum = fileEntry.checksum; bundle.size = fileEntry.size; bundle.load = bundleInfo.load; bundle.priority = GetPriority(buildInfo, bundleInfo, bundleSlice); if (bundleSlice.lastBuildSize != fileEntry.size) { bundleSlice.lastBuildSize = fileEntry.size; data.MarkAsDirty(); } for (int assetIndex = 0, assetCount = bundleSlice.GetAssetCount(); assetIndex < assetCount; assetIndex++) { var assetPath = bundleSlice.GetAssetPath(assetIndex); bundle.assets.Add(assetPath); } bundle.dependencies = assetBundleManifest.GetAllDependencies(assetBundle); buildInfo.CreateEntry(bundle); manifest.bundles.Add(bundle); if (bundleSlice.streamingAssets || data.streamingAssetsAnyway) { embeddedManifest.bundles.Add(fileEntry); } } } } if (zipArchiveManifest != null) { foreach (var zipArchive in zipArchiveManifest.archives) { BundleBuilderData.BundleInfo bundleInfo; BundleBuilderData.BundleSplit bundleSplit; BundleBuilderData.BundleSlice bundleSlice; if (TryGetBundleSlice(data, zipArchive.name, out bundleInfo, out bundleSplit, out bundleSlice)) { var fileEntry = EncryptFileEntry(data, buildInfo, bundleSplit.encrypted, zipArchive.name, buildInfo.zipArchivePath); var bundle = new Manifest.BundleInfo(); bundle.comment = bundleInfo.note; bundle.tag = bundleInfo.tag; bundle.encrypted = bundleSplit.encrypted; bundle.rsize = fileEntry.rsize; bundle.type = Manifest.BundleType.ZipArchive; bundle.name = fileEntry.name; bundle.checksum = fileEntry.checksum; bundle.size = fileEntry.size; bundle.load = bundleInfo.load; bundle.priority = GetPriority(buildInfo, bundleInfo, bundleSlice); foreach (var assetPath in zipArchive.assets) { bundle.assets.Add(assetPath); } manifest.bundles.Add(bundle); if (bundleSlice.streamingAssets || data.streamingAssetsAnyway) { embeddedManifest.bundles.Add(fileEntry); } } } } if (fileListManifest != null) { foreach (var fileList in fileListManifest.fileLists) { var bundleInfo = GetBundleInfo(data, fileList.name); var fileListPath = Path.Combine(buildInfo.packagePath, fileList.name); var fileEntry = GenFileEntry(fileList.name, fileListPath); var bundle = new Manifest.BundleInfo(); bundle.comment = bundleInfo.note; bundle.tag = bundleInfo.tag; bundle.type = Manifest.BundleType.FileList; bundle.name = fileList.name; bundle.checksum = fileEntry.checksum; bundle.size = fileEntry.size; bundle.load = bundleInfo.load; bundle.priority = bundleInfo.priority; buildInfo.filelist.Add(fileList.name); foreach (var bundleTargets in bundleInfo.targets) { var targetPath = bundleTargets.targetPath; bundle.assets.Add(targetPath); } manifest.bundles.Add(bundle); if (bundleInfo.streamingAssets || data.streamingAssetsAnyway) { embeddedManifest.bundles.Add(fileEntry); } } } WriteManifest(buildInfo, manifest); WriteEmbeddedManifest(buildInfo, embeddedManifest); if (buildInfo.buildTarget == EditorUserBuildSettings.activeBuildTarget) { BuildStreamingAssets(buildInfo, fileListManifest); } return(embeddedManifest); }