Beispiel #1
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Please pass arguments in the form of \"./AssetPackager AssetFolderName outputfilename.dat\"");
                Console.WriteLine("If you're using custom asset loaders, use \"./AssetPackager AssetFolderName outputfilename.dat loaderlibrary.dll\"");
                Console.WriteLine("Aborting build.");
                return;
            }

            string inputFolder = args[0];
            string outputFile  = args[1];

            if (args.Length > 2)
            {
                string assemblyPath = args[2];
                AssetLoaderResolver.AddAssembly(LoadPlugin(assemblyPath));
            }

            AssetLoaderResolver.InitializeAndFindAssetWriters();

            if (!Directory.Exists(inputFolder))
            {
                Console.WriteLine("Please ensure that the input folder is a valid path.");
                Console.WriteLine("Aborting build.");
                return;
            }

            List <FileInfo>  inputFiles      = ExploreFolder(inputFolder);
            List <FileInfo>  supportedFiles  = new List <FileInfo>();
            HashSet <string> inUseAssetNames = new HashSet <string>();

            foreach (FileInfo inputFile in inputFiles)
            {
                if (AssetLoaderResolver.SupportedExtension(inputFile.Extension))
                {
                    Console.WriteLine($"Found file: {inputFile}.");
                    if (inUseAssetNames.Contains(AssetFileHelpers.GetAssetName(inputFile)))
                    {
                        Console.WriteLine($"Duplicate asset name found! {inputFile.FullName} collides with {supportedFiles.First(x => AssetFileHelpers.GetAssetName(x) == AssetFileHelpers.GetAssetName(inputFile)).FullName}");
                        Console.WriteLine("Aborting build.");
                        return;
                    }

                    supportedFiles.Add(inputFile);
                    inUseAssetNames.Add(AssetFileHelpers.GetAssetName(inputFile));
                }
                else
                {
                    Console.WriteLine($"Found unsupported file: {inputFile}.");
                }
            }

            Console.WriteLine("\n\nStarting build...\n\n");

            using FileStream outputFileStream = File.OpenWrite(outputFile);

            AssetPackageWriter.WriteAssetPackage(outputFileStream, supportedFiles, true);
        }
Beispiel #2
0
        public static void WriteAssetPackage(FileStream outputFileStream, IList <FileInfo> inputFiles,
                                             bool useCache)
        {
            using BinaryWriter headerWriter = new BinaryWriter(outputFileStream, Encoding.UTF8);

            //Write amount of assets
            headerWriter.Write(inputFiles.Count);
            headerWriter.Flush();

            foreach (FileInfo file in inputFiles)
            {
                var writer = AssetLoaderResolver.FindAssetWriter(file.Extension);

                var        metaFile = AssetFileHelpers.GetMetaFile(file);
                IAssetMeta meta     = new JsonAssetMeta();

                writer.GetDefaultMeta(meta);

                if (metaFile.Exists)
                {
                    using var fs = metaFile.OpenRead();
                    meta.ReadFromStream(fs);
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();

                Console.WriteLine($"Processing file: {file}.");

                using MemoryStream uncompressedOutput = new MemoryStream();
                using MemoryStream compressedOutput   = new MemoryStream();

                if (AssetCacher.HasCachedFile(file, meta) && useCache)
                {
                    AssetCacher.LoadCachedFile(file, meta, uncompressedOutput);
                    Console.WriteLine("Cached found!");
                }
                else
                {
                    Console.WriteLine("No cached asset found");
                    writer.LoadAndWriteToStream(file, meta, uncompressedOutput);
                    AssetCacher.SaveCachedFile(file, meta, uncompressedOutput);
                }

                Console.WriteLine($"Processing took: {watch.ElapsedMilliseconds} milliseconds.");

                watch.Restart();
                Console.WriteLine("Compressing...");

                using DeflateStream deflate = new DeflateStream(compressedOutput, CompressionLevel.Optimal, true);

                uncompressedOutput.Seek(0, SeekOrigin.Begin);
                uncompressedOutput.CopyTo(deflate);
                deflate.Flush();

                string      assetName = AssetFileHelpers.GetAssetName(file);
                System.Type assetType = writer.GetAssetType();

                long length           = uncompressedOutput.Length;
                long compressedLength = compressedOutput.Length;

                AssetHeader.WriteAssetHeader(headerWriter, assetName, assetType, AssetCompression.Deflate, length, compressedLength);
                compressedOutput.Seek(0, SeekOrigin.Begin);
                compressedOutput.CopyTo(outputFileStream);

                watch.Stop();
                Console.WriteLine($"Compression took: {watch.ElapsedMilliseconds} milliseconds.");
            }
        }