Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/nes-containers.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-11-04 10:15:06 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-11-04 10:15:06 +0300
commit719ec3ac1d864319148ec8a9679c97616b743765 (patch)
tree93e6929b26f6fcbad29965c2423bfb6246400a10 /UnifFile.cs
parentbc05122b12da25b30f011d5ef6a2277863413b50 (diff)
Refactoring
Diffstat (limited to 'UnifFile.cs')
-rw-r--r--UnifFile.cs135
1 files changed, 79 insertions, 56 deletions
diff --git a/UnifFile.cs b/UnifFile.cs
index b8532bf..c2d69a6 100644
--- a/UnifFile.cs
+++ b/UnifFile.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
@@ -14,7 +15,7 @@ namespace com.clusterrr.Famicom.Containers
/// <summary>
/// UNIF fields
/// </summary>
- public Dictionary<string, byte[]> Fields = new Dictionary<string, byte[]>();
+ private Dictionary<string, byte[]> fields = new Dictionary<string, byte[]>();
/// <summary>
/// UNIF version
@@ -22,6 +23,28 @@ namespace com.clusterrr.Famicom.Containers
public int Version = 7;
/// <summary>
+ /// Get/set UNIF field
+ /// </summary>
+ /// <param name="key">UNIF data block key</param>
+ /// <returns></returns>
+ public IEnumerable<byte> this[string key]
+ {
+ get
+ {
+ if (key.Length != 4) throw new ArgumentException("UNIF data block key must be 4 characters long");
+ return Array.AsReadOnly(fields[key]);
+ }
+ set
+ {
+ if (key.Length != 4) throw new ArgumentException("UNIF data block key must be 4 characters long");
+ if (value == null && fields.ContainsKey(key))
+ fields.Remove(key);
+ else
+ fields[key] = value.ToArray();
+ }
+ }
+
+ /// <summary>
/// Constructor to create empty UnifFile object
/// </summary>
public UnifFile()
@@ -38,7 +61,7 @@ namespace com.clusterrr.Famicom.Containers
var header = new byte[32];
Array.Copy(data, header, 32);
if (header[0] != 'U' || header[1] != 'N' || header[2] != 'I' || header[3] != 'F')
- throw new InvalidDataException("Invalid UNIF file");
+ throw new InvalidDataException("Invalid UNIF header");
Version = header[4] | (header[5] << 8) | (header[6] << 16) | (header[7] << 24);
int pos = 32;
while (pos < data.Length)
@@ -49,7 +72,7 @@ namespace com.clusterrr.Famicom.Containers
pos += 4;
var fieldData = new byte[length];
Array.Copy(data, pos, fieldData, 0, length);
- Fields[type] = fieldData;
+ fields[type] = fieldData;
pos += length;
}
}
@@ -87,15 +110,15 @@ namespace com.clusterrr.Famicom.Containers
header[7] = (byte)((Version >> 24) & 0xFF);
data.AddRange(header);
- foreach (var name in Fields.Keys)
+ foreach (var name in fields.Keys)
{
data.AddRange(Encoding.UTF8.GetBytes(name));
- int len = Fields[name].Length;
+ int len = fields[name].Length;
data.Add((byte)(len & 0xFF));
data.Add((byte)((len >> 8) & 0xFF));
data.Add((byte)((len >> 16) & 0xFF));
data.Add((byte)((len >> 24) & 0xFF));
- data.AddRange(Fields[name]);
+ data.AddRange(fields[name]);
}
File.WriteAllBytes(fileName, data.ToArray());
@@ -136,14 +159,14 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (Fields.ContainsKey("MAPR"))
- return UTF8NToString(Fields["MAPR"]);
+ if (fields.ContainsKey("MAPR"))
+ return UTF8NToString(fields["MAPR"]);
else
return null;
}
set
{
- Fields["MAPR"] = StringToUTF8N(value);
+ fields["MAPR"] = StringToUTF8N(value);
}
}
@@ -155,18 +178,18 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (!Fields.ContainsKey("DINF"))
+ if (!fields.ContainsKey("DINF"))
return null;
- return UTF8NToString(Fields["DINF"], 100);
+ return UTF8NToString(fields["DINF"], 100);
}
set
{
- if (!Fields.ContainsKey("DINF"))
- Fields["DINF"] = new byte[204];
+ if (!fields.ContainsKey("DINF"))
+ fields["DINF"] = new byte[204];
for (int i = 0; i < 100; i++)
- Fields["DINF"][i] = 0;
+ fields["DINF"][i] = 0;
var name = StringToUTF8N(value);
- Array.Copy(name, 0, Fields["DINF"], 0, Math.Min(100, name.Length));
+ Array.Copy(name, 0, fields["DINF"], 0, Math.Min(100, name.Length));
}
}
@@ -177,18 +200,18 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (!Fields.ContainsKey("DINF"))
+ if (!fields.ContainsKey("DINF"))
return null;
- return UTF8NToString(Fields["DINF"], 100, 104);
+ return UTF8NToString(fields["DINF"], 100, 104);
}
set
{
- if (!Fields.ContainsKey("DINF"))
- Fields["DINF"] = new byte[204];
+ if (!fields.ContainsKey("DINF"))
+ fields["DINF"] = new byte[204];
for (int i = 104; i < 104 + 100; i++)
- Fields["DINF"][i] = 0;
+ fields["DINF"][i] = 0;
var name = StringToUTF8N(value);
- Array.Copy(name, 0, Fields["DINF"], 104, Math.Min(100, name.Length));
+ Array.Copy(name, 0, fields["DINF"], 104, Math.Min(100, name.Length));
}
}
@@ -199,22 +222,22 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (!Fields.ContainsKey("DINF"))
+ if (!fields.ContainsKey("DINF"))
return new DateTime();
return new DateTime(
- year: Fields["DINF"][102] | (Fields["DINF"][103] << 8),
- month: Fields["DINF"][101],
- day: Fields["DINF"][100]
+ year: fields["DINF"][102] | (fields["DINF"][103] << 8),
+ month: fields["DINF"][101],
+ day: fields["DINF"][100]
);
}
set
{
- if (!Fields.ContainsKey("DINF"))
- Fields["DINF"] = new byte[204];
- Fields["DINF"][100] = (byte)value.Day;
- Fields["DINF"][101] = (byte)value.Month;
- Fields["DINF"][102] = (byte)(value.Year & 0xFF);
- Fields["DINF"][103] = (byte)(value.Year >> 8);
+ if (!fields.ContainsKey("DINF"))
+ fields["DINF"] = new byte[204];
+ fields["DINF"][100] = (byte)value.Day;
+ fields["DINF"][101] = (byte)value.Month;
+ fields["DINF"][102] = (byte)(value.Year & 0xFF);
+ fields["DINF"][103] = (byte)(value.Year >> 8);
}
}
@@ -225,14 +248,14 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (Fields.ContainsKey("NAME"))
- return UTF8NToString(Fields["NAME"]);
+ if (fields.ContainsKey("NAME"))
+ return UTF8NToString(fields["NAME"]);
else
return null;
}
set
{
- Fields["NAME"] = StringToUTF8N(value);
+ fields["NAME"] = StringToUTF8N(value);
}
}
@@ -243,14 +266,14 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (Fields.ContainsKey("TVCI") && Fields["TVCI"].Length > 0)
- return (NesFile.Timing)Fields["TVCI"][0];
+ if (fields.ContainsKey("TVCI") && fields["TVCI"].Length > 0)
+ return (NesFile.Timing)fields["TVCI"][0];
else
return NesFile.Timing.Ntsc;
}
set
{
- Fields["TVCI"] = new byte[] { (byte)value };
+ fields["TVCI"] = new byte[] { (byte)value };
}
}
@@ -261,14 +284,14 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (Fields.ContainsKey("CTRL") && Fields["CTRL"].Length > 0)
- return (Controller)Fields["CTRL"][0];
+ if (fields.ContainsKey("CTRL") && fields["CTRL"].Length > 0)
+ return (Controller)fields["CTRL"][0];
else
return Controller.None;
}
set
{
- Fields["CTRL"] = new byte[] { (byte)value };
+ fields["CTRL"] = new byte[] { (byte)value };
}
}
@@ -279,32 +302,32 @@ namespace com.clusterrr.Famicom.Containers
{
get
{
- if (Fields.ContainsKey("BATR") && Fields["BATR"].Length > 0)
- return Fields["BATR"][0] != 0;
+ if (fields.ContainsKey("BATR") && fields["BATR"].Length > 0)
+ return fields["BATR"][0] != 0;
else
return false;
}
set
{
- Fields["BATR"] = new byte[] { (byte)(value ? 1 : 0) };
+ fields["BATR"] = new byte[] { (byte)(value ? 1 : 0) };
}
}
/// <summary>
/// Mirroring type
/// </summary>
- public NesFile.MirroringType Mirroring
+ public MirroringType Mirroring
{
get
{
- if (Fields.ContainsKey("MIRR") && Fields["MIRR"].Length > 0)
- return (NesFile.MirroringType)Fields["MIRR"][0];
+ if (fields.ContainsKey("MIRR") && fields["MIRR"].Length > 0)
+ return (MirroringType)fields["MIRR"][0];
else
- return NesFile.MirroringType.Unknown;
+ return MirroringType.Unknown;
}
set
{
- Fields["MIRR"] = new byte[] { (byte)value };
+ fields["MIRR"] = new byte[] { (byte)value };
}
}
@@ -313,22 +336,22 @@ namespace com.clusterrr.Famicom.Containers
/// </summary>
public void CalculateAndStoreCRCs()
{
- foreach (var key in Fields.Keys.Where(k => k.StartsWith("PRG")))
+ foreach (var key in fields.Keys.Where(k => k.StartsWith("PRG")))
{
var num = key[3];
- var crc32 = Crc32Calculator.CalculateCRC32(Fields[key]);
- Fields[$"PCK{num}"] = new byte[] {
+ var crc32 = Crc32Calculator.CalculateCRC32(fields[key]);
+ fields[$"PCK{num}"] = new byte[] {
(byte)(crc32 & 0xFF),
(byte)((crc32 >> 8) & 0xFF),
(byte)((crc32 >> 16) & 0xFF),
(byte)((crc32 >> 24) & 0xFF)
};
}
- foreach (var key in Fields.Keys.Where(k => k.StartsWith("CHR")))
+ foreach (var key in fields.Keys.Where(k => k.StartsWith("CHR")))
{
var num = key[3];
- var crc32 = Crc32Calculator.CalculateCRC32(Fields[key]);
- Fields[$"CCK{num}"] = new byte[] {
+ var crc32 = Crc32Calculator.CalculateCRC32(fields[key]);
+ fields[$"CCK{num}"] = new byte[] {
(byte)(crc32 & 0xFF),
(byte)((crc32 >> 8) & 0xFF),
(byte)((crc32 >> 16) & 0xFF),
@@ -343,8 +366,8 @@ namespace com.clusterrr.Famicom.Containers
/// <returns></returns>
public uint CalculateCRC32()
=> Crc32Calculator.CalculateCRC32(
- Enumerable.Concat(Fields.Where(k => k.Key.StartsWith("PRG")).OrderBy(k => k.Key).SelectMany(i => i.Value),
- Fields.Where(k => k.Key.StartsWith("CHR")).OrderBy(k => k.Key).SelectMany(i => i.Value)).ToArray()
+ Enumerable.Concat(fields.Where(k => k.Key.StartsWith("PRG")).OrderBy(k => k.Key).SelectMany(i => i.Value),
+ fields.Where(k => k.Key.StartsWith("CHR")).OrderBy(k => k.Key).SelectMany(i => i.Value)).ToArray()
);
/// <summary>