/// <summary> /// Inspects data in the stream to determine if it appears to be a LUIGI ROM. /// </summary> /// <param name="stream">The stream containing the data to inspect.</param> /// <returns><c>RomFormat.Luigi</c> if the data at the beginning of <paramref name="stream"/> is a valid /// LUIGI file header, otherwise <c>RomFormat.None</c>.</returns> internal static RomFormat CheckFormat(System.IO.Stream stream) { var position = 0L; var format = RomFormat.None; try { position = stream.Position; if (LuigiFileHeader.Inflate(stream) != null) { format = RomFormat.Luigi; } } catch (System.Exception) { } finally { if (stream != null) { stream.Seek(position, System.IO.SeekOrigin.Begin); } } return(format); }
/// <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); }