From b63b7543cd598bb2f8802a3df5484b988748b611 Mon Sep 17 00:00:00 2001 From: Alexey 'Cluster' Avdyukhin Date: Fri, 4 Nov 2022 11:53:16 +0400 Subject: Refactoring, comments --- FdsFile.cs | 23 ++++++++++++----------- NesFile.cs | 44 ++++++++++++++++++++++++++++---------------- NesHeaderFixer.cs | 14 +++++++------- UnifFile.cs | 33 +++++++++++++++++++++------------ 4 files changed, 68 insertions(+), 46 deletions(-) diff --git a/FdsFile.cs b/FdsFile.cs index 2c798c2..ff8a48f 100644 --- a/FdsFile.cs +++ b/FdsFile.cs @@ -59,14 +59,18 @@ namespace com.clusterrr.Famicom.Containers /// Create FdsFile object from raw .fds file contents /// /// - /// - public static FdsFile FromBytes(byte[] data) - { - return new FdsFile(data); - } + /// FdsFile object + public static FdsFile FromBytes(byte[] data) =>new FdsFile(data); + + /// + /// Create FileFile object from the specified .nes file + /// + /// Path to the .fds file + /// FdsFile object + public static FdsFile FromFile(string filename) => new FdsFile(filename); /// - /// Return FDS file contents + /// Returns .fds file contents /// /// FDS file contents public byte[] ToBytes(bool useHeader = false) @@ -86,13 +90,10 @@ namespace com.clusterrr.Famicom.Containers } /// - /// Save to .fds file + /// Save as .fds file /// /// Target filename /// Option to add .fds file header (ignored by most emulators) - public void Save(string filename, bool useHeader = false) - { - File.WriteAllBytes(filename, ToBytes(useHeader)); - } + public void Save(string filename, bool useHeader = false) => File.WriteAllBytes(filename, ToBytes(useHeader)); } } diff --git a/NesFile.cs b/NesFile.cs index 4c81847..7ea1ad2 100644 --- a/NesFile.cs +++ b/NesFile.cs @@ -582,7 +582,7 @@ namespace com.clusterrr.Famicom.Containers } /// - /// Create NesFile object from raw .nes file data + /// Create NesFile object from raw .nes file contents /// /// Raw .nes file data public NesFile(byte[] data) @@ -698,15 +698,20 @@ namespace com.clusterrr.Famicom.Containers /// Create NesFile object from raw .nes file contents /// /// Raw ROM data - public static NesFile FromBytes(byte[] data) - { - return new NesFile(data); - } + /// NesFile object + public static NesFile FromBytes(byte[] data) => new NesFile(data); /// - /// Return iNES file contents (header + PRG + CHR) + /// Create NesFile object from the specified .nes file /// - /// iNES file contents + /// Path to the .nes file + /// NesFile object + public static NesFile FromFile(string filename) => new NesFile(filename); + + /// + /// Returns .nes file contents (header + PRG + CHR) + /// + /// .nes file contents public byte[] ToBytes() { var data = new List(); @@ -735,6 +740,16 @@ namespace com.clusterrr.Famicom.Containers if (length8k > 0xFF) throw new ArgumentOutOfRangeException("CHR size is too big for iNES, use NES 2.0 instead"); header[5] = (byte)Math.Ceiling((double)chr.Length / 0x2000); chrSizePadded = header[5] * 0x2000UL; + switch (Mirroring) + { + case MirroringType.Unknown: + case MirroringType.Horizontal: + case MirroringType.Vertical: + case MirroringType.FourScreenVram: + break; + default: + throw new InvalidDataException($"{Mirroring} mirroring is not supported by iNES"); + } // Hard-wired nametable mirroring type if (Mirroring == MirroringType.Vertical) header[6] |= 1; @@ -867,6 +882,12 @@ namespace com.clusterrr.Famicom.Containers return data.ToArray(); } + /// + /// Save as .nes file + /// + /// Target filename + public void Save(string filename) => File.WriteAllBytes(filename, ToBytes()); + private static ulong ExponentToSize(byte exponent, byte multiplier) => (1UL << exponent) * (ulong)(multiplier * 2 + 1); @@ -920,15 +941,6 @@ namespace com.clusterrr.Famicom.Containers return (e, m, ExponentToSize(e, m)); } - /// - /// Save iNES file - /// - /// Target filename - public void Save(string fileName) - { - File.WriteAllBytes(fileName, ToBytes()); - } - /// /// Calculate MD5 checksum of ROM (CHR+PRG without header) /// diff --git a/NesHeaderFixer.cs b/NesHeaderFixer.cs index 1f428b1..3d7ce8c 100644 --- a/NesHeaderFixer.cs +++ b/NesHeaderFixer.cs @@ -59,26 +59,26 @@ namespace com.clusterrr.Famicom.Containers.HeaderFixer } if (mirroring >= 0) { - if (mirroring == 8 && nes.Mirroring == NesFile.MirroringType.FourScreenVram) + if (mirroring == 8 && nes.Mirroring == MirroringType.FourScreenVram) { // no four-screen - nes.Mirroring = NesFile.MirroringType.Horizontal; + nes.Mirroring = MirroringType.Horizontal; fixType |= NesFixType.Mirroring; } - NesFile.MirroringType needMirroring = NesFile.MirroringType.Unknown; + MirroringType needMirroring = MirroringType.Unknown; switch (mirroring) { case 0: - needMirroring = NesFile.MirroringType.Horizontal; + needMirroring = MirroringType.Horizontal; break; case 1: - needMirroring = NesFile.MirroringType.Vertical; + needMirroring = MirroringType.Vertical; break; case 2: - needMirroring = NesFile.MirroringType.FourScreenVram; + needMirroring = MirroringType.FourScreenVram; break; } - if (needMirroring != NesFile.MirroringType.Unknown && needMirroring != nes.Mirroring) + if (needMirroring != MirroringType.Unknown && needMirroring != nes.Mirroring) { nes.Mirroring = needMirroring; fixType |= NesFixType.Mirroring; diff --git a/UnifFile.cs b/UnifFile.cs index 56be6be..f098468 100644 --- a/UnifFile.cs +++ b/UnifFile.cs @@ -63,7 +63,7 @@ namespace com.clusterrr.Famicom.Containers } /// - /// Create UnifFile object from raw data + /// Create UnifFile object from raw .unf file contents /// /// Raw UNIF data public UnifFile(byte[] data) @@ -90,7 +90,7 @@ namespace com.clusterrr.Famicom.Containers /// /// Create UnifFile object from specified file /// - /// + /// Path to the .unf file public UnifFile(string fileName) : this(File.ReadAllBytes(fileName)) { } @@ -99,17 +99,21 @@ namespace com.clusterrr.Famicom.Containers /// Create UnifFile object from raw .unf file contents /// /// - /// - public static UnifFile FromBytes(byte[] data) - { - return new UnifFile(data); - } + /// UnifFile object + public static UnifFile FromBytes(byte[] data) => new UnifFile(data); /// - /// Save UNIF file + /// Create UnifFile object from specified file /// - /// Target filename - public void Save(string fileName) + /// Path to the .unf file + /// UnifFile object + public static UnifFile FromFile(string filename) => new UnifFile(filename); + + /// + /// Returns .unf file contents + /// + /// + public byte[] ToBytes() { var data = new List(); var header = new byte[32]; @@ -130,10 +134,15 @@ namespace com.clusterrr.Famicom.Containers data.Add((byte)((len >> 24) & 0xFF)); data.AddRange(fields[name]); } - - File.WriteAllBytes(fileName, data.ToArray()); + return data.ToArray(); } + /// + /// Save as .unf file + /// + /// Target filename + public void Save(string filename) => File.WriteAllBytes(filename, ToBytes()); + /// /// Convert string to null-terminated UTF string /// -- cgit v1.2.3