|
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.IO; |
5 | 5 | using System.Runtime.Serialization.Formatters.Binary; |
6 | | - |
7 | 6 | using AssetBundleBrowser.AssetBundleDataSource; |
8 | | -using System.Linq; |
9 | 7 |
|
10 | 8 | namespace AssetBundleBrowser |
11 | 9 | { |
@@ -182,7 +180,7 @@ internal void OnEnable(EditorWindow parent) |
182 | 180 |
|
183 | 181 | m_TargetContent = new GUIContent("Build Target", "Choose target platform to build for."); |
184 | 182 | m_CompressionContent = new GUIContent("Compression", "Choose no compress, standard (LZMA), or chunk based (LZ4)"); |
185 | | - m_BundleNameContent = new GUIContent("Bundle Name", "Choose none, append hash, or replace by hash (Including manifest)"); |
| 183 | + m_BundleNameContent = new GUIContent("Bundle Name", "Choose none, append hash, or replace by hash"); |
186 | 184 |
|
187 | 185 | if (m_UserData.m_UseDefaultPath) |
188 | 186 | { |
@@ -396,10 +394,7 @@ private void ExecuteBuild() |
396 | 394 | if (m_UserData.m_BundleNameOption == BundleNameOptions.AppendHash) |
397 | 395 | opt |= BuildAssetBundleOptions.AppendHashToAssetBundleName; |
398 | 396 | else if (m_UserData.m_BundleNameOption == BundleNameOptions.ReplaceByHash) |
399 | | - { |
400 | | - opt |= BuildAssetBundleOptions.AppendHashToAssetBundleName; |
401 | 397 | replaceByHash = true; |
402 | | - } |
403 | 398 |
|
404 | 399 | // toggle options |
405 | 400 | foreach (var tog in m_ToggleData) |
@@ -494,73 +489,64 @@ private void ResetPathToDefault() |
494 | 489 | /// Remove manifest file from build folder |
495 | 490 | /// </summary> |
496 | 491 | /// <param name="outputDirectory"></param> |
497 | | - internal static void WithoutManifestFile(string outputDirectory) |
| 492 | + internal static bool WithoutManifestFile(string outputDirectory) |
498 | 493 | { |
499 | 494 | // filter only extension is manifest |
500 | 495 | string[] files = Directory.GetFiles(outputDirectory, "*.manifest", SearchOption.AllDirectories); |
501 | 496 | foreach (string file in files) |
502 | 497 | { |
503 | 498 | if (File.Exists(file)) File.Delete(file); |
504 | 499 | } |
| 500 | + |
| 501 | + return true; |
505 | 502 | } |
506 | 503 |
|
507 | 504 | /// <summary> |
508 | | - /// Repace bundle name by hash (keep hash) |
| 505 | + /// Repace bundle name by hash (read hash from manifest to replace) |
509 | 506 | /// </summary> |
510 | 507 | /// <param name="outputDirectory"></param> |
511 | | - internal static void ReplaceBundleNameByHash(string outputDirectory) |
| 508 | + internal static bool ReplaceBundleNameByHash(string outputDirectory) |
512 | 509 | { |
513 | | - // set replacement map for manifest file to replace hash |
514 | | - Dictionary<string, string> dictReplacement = new Dictionary<string, string>(); |
| 510 | + // outputDirectory last path name = manifestName |
| 511 | + var firstIdx = outputDirectory.LastIndexOf("\\") == -1 ? outputDirectory.LastIndexOf("/") : outputDirectory.LastIndexOf("\\"); |
| 512 | + string manifestName = outputDirectory.Substring(firstIdx + 1, outputDirectory.Length - firstIdx - 1); |
| 513 | + |
| 514 | + string manifestFullPath = string.Empty; |
515 | 515 |
|
516 | | - // get all file |
| 516 | + // search from all file to find menifest |
517 | 517 | string[] files = Directory.GetFiles(outputDirectory, "*.*", SearchOption.AllDirectories); |
518 | 518 | foreach (var file in files) |
519 | 519 | { |
520 | | - // get fileName without extension |
521 | | - string fileName = Path.GetFileNameWithoutExtension(file); |
522 | | - |
523 | | - // Hash128 = 16 bytes (hex str => 0xFF = 1 bytes = 1 set, 16 * 2 = 32) |
524 | | - int hashSize = 32; |
525 | | - |
526 | | - // append hash has _ |
527 | | - var lastIdx = file.LastIndexOf("_"); |
528 | | - // path \ for win, / for mac, linux |
529 | | - var firstIdx = (file.LastIndexOf("\\") == -1 ? file.LastIndexOf("/") : file.LastIndexOf("\\")); |
530 | | - |
531 | | - // fileName.Length > hashSize = including hash code |
532 | | - if (lastIdx != -1 && firstIdx != -1 && fileName.Length > hashSize) |
| 520 | + if (file.IndexOf(manifestName) != -1) |
533 | 521 | { |
534 | | - string originFileName = file.Substring(firstIdx + 1, lastIdx - firstIdx - 1); |
535 | | - string hash = file.Substring(firstIdx + 2 + originFileName.Length, hashSize); |
536 | | - |
537 | | - // set pair |
538 | | - dictReplacement.Add(originFileName, hash); |
539 | | - |
540 | | - // replace origin file name to empty (only keep hash code) |
541 | | - string newFile = file.Replace($"{originFileName}_", string.Empty); |
542 | | - |
543 | | - // rename it |
544 | | - if (File.Exists(file)) File.Move(file, newFile); |
| 522 | + manifestFullPath = Path.GetFullPath(file); |
| 523 | + break; |
545 | 524 | } |
546 | 525 | } |
547 | 526 |
|
548 | | - // get all manifest file |
549 | | - string[] manifestFiles = Directory.GetFiles(outputDirectory, "*.manifest", SearchOption.AllDirectories); |
550 | | - foreach (string file in manifestFiles) |
| 527 | + // file stream to read manifest |
| 528 | + var fs = new FileStream(manifestFullPath, FileMode.Open, FileAccess.Read, FileShare.None); |
| 529 | + var bundle = AssetBundle.LoadFromStream(fs); |
| 530 | + fs.Dispose(); |
| 531 | + |
| 532 | + // load manifest asset |
| 533 | + AssetBundleManifest manifest = bundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); |
| 534 | + |
| 535 | + // replace all bundle file name by hash |
| 536 | + foreach (var file in files) |
551 | 537 | { |
552 | | - foreach (var pair in dictReplacement) |
553 | | - { |
554 | | - string originFileName = pair.Key; |
555 | | - string hash = pair.Value; |
| 538 | + string bundleName = file.Replace(outputDirectory, string.Empty); |
| 539 | + bundleName = bundleName.Substring(1, bundleName.Length - 1); |
| 540 | + // skip process manifest & .manifest extension |
| 541 | + if (bundleName.IndexOf(manifestName) != -1 || file.IndexOf(".manifest") != -1) continue; |
556 | 542 |
|
557 | | - if (file.IndexOf(originFileName) != -1) |
558 | | - { |
559 | | - string newFile = file.Replace(originFileName, hash); |
560 | | - if (File.Exists(file)) File.Move(file, newFile); |
561 | | - } |
562 | | - } |
| 543 | + string fileName = Path.GetFileNameWithoutExtension(file); |
| 544 | + string hash = manifest.GetAssetBundleHash(bundleName).ToString(); |
| 545 | + string newFile = file.Replace(fileName, hash); |
| 546 | + if (File.Exists(file)) File.Move(file, newFile); |
563 | 547 | } |
| 548 | + |
| 549 | + return true; |
564 | 550 | } |
565 | 551 |
|
566 | 552 | //Note: this is the provided BuildTarget enum with some entries removed as they are invalid in the dropdown |
|
0 commit comments