From ee6c050330f844efdacf5de3acae881bf5417988 Mon Sep 17 00:00:00 2001 From: Perfare Date: Tue, 27 Feb 2018 17:23:15 +0800 Subject: [PATCH] new crunch library after Unity 2017.3 --- Unity Studio/Unity Classes/Texture2D.cs | 15 ++++++---- .../Texture2DConverter.cs | 29 ++++++++++++++----- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Unity Studio/Unity Classes/Texture2D.cs b/Unity Studio/Unity Classes/Texture2D.cs index 6e8d8c2..0e30f31 100644 --- a/Unity Studio/Unity Classes/Texture2D.cs +++ b/Unity Studio/Unity Classes/Texture2D.cs @@ -86,11 +86,14 @@ namespace Unity_Studio //texgenpack private texgenpack_texturetype texturetype; + private int[] version; + public Texture2D(AssetPreloadData preloadData, bool readSwitch) { var sourceFile = preloadData.sourceFile; var a_Stream = preloadData.sourceFile.a_Stream; a_Stream.Position = preloadData.Offset; + version = sourceFile.version; if (sourceFile.platform == -2) { @@ -100,7 +103,7 @@ namespace Unity_Studio } m_Name = a_Stream.ReadAlignedString(a_Stream.ReadInt32()); - if (sourceFile.version[0] > 2017 || (sourceFile.version[0] == 2017 && sourceFile.version[1] >= 3))//2017.3 and up + if (version[0] > 2017 || (version[0] == 2017 && version[1] >= 3))//2017.3 and up { var m_ForcedFallbackFormat = a_Stream.ReadInt32(); var m_DownscaleFallback = a_Stream.ReadBoolean(); @@ -111,7 +114,7 @@ namespace Unity_Studio m_CompleteImageSize = a_Stream.ReadInt32(); m_TextureFormat = (TextureFormat)a_Stream.ReadInt32(); - if (sourceFile.version[0] < 5 || (sourceFile.version[0] == 5 && sourceFile.version[1] < 2)) + if (version[0] < 5 || (version[0] == 5 && version[1] < 2)) { m_MipMap = a_Stream.ReadBoolean(); } else { @@ -131,15 +134,15 @@ namespace Unity_Studio m_Aniso = a_Stream.ReadInt32(); m_MipBias = a_Stream.ReadSingle(); m_WrapMode = a_Stream.ReadInt32(); - if (sourceFile.version[0] >= 2017)//2017.x and up + if (version[0] >= 2017)//2017.x and up { int m_WrapV = a_Stream.ReadInt32(); int m_WrapW = a_Stream.ReadInt32(); } - if (sourceFile.version[0] >= 3) + if (version[0] >= 3) { m_LightmapFormat = a_Stream.ReadInt32(); - if (sourceFile.version[0] >= 4 || sourceFile.version[1] >= 5) { m_ColorSpace = a_Stream.ReadInt32(); } //3.5.0 and up + if (version[0] >= 4 || version[1] >= 5) { m_ColorSpace = a_Stream.ReadInt32(); } //3.5.0 and up } image_data_size = a_Stream.ReadInt32(); @@ -151,7 +154,7 @@ namespace Unity_Studio dwCaps += 0x400008; } - if (image_data_size == 0 && ((sourceFile.version[0] == 5 && sourceFile.version[1] >= 3) || sourceFile.version[0] > 5))//5.3.0 and up + if (image_data_size == 0 && ((version[0] == 5 && version[1] >= 3) || version[0] > 5))//5.3.0 and up { offset = a_Stream.ReadUInt32(); size = a_Stream.ReadUInt32(); diff --git a/Unity Studio/Unity Studio Classes/Texture2DConverter.cs b/Unity Studio/Unity Studio Classes/Texture2DConverter.cs index f3aff78..5068725 100644 --- a/Unity Studio/Unity Studio Classes/Texture2DConverter.cs +++ b/Unity Studio/Unity Studio Classes/Texture2DConverter.cs @@ -18,7 +18,10 @@ namespace Unity_Studio private static extern bool Ponvert(byte[] buffer, IntPtr bmp, int nWidth, int nHeight, int len, int type, int bmpsize, bool fixAlpha); [DllImport("crunch.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern bool DecompressCRN(byte[] pSrc_file_data, int src_file_size, out IntPtr dxtdata, out int dxtsize); + private static extern bool DecompressCRN(byte[] pSrc_file_data, int src_file_size, out IntPtr uncompressedData, out int uncompressedSize); + + [DllImport("crunchunity.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern bool DecompressUnityCRN(byte[] pSrc_file_data, int src_file_size, out IntPtr uncompressedData, out int uncompressedSize); [DllImport("texgenpack.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void texgenpackdecode(int texturetype, byte[] texturedata, int width, int height, IntPtr bmp, bool fixAlpha); @@ -318,13 +321,25 @@ namespace Unity_Studio private void DecompressCRN() { - if (DecompressCRN(image_data, image_data_size, out IntPtr dxtdata, out int dxtsize)) + IntPtr uncompressedData; + int uncompressedSize; + bool result; + if (version[0] > 2017 || (version[0] == 2017 && version[1] >= 3)) //2017.3 and up { - var dxtbytes = new byte[dxtsize]; - Marshal.Copy(dxtdata, dxtbytes, 0, dxtsize); - Marshal.FreeHGlobal(dxtdata); - image_data = dxtbytes; - image_data_size = dxtsize; + result = DecompressUnityCRN(image_data, image_data_size, out uncompressedData, out uncompressedSize); + } + else + { + result = DecompressCRN(image_data, image_data_size, out uncompressedData, out uncompressedSize); + } + + if (result) + { + var uncompressedBytes = new byte[uncompressedSize]; + Marshal.Copy(uncompressedData, uncompressedBytes, 0, uncompressedSize); + Marshal.FreeHGlobal(uncompressedData); + image_data = uncompressedBytes; + image_data_size = uncompressedSize; } }