cleanup & format

This commit is contained in:
Lucent 2023-10-27 23:47:09 +02:00
parent c45fe707d2
commit 92ff4d3f28
2 changed files with 68 additions and 65 deletions

View file

@ -1,6 +1,4 @@
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Windows.Forms;
namespace SharpCreditsCrediter namespace SharpCreditsCrediter
{ {

View file

@ -2,88 +2,91 @@
namespace SharpCreditsCrediter namespace SharpCreditsCrediter
{ {
internal class ROMFile internal class ROMFile
{ {
private FileStream? romFile; private FileStream? romFile;
const long ZM_credits_start = 0x54C10C; const long ZM_credits_start = 0x54C10C;
const long ZM_credits_end = 0x54E2A9; const long ZM_credits_end = 0x54E2A9;
const long MF_credits_start = 0x74B0B0; const long MF_credits_start = 0x74B0B0;
const long MF_credits_end = 0x74DC25; const long MF_credits_end = 0x74DC25;
private bool isZM = true; private bool isZM = true;
private readonly Dictionary<char, int> textToIntTable = new Dictionary<char, int>() { private readonly Dictionary<char, int> textToIntTable = new Dictionary<char, int>() {
{ ' ', 0x20 }, { 'A', 0x41 }, { 'B', 0x42 }, { 'C', 0x43 }, { 'D', 0x44 }, { 'E', 0x45 }, { 'F', 0x46 }, { 'G', 0x47 }, { ' ', 0x20 }, { 'A', 0x41 }, { 'B', 0x42 }, { 'C', 0x43 }, { 'D', 0x44 }, { 'E', 0x45 }, { 'F', 0x46 }, { 'G', 0x47 },
{ 'H', 0x48 }, { 'I', 0x49 }, { 'J', 0x4A }, { 'K', 0x4B }, { 'L', 0x4C }, { 'M', 0x4D }, { 'N', 0x4E }, { 'O', 0x4F }, { 'H', 0x48 }, { 'I', 0x49 }, { 'J', 0x4A }, { 'K', 0x4B }, { 'L', 0x4C }, { 'M', 0x4D }, { 'N', 0x4E }, { 'O', 0x4F },
{ 'P', 0x50 }, { 'Q', 0x51 }, { 'R', 0x52 }, { 'S', 0x53 }, { 'T', 0x54 }, { 'U', 0x55 }, { 'V', 0x56 }, { 'W', 0x57 }, { 'P', 0x50 }, { 'Q', 0x51 }, { 'R', 0x52 }, { 'S', 0x53 }, { 'T', 0x54 }, { 'U', 0x55 }, { 'V', 0x56 }, { 'W', 0x57 },
{ 'X', 0x58 }, { 'Y', 0x59 }, { 'Z', 0x5A }, { 'a', 0x61 }, { 'b', 0x62 }, { 'c', 0x63 }, { 'd', 0x64 }, { 'e', 0x65 }, { 'X', 0x58 }, { 'Y', 0x59 }, { 'Z', 0x5A }, { 'a', 0x61 }, { 'b', 0x62 }, { 'c', 0x63 }, { 'd', 0x64 }, { 'e', 0x65 },
{ 'f', 0x66 }, { 'g', 0x67 }, { 'h', 0x68 }, { 'i', 0x69 }, { 'j', 0x6A }, { 'k', 0x6B }, { 'l', 0x6C }, { 'm', 0x6D }, { 'f', 0x66 }, { 'g', 0x67 }, { 'h', 0x68 }, { 'i', 0x69 }, { 'j', 0x6A }, { 'k', 0x6B }, { 'l', 0x6C }, { 'm', 0x6D },
{ 'n', 0x6E }, { 'o', 0x6F }, { 'p', 0x70 }, { 'q', 0x71 }, { 'r', 0x72 }, { 's', 0x73 }, { 't', 0x74 }, { 'u', 0x75 }, { 'n', 0x6E }, { 'o', 0x6F }, { 'p', 0x70 }, { 'q', 0x71 }, { 'r', 0x72 }, { 's', 0x73 }, { 't', 0x74 }, { 'u', 0x75 },
{ 'v', 0x76 }, { 'w', 0x77 }, { 'x', 0x78 }, { 'y', 0x79 }, { 'z', 0x7A }, { '&', 0x26 } { 'v', 0x76 }, { 'w', 0x77 }, { 'x', 0x78 }, { 'y', 0x79 }, { 'z', 0x7A }, { '&', 0x26 }
}; };
private readonly Dictionary<int, string> intToTextTable = new Dictionary<int, string>() { private readonly Dictionary<int, string> intToTextTable = new Dictionary<int, string>() {
{ 0x20, " " }, { 0x41, "A" }, { 0x42, "B" }, { 0x43, "C" }, { 0x44, "D" }, { 0x45, "E" }, { 0x46, "F" }, { 0x47, "G" }, { 0x20, " " }, { 0x41, "A" }, { 0x42, "B" }, { 0x43, "C" }, { 0x44, "D" }, { 0x45, "E" }, { 0x46, "F" }, { 0x47, "G" },
{ 0x48, "H" }, { 0x49, "I" }, { 0x4A, "J" }, { 0x4B, "K" }, { 0x4C, "L" }, { 0x4D, "M" }, { 0x4E, "N" }, { 0x4F, "O" }, { 0x48, "H" }, { 0x49, "I" }, { 0x4A, "J" }, { 0x4B, "K" }, { 0x4C, "L" }, { 0x4D, "M" }, { 0x4E, "N" }, { 0x4F, "O" },
{ 0x50, "P" }, { 0x51, "Q" }, { 0x52, "R" }, { 0x53, "S" }, { 0x54, "T" }, { 0x55, "U" }, { 0x56, "V" }, { 0x57, "W" }, { 0x50, "P" }, { 0x51, "Q" }, { 0x52, "R" }, { 0x53, "S" }, { 0x54, "T" }, { 0x55, "U" }, { 0x56, "V" }, { 0x57, "W" },
{ 0x58, "X" }, { 0x59, "Y" }, { 0x5A, "Z" }, { 0x61, "a" }, { 0x62, "b" }, { 0x63, "c" }, { 0x64, "d" }, { 0x65, "e" }, { 0x58, "X" }, { 0x59, "Y" }, { 0x5A, "Z" }, { 0x61, "a" }, { 0x62, "b" }, { 0x63, "c" }, { 0x64, "d" }, { 0x65, "e" },
{ 0x66, "f" }, { 0x67, "g" }, { 0x68, "h" }, { 0x69, "i" }, { 0x6A, "j" }, { 0x6B, "k" }, { 0x6C, "l" }, { 0x6D, "m" }, { 0x66, "f" }, { 0x67, "g" }, { 0x68, "h" }, { 0x69, "i" }, { 0x6A, "j" }, { 0x6B, "k" }, { 0x6C, "l" }, { 0x6D, "m" },
{ 0x6E, "n" }, { 0x6F, "o" }, { 0x70, "p" }, { 0x71, "q" }, { 0x72, "r" }, { 0x73, "s" }, { 0x74, "t" }, { 0x75, "u" }, { 0x6E, "n" }, { 0x6F, "o" }, { 0x70, "p" }, { 0x71, "q" }, { 0x72, "r" }, { 0x73, "s" }, { 0x74, "t" }, { 0x75, "u" },
{ 0x76, "v" }, { 0x77, "w" }, { 0x78, "x" }, { 0x79, "y" }, { 0x7A, "z" }, { 0x26, "&" } { 0x76, "v" }, { 0x77, "w" }, { 0x78, "x" }, { 0x79, "y" }, { 0x7A, "z" }, { 0x26, "&" }
}; };
private string intToText(int value) private string intToText(int value)
{ {
try { try
{
return intToTextTable[value]; return intToTextTable[value];
} catch(Exception) }
catch (Exception)
{ {
return String.Format("{0:X2}", Convert.ToInt32(value)); return String.Format("{0:X2}", Convert.ToInt32(value));
} }
} }
private int textToInt(char value) private int textToInt(char value)
{ {
try try
{ {
return textToIntTable[value]; return textToIntTable[value];
} }
catch (Exception) catch (Exception)
{ {
return 0; return 0;
} }
} }
public ROMFile(String pathToROM) public ROMFile(String pathToROM)
{ {
this.changeFile(pathToROM); this.changeFile(pathToROM);
} }
private void probeGame() private void probeGame()
{ {
if(this.isLoaded()) if (this.isLoaded())
{ {
#pragma warning disable CS8602 // isLoaded checks already if romFile is null or not #pragma warning disable CS8602 // isLoaded checks already if romFile is null or not
byte[] gameCodeBuf = new byte[4]; byte[] gameCodeBuf = new byte[4];
romFile.Position = 0xAC; romFile.Position = 0xAC;
if (romFile.Read(gameCodeBuf, 0, 4) == 4) if (romFile.Read(gameCodeBuf, 0, 4) == 4)
{ {
string gameCode = Encoding.ASCII.GetString(gameCodeBuf); string gameCode = Encoding.ASCII.GetString(gameCodeBuf);
if (gameCode == "AMTE") if (gameCode == "AMTE")
{ {
this.isZM = false; this.isZM = false;
} else }
else
{ {
this.isZM = true; this.isZM = true;
} }
} }
#pragma warning restore CS8602 #pragma warning restore CS8602
} }
} }
public void changeFile(String pathToROM) public void changeFile(String pathToROM)
{ {
if (this.isLoaded()) this.Dispose(); if (this.isLoaded()) this.Dispose();
romFile = File.Open(pathToROM, FileMode.Open); romFile = File.Open(pathToROM, FileMode.Open);
this.probeGame(); this.probeGame();
} }
public bool isLoaded() public bool isLoaded()
{ {
@ -95,12 +98,12 @@ namespace SharpCreditsCrediter
this.isZM = isZM; this.isZM = isZM;
} }
public bool getisZM() public bool getisZM()
{ {
return isZM; return isZM;
} }
public void Dispose() public void Dispose()
{ {
romFile?.Dispose(); romFile?.Dispose();
} }
@ -112,26 +115,26 @@ namespace SharpCreditsCrediter
{ {
long credits_start = isZM ? ZM_credits_start : MF_credits_start; long credits_start = isZM ? ZM_credits_start : MF_credits_start;
long credits_end = isZM ? ZM_credits_end : MF_credits_end; long credits_end = isZM ? ZM_credits_end : MF_credits_end;
romFile.Position = credits_start; romFile.Position = credits_start;
bool end_reached = false; bool end_reached = false;
while (romFile.Position < credits_end && !end_reached) while (romFile.Position < credits_end && !end_reached)
{ {
int curChar = romFile.ReadByte(); int curChar = romFile.ReadByte();
if (curChar == 6) end_reached = true; if (curChar == 6) end_reached = true;
credits += intToText(curChar) + " "; credits += intToText(curChar) + " ";
for (int i = 0; i < 35; i++) for (int i = 0; i < 35; i++)
{ {
curChar = romFile.ReadByte(); curChar = romFile.ReadByte();
if (curChar != 0) if (curChar != 0)
credits += intToText(curChar); credits += intToText(curChar);
} }
credits += Environment.NewLine; credits += Environment.NewLine;
} }
} }
return credits; return credits;
} }
@ -149,7 +152,8 @@ namespace SharpCreditsCrediter
var currentLine = credits.ReadLine(); var currentLine = credits.ReadLine();
if (currentLine != null) if (currentLine != null)
{ {
if (currentLine.Length > 38) { if (currentLine.Length > 38)
{
currentLine = currentLine[..38]; currentLine = currentLine[..38];
} }
@ -177,16 +181,17 @@ namespace SharpCreditsCrediter
} }
} }
romFile.Flush(); romFile.Flush();
return true; return true;
} }
return false; return false;
} catch (Exception) }
catch (Exception)
{ {
return false; return false;
} }
} }
} }
} }