Skip to content

Commit

Permalink
Text encoding fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ClusterM committed Sep 4, 2023
1 parent 0859cf1 commit 7bd25fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
22 changes: 14 additions & 8 deletions FdsBlockDiskInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace com.clusterrr.Famicom.Containers
[StructLayout(LayoutKind.Sequential, Size = 56, Pack = 1)]
public class FdsBlockDiskInfo : IFdsBlock, IEquatable<FdsBlockDiskInfo>
{
static Encoding textEncoding = Encoding.GetEncoding("ISO-8859-1");

/// <summary>
/// Disk side
/// </summary>
Expand Down Expand Up @@ -662,11 +664,11 @@ public enum DiskTypesOther
public bool IsValid { get => blockType == ValidTypeID; }

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)]
readonly byte[] diskVerification = Encoding.ASCII.GetBytes("*NINTENDO-HVC*");
readonly byte[] diskVerification = textEncoding.GetBytes("*NINTENDO-HVC*");
/// <summary>
/// Literal ASCII string: *NINTENDO-HVC*
/// </summary>
public string DiskVerification => Encoding.ASCII.GetString(diskVerification).Trim(new char[] { '\0', ' ' }); /*set => diskVerification = value.PadRight(14).ToCharArray(0, value.Length > 14 ? 14 : value.Length);*/
public string DiskVerification => textEncoding.GetString(diskVerification).Trim(new char[] { '\0', ' ' }); /*set => diskVerification = value.PadRight(14).ToCharArray(0, value.Length > 14 ? 14 : value.Length);*/

[MarshalAs(UnmanagedType.U1)]
private byte manufacturerCode;
Expand All @@ -680,17 +682,20 @@ public enum DiskTypesOther
/// <summary>
/// 3-letter ASCII code per game (e.g. ZEL for The Legend of Zelda)
/// </summary>
public string? GameName {
public string? GameName
{
get
{
if (gameName.Where(b => b != 0).Any())
return Encoding.ASCII.GetString(gameName).Trim(new char[] { '\0', ' ' });
return textEncoding.GetString(gameName).TrimEnd(new char[] { '\0' });
else
return null;
return null;
}
set {
set
{
if (value?.Length > 3) throw new InvalidDataException($"Game name \"{value}\" too long, must be <= 3");
if (value != null)
gameName = Encoding.ASCII.GetBytes(value.PadRight(3)).Take(3).ToArray();
gameName = textEncoding.GetBytes(value.PadRight(3, '\0')).Take(3).ToArray();
else
gameName = new byte[3];
}
Expand Down Expand Up @@ -823,7 +828,8 @@ public DateTime? ManufacturingDate
(byte)(((value.Value.Month) % 10) | (((value.Value.Month) / 10) << 4)),
(byte)(((value.Value.Day) % 10) | (((value.Value.Day) / 10) << 4))
};
} else
}
else
{
manufacturingDate = new byte[3];
}
Expand Down
13 changes: 11 additions & 2 deletions FdsBlockFileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace com.clusterrr.Famicom.Containers
[StructLayout(LayoutKind.Sequential, Size = 16, Pack = 1)]
public class FdsBlockFileHeader : IFdsBlock, IEquatable<FdsBlockFileHeader>
{
static Encoding textEncoding = Encoding.GetEncoding("ISO-8859-1");

/// <summary>
/// Kind of the file
/// </summary>
Expand Down Expand Up @@ -57,11 +59,18 @@ public enum Kind
public byte FileIndicateCode { get => fileIndicateCode; set => fileIndicateCode = value; }

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
private byte[] fileName = Encoding.ASCII.GetBytes("FILENAME");
private byte[] fileName = textEncoding.GetBytes("FILENAME");
/// <summary>
/// Filename
/// </summary>
public string FileName { get => Encoding.ASCII.GetString(fileName).Trim(new char[] { '\0', ' ' }); set => fileName = Encoding.ASCII.GetBytes(value.PadRight(8)).Take(8).ToArray(); }
public string FileName
{
get => textEncoding.GetString(fileName).TrimEnd(new char[] { '\0' }); set
{
if (value.Length > 8) throw new InvalidDataException($"Filename \"{value}\" too long, must be <= 8");
fileName = textEncoding.GetBytes(value.PadRight(8, '\0')).Take(8).ToArray();
}
}

[MarshalAs(UnmanagedType.U2)]
// the destination address when loading
Expand Down
6 changes: 3 additions & 3 deletions NesContainers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
<PackageProjectUrl>https://github.com/ClusterM/nes-containers</PackageProjectUrl>
<Copyright>Alexey Cluster, 2023</Copyright>
<Description>A simple .NET Standard 2.0 library for reading and modifying NES/Famicom ROM containers: .nes (iNES, NES 2.0), .unf (UNIF), and .fds (Famicom Disk System images).</Description>
<Version>1.1.8</Version>
<Version>1.1.9</Version>
<RepositoryUrl>https://github.com/ClusterM/nes-containers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>NES;Famicom;FamicomDiskSystem</PackageTags>
<AssemblyVersion>1.1.8</AssemblyVersion>
<FileVersion>1.1.8</FileVersion>
<AssemblyVersion>1.1.9</AssemblyVersion>
<FileVersion>1.1.9</FileVersion>
<Authors>Alexey Cluster</Authors>
</PropertyGroup>

Expand Down

0 comments on commit 7bd25fb

Please sign in to comment.