using System.IO; using SabreTools.Data.Extensions; using SabreTools.Data.Models.NintendoDisc; namespace SabreTools.Wrappers { public partial class NintendoDisc : WrapperBase { #region Descriptive Properties /// public override string DescriptionString => "Nintendo GameCube / Wii Disc Image"; #endregion #region Extension Properties /// public DiscHeader Header => Model.Header; /// /// Detected platform (GameCube or Wii) /// public Platform Platform => Header.GetPlatform(); /// public string GameId => Model.Header.GameId; /// /// 2-character ASCII maker / publisher code (e.g. "01") /// public string MakerCode => GameId.Length >= 6 ? GameId.Substring(4, 2) : string.Empty; /// public string GameTitle => Model.Header.GameTitle; /// public byte DiscNumber => Model.Header.DiscNumber; /// public byte DiscVersion => Model.Header.DiscVersion; /// public WiiPartitionTableEntry[]? PartitionTableEntries => Model.PartitionTableEntries; /// public WiiRegionData? RegionData => Model.RegionData; #endregion #region Constructors /// public NintendoDisc(Disc model, byte[] data) : base(model, data) { } /// public NintendoDisc(Disc model, byte[] data, int offset) : base(model, data, offset) { } /// public NintendoDisc(Disc model, byte[] data, int offset, int length) : base(model, data, offset, length) { } /// public NintendoDisc(Disc model, Stream data) : base(model, data) { } /// public NintendoDisc(Disc model, Stream data, long offset) : base(model, data, offset) { } /// public NintendoDisc(Disc model, Stream data, long offset, long length) : base(model, data, offset, length) { } #endregion #region Static Constructors /// /// Create a Nintendo disc image wrapper from a byte array and offset /// /// Byte array representing the disc image /// Offset within the array to parse /// A NintendoDisc wrapper on success, null on failure public static NintendoDisc? Create(byte[]? data, int offset) { // If the data is invalid if (data is null || data.Length == 0) return null; // If the offset is out of bounds if (offset < 0 || offset >= data.Length) return null; // Create a memory stream and use that var dataStream = new MemoryStream(data, offset, data.Length - offset); return Create(dataStream); } /// /// Create a Nintendo disc image wrapper from a Stream /// /// Stream representing the disc image /// A NintendoDisc wrapper on success, null on failure public static NintendoDisc? Create(Stream? data) { // If the data is invalid if (data is null || !data.CanRead) return null; try { long currentOffset = data.Position; var model = new Serialization.Readers.NintendoDisc().Deserialize(data); if (model is null) return null; return new NintendoDisc(model, data, currentOffset); } catch { return null; } } #endregion } }