Пример #1
0
            public void Initialize(string buildReportPath, string productName, string companyName, string gameIdentifier, string gameFrameworkVersion, string unityVersion, string applicableGameVersion, int internalResourceVersion,
                                   Platform platforms, AssetBundleCompressionType assetBundleCompression, string compressionHelperTypeName, bool additionalCompressionSelected, bool forceRebuildAssetBundleSelected, string buildEventHandlerTypeName, string outputDirectory, BuildAssetBundleOptions buildAssetBundleOptions, SortedDictionary <string, ResourceData> resourceDatas)
            {
                if (string.IsNullOrEmpty(buildReportPath))
                {
                    throw new GameFrameworkException("Build report path is invalid.");
                }

                m_BuildReportName         = Utility.Path.GetRegularPath(Path.Combine(buildReportPath, BuildReportName));
                m_BuildLogName            = Utility.Path.GetRegularPath(Path.Combine(buildReportPath, BuildLogName));
                m_ProductName             = productName;
                m_CompanyName             = companyName;
                m_GameIdentifier          = gameIdentifier;
                m_GameFrameworkVersion    = gameFrameworkVersion;
                m_UnityVersion            = unityVersion;
                m_ApplicableGameVersion   = applicableGameVersion;
                m_InternalResourceVersion = internalResourceVersion;
                m_Platforms = platforms;
                m_AssetBundleCompression          = assetBundleCompression;
                m_CompressionHelperTypeName       = compressionHelperTypeName;
                m_AdditionalCompressionSelected   = additionalCompressionSelected;
                m_ForceRebuildAssetBundleSelected = forceRebuildAssetBundleSelected;
                m_BuildEventHandlerTypeName       = buildEventHandlerTypeName;
                m_OutputDirectory         = outputDirectory;
                m_BuildAssetBundleOptions = buildAssetBundleOptions;
                m_LogBuilder    = new StringBuilder();
                m_ResourceDatas = resourceDatas;
            }
Пример #2
0
 private void CompressBundle(BundleFileInstance bundleInst, string path, AssetBundleCompressionType compType)
 {
     using (FileStream fs = File.OpenWrite(path))
         using (AssetsFileWriter w = new AssetsFileWriter(fs))
         {
             bundleInst.file.Pack(bundleInst.file.reader, w, compType);
         }
 }
Пример #3
0
        private async Task AskForLocationAndCompress()
        {
            if (bundleInst != null)
            {
                //temporary, maybe I should just write to a memory stream or smth
                //edit: looks like uabe just asks you to open a file instead of
                //using your currently opened one, so that may be the workaround
                if (changesMade)
                {
                    ButtonResult continueWithChanges = await MessageBoxUtil.ShowDialog(
                        this, "Note", "You've modified this file, but only file before changes is loaded. If you want to compress the file with " +
                        "changes, please close this bundle and open the new file. Click Ok to compress the file without changes.",
                        ButtonEnum.OkCancel);

                    if (continueWithChanges == ButtonResult.Cancel)
                    {
                        return;
                    }
                }

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "Save as...";

                string file = await sfd.ShowAsync(this);

                if (file == null)
                {
                    return;
                }

                const string lz4Option    = "LZ4";
                const string lzmaOption   = "LZMA";
                const string cancelOption = "Cancel";
                string       result       = await MessageBoxUtil.ShowDialogCustom(
                    this, "Note", "What compression method do you want to use?\nLZ4: Faster but larger size\nLZMA: Slower but smaller size",
                    lz4Option, lzmaOption, cancelOption);

                AssetBundleCompressionType compType = result switch
                {
                    lz4Option => AssetBundleCompressionType.LZ4,
                    lzmaOption => AssetBundleCompressionType.LZMA,
                    _ => AssetBundleCompressionType.NONE
                };

                if (compType != AssetBundleCompressionType.NONE)
                {
                    CompressBundle(bundleInst, file, compType);
                }
            }
            else
            {
                await MessageBoxUtil.ShowDialog(this, "Note", "Please open a bundle file before using compress.");
            }
        }
Пример #4
0
        public void OnPreprocessAllPlatforms(string productName, string companyName, string gameIdentifier, string gameFrameworkVersion, string unityVersion, string applicableGameVersion, int internalResourceVersion,
                                             Platform platforms, AssetBundleCompressionType assetBundleCompression, string compressionHelperTypeName, bool additionalCompressionSelected, bool forceRebuildAssetBundleSelected, string buildEventHandlerTypeName, string outputDirectory, BuildAssetBundleOptions buildAssetBundleOptions,
                                             string workingPath, bool outputPackageSelected, string outputPackagePath, bool outputFullSelected, string outputFullPath, bool outputPackedSelected, string outputPackedPath, string buildReportPath)
        {
            string streamingAssetsPath = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "StreamingAssets"));

            string[] fileNames = Directory.GetFiles(streamingAssetsPath, "*", SearchOption.AllDirectories);
            foreach (string fileName in fileNames)
            {
                if (fileName.Contains(".gitkeep"))
                {
                    continue;
                }

                File.Delete(fileName);
            }

            Utility.Path.RemoveEmptyDirectory(streamingAssetsPath);
        }
Пример #5
0
        public bool Pack(AssetsFileReader reader, AssetsFileWriter writer, AssetBundleCompressionType compType)
        {
            reader.Position = 0;
            writer.Position = 0;
            if (Read(reader, false))
            {
                AssetBundleHeader06 newHeader = new AssetBundleHeader06()
                {
                    signature         = bundleHeader6.signature,
                    fileVersion       = bundleHeader6.fileVersion,
                    minPlayerVersion  = bundleHeader6.minPlayerVersion,
                    fileEngineVersion = bundleHeader6.fileEngineVersion,
                    totalFileSize     = 0,
                    compressedSize    = 0,
                    decompressedSize  = 0,
                    flags             = 0x43
                };

                AssetBundleBlockAndDirectoryList06 newBlockAndDirList = new AssetBundleBlockAndDirectoryList06()
                {
                    checksumLow    = 0,
                    checksumHigh   = 0,
                    blockCount     = 0,
                    blockInf       = null,
                    directoryCount = bundleInf6.directoryCount,
                    dirInf         = bundleInf6.dirInf
                };

                List <AssetBundleBlockInfo06> newBlocks = new List <AssetBundleBlockInfo06>();

                reader.Position = bundleHeader6.GetFileDataOffset();
                int    fileDataLength = (int)(bundleHeader6.totalFileSize - reader.Position);
                byte[] fileData       = reader.ReadBytes(fileDataLength);

                //todo, we just write everything to memory and then write to file
                //we could calculate the blocks we need ahead of time and correctly
                //size the block listing before this so we can write directly to file
                byte[] compressedFileData;
                switch (compType)
                {
                case AssetBundleCompressionType.LZMA:
                {
                    compressedFileData = SevenZipHelper.Compress(fileData);
                    newBlocks.Add(new AssetBundleBlockInfo06()
                        {
                            compressedSize   = (uint)compressedFileData.Length,
                            decompressedSize = (uint)fileData.Length,
                            flags            = 0x41
                        });
                    break;
                }

                case AssetBundleCompressionType.LZ4:
                {
                    using (var memStreamCom = new MemoryStream())
                        using (var binaryWriter = new BinaryWriter(memStreamCom))
                        {
                            using (var memStreamUnc = new MemoryStream(fileData))
                                using (var binaryReader = new BinaryReader(memStreamUnc))
                                {
                                    //compress into 0x20000 blocks
                                    byte[] uncompressedBlock = binaryReader.ReadBytes(131072);
                                    while (uncompressedBlock.Length != 0)
                                    {
                                        byte[] compressedBlock = LZ4Codec.Encode32HC(uncompressedBlock, 0, uncompressedBlock.Length);

                                        if (compressedBlock.Length > uncompressedBlock.Length)
                                        {
                                            newBlocks.Add(new AssetBundleBlockInfo06()
                                                {
                                                    compressedSize   = (uint)uncompressedBlock.Length,
                                                    decompressedSize = (uint)uncompressedBlock.Length,
                                                    flags            = 0x0
                                                });
                                            binaryWriter.Write(uncompressedBlock);
                                        }
                                        else
                                        {
                                            newBlocks.Add(new AssetBundleBlockInfo06()
                                                {
                                                    compressedSize   = (uint)compressedBlock.Length,
                                                    decompressedSize = (uint)uncompressedBlock.Length,
                                                    flags            = 0x3
                                                });
                                            binaryWriter.Write(compressedBlock);
                                        }

                                        uncompressedBlock = binaryReader.ReadBytes(131072);
                                    }
                                }

                            compressedFileData = memStreamCom.ToArray();
                        }
                    break;
                }

                case AssetBundleCompressionType.NONE:
                {
                    compressedFileData = fileData;
                    newBlocks.Add(new AssetBundleBlockInfo06()
                        {
                            compressedSize   = (uint)fileData.Length,
                            decompressedSize = (uint)fileData.Length,
                            flags            = 0x00
                        });
                    break;
                }

                default:
                {
                    return(false);
                }
                }

                newBlockAndDirList.blockInf = newBlocks.ToArray();

                byte[] bundleInfoBytes;
                using (var memStream = new MemoryStream())
                {
                    var afw = new AssetsFileWriter(memStream);
                    newBlockAndDirList.Write(afw);
                    bundleInfoBytes = memStream.ToArray();
                }

                if (bundleInfoBytes == null || bundleInfoBytes.Length == 0)
                {
                    return(false);
                }

                //listing is usually lz4 even if the data blocks are lzma
                byte[] bundleInfoBytesCom = LZ4Codec.Encode32HC(bundleInfoBytes, 0, bundleInfoBytes.Length);

                byte[] bundleHeaderBytes = null;
                using (var memStream = new MemoryStream())
                {
                    var afw = new AssetsFileWriter(memStream);
                    newHeader.Write(afw);
                    bundleHeaderBytes = memStream.ToArray();
                }

                if (bundleHeaderBytes == null || bundleHeaderBytes.Length == 0)
                {
                    return(false);
                }

                uint totalFileSize = (uint)(bundleHeaderBytes.Length + bundleInfoBytesCom.Length + compressedFileData.Length);
                newHeader.totalFileSize    = totalFileSize;
                newHeader.decompressedSize = (uint)bundleInfoBytes.Length;
                newHeader.compressedSize   = (uint)bundleInfoBytesCom.Length;

                newHeader.Write(writer);
                if (newHeader.fileVersion >= 7)
                {
                    writer.Align16();
                }

                writer.Write(bundleInfoBytesCom);
                writer.Write(compressedFileData);

                return(true);
            }
            return(false);
        }
Пример #6
0
 public void OnPostprocessAllPlatforms(string productName, string companyName, string gameIdentifier, string gameFrameworkVersion, string unityVersion, string applicableGameVersion, int internalResourceVersion,
                                       Platform platforms, AssetBundleCompressionType assetBundleCompression, string compressionHelperTypeName, bool additionalCompressionSelected, bool forceRebuildAssetBundleSelected, string buildEventHandlerTypeName, string outputDirectory, BuildAssetBundleOptions buildAssetBundleOptions,
                                       string workingPath, bool outputPackageSelected, string outputPackagePath, bool outputFullSelected, string outputFullPath, bool outputPackedSelected, string outputPackedPath, string buildReportPath)
 {
 }
Пример #7
0
 private static void CompressBundle(BundleFileInstance bundleInst, string path, AssetBundleCompressionType compType)
 {
     using var fs     = File.OpenWrite(path);
     using var writer = new AssetsFileWriter(fs);
     bundleInst.file.Pack(bundleInst.file.reader, writer, compType);
 }
Пример #8
0
        public bool Pack(AssetsFileReader reader, AssetsFileWriter writer, AssetBundleCompressionType compType, bool blockDirAtEnd = true)
        {
            reader.Position = 0;
            writer.Position = 0;
            if (Read(reader, false))
            {
                AssetBundleHeader06 newHeader = new AssetBundleHeader06()
                {
                    signature         = bundleHeader6.signature,
                    fileVersion       = bundleHeader6.fileVersion,
                    minPlayerVersion  = bundleHeader6.minPlayerVersion,
                    fileEngineVersion = bundleHeader6.fileEngineVersion,
                    totalFileSize     = 0,
                    compressedSize    = 0,
                    decompressedSize  = 0,
                    flags             = (uint)(0x43 | (blockDirAtEnd ? 0x80 : 0x00))
                };

                AssetBundleBlockAndDirectoryList06 newBlockAndDirList = new AssetBundleBlockAndDirectoryList06()
                {
                    checksumLow    = 0,
                    checksumHigh   = 0,
                    blockCount     = 0,
                    blockInf       = null,
                    directoryCount = bundleInf6.directoryCount,
                    dirInf         = bundleInf6.dirInf
                };

                //write header now and overwrite it later
                long startPos = writer.Position;

                newHeader.Write(writer);
                if (newHeader.fileVersion >= 7)
                {
                    writer.Align16();
                }

                int headerSize = (int)(writer.Position - startPos);

                long totalCompressedSize = 0;
                List <AssetBundleBlockInfo06> newBlocks = new List <AssetBundleBlockInfo06>();
                List <Stream> newStreams = new List <Stream>(); //used if blockDirAtEnd == false

                long fileDataOffset = bundleHeader6.GetFileDataOffset();
                int  fileDataLength = (int)(bundleHeader6.totalFileSize - fileDataOffset);

                SegmentStream bundleDataStream = new SegmentStream(reader.BaseStream, fileDataOffset, fileDataLength);

                switch (compType)
                {
                case AssetBundleCompressionType.LZMA:
                {
                    Stream writeStream;
                    if (blockDirAtEnd)
                    {
                        writeStream = writer.BaseStream;
                    }
                    else
                    {
                        writeStream = GetTempFileStream();
                    }

                    long writeStreamStart = writeStream.Position;
                    SevenZipHelper.Compress(bundleDataStream, writeStream);
                    uint writeStreamLength = (uint)(writeStream.Position - writeStreamStart);

                    AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
                    {
                        compressedSize   = writeStreamLength,
                        decompressedSize = (uint)fileDataLength,
                        flags            = 0x41
                    };

                    totalCompressedSize += blockInfo.compressedSize;
                    newBlocks.Add(blockInfo);

                    if (!blockDirAtEnd)
                    {
                        newStreams.Add(writeStream);
                    }

                    break;
                }

                case AssetBundleCompressionType.LZ4:
                {
                    //compress into 0x20000 blocks
                    BinaryReader bundleDataReader  = new BinaryReader(bundleDataStream);
                    byte[]       uncompressedBlock = bundleDataReader.ReadBytes(0x20000);
                    while (uncompressedBlock.Length != 0)
                    {
                        Stream writeStream;
                        if (blockDirAtEnd)
                        {
                            writeStream = writer.BaseStream;
                        }
                        else
                        {
                            writeStream = GetTempFileStream();
                        }

                        byte[] compressedBlock = LZ4Codec.Encode32HC(uncompressedBlock, 0, uncompressedBlock.Length);

                        if (compressedBlock.Length > uncompressedBlock.Length)
                        {
                            writeStream.Write(uncompressedBlock, 0, uncompressedBlock.Length);

                            AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
                            {
                                compressedSize   = (uint)uncompressedBlock.Length,
                                decompressedSize = (uint)uncompressedBlock.Length,
                                flags            = 0x0
                            };

                            totalCompressedSize += blockInfo.compressedSize;

                            newBlocks.Add(blockInfo);
                        }
                        else
                        {
                            writeStream.Write(compressedBlock, 0, compressedBlock.Length);

                            AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
                            {
                                compressedSize   = (uint)compressedBlock.Length,
                                decompressedSize = (uint)uncompressedBlock.Length,
                                flags            = 0x3
                            };

                            totalCompressedSize += blockInfo.compressedSize;

                            newBlocks.Add(blockInfo);
                        }

                        if (!blockDirAtEnd)
                        {
                            newStreams.Add(writeStream);
                        }

                        uncompressedBlock = bundleDataReader.ReadBytes(0x20000);
                    }
                    break;
                }

                case AssetBundleCompressionType.NONE:
                {
                    AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
                    {
                        compressedSize   = (uint)fileDataLength,
                        decompressedSize = (uint)fileDataLength,
                        flags            = 0x00
                    };

                    totalCompressedSize += blockInfo.compressedSize;

                    newBlocks.Add(blockInfo);

                    if (blockDirAtEnd)
                    {
                        bundleDataStream.CopyToCompat(writer.BaseStream);
                    }
                    else
                    {
                        newStreams.Add(bundleDataStream);
                    }

                    break;
                }
                }

                newBlockAndDirList.blockInf = newBlocks.ToArray();

                byte[] bundleInfoBytes;
                using (MemoryStream memStream = new MemoryStream())
                {
                    AssetsFileWriter infoWriter = new AssetsFileWriter(memStream);
                    newBlockAndDirList.Write(infoWriter);
                    bundleInfoBytes = memStream.ToArray();
                }

                //listing is usually lz4 even if the data blocks are lzma
                byte[] bundleInfoBytesCom = LZ4Codec.Encode32HC(bundleInfoBytes, 0, bundleInfoBytes.Length);

                long totalFileSize = headerSize + bundleInfoBytesCom.Length + totalCompressedSize;
                newHeader.totalFileSize    = totalFileSize;
                newHeader.decompressedSize = (uint)bundleInfoBytes.Length;
                newHeader.compressedSize   = (uint)bundleInfoBytesCom.Length;

                if (!blockDirAtEnd)
                {
                    writer.Write(bundleInfoBytesCom);
                    foreach (Stream newStream in newStreams)
                    {
                        newStream.Position = 0;
                        newStream.CopyToCompat(writer.BaseStream);
                        newStream.Close();
                    }
                }
                else
                {
                    writer.Write(bundleInfoBytesCom);
                }

                writer.Position = 0;
                newHeader.Write(writer);
                if (newHeader.fileVersion >= 7)
                {
                    writer.Align16();
                }

                return(true);
            }
            return(false);
        }