SharpCreditsCrediter/MainForm.cs
2023-10-27 22:36:27 +02:00

134 lines
No EOL
4.9 KiB
C#

using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace SharpCreditsCrediter
{
public partial class MainForm : Form
{
private ROMFile? curROM;
private string? curROMPath;
public MainForm()
{
InitializeComponent();
}
private void bLoadROM_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "GBA ROMs (*.gba)|*.gba",
Title = "Open the ROM"
};
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
if (curROM != null)
{
curROM.changeFile(ofd.FileName);
}
else
{
curROM = new ROMFile(ofd.FileName);
}
curROMPath = ofd.FileName;
bExtract.Enabled = bImport.Enabled = curROM.isLoaded();
rIsZM.Checked = curROM.getisZM();
rIsFusion.Checked = !curROM.getisZM();
MessageBox.Show("ROM loaded successfully.\nProbed game: " + (curROM.getisZM() ? "Zero Mission" : "Fusion"), "SCC", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Cannot load ROM: " + ex.Message);
}
}
}
private void bExtract_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "Credits text (*.txt)|*.txt",
Title = "Open the text file",
CheckFileExists = false
};
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
this.UseWaitCursor = true;
FileStream creditsFile = new FileStream(ofd.FileName, FileMode.Create);
#pragma warning disable CS8602 // if this button is enabled, curROM is already != null
creditsFile.Write(Encoding.ASCII.GetBytes(curROM.getCredits()));
#pragma warning restore CS8602
creditsFile.Dispose();
this.UseWaitCursor = false;
MessageBox.Show("Credits exported successfully.", "SCC", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
this.UseWaitCursor = false;
MessageBox.Show("Could not open file: " + ex.Message, "SCC", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void bImport_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "Credits text (*.txt)|*.txt",
Title = "Open the text file"
};
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
StreamReader creditsFile = new StreamReader(ofd.FileName);
OpenFileDialog ofdOutput = new OpenFileDialog()
{
Filter = "GBA ROMs (*.gba)|*.gba",
Title = "Open the output ROM",
CheckFileExists = false
};
if (ofdOutput.ShowDialog() == DialogResult.OK)
{
this.UseWaitCursor = true;
#pragma warning disable CS8602 // if this button is enabled, curROM
#pragma warning disable CS8604 // and curROMPath are already != null
curROM.Dispose();
File.Copy(curROMPath, ofdOutput.FileName, true);
curROM.changeFile(ofdOutput.FileName);
#pragma warning restore CS8604
if (curROM.writeCredits(creditsFile))
{
this.UseWaitCursor = false;
MessageBox.Show("Credits imported successfully.", "SCC", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
this.UseWaitCursor = false;
MessageBox.Show("Failed to import the credits.", "SCC", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#pragma warning restore CS8602
curROM.changeFile(curROMPath);
creditsFile.Dispose();
}
}
catch (Exception ex)
{
this.UseWaitCursor = false;
MessageBox.Show("Could not open file: " + ex.Message, "SCC", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}