/// <summary> /// Creates a new instance of a LuigiDataBlock by inflating it from a BinaryReader. /// </summary> /// <param name="reader">The binary reader containing the data to deserialize to create the object.</param> /// <returns>A new instance of a LuigiDataBlock.</returns> /// <remarks>It is assumed that the reader is currently positioned at the beginning of a serialized LUIGI data block.</remarks> public static LuigiDataBlock Inflate(INTV.Core.Utility.BinaryReader reader) { LuigiDataBlock dataBlock = null; var blockType = (LuigiDataBlockType)reader.ReadByte(); switch (blockType) { case LuigiDataBlockType.SetScrambleKey: dataBlock = new LuigiScrambleKeyBlock(); break; case LuigiDataBlockType.MemoryMapAndPermissionsTable: dataBlock = new LuigiMemoryMapAndPermissionsTableBlock(); break; case LuigiDataBlockType.DataHunk: dataBlock = new LuigiDataHunkBlock(); break; case LuigiDataBlockType.Metadata: dataBlock = new LuigiMetadataBlock(); break; case LuigiDataBlockType.EndOfFile: dataBlock = new LuigiEndOfFileBlock(); break; default: dataBlock = new LuigiDataBlock((LuigiDataBlockType)blockType); break; } dataBlock._deserializeByteCount = BlockTypeSize; dataBlock._deserializeByteCount += dataBlock.Deserialize(reader); return(dataBlock); }
/// <summary> /// Creates a new instance of a LuigiDataBlock by inflating it from a Stream. /// </summary> /// <param name="stream">The stream containing the data to deserialize to create the object.</param> /// <returns>A new instance of a LuigiDataBlock.</returns> public static LuigiDataBlock Inflate(System.IO.Stream stream) { LuigiDataBlock dataBlock = null; using (var reader = new INTV.Core.Utility.BinaryReader(stream)) { dataBlock = Inflate(reader); } return(dataBlock); }
/// <summary> /// Locates the first data block of the requested type in the ROM. /// </summary> /// <typeparam name="T">The type of LUIGI block to locate.</typeparam> /// <returns>The data block, or <c>null</c> if no block of the requested type is in the ROM.</returns> internal T LocateDataBlock <T>() where T : LuigiDataBlock { var dataBlock = default(T); if (StreamUtilities.FileExists(RomPath)) { using (var file = StreamUtilities.OpenFileStream(RomPath)) { if (file != null) { if (file.Length > 0) { var desiredBlockType = LuigiDataBlock.GetBlockType <T>(); var luigiHeader = LuigiFileHeader.Inflate(file); var bytesRead = luigiHeader.DeserializeByteCount; // Start looking for desired block immediately after header. var block = LuigiDataBlock.Inflate(file); bytesRead += block.DeserializeByteCount; if (StopIfScrambleKeyBlockFound(desiredBlockType, block.Type)) { // Stop looking. If we hit the scramble key, there's nothing more to be looked at. bytesRead = (int)file.Length; } while ((bytesRead < file.Length) && (block.Type != desiredBlockType) && (block.Type != LuigiDataBlockType.EndOfFile)) { block = LuigiDataBlock.Inflate(file); bytesRead += block.DeserializeByteCount; if (StopIfScrambleKeyBlockFound(desiredBlockType, block.Type)) { break; } } if (block.Type == desiredBlockType) { dataBlock = block as T; } } } } } return(dataBlock); }