diff --git a/AssetStudio/AssetsManager.cs b/AssetStudio/AssetsManager.cs index fd8ca3b..b29613f 100644 --- a/AssetStudio/AssetsManager.cs +++ b/AssetStudio/AssetsManager.cs @@ -169,7 +169,7 @@ namespace AssetStudio } catch (NotSupportedException e) { - Logger.Error(e.Message, e); + Logger.Error(e.Message); reader.Dispose(); } catch (Exception e) @@ -203,7 +203,7 @@ namespace AssetStudio } catch (NotSupportedException e) { - Logger.Error(e.Message, e); + Logger.Error(e.Message); resourceFileReaders.Add(reader.FileName, reader); } catch (Exception e) @@ -221,7 +221,7 @@ namespace AssetStudio Logger.Info("Loading " + reader.FullPath); try { - var bundleFile = new BundleFile(reader); + var bundleFile = new BundleFile(reader, SpecifyUnityVersion); foreach (var file in bundleFile.fileList) { var dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), file.fileName); @@ -236,6 +236,10 @@ namespace AssetStudio } } } + catch (NotSupportedException e) + { + Logger.Error(e.Message); + } catch (Exception e) { var str = $"Error while reading bundle file {reader.FullPath}"; diff --git a/AssetStudio/BundleFile.cs b/AssetStudio/BundleFile.cs index aea03af..1d5b7d8 100644 --- a/AssetStudio/BundleFile.cs +++ b/AssetStudio/BundleFile.cs @@ -15,6 +15,13 @@ namespace AssetStudio BlockInfoNeedPaddingAtStart = 0x200 } + [Flags] + public enum CnEncryptionFlags + { + OldFlag = 0x200, + NewFlag = 0x400 + } + [Flags] public enum StorageBlockFlags { @@ -66,7 +73,7 @@ namespace AssetStudio public StreamFile[] fileList; - public BundleFile(FileReader reader) + public BundleFile(FileReader reader, string specUnityVer = "") { m_Header = new Header(); m_Header.signature = reader.ReadStringToNull(); @@ -92,6 +99,30 @@ namespace AssetStudio break; case "UnityFS": ReadHeader(reader); + + bool isUnityCnEnc = false; + string unityVer = string.IsNullOrEmpty(specUnityVer) ? m_Header.unityRevision : specUnityVer; + int[] ver = new string(unityVer.SkipWhile(x => !char.IsDigit(x)).TakeWhile(x => char.IsDigit(x) || x == '.').ToArray()).Split('.').Select(x => int.Parse(x)).ToArray(); + if (ver[0] != 0) + { + // https://issuetracker.unity3d.com/issues/files-within-assetbundles-do-not-start-on-aligned-boundaries-breaking-patching-on-nintendo-switch + if (ver[0] < 2020 || + (ver[0] == 2020 && ver[1] <= 3 && ver[2] < 34) || + (ver[0] == 2021 && ver[1] <= 3 && ver[2] < 2) || + (ver[0] == 2022 && ver[1] <= 1 && ver[2] < 1)) + { + isUnityCnEnc = ((CnEncryptionFlags)m_Header.flags & CnEncryptionFlags.OldFlag) != 0; + } + else + { + isUnityCnEnc = ((CnEncryptionFlags)m_Header.flags & CnEncryptionFlags.NewFlag) != 0; + } + } + if (isUnityCnEnc) + { + throw new NotSupportedException("Unsupported bundle file. UnityCN encryption was detected."); + } + ReadBlocksInfoAndDirectory(reader); using (var blocksStream = CreateBlocksStream(reader.FullPath)) { diff --git a/AssetStudioGUI/Studio.cs b/AssetStudioGUI/Studio.cs index 8c898b9..322e24b 100644 --- a/AssetStudioGUI/Studio.cs +++ b/AssetStudioGUI/Studio.cs @@ -85,7 +85,7 @@ namespace AssetStudioGUI private static int ExtractBundleFile(FileReader reader, string savePath) { Logger.Info($"Decompressing {reader.FileName} ..."); - var bundleFile = new BundleFile(reader); + var bundleFile = new BundleFile(reader, assetsManager.SpecifyUnityVersion); reader.Dispose(); if (bundleFile.fileList.Length > 0) {