private static uint GetCrcs(LuigiFileHeader header, string romPath, string cfgPath, out uint cfgCrc, out bool usedLuigiFileCrc) { usedLuigiFileCrc = false; uint romCrc = 0; cfgCrc = 0; if (header != null) { if (header.Version > 0) { if ((header.OriginalRomFormat == RomFormat.Bin) || (header.OriginalRomFormat == RomFormat.Rom)) { romCrc = header.OriginalRomCrc32; // Report the original file's CRC to help identify the ROM. } if (header.OriginalRomFormat == RomFormat.Bin) { cfgCrc = header.OriginalCfgCrc32; } } } if (romCrc == 0 && StreamUtilities.FileExists(romPath)) { usedLuigiFileCrc = true; romCrc = INTV.Core.Utility.Crc32.OfFile(romPath); } return(romCrc); }
/// <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> /// Get up-to-date CRC32 values for the ROM. /// </summary> /// <param name="romPath">Absolute path to the ROM.</param> /// <param name="cfgPath">Absolute path to the configuration file (may be <c>null</c>).</param> /// <param name="cfgCrc">Receives the CRC32 of the file at <paramref name="cfgPath"/></param> /// <returns>The CRC32 of the file at <paramref name="romPath"/>.</returns> internal static uint GetCrcs(string romPath, string cfgPath, out uint cfgCrc) { bool dontCare; var romCrc = GetCrcs(LuigiFileHeader.GetHeader(romPath), romPath, cfgPath, out cfgCrc, out dontCare); return(romCrc); }
/// <summary> /// Safely retrieves the LUIGI header for a ROM. /// </summary> /// <param name="rom">The ROM whose LUIGI header is requested.</param> /// <returns>The <see cref="LuigiFileHeader"/> for the ROM, or <c>null</c> if the ROM is not in the LUIGI format.</returns> public static LuigiFileHeader GetLuigiHeader(this IRom rom) { LuigiFileHeader luigiHeader = null; if ((rom != null) && (rom.Format == RomFormat.Luigi) && !string.IsNullOrEmpty(rom.RomPath) && StreamUtilities.FileExists(rom.RomPath) && LuigiFileHeader.PotentialLuigiFile(rom.RomPath)) { luigiHeader = LuigiFileHeader.GetHeader(rom.RomPath); } return(luigiHeader); }
/// <summary> /// If the given ROM is a LUIGI format ROM, get the LUIGI header. /// </summary> /// <param name="rom">The ROM whose LUIGI header is desired.</param> /// <returns>The LUIGI header, or <c>null</c> if <paramref name="rom"/> does not refer to a LUIGI format ROM.</returns> public static LuigiFileHeader GetLuigiHeader(IRom rom) { LuigiFileHeader header = null; var luigiRom = AsSpecificRomType <LuigiFormatRom>(rom); if (luigiRom != null) { header = luigiRom.Header; } return(header); }
/// <summary> /// Examines the given file and attempts to determine if it is a program in .luigi format. /// </summary> /// <param name="filePath">The path to the LUIGI file.</param> /// <returns>A valid LuigiFormatRom if file is a valid .luigi file, otherwise <c>null</c>.</returns> internal static LuigiFormatRom Create(string filePath) { LuigiFormatRom rom = null; if (CheckFormat(filePath) == RomFormat.Luigi) { LuigiFileHeader header = LuigiFileHeader.GetHeader(filePath); if (header != null) { rom = new LuigiFormatRom() { Format = RomFormat.Luigi, IsValid = true, RomPath = filePath, Header = header }; } } return(rom); }
/// <inheritdoc /> internal override void LoadComplete(FileSystem fileSystem, bool updateRomList) { base.LoadComplete(fileSystem, updateRomList); if (Description == null) { if (Rom != null) { if (Crc32 == 0) { var filePath = Rom.FilePath; INTV.Core.Model.LuigiFileHeader luigiHeader = LuigiFileHeader.GetHeader(filePath); if (luigiHeader != null) { string cfgFile = null; string romFile = filePath; uint crc = 0u; if (luigiHeader.Version > 0) { crc = luigiHeader.OriginalRomCrc32; } else { var jzIntvConfiguration = INTV.Shared.Utility.SingleInstanceApplication.Instance.GetConfiguration <INTV.JzIntv.Model.Configuration>(); var luigiToBinPath = jzIntvConfiguration.GetProgramPath(JzIntv.Model.ProgramFile.Luigi2Bin); var luigiToBinResult = RunExternalProgram.Call(luigiToBinPath, "\"" + filePath + "\"", System.IO.Path.GetDirectoryName(filePath)); if (luigiToBinResult == 0) { romFile = System.IO.Path.ChangeExtension(filePath, RomFormat.Bin.FileExtension()); cfgFile = System.IO.Path.ChangeExtension(filePath, ProgramFileKind.CfgFile.FileExtension()); if (!System.IO.File.Exists(cfgFile)) { cfgFile = null; } } } var rom = INTV.Core.Model.Rom.Create(romFile, cfgFile); var programInfo = rom.GetProgramInformation(); var programDescription = new ProgramDescription(rom.Crc, rom, programInfo); Description = programDescription; } } } } }
/// <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); }
/// <summary> /// Checks the format of the ROM at the given absolute path. /// </summary> /// <param name="filePath">Absolute path to a ROM file.</param> /// <returns>The format of the ROM.</returns> internal static RomFormat CheckFormat(string filePath) { var format = CheckMemo(filePath); if (format == RomFormat.None) { using (var file = StreamUtilities.OpenFileStream(filePath)) { if (file != null) { if (file.Length > 0) { if (LuigiFileHeader.GetHeader(filePath) != null) { format = RomFormat.Luigi; AddMemo(filePath, format); } } } } } return(format); }