private async Task <ByteArrayReader> decompressChunks() { int start = CompressedChunks.Min(ch => ch.UncompressedOffset); int totalSize = CompressedChunks.SelectMany(ch => ch.Header.Blocks).Aggregate(start, (total, block) => total + block.UncompressedSize); byte[] data = new byte[totalSize]; foreach (DomainCompressedChunk chunk in CompressedChunks) { byte[] chunkData = new byte[chunk.Header.Blocks.Sum(block => block.UncompressedSize)]; int uncompressedOffset = 0; foreach (DomainCompressedChunkBlock block in chunk.Header.Blocks) { if (((CompressionTypes)CompressionFlags & CompressionTypes.LZO_ENC) > 0) { await block.CompressedData.Decrypt(); } byte[] decompressed = await block.CompressedData.Decompress(block.UncompressedSize); int offset = uncompressedOffset; await Task.Run(() => Array.ConstrainedCopy(decompressed, 0, chunkData, offset, block.UncompressedSize)); uncompressedOffset += block.UncompressedSize; } await Task.Run(() => Array.ConstrainedCopy(chunkData, 0, data, chunk.UncompressedOffset, chunk.Header.UncompressedSize)); } return(ByteArrayReader.CreateNew(data, start)); }
public override async Task SetObject(string filename, List <DomainNameTableEntry> nameTable, object configuration) { this.Modified = true; Font = File.ReadAllBytes(filename); FontLength = BitConverter.GetBytes(Font.Length); ByteArrayReader FontReader = ByteArrayReader.CreateNew(FontLength.Concat(Font).ToArray(), 0); PropertyHeader.GetProperty("Buffer").FirstOrDefault().Value.SetPropertyValue(FontReader); PropertyHeader.GetProperty("Buffer").FirstOrDefault().Value.GetBuilderSize(); // GlyphTypeface ttf = new GlyphTypeface(new Uri(filename)); // PropertyHeader.GetProperty("FontFamilyNameArray").First().Value.SetPropertyValue(new DomainString("Lovely Creamy")); // PropertyHeader.GetProperty("FontStyleNameArray").First().Value.SetPropertyValue(new DomainString("Lovely Creamy")); // PropertyHeader.GetProperty("FontFullNameArray").First().Value.SetPropertyValue(new DomainString("Lovely Creamy")); var FontFamilyName = (DomainPropertyNameArray)PropertyHeader.GetProperty("FontFamilyNameArray").First().Value; var FontStyleName = (DomainPropertyNameArray)PropertyHeader.GetProperty("FontStyleNameArray").First().Value; var FontFullName = (DomainPropertyNameArray)PropertyHeader.GetProperty("FontFullNameArray").First().Value; // string FontFamilyName = ttf.Win32FamilyNames.Values.FirstOrDefault(); // string FontStyleNamee = ttf.Win32FaceNames.Values.FirstOrDefault(); Debug.WriteLine("hi2"); }
public async Task <DomainHeader> LoadUpkFile(string Filename) { byte[] data = await Task.Run(() => File.ReadAllBytes(Filename)); ByteArrayReader reader = ByteArrayReader.CreateNew(data, 0); DomainHeader header = new DomainHeader(reader) { FullFilename = Filename, FileSize = data.LongLength }; return(header); }
public async Task <int> BuildCompressedChunkBlockData(ByteArrayReader reader) { UncompressedSize = reader.Remaining; byte[] compressed = await reader.Compress(); CompressedData = ByteArrayReader.CreateNew(compressed, 0); await CompressedData.Encrypt(); // TODO: Fix this to use the flag CompressedSize = CompressedData.Remaining; return(CompressedSize + sizeof(int) * 2); }
public async Task <ByteArrayReader> DecompressChunk(uint flags) { const BulkDataCompressionTypes nothingTodo = BulkDataCompressionTypes.Unused | BulkDataCompressionTypes.StoreInSeparatefile; if (((BulkDataCompressionTypes)BulkDataFlags & nothingTodo) > 0) { return(null); } byte[] chunkData = new byte[Header.Blocks.Sum(block => block.UncompressedSize)]; int uncompressedOffset = 0; foreach (DomainCompressedChunkBlock block in Header.Blocks) { if (((BulkDataCompressionTypes)BulkDataFlags & BulkDataCompressionTypes.LZO_ENC) > 0) { await block.CompressedData.Decrypt(); } byte[] decompressed; const BulkDataCompressionTypes validCompression = BulkDataCompressionTypes.LZO | BulkDataCompressionTypes.LZO_ENC; if (((BulkDataCompressionTypes)BulkDataFlags & validCompression) > 0) { decompressed = await block.CompressedData.Decompress(block.UncompressedSize); } else { if (BulkDataFlags == 0) { decompressed = block.CompressedData.GetBytes(); } else { throw new Exception($"Unsupported bulk data compression type 0x{BulkDataFlags:X8}"); } } int offset = uncompressedOffset; await Task.Run(() => Array.ConstrainedCopy(decompressed, 0, chunkData, offset, block.UncompressedSize)); uncompressedOffset += block.UncompressedSize; } return(ByteArrayReader.CreateNew(chunkData, 0)); }
public override int GetBuilderSize() { BuilderSize = PropertyHeader.GetBuilderSize() + base.GetBuilderSize() + sizeof(int); foreach (byte[] Sound in Sounds) { BulkDataCompressionTypes flags = Sound == null || Sound.Length == 0 ? BulkDataCompressionTypes.Unused | BulkDataCompressionTypes.StoreInSeparatefile : BulkDataCompressionTypes.LZO_ENC; BuilderSize += Task.Run(() => ProcessUncompressedBulkData(ByteArrayReader.CreateNew(Sound, 0), flags)).Result + sizeof(int) * 2; } return(BuilderSize); }
public override int GetBuilderSize() { BuilderSize = PropertyHeader.GetBuilderSize() + base.GetBuilderSize() + sizeof(int); foreach (DomainMipMap mipMap in MipMaps) { BulkDataCompressionTypes flags = mipMap.ImageData == null || mipMap.ImageData.Length == 0 ? BulkDataCompressionTypes.Unused | BulkDataCompressionTypes.StoreInSeparatefile : BulkDataCompressionTypes.LZO_ENC; BuilderSize += Task.Run(() => ProcessUncompressedBulkData(ByteArrayReader.CreateNew(mipMap.ImageData, 0), flags)).Result + sizeof(int) * 2; } BuilderSize += Guid.Length; return(BuilderSize); }
public override async Task SetObject(string filename, List <DomainNameTableEntry> nameTable) { ImageEngineImage image = await Task.Run(() => new ImageEngineImage(filename)); int width = image.Width; int height = image.Height; DomainPropertyIntValue sizeX = PropertyHeader.GetProperty("SizeX").FirstOrDefault()?.Value as DomainPropertyIntValue; DomainPropertyIntValue sizeY = PropertyHeader.GetProperty("SizeY").FirstOrDefault()?.Value as DomainPropertyIntValue; sizeX?.SetPropertyValue(width); sizeY?.SetPropertyValue(height); DomainPropertyIntValue mipTailBaseIdx = PropertyHeader.GetProperty("MipTailBaseIdx").FirstOrDefault()?.Value as DomainPropertyIntValue; mipTailBaseIdx?.SetPropertyValue((int)Math.Log(width > height ? width : height, 2)); DomainPropertyStringValue filePath = PropertyHeader.GetProperty("SourceFilePath").FirstOrDefault()?.Value as DomainPropertyStringValue; DomainPropertyStringValue fileTime = PropertyHeader.GetProperty("SourceFileTimestamp").FirstOrDefault()?.Value as DomainPropertyStringValue; filePath?.SetPropertyValue(filename); fileTime?.SetPropertyValue(File.GetLastWriteTime(filename).ToString("yyyy-MM-dd hh:mm:ss")); DomainPropertyByteValue pfFormat = PropertyHeader.GetProperty("Format").FirstOrDefault()?.Value as DomainPropertyByteValue; ImageEngineFormat imageFormat = image.Format.InternalFormat; if (!imageFormat.ToString().Contains("DDS")) { throw new Exception($"Image is not in a DDS format. It is actually {imageFormat}."); } if (pfFormat != null) { string formatStr = imageFormat.ToString().Replace("DDS", "PF"); if (formatStr.Contains("ARGB")) { formatStr = "PF_A8R8G8B8"; } else if (formatStr.Contains("G8")) { formatStr = "PF_G8"; } DomainNameTableEntry formatTableEntry = nameTable.SingleOrDefault(nt => nt.Name.String == formatStr) ?? nameTable.AddDomainNameTableEntry(formatStr); pfFormat.SetPropertyValue(formatTableEntry); } MipMaps.Clear(); while (true) { MemoryStream stream = new MemoryStream(); image.Save(stream, imageFormat, MipHandling.KeepTopOnly); await stream.FlushAsync(); MipMaps.Add(new DomainMipMap { ImageData = (await ByteArrayReader.CreateNew(stream.ToArray(), 0x80).Splice()).GetBytes(), // Strip off 128 bytes for the DDS header Width = image.Width, Height = image.Height }); if (width == 1 && height == 1) { break; } if (width > 1) { width /= 2; } if (height > 1) { height /= 2; } if (image.Width > 4 && image.Height > 4) { image.Resize(0.5, false); } } }