コード例 #1
0
        /// <summary>
        /// Applies the given filename patch to the in-memory RomData
        /// </summary>
        /// <param name="filename"></param>
        /// <returns>SHA256 hash of the patch.</returns>
        public static byte[] ApplyPatch(string filename)
        {
            var hashAlg = new SHA256Managed();

            using (var filestream = File.OpenRead(filename))
                using (var cryptoStream = new CryptoStream(filestream, hashAlg, CryptoStreamMode.Read))
                    using (var decompressStream = new GZipStream(cryptoStream, CompressionMode.Decompress))
                        using (var memoryStream = new MemoryStream())
                        {
                            decompressStream.CopyTo(memoryStream);
                            memoryStream.Seek(0, SeekOrigin.Begin);
                            using (var reader = new BinaryReader(memoryStream))
                            {
                                var magic   = ReadWriteUtils.ReadU32(reader);
                                var version = ReadWriteUtils.ReadU32(reader);

                                // Validate patch magic and version values
                                PatchUtils.Validate(magic, version);

                                while (reader.BaseStream.Position != reader.BaseStream.Length)
                                {
                                    var fileIndex = ReadWriteUtils.ReadS32(reader);
                                    var fileAddr  = ReadWriteUtils.ReadS32(reader);
                                    var index     = ReadWriteUtils.ReadS32(reader);
                                    var isStatic  = ReadWriteUtils.ReadS32(reader) != 0 ? true : false;
                                    var length    = ReadWriteUtils.ReadS32(reader);
                                    var data      = reader.ReadBytes(length);
                                    if (fileIndex >= RomData.MMFileList.Count)
                                    {
                                        var newFile = new MMFile
                                        {
                                            Addr         = fileAddr,
                                            IsCompressed = false,
                                            Data         = data,
                                            End          = fileAddr + data.Length,
                                            IsStatic     = isStatic,
                                        };
                                        RomUtils.AppendFile(newFile);
                                    }
                                    if (index == -1)
                                    {
                                        RomData.MMFileList[fileIndex].Data = data;
                                        if (data.Length == 0)
                                        {
                                            RomData.MMFileList[fileIndex].Cmp_Addr = -1;
                                            RomData.MMFileList[fileIndex].Cmp_End  = -1;
                                        }
                                    }
                                    else
                                    {
                                        CheckCompressed(fileIndex);
                                        ReadWriteUtils.Arr_Insert(data, 0, data.Length, RomData.MMFileList[fileIndex].Data, index);
                                    }
                                }
                            }

                            return(hashAlg.Hash);
                        }
        }
コード例 #2
0
 public static void ReadFileTable(BinaryReader ROM)
 {
     RomData.MMFileList = new List <MMFile>();
     ROM.BaseStream.Seek(FILE_TABLE, SeekOrigin.Begin);
     while (true)
     {
         MMFile Current_File = new MMFile
         {
             Addr     = ReadWriteUtils.ReadS32(ROM),
             End      = ReadWriteUtils.ReadS32(ROM),
             Cmp_Addr = ReadWriteUtils.ReadS32(ROM),
             Cmp_End  = ReadWriteUtils.ReadS32(ROM)
         };
         Current_File.IsCompressed = Current_File.Cmp_End != 0;
         if (Current_File.Addr == Current_File.End)
         {
             break;
         }
         RomData.MMFileList.Add(Current_File);
     }
     ExtractAll(ROM);
 }