Example #1
0
        /// <summary>
        /// Returns a reference to a decompressed file
        /// </summary>
        /// <param name="record">The DMA address used to reference the file</param>
        /// <returns></returns>
        protected RomFile GetFile(FileRecord record)
        {
            MemoryStream ms;

            byte[] data;
            byte[] decompressedData;

            if (record.VirtualAddress == CachedFileAddress)
            {
                ms = new MemoryStream(CachedFile);
                return(new RomFile(record, ms, Version));
            }

            using (FileStream fs = new FileStream(RomLocation, FileMode.Open, FileAccess.Read))
            {
                data        = new byte[record.DataAddress.Size];
                fs.Position = record.DataAddress.Start;
                fs.Read(data, 0, record.DataAddress.Size);

                if (record.IsCompressed)
                {
                    ms = new MemoryStream(data);
                    decompressedData = Yaz.Decode(ms, record.DataAddress.Size);
                }
                else
                {
                    decompressedData = data;
                }
            }
            CachedFile        = decompressedData;
            ms                = new MemoryStream(decompressedData);
            CachedFileAddress = record.VirtualAddress;
            return(new RomFile(record, ms, Version));
        }
Example #2
0
        public void GlobalSetup()
        {
            Rom      = RomFile.From(PathUtil.GetInputRomFilePath());
            CodeFile = Rom.Files.First(x => x.VirtualStart == CodeVirtualStart);
            var slice = Rom.Slice(CodeFile);

            CodeBytes = Yaz.Decode(slice);
        }
Example #3
0
        static void PerformCompression(RomFile rom, VirtualFile entry)
        {
            var slice   = rom.Slice(entry);
            var decoded = Yaz.Decode(slice);
            var encoded = Yaz.EncodeWithHeader(decoded, slice); // Yaz.EncodeAndCopy(decoded);
            var aligned = Yaz.AlignTo16(encoded);

            // Currently only compares compressed lengths, as compressed output is likely slightly different due to optimization.
            Assert.AreEqual(slice.Length, aligned);
        }
Example #4
0
        public byte[] DecodeNew()
        {
            byte[] result = null;

            foreach (var sample in Samples)
            {
                result = Yaz.Decode(sample.AsSpan());
            }

            return(result);
        }
Example #5
0
        public byte[] DecodeNew()
        {
            byte[] result = null;

            foreach (var sample in Samples)
            {
                var slice = Rom.Slice(sample);
                result = Yaz.Decode(slice);
            }

            return(result);
        }
Example #6
0
        public byte[] EncodeNew()
        {
            byte[] result = null;

            foreach (var sample in Samples)
            {
                result = Yaz.Decode(sample.AsSpan());
                var length = Yaz.Encode(result, out var encoded);
                result = encoded;
            }

            return(result);
        }
Example #7
0
        public static void CheckCompressed(int fileIndex, List <MMFile> mmFileList = null)
        {
            if (mmFileList == null)
            {
                mmFileList = RomData.MMFileList;
            }
            var file = mmFileList[fileIndex];

            if (file.IsCompressed && !file.WasEdited)
            {
                file.Data      = Yaz.Decode(file.Data);
                file.WasEdited = true;
            }
        }
Example #8
0
        public int EncodeNew()
        {
            int result = 0;

            foreach (var sample in Samples)
            {
                var slice   = Rom.Slice(sample);
                var decoded = Yaz.Decode(slice);
                result = Yaz.EncodeWithHeader(decoded, slice);
                // Fill remaining bytes with 0.
                slice.Slice(result).Fill(0);
            }

            return(result);
        }
Example #9
0
        public static void TestYazCompression(IExperimentFace face, List <string> filePath)
        {
            ORom          rom = new ORom(filePath[0], ORom.Build.N0);
            StringBuilder sb  = new StringBuilder();

            foreach (var file in rom.Files)
            {
                if (!file.IsCompressed)
                {
                    continue;
                }

                try
                {
                    Stream       vanillaFile      = rom.Files.GetPhysicalFile(file.VRom);
                    var          decompressedFile = Yaz.Decode(vanillaFile, file.Rom.Size);
                    MemoryStream ms = new MemoryStream(file.Rom.Size);
                    sb.AppendLine($"{ Yaz.Encode(decompressedFile, decompressedFile.Length, ms):X8}");
                    while (ms.Length < ms.Capacity)
                    {
                        ms.WriteByte(0);
                    }

                    vanillaFile.Position = 0;
                    ms.Position          = 0;

                    BinaryReader original = new BinaryReader(vanillaFile);
                    BinaryReader test     = new BinaryReader(ms);

                    sb.AppendLine($"{file.VRom} - original: {original.BaseStream.Length:X8} test: {test.BaseStream.Length:X8}");

                    for (int i = 0; i < file.Rom.Size; i += 4)
                    {
                        int left  = original.ReadBigInt32();
                        int right = test.ReadBigInt32();
                        if (left != right)
                        {
                            sb.AppendLine($"{file.VRom} - {i:X8} does not match, comparison stopped");
                        }
                    }
                }
                catch (Exception e)
                {
                    sb.AppendLine($"{file.VRom} - Exception {e.Message} {e.InnerException}");
                }
            }
            face.OutputText(sb.ToString());
        }
Example #10
0
        public static void CheckCompressed(int fileIndex, List <MMFile> mmFileList = null)
        {
            if (mmFileList == null)
            {
                mmFileList = RomData.MMFileList;
            }
            var file = mmFileList[fileIndex];

            if (file.IsCompressed && !file.WasEdited)
            {
                using (var stream = new MemoryStream(file.Data))
                {
                    file.Data = Yaz.Decode(stream, file.Data.Length);
                }
                file.WasEdited = true;
            }
        }
Example #11
0
        public static List <byte[]> GetFilesFromArchive(int fileIndex)
        {
            CheckCompressed(fileIndex);
            var data         = RomData.MMFileList[fileIndex].Data;
            var headerLength = ReadWriteUtils.Arr_ReadS32(data, 0);
            var pointer      = headerLength;
            var files        = new List <byte[]>();

            for (var i = 4; i < headerLength; i += 4)
            {
                var nextFileOffset = headerLength + ReadWriteUtils.Arr_ReadS32(data, i);
                var fileLength     = nextFileOffset - pointer;
                var dest           = new byte[fileLength];
                ReadWriteUtils.Arr_Insert(data, pointer, fileLength, dest, 0);
                pointer += fileLength;
                var decompressed = Yaz.Decode(dest);
                files.Add(decompressed);
            }
            return(files);
        }
Example #12
0
        static void HandleDecode(string input, string output, bool legacy, IConsole console)
        {
            var inputBytes = File.ReadAllBytes(input);

            byte[] outputBytes = null;

            // Perform decode.
            if (legacy)
            {
                // Legacy decode.
                using (var memoryStream = new MemoryStream(inputBytes))
                {
                    outputBytes = MzxYaz.Decode(memoryStream, inputBytes.Length);
                }
            }
            else
            {
                // New decode.
                outputBytes = Yaz.Decode(inputBytes);
            }

            File.WriteAllBytes(output, outputBytes);
        }
Example #13
0
        public static void TestYazSimple(IExperimentFace face, List <string> filePath)
        {
            StringBuilder sb  = new StringBuilder();
            ORom          rom = new ORom(filePath[0], ORom.Build.N0);

            RomFile rfile       = rom.Files.GetFile(ORom.FileList.code);
            var     dmarec_code = rfile.Record;

            BinaryReader compressed   = new BinaryReader((MemoryStream)rom.Files.GetPhysicalFile(dmarec_code.VRom));
            BinaryReader decompressed = new BinaryReader(rfile);

            byte[] decompressedbuffer = decompressed.ReadBytes(rfile.Record.VRom.Size);

            //var buffer = new MemoryStream(0x20_0000);
            var buffer = new byte[0];

            Stopwatch stopwatch = Stopwatch.StartNew();
            //int compressedSize = Yaz.Encode(decompressedbuffer, rfile.Record.VirtualAddress.Size, buffer);
            int compressedSize = Yaz.Encode(decompressedbuffer, rfile.Record.VRom.Size, out buffer);

            Yaz.GetEncodingData(buffer, compressedSize, out int metaSize);

            stopwatch.Stop();

            sb.AppendLine($"Compression time: {stopwatch.Elapsed.Seconds}.{stopwatch.Elapsed.Milliseconds:D3}");
            sb.AppendLine($"Compressed Size: {compressedSize:X6}, Meta Size {metaSize:X6}");

            decompressed.Seek(0);
            //buffer.Position = 0;

            //compare compressed output
            if (compressedSize != dmarec_code.Rom.Size)
            {
                sb.AppendLine($"Compression size mismatch: Original {dmarec_code.Rom.Size:X6} || New {compressedSize:X6}");
            }

            int mismatchMax = 8;

            for (int i = 0; i < dmarec_code.Rom.Size; i++)
            {
                byte a = compressed.ReadByte();
                //byte b = (byte)buffer.ReadByte();
                byte b = buffer[i];
                if (a == b)
                {
                    continue;
                }

                mismatchMax--;
                sb.AppendLine($"COMPRESS MISMATCH {i:X6}: {a:X2} {b:X2}");
                if (mismatchMax <= 0)
                {
                    break;
                }
            }
            compressed.Seek(0);
            //buffer.Position = 0;

            byte[] dbuffer = Yaz.Decode(new MemoryStream(buffer), compressedSize);
            decompressed.BaseStream.Position = 0;

            for (int i = 0; i < dbuffer.Length; i++)
            {
                if (dbuffer[i] != decompressed.ReadByte())
                {
                    sb.AppendLine($"File Size: {dbuffer.Length:X}, Compressed: {compressedSize:X}, Failed Match: {i:X}");
                    break;
                }
            }

            sb.AppendLine("Test Complete");
            face.OutputText(sb.ToString());
        }