Beispiel #1
0
        /// <summary>
        /// Get the specified asset from the pak file
        /// </summary>
        public Stream GetAsset(string id, string assetPath)
        {
            var pkg = this.m_configuration.Repository.GetRepository().Get(id, null);

            if (pkg == null)
            {
                throw new KeyNotFoundException($"Package {id} not found");
            }

            // Open the package
            var unpack      = pkg.Unpack();
            var assetObject = unpack.Assets.FirstOrDefault(o => o.Name == $"{assetPath}");

            if (assetObject == null)
            {
                throw new FileNotFoundException();
            }


            RestOperationContext.Current.OutgoingResponse.ContentType = assetObject.MimeType;
            byte[] content;
            if (assetObject.Content is byte[] bytea)
            {
                content = bytea;
            }
            else if (assetObject.Content is String stra)
            {
                content = System.Text.Encoding.UTF8.GetBytes(stra);
            }
            else if (assetObject.Content is XElement xela)
            {
                content = System.Text.Encoding.UTF8.GetBytes(xela.ToString());
            }
            else if (assetObject.Content is AppletAssetHtml html)
            {
                content = System.Text.Encoding.UTF8.GetBytes(html.Html.ToString());
            }
            else
            {
                throw new InvalidOperationException("Cannot render this type of data");
            }

            if (Encoding.UTF8.GetString(content as byte[], 0, 4) == "LZIP")
            {
                using (var ms = new MemoryStream(content as byte[]))
                    using (var ls = new SharpCompress.Compressors.LZMA.LZipStream(ms, SharpCompress.Compressors.CompressionMode.Decompress))
                    {
                        var oms = new MemoryStream();
                        ls.CopyTo(oms);
                        oms.Seek(0, SeekOrigin.Begin);
                        return(oms);
                    }
            }
            else
            {
                return(new MemoryStream(content));
            }
        }