/// <summary> /// Open IMG archive /// </summary> /// <param name="archiveFileName">Archive file name</param> /// <param name="archiveMode">Archive mode</param> /// <returns>IMG archive</returns> /// <exception cref="InvalidOperationException">Return value is null</exception> /// <exception cref="ArgumentNullException">Archive file name is null</exception> /// <exception cref="FileNotFoundException">Archive file not found</exception> /// <exception cref="InvalidDataException">Archive error</exception> public static IMGArchive Open(string archiveFileName, EIMGArchiveMode archiveMode) { return(Open(archiveFileName, archiveMode, Encoding.UTF8)); }
/// <summary> /// Open IMG archive /// </summary> /// <param name="archiveFileName">Archive file name</param> /// <param name="archiveMode">Archive mode</param> /// <param name="entryNameEncoding">Entry name encoding</param> /// <returns>IMG archive</returns> /// <exception cref="InvalidOperationException">Return value is null</exception> /// <exception cref="ArgumentNullException">Archive file name is null</exception> /// <exception cref="FileNotFoundException">Archive file not found</exception> /// <exception cref="InvalidDataException">Archive error</exception> public static IMGArchive Open(string archiveFileName, EIMGArchiveMode archiveMode, Encoding entryNameEncoding) { IMGArchive ret = null; try { Encoding encoding = ((entryNameEncoding == null) ? Encoding.UTF8 : entryNameEncoding); if (archiveFileName != null) { if ((archiveMode == EIMGArchiveMode.Create) || File.Exists(archiveFileName)) { ret = new IMGArchive(File.Open(archiveFileName, (archiveMode == EIMGArchiveMode.Create) ? FileMode.Create : FileMode.Open, (archiveMode != EIMGArchiveMode.Read) ? FileAccess.ReadWrite : FileAccess.Read), archiveMode, entryNameEncoding); if (archiveMode == EIMGArchiveMode.Create) { byte[] header_bytes = new byte[] { 0x56, 0x45, 0x52, 0x32, 0x0, 0x0, 0x0, 0x0 }; ret.Stream.Write(header_bytes, 0, header_bytes.Length); while (ret.Stream.Length < 2048) { ret.Stream.WriteByte(0); } } else { byte[] version_bytes = new byte[4]; if (ret.Stream.Read(version_bytes, 0, version_bytes.Length) == version_bytes.Length) { string version = Encoding.UTF8.GetString(version_bytes); byte[] int_bytes = new byte[4]; if (ret.Stream.Read(int_bytes, 0, int_bytes.Length) == int_bytes.Length) { uint num_entries = int_bytes[0] | (((uint)(int_bytes[1])) << 8) | (((uint)(int_bytes[2])) << 16) | (((uint)(int_bytes[3])) << 24); if ((version == "VER2") && (ret.Stream.Length >= (num_entries * 8))) { for (uint num_entry = 0U; num_entry != num_entries; num_entry++) { if (ret.Stream.Read(int_bytes, 0, int_bytes.Length) == int_bytes.Length) { long offset = (int_bytes[0] | (((long)(int_bytes[1])) << 8) | (((long)(int_bytes[2])) << 16) | (((long)(int_bytes[3])) << 24)) * 2048L; byte[] short_bytes = new byte[2]; if (ret.Stream.Read(short_bytes, 0, short_bytes.Length) == short_bytes.Length) { int length = (short_bytes[0] | (short_bytes[1] << 8)) * 2048; if (ret.Stream.Read(short_bytes, 0, short_bytes.Length) == short_bytes.Length) { byte[] full_name_bytes_raw = new byte[24]; if (ret.Stream.Read(full_name_bytes_raw, 0, full_name_bytes_raw.Length) == full_name_bytes_raw.Length) { int full_name_bytes_count = IMGUtils.GetNullTerminatedByteStringLength(full_name_bytes_raw); if (full_name_bytes_count > 0) { string full_name = encoding.GetString(full_name_bytes_raw, 0, full_name_bytes_count); ret.entries.Add(full_name.ToLower(), new IMGArchiveEntry(ret, offset, length, full_name)); } else { throw new InvalidDataException("IMG entry name can't be empty."); } } else { throw new InvalidDataException("IMG entry name is missing."); } } else { throw new InvalidDataException("IMG entry length is missing."); } } else { throw new InvalidDataException("IMG entry length is missing."); } } else { throw new InvalidDataException("IMG entry offset is missing."); } } } else { throw new InvalidDataException("\"" + archiveFileName + "\" is not an IMG file"); } } else { throw new InvalidDataException("\"" + archiveFileName + "\" is not an IMG file"); } } else { throw new InvalidDataException("\"" + archiveFileName + "\" is not an IMG file"); } } } else { throw new FileNotFoundException("Archive not found", archiveFileName); } } else { throw new ArgumentNullException("archiveFileName"); } if (ret == null) { throw new InvalidOperationException("Return value is null"); } } catch (Exception e) { if (ret != null) { ret.Dispose(); ret = null; } throw e; } return(ret); }
/// <summary> /// Constructor /// </summary> /// <param name="stream">IMG stream</param> /// <param name="mode">IMG archive mode</param> /// <param name="entryNameEncoding">Entry name encoding</param> internal IMGArchive(Stream stream, EIMGArchiveMode mode, Encoding entryNameEncoding) { this.stream = stream; this.mode = mode; this.entryNameEncoding = entryNameEncoding; }