Example #1
0
        public void CreateAndLoadPackage()
        {
            // Specify the header information
            AssetPackageHeader header = new AssetPackageHeader()
            {
                PackagePath = "test.zap", // The package path and file name
                Assets = new List<AssetMeta>() {
                    new AssetMeta()
                    {
                        Path = "test.txt" // Path to a file on disk
                    },
                }
            };

            // Write asset package to disk
            PackageManager.WritePackage(header);

            // Read asset package headers to the asset manager asset registry
            PackageManager.AddPackage("test.zap");

            // Get a stream to an asset
            var stream = PackageManager.GetStream("test.txt");

            Assert.IsNotNull(stream, "The stream returned should not be null.");

            // Read the asset from the stream
            string text;
            using (StreamReader reader = new StreamReader(stream))
            {
                text = reader.ReadToEnd();
            }

            Assert.AreEqual(testContents, text, "The string read from the file should be equal to the written string.");
        }
Example #2
0
 /// <summary>
 /// Adds a package to the AssetManager package registry. Allows assets to be loaded from the packages.
 /// </summary>
 /// <param name="path">The path to the package inside BasePath.</param>
 public static void AddPackage(string path)
 {
     // Create stream from the package file
     using (var package = GetStream(path))
     {
         using (var processed = SetPackageReadProcessors(package))
         {
             // Read header
             package.Seek(0, SeekOrigin.Begin);
             AssetPackageHeader header = new AssetPackageHeader();
             header.Read(processed);
             foreach (var item in header.Assets)
             {
                 item.Package = path;
             }
             AddPackage(header);
         }
     }
 }
Example #3
0
        public void CompressedPackage()
        {
            // Specify the header information
            AssetPackageHeader header = new AssetPackageHeader()
            {
                PackagePath = "test.zap", // The package path and file name
                Assets = new List<AssetMeta>() {
                    new AssetMeta()
                    {
                        Path = "test.txt" // Path to a file on disk
                    },
                }
            };

            // Insert asset data compression processor to the asset manager
            PackageManager.InsertAssetProcessor(new AssetDataCompressionProcessor(System.IO.Compression.CompressionLevel.Optimal));

            // Build asset package to disk from asset files on disk
            PackageManager.WritePackage(header);

            // Read asset package headers to the asset manager asset registry
            PackageManager.AddPackage("test.zap");

            // Get a stream to an asset
            var stream = PackageManager.GetStream("test.txt");

            Assert.IsNotNull(stream, "The stream returned should not be null.");

            // Read the asset from the stream
            string text;
            using (StreamReader reader = new StreamReader(stream))
            {
                text = reader.ReadToEnd();
            }

            Assert.AreEqual(testContents, text, "The string read from the file should be equal to the written string.");
        }
Example #4
0
 public static void AddPackage(AssetPackageHeader header)
 {
     // Insert assets to the dictionary
     foreach (var asset in header.Assets)
     {
         assetPaths.Add(asset.Path, asset);
     }
 }
Example #5
0
        /// <summary>
        /// Writes a package file using the header information
        /// </summary>
        /// <param name="header"></param>
        public static void WritePackage(AssetPackageHeader header)
        {
            var path = Path.Combine(appPath, basePath, header.PackagePath);

            Stream package = File.Create(path);

            using(package = SetPackageWriteProcessors(package))
            {
                // Write file paths and placeholder sizes and positions so that we can start writing the files themselves.
                header.Write(package);

                foreach (var asset in header.Assets)
                {
                    // Get the asset full path in file system
                    var assetPath = Path.Combine(appPath, basePath, asset.Path);

                    using(MemoryStream ms = new MemoryStream())
                    {
                        // Set asset position before write
                        asset.Position = package.Position;

                        // Process them bytes, nao!
                        using(var assetStream = SetAssetWriteProcessors(ms))
                        {
                            var bytes = File.ReadAllBytes(assetPath);
                            assetStream.Write(bytes, 0, bytes.Length);
                        }

                        // Get processed bytes and write them to the file
                        var processedBytes = ms.ToArray();
                        package.Write(processedBytes, 0, processedBytes.Length);

                        // Set asset size
                        asset.Size = processedBytes.Length;
                    }
                }

                // Return to the start of the file
                package.Seek(0, SeekOrigin.Begin);

                // Rewrite file header with the correct sizes and positions
                header.Write(package);
            }
        }