diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2021-05-30 10:00:19 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2021-05-30 10:00:19 +0300 |
commit | 19e49d749afa934322dd247b547152d66f78db9d (patch) | |
tree | 26a912bba537efd6067653b5d219c4b7de47471f | |
parent | 3896f9b958ea75e00c1d4d9ca032c3b3c7b1da06 (diff) |
Many FDS fixes and refactoring
-rw-r--r-- | Crc32Calculator.cs | 2 | ||||
-rw-r--r-- | FdsBlockDiskInfo.cs | 45 | ||||
-rw-r--r-- | FdsBlockFileAmount.cs | 11 | ||||
-rw-r--r-- | FdsBlockFileData.cs | 17 | ||||
-rw-r--r-- | FdsBlockFileHeader.cs | 10 | ||||
-rw-r--r-- | FdsBlockParser.cs | 23 | ||||
-rw-r--r-- | FdsDiskSide.cs | 14 | ||||
-rw-r--r-- | IFdsBlock.cs | 3 |
8 files changed, 57 insertions, 68 deletions
diff --git a/Crc32Calculator.cs b/Crc32Calculator.cs index d1bd837..45a2be1 100644 --- a/Crc32Calculator.cs +++ b/Crc32Calculator.cs @@ -2,7 +2,7 @@ {
public static class Crc32Calculator
{
- static uint[] table = new uint[256];
+ static readonly uint[] table = new uint[256];
static Crc32Calculator()
{
diff --git a/FdsBlockDiskInfo.cs b/FdsBlockDiskInfo.cs index 3fd6423..9fbf5df 100644 --- a/FdsBlockDiskInfo.cs +++ b/FdsBlockDiskInfo.cs @@ -5,7 +5,7 @@ using System.Text; namespace com.clusterrr.Famicom.Containers
{
- [StructLayout(LayoutKind.Sequential, Size = 58, Pack = 1, CharSet = CharSet.Ansi)]
+ [StructLayout(LayoutKind.Sequential, Size = 58, Pack = 1)]
public class FdsBlockDiskInfo : IFdsBlock, IEquatable<FdsBlockDiskInfo>
{
public enum DiskSides
@@ -70,14 +70,15 @@ namespace com.clusterrr.Famicom.Containers [MarshalAs(UnmanagedType.U1)]
// Raw byte: 0x01
- private byte blockType = 1;
+ private readonly byte blockType = 1;
+ public byte ValidTypeID { get => 1; }
/// <summary>
/// True if block type ID is valid
/// </summary>
- public bool IsValid { get => blockType == 1; }
+ public bool IsValid { get => blockType == ValidTypeID; }
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)]
- byte[] diskVerification = Encoding.ASCII.GetBytes("*NINTENDO-HVC*");
+ readonly byte[] diskVerification = Encoding.ASCII.GetBytes("*NINTENDO-HVC*");
/// <summary>
/// Literal ASCII string: *NINTENDO-HVC*
/// </summary>
@@ -91,20 +92,20 @@ namespace com.clusterrr.Famicom.Containers public Manufacturer ManufacturerCode { get => (Manufacturer)manufacturerCode; set => manufacturerCode = (byte)value; }
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
- char[] gameName;
+ byte[] gameName;
/// <summary>
/// 3-letter ASCII code per game (e.g. ZEL for The Legend of Zelda)
/// </summary>
- public string GameName { get => new string(gameName).Trim(new char[] { '\0', ' ' }); set => gameName = value.PadRight(3).ToCharArray(0, value.Length > 3 ? 3 : value.Length); }
+ public string GameName { get => Encoding.ASCII.GetString(gameName).Trim(new char[] { '\0', ' ' }); set => gameName = Encoding.ASCII.GetBytes(value.PadRight(3)).Take(3).ToArray(); }
[MarshalAs(UnmanagedType.U1)]
- char gameType;
+ byte gameType;
/// <summary>
/// = = 0x20, " " — Normal disk
/// = = 0x45, "E" — Event(e.g.Japanese national DiskFax tournaments)
/// = = 0x52, "R" — Reduction in price via advertising
/// </summary>
- public char GameType { get => gameType; set => gameType = value; }
+ public char GameType { get => (char)gameType; set => gameType = (byte)value; }
[MarshalAs(UnmanagedType.U1)]
byte gameVersion;
@@ -137,7 +138,7 @@ namespace com.clusterrr.Famicom.Containers [MarshalAs(UnmanagedType.U1)]
// Speculative: (Err.10) Possibly indicates disk #; usually $00
// Speculative: = = 0x00, yellow disk, = = 0x01, blue or gold disk, = = 0xFE, white disk, = = 0xFF, blue disk
- byte unknown01 = 0x00;
+ readonly byte unknown01 = 0x00;
[MarshalAs(UnmanagedType.U1)]
byte bootFile;
/// <summary>
@@ -145,15 +146,15 @@ namespace com.clusterrr.Famicom.Containers /// </summary>
public byte BootFile { get => bootFile; set => bootFile = value; }
[MarshalAs(UnmanagedType.U1)]
- byte unknown02 = 0xFF;
+ readonly byte unknown02 = 0xFF;
[MarshalAs(UnmanagedType.U1)]
- byte unknown03 = 0xFF;
+ readonly byte unknown03 = 0xFF;
[MarshalAs(UnmanagedType.U1)]
- byte unknown04 = 0xFF;
+ readonly byte unknown04 = 0xFF;
[MarshalAs(UnmanagedType.U1)]
- byte unknown05 = 0xFF;
+ readonly byte unknown05 = 0xFF;
[MarshalAs(UnmanagedType.U1)]
- byte unknown06 = 0xFF;
+ readonly byte unknown06 = 0xFF;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
byte[] manufacturingDate;
@@ -198,16 +199,16 @@ namespace com.clusterrr.Famicom.Containers [MarshalAs(UnmanagedType.U1)]
// Raw byte: $61. Speculative: Region code?
- byte unknown07 = 0x61;
+ readonly byte unknown07 = 0x61;
[MarshalAs(UnmanagedType.U1)]
// Raw byte: $00. Speculative: Location/site?
- byte unknown08 = 0x00;
+ readonly byte unknown08 = 0x00;
[MarshalAs(UnmanagedType.U2)]
// Raw bytes: $00 $02
- ushort unknown09 = 0x0200;
+ readonly ushort unknown09 = 0x0200;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
// Speculative: some kind of game information representation?
- byte[] unknown10;
+ readonly byte[] unknown10;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
byte[] rewrittenDate;
@@ -244,10 +245,10 @@ namespace com.clusterrr.Famicom.Containers }
[MarshalAs(UnmanagedType.U1)]
- byte unknown11 = 0x00;
+ readonly byte unknown11 = 0x00;
[MarshalAs(UnmanagedType.U1)]
// Raw byte: $80
- byte unknown12 = 0x80;
+ readonly byte unknown12 = 0x80;
[MarshalAs(UnmanagedType.U2)]
ushort diskWriterSerialNumber;
@@ -258,7 +259,7 @@ namespace com.clusterrr.Famicom.Containers [MarshalAs(UnmanagedType.U1)]
// Raw byte: $07
- byte unknown13 = 0x07;
+ readonly byte unknown13 = 0x07;
[MarshalAs(UnmanagedType.U1)]
byte diskRewriteCount = 0x00;
@@ -285,7 +286,7 @@ namespace com.clusterrr.Famicom.Containers public DiskSides ActualDiskSide { get => (DiskSides)actualDiskSide; set => actualDiskSide = (byte)value; }
[MarshalAs(UnmanagedType.U1)]
- byte unknown14 = 0x00;
+ readonly byte unknown14 = 0x00;
[MarshalAs(UnmanagedType.U1)]
byte price = 0x00;
diff --git a/FdsBlockFileAmount.cs b/FdsBlockFileAmount.cs index 21e8211..127f25f 100644 --- a/FdsBlockFileAmount.cs +++ b/FdsBlockFileAmount.cs @@ -6,10 +6,11 @@ namespace com.clusterrr.Famicom.Containers public class FdsBlockFileAmount : IFdsBlock, IEquatable<FdsBlockFileAmount>
{
private byte blockType = 2;
+ public byte ValidTypeID { get => 2; }
/// <summary>
/// True if block type ID is valid
/// </summary>
- public bool IsValid { get => blockType == 2; }
+ public bool IsValid { get => blockType == ValidTypeID; }
private byte fileAmount;
public byte FileAmount { get => fileAmount; set => fileAmount = value; }
@@ -28,9 +29,11 @@ namespace com.clusterrr.Famicom.Containers public static FdsBlockFileAmount FromBytes(byte[] rawData, int position = 0)
{
- var retobj = new FdsBlockFileAmount();
- retobj.blockType = rawData[position];
- retobj.fileAmount = rawData[position + 1];
+ var retobj = new FdsBlockFileAmount
+ {
+ blockType = rawData[position],
+ fileAmount = rawData[position + 1]
+ };
return retobj;
}
diff --git a/FdsBlockFileData.cs b/FdsBlockFileData.cs index 6b2b280..40f6e37 100644 --- a/FdsBlockFileData.cs +++ b/FdsBlockFileData.cs @@ -7,10 +7,11 @@ namespace com.clusterrr.Famicom.Containers public class FdsBlockFileData : IFdsBlock, IEquatable<FdsBlockFileData>
{
private byte blockType = 4;
+ public byte ValidTypeID { get => 4; }
/// <summary>
/// True if block type ID is valid
/// </summary>
- public bool IsValid { get => blockType == 4; }
+ public bool IsValid { get => blockType == ValidTypeID; }
private byte[] data = new byte[0];
public IEnumerable<byte> Data
@@ -33,17 +34,21 @@ namespace com.clusterrr.Famicom.Containers public static FdsBlockFileData FromBytes(byte[] rawData, int position = 0, int size = -1)
{
- var retobj = new FdsBlockFileData();
- retobj.blockType = rawData[position];
- retobj.data = new byte[size < 0 ? rawData.Length - position - 1 : size - 1];
+ var retobj = new FdsBlockFileData
+ {
+ blockType = rawData[position],
+ data = new byte[size < 0 ? rawData.Length - position - 1 : size - 1]
+ };
Array.Copy(rawData, position + 1, retobj.data, 0, retobj.data.Length);
return retobj;
}
public byte[] ToBytes()
{
- var result = new List<byte>();
- result.Add(blockType);
+ var result = new List<byte>
+ {
+ blockType
+ };
result.AddRange(Data);
return result.ToArray();
}
diff --git a/FdsBlockFileHeader.cs b/FdsBlockFileHeader.cs index 60994a6..8d140e6 100644 --- a/FdsBlockFileHeader.cs +++ b/FdsBlockFileHeader.cs @@ -1,10 +1,11 @@ using System;
using System.Linq;
using System.Runtime.InteropServices;
+using System.Text;
namespace com.clusterrr.Famicom.Containers
{
- [StructLayout(LayoutKind.Sequential, Size = 18, Pack = 1, CharSet = CharSet.Ansi)]
+ [StructLayout(LayoutKind.Sequential, Size = 18, Pack = 1)]
public class FdsBlockFileHeader : IFdsBlock, IEquatable<FdsBlockFileHeader>
{
public enum Kind
@@ -15,7 +16,8 @@ namespace com.clusterrr.Famicom.Containers }
[MarshalAs(UnmanagedType.U1)]
- private byte blockType = 3;
+ private readonly byte blockType = 3;
+ public byte ValidTypeID { get => 3; }
/// <summary>
/// True if block type ID is valid
/// </summary>
@@ -31,8 +33,8 @@ namespace com.clusterrr.Famicom.Containers public byte FileIndicateCode { get => fileIndicateCode; set => fileIndicateCode = value; }
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- private char[] fileName;
- public string FileName { get => new string(fileName).Trim(new char[] { '\0', ' ' }); set => fileName = value.PadRight(0).ToCharArray(0, value.Length > 8 ? 8 : value.Length); }
+ private byte[] fileName;
+ public string FileName { get => Encoding.ASCII.GetString(fileName).Trim(new char[] { '\0', ' ' }); set => fileName = Encoding.ASCII.GetBytes(value.PadRight(8)).Take(8).ToArray(); }
[MarshalAs(UnmanagedType.U2)]
// the destination address when loading
diff --git a/FdsBlockParser.cs b/FdsBlockParser.cs deleted file mode 100644 index 23722d7..0000000 --- a/FdsBlockParser.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Text;
-
-namespace com.clusterrr.Famicom.Containers
-{
- public static class FdsBlockParser
- {
- public static IFdsBlock FromBytes(byte[] data)
- {
- // Check block type
- switch (data[0])
- {
- case 0: return FdsBlockDiskInfo.FromBytes(data);
- case 1: return FdsBlockFileAmount.FromBytes(data);
- case 2: return FdsBlockFileHeader.FromBytes(data);
- case 3: return FdsBlockFileData.FromBytes(data);
- default: throw new InvalidDataException("Invalid FDS block type");
- }
- }
- }
-}
diff --git a/FdsDiskSide.cs b/FdsDiskSide.cs index 8ea8e91..049b00a 100644 --- a/FdsDiskSide.cs +++ b/FdsDiskSide.cs @@ -6,7 +6,7 @@ namespace com.clusterrr.Famicom.Containers {
public class FdsDiskSide
{
- FdsBlockDiskInfo diskInfoBlock;
+ readonly FdsBlockDiskInfo diskInfoBlock;
/// <summary>
/// Disk info block
/// </summary>
@@ -76,13 +76,13 @@ namespace com.clusterrr.Famicom.Containers /// </summary>
public byte Price { get => diskInfoBlock.Price; set => diskInfoBlock.Price = value; }
- FdsBlockFileAmount fileAmountBlock;
+ readonly FdsBlockFileAmount fileAmountBlock;
/// <summary>
/// Non-hidden file amount
/// </summary>
public byte FileAmount { get => fileAmountBlock.FileAmount; set => fileAmountBlock.FileAmount = value; }
- IList<FdsDiskFile> files;
+ readonly IList<FdsDiskFile> files;
/// <summary>
/// Files on disk
@@ -154,9 +154,11 @@ namespace com.clusterrr.Famicom.Containers public IEnumerable<IFdsBlock> GetBlocks()
{
- var blocks = new List<IFdsBlock>();
- blocks.Add(diskInfoBlock);
- blocks.Add(fileAmountBlock);
+ var blocks = new List<IFdsBlock>
+ {
+ diskInfoBlock,
+ fileAmountBlock
+ };
blocks.AddRange(files.SelectMany(f => new IFdsBlock[] { f.HeaderBlock, f.DataBlock }));
return blocks;
}
diff --git a/IFdsBlock.cs b/IFdsBlock.cs index 8943469..37a4ac0 100644 --- a/IFdsBlock.cs +++ b/IFdsBlock.cs @@ -2,9 +2,8 @@ {
public interface IFdsBlock
{
+ byte ValidTypeID { get; }
bool IsValid { get; }
- bool CrcOk { get; set; }
- bool EndOfHeadMeet { get; set; }
uint Length { get; }
byte[] ToBytes();
}
|