private static void CompressRom_new(Stream rom, DmaData dmadata, List <int> exclusions, Stream sw) { BinaryReader br = new BinaryReader(rom); MemoryStream outRom = new MemoryStream(0x200_0000); List <FileRecord> newDmaTable = new List <FileRecord>(); Console.WriteLine(); int cur = 0; for (int i = 0; i < dmadata.Table.Count; i++) { FileRecord record = dmadata.Table[i]; if (record.VRom.End == 0) { var r = new FileRecord(new FileAddress(), new FileAddress()); newDmaTable.Add(r); break; } br.BaseStream.Position = record.Rom.Start; byte[] data = br.ReadBytes(record.VRom.Size); int dstsize; FileAddress physical; if (!exclusions.Contains(i)) { dstsize = Yaz.Encode(data, data.Length, outRom); dstsize = Align.To16(dstsize); physical = new FileAddress(cur, cur + dstsize); } else { dstsize = data.Length; dstsize = Align.To16(dstsize); outRom.Write(data, 0, data.Length); physical = new FileAddress(cur, 0); exclusions.Remove(i); } var newRec = new FileRecord(record.VRom, physical); newDmaTable.Add(newRec); cur += dstsize; outRom.Position = cur; } br.Close(); WriteFileTable(outRom, dmadata.Address.VRom, newDmaTable); CRC.Write(outRom); outRom.Position = 0; outRom.CopyTo(sw); }
private static void CompressRom(Rom rom, Dictionary <long, FileRecord> refFileList, Stream sw) { int filesProcessed = 0; int filesTotal; List <FileRecord> newDmaTable = new List <FileRecord>(); Console.WriteLine(); foreach (FileRecord record in rom.Files) { Stream outstream; bool IsCompressed = false; filesTotal = rom.Files.Count(); //get file outstream = rom.Files.GetFile(record.VRom.Start); //Compress if the file can't be found in the don't compress list, //or if the file listed should be compressed (legacy) if (!refFileList.ContainsKey(record.VRom.Start) || refFileList[record.VRom.Start].IsCompressed) { //compress file IsCompressed = true; MemoryStream ms = new MemoryStream(); using (BinaryReader br = new BinaryReader(outstream)) { byte[] data = br.ReadBytes(record.VRom.Size); Yaz.Encode(data, data.Length, ms); ms.Position = 0; } outstream = ms; } //data contains the data to write to rom AppendFile(sw, newDmaTable, outstream, record, IsCompressed); //writes progress to console window filesProcessed++; Console.Write(processedFiles, filesProcessed, filesTotal); } sw.PadToLength(0x2000000); WriteFileTable(sw, rom.Files.GetDmaDataStart(), newDmaTable); CRC.Write(sw); }
public static void DecompressRom(Rom compressedRom, Stream sw) { List <FileRecord> newDmaTable = new List <FileRecord>(); int filesProcessed = 0; int filesTotal; filesTotal = compressedRom.Files.Count(); //build the new dmadata table foreach (FileRecord record in compressedRom.Files) { FileAddress vrom = record.VRom; //current address //MM Check for empty file int start = (record.Rom.Start == -1) ? -1 : vrom.Start; FileRecord rec = new FileRecord(vrom, new FileAddress(start, 0)); newDmaTable.Add(rec); } //write the decompressed files to the stream foreach (FileRecord record in compressedRom.Files.OrderBy(x => x.VRom.Start)) { FileAddress vrom = record.VRom; //current address sw.PadToLength(vrom.Start); //If file exists (supports MM) if (record.Rom.Start != -1) { var file = compressedRom.Files.GetFile(vrom); ((Stream)file).CopyTo(sw); } //writes progress to console window filesProcessed++; Console.Write(processedFiles, filesProcessed, filesTotal); } sw.PadToLength(0x4000000); WriteFileTable(sw, compressedRom.Files.GetDmaDataStart(), newDmaTable); CRC.Write(sw); }
public static void CompressRomOptimized(ORom uncompressedRom, ORom refRom, Stream sw) { int filesProcessed = 0; int filesTotal; List <FileRecord> newDmaTable = new List <FileRecord>(); filesTotal = uncompressedRom.Files.Count(); foreach (FileRecord record in uncompressedRom.Files) { Stream outstream = GetFile_CompressedOptimized(uncompressedRom, refRom, record, out bool IsCompressed); AppendFile(sw, newDmaTable, outstream, record, IsCompressed); //writes progress to console window filesProcessed++; Console.Write(processedFiles, filesProcessed, filesTotal); } sw.PadToLength(0x2000000); WriteFileTable(sw, uncompressedRom.Files.GetDmaDataStart(), newDmaTable); CRC.Write(sw); }
public static void DecompressRom(Rom compressedRom, Stream sw) { List <FileRecord> newDmaTable = new List <FileRecord>(); FileAddress addr; //current address int filesProcessed = 0; int filesTotal; filesTotal = compressedRom.Files.Count(); foreach (FileRecord record in compressedRom.Files.OrderBy(x => x.VirtualAddress.Start)) { addr = record.VirtualAddress; sw.PadToLength(addr.Start); //MM Check if (record.PhysicalAddress.Start == -1) { newDmaTable.Add(new FileRecord(addr, new FileAddress(record.PhysicalAddress.Start, 0), record.Index)); } else { var file = compressedRom.Files.GetFile(addr); ((Stream)file).CopyTo(sw); newDmaTable.Add(new FileRecord(addr, new FileAddress(addr.Start, 0), record.Index)); } //writes progress to console window filesProcessed++; Console.Write(processedFiles, filesProcessed, filesTotal); } sw.PadToLength(0x4000000); WriteFileTable(sw, compressedRom.Files.GetDmaDataStart(), newDmaTable.OrderBy(x => x.Index).ToList()); CRC.Write(sw); }
/// <summary> /// Patches files into a rom, where file VROM mappings are identical /// </summary> /// <param name="OutputFilename"></param> /// <param name="hostRom"></param> /// <param name="UpdateFiles"></param> /// <param name="GetFile"></param> private static void PatchFiles_SameVrom_Compressed(Stream output, ORom hostRom, Dictionary <long, string> UpdateFiles, GetFileDelegate GetFile) { List <FileRecord> newDmaTable = new List <FileRecord>(); foreach (FileRecord record in hostRom.Files) { long physicalStart = output.Position; long vromFilesize; long romFilesize; Stream file; //if the file is being replaced with a custom file if (UpdateFiles.ContainsKey(record.VRom.Start)) { //get source file file = GetFile(UpdateFiles[record.VRom.Start]); //Get virtual file size if (record.IsCompressed) { Yaz.DecodeSize(file, out vromFilesize); } else { vromFilesize = Align.To16(file.Length); } //get the physical file size with padding romFilesize = Align.To16(file.Length); } else //copy a source rom file. { vromFilesize = record.VRom.Size; romFilesize = record.Data.Size; if (romFilesize > 0x800000) { throw new Exception("Internal file too large"); } file = hostRom.Files.GetPhysicalFile(record.VRom.Start); } //copy file file.CopyTo(output); file.Close(); //pad out output.PadToLength(physicalStart + romFilesize); //generate a new file table record newDmaTable.Add(new FileRecord( new FileAddress(record.VRom.Start, record.VRom.Start + vromFilesize), new FileAddress(physicalStart, (record.IsCompressed) ? physicalStart + romFilesize : 0))); } output.PadToLength(0x2000000); WriteFileTable(output, hostRom.Files.GetDmaDataStart(), newDmaTable); //write crc CRC.Write(output); }