Skip to content

Commit

Permalink
1.01
Browse files Browse the repository at this point in the history
Added an icon to the exe (oops!)
Added dialog for confirmation when creating duplicate save data
Added dialog for clicking "Clear All" with unsaved edits
Saving last used directory for next time program is launched
Each browse button now defaults to open in the last folder for that side, instead of either side
Implemented proper text decoding
Resized BRAM windows to see longer names
Disable window resizing
  • Loading branch information
Widdiful committed Jan 24, 2022
1 parent 55f70cc commit e41170e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 39 deletions.
12 changes: 12 additions & 0 deletions BRAM_Manager/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="BRAM_Manager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<userSettings>
<BRAM_Manager.Properties.Settings>
<setting name="savedInitialDirectory" serializeAs="String">
<value>c:\\</value>
</setting>
</BRAM_Manager.Properties.Settings>
</userSettings>
</configuration>
6 changes: 6 additions & 0 deletions BRAM_Manager/BRAM_Manager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -81,5 +84,8 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="icon.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
25 changes: 14 additions & 11 deletions BRAM_Manager/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 86 additions & 17 deletions BRAM_Manager/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,27 @@

namespace BRAM_Manager {
public partial class MainForm : Form {
public string initialDirectory = "c:\\";
public string savedInitialDirectory = "c:\\";
public BRAM leftBRAM, rightBRAM;

public char[] characterMap = new char[] {
' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '♤', '♡', '◇', '♧', '○', '●', '/',
'\\', '円', '年', '月', '日', '時', '分', '秒', '◢', '◣', '◥', '◤', '→', '←', '↑', '↓',
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '¥', ']', '^', '_',
' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', ':', '}', '~', '☓',
' ', '。', '「', '」', '、', '・', 'を', 'ぁ', '╭', '─', '╮', '│', '│', '╰', '─', '╯',
'ー', 'あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', 'さ', 'し', 'す', 'せ', 'そ',
' ', '。', '「', '」', '、', '・', 'ヲ', 'ァ', '┬', '│', '┴', 'ォ', 'ャ', 'ュ', 'ョ', 'ッ',
'ー', 'ア', 'イ', 'ウ', 'エ', 'オ', 'カ', 'キ', 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ',
'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', 'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', 'マ',
'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', 'リ', 'ル', 'レ', 'ロ', 'ワ', 'ン', '゛', '゜',
'た', 'ち', 'つ', 'て', 'と', 'な', 'に', 'ぬ', 'ね', 'の', 'は', 'ひ', 'ふ', 'へ', 'ほ', 'ま',
'み', 'む', 'め', 'も', 'や', 'ゆ', 'よ', 'ら', 'り', 'る', 'れ', 'ろ', 'わ', 'ん', '゛', '©'};

public struct BRAM {
public byte[] data;
public byte[] header;
Expand All @@ -33,22 +51,30 @@ public struct BRAMEntry {

public MainForm() {
InitializeComponent();

savedInitialDirectory = Properties.Settings.Default.savedInitialDirectory;
}

//--------------------------------------------------
// Main functions
//--------------------------------------------------

// Opens a file browser and returns the path to the selected file
public string OpenFileBrowser() {
return OpenFileBrowser(savedInitialDirectory);
}

// Opens a file browser and returns the path to the selected file
public string OpenFileBrowser(string initialDirectory) {
using (OpenFileDialog openFileDialog = new OpenFileDialog()) {
openFileDialog.InitialDirectory = initialDirectory;
openFileDialog.Filter = "BRAM files (*.bup;*.sav)|*.bup;*.sav|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK) {
initialDirectory = Path.GetDirectoryName(openFileDialog.FileName);
savedInitialDirectory = Path.GetDirectoryName(openFileDialog.FileName);
Properties.Settings.Default.savedInitialDirectory = savedInitialDirectory;
Properties.Settings.Default.Save();
return openFileDialog.FileName;
}
}
Expand All @@ -63,15 +89,17 @@ public string SaveFileBrowser(string defaultDirectory = "", string defaultFileNa
if (defaultDirectory.Length > 0)
saveFileDialog.InitialDirectory = defaultDirectory;
else
saveFileDialog.InitialDirectory = initialDirectory;
saveFileDialog.InitialDirectory = savedInitialDirectory;
if (defaultFileName.Length > 0)
saveFileDialog.FileName = defaultFileName;
saveFileDialog.Filter = "BRAM files (*.bup;*.sav)|*.bup;*.sav|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;

if (saveFileDialog.ShowDialog() == DialogResult.OK) {
initialDirectory = Path.GetDirectoryName(saveFileDialog.FileName);
savedInitialDirectory = Path.GetDirectoryName(saveFileDialog.FileName);
Properties.Settings.Default.savedInitialDirectory = savedInitialDirectory;
Properties.Settings.Default.Save();
return saveFileDialog.FileName;
}

Expand All @@ -91,6 +119,18 @@ public byte[] ReadBytes(byte[] data, int from, int to) {
return result;
}

public string DecodeBytes(byte[] data) {
string result = string.Empty;

foreach (byte Byte in data) {
if (characterMap.Length > Byte) {
result += characterMap[Byte];
}
}

return result;
}

// Writes bytes to a byte array, given a starting location and a length
public void WriteBytes(byte[] input, byte[] dest, int startLoc, int length) {
int index = 0;
Expand All @@ -105,7 +145,17 @@ public BRAM ReadFile(string file) {
BRAM result = new BRAM();
result.saves = new List<BRAMEntry>();

string dataString = System.IO.File.ReadAllText(file);
string dataString;

try {
dataString = System.IO.File.ReadAllText(file);
}
catch (IOException)
{
MessageBox.Show("File is in use.");
return result;
}

byte[] data = System.IO.File.ReadAllBytes(file);
if (dataString.StartsWith("HUBM")) {
int length = data.Length;
Expand All @@ -127,7 +177,8 @@ public BRAM ReadFile(string file) {

newEntry.length = nextLength;
newEntry.data = ReadBytes(data, nextSaveIndex, nextSaveIndex + nextLength);
newEntry.name = System.Text.Encoding.UTF8.GetString(ReadBytes(data, nextSaveIndex + 6, nextSaveIndex + 16));
//newEntry.name = System.Text.Encoding.UTF8.GetString(ReadBytes(data, nextSaveIndex + 6, nextSaveIndex + 16));
newEntry.name = DecodeBytes(ReadBytes(data, nextSaveIndex + 6, nextSaveIndex + 16));
newEntry.startsAt = nextSaveIndex;

result.saves.Add(newEntry);
Expand All @@ -138,7 +189,7 @@ public BRAM ReadFile(string file) {
result.loaded = true;
}
else {
System.Windows.Forms.MessageBox.Show("File is not a valid BRAM file.");
MessageBox.Show("File is not a valid BRAM file.");
}

return result;
Expand Down Expand Up @@ -211,15 +262,17 @@ public void OpenAndLoadFile(string path, ref BRAM bram, ref ListBox list, ref Te
// Main interface

private void LeftBrowse_Click(object sender, EventArgs e) {
string path = OpenFileBrowser();
string openPath = LeftAddress.Text.Length > 0 ? Path.GetDirectoryName(LeftAddress.Text) : savedInitialDirectory;
string path = OpenFileBrowser(openPath);
if (path.Length == 0)
return;

OpenAndLoadFile(path, ref leftBRAM, ref LeftList, ref LeftAddress, ref LeftFreeSpace);
}

private void RightBrowse_Click(object sender, EventArgs e) {
string path = OpenFileBrowser();
string openPath = RightAddress.Text.Length > 0 ? Path.GetDirectoryName(RightAddress.Text) : savedInitialDirectory;
string path = OpenFileBrowser(openPath);
if (path.Length == 0)
return;

Expand Down Expand Up @@ -252,7 +305,7 @@ private void DeleteButton_Click(object sender, EventArgs e) {
selectedList = ref RightList;
}
else {
System.Windows.Forms.MessageBox.Show("Please select a file to delete.");
MessageBox.Show("Please select a file to delete.");
return;
}

Expand Down Expand Up @@ -282,7 +335,7 @@ private void DeleteButton_Click(object sender, EventArgs e) {

private void CopyButton_Click(object sender, EventArgs e) {
if (!(leftBRAM.loaded && rightBRAM.loaded)) {
System.Windows.Forms.MessageBox.Show("Please load two BRAM files.");
MessageBox.Show("Please load two BRAM files.");
return;
}

Expand All @@ -301,15 +354,24 @@ private void CopyButton_Click(object sender, EventArgs e) {
otherList = LeftList;
}
else {
System.Windows.Forms.MessageBox.Show("Please select a file to copy.");
MessageBox.Show("Please select a file to copy.");
return;
}

BRAMEntry entry = selectedBRAM.saves[selectedList.SelectedIndex];
if (otherBRAM.freeSpace < entry.length) {
System.Windows.Forms.MessageBox.Show("Not enough space in destination BRAM.");
MessageBox.Show("Not enough space in destination BRAM.");
return;
}

foreach (BRAMEntry otherEntry in otherBRAM.saves) {
if (otherEntry.name == entry.name) {
if (MessageBox.Show("Filename already exists, which can cause issues. Are you sure you want to copy?", "", MessageBoxButtons.YesNo) == DialogResult.No) {
return;
}
}
}

otherBRAM.saves.Add(entry);
otherList.Items.Add(String.Format("{0} ({1}B)", entry.name, entry.length));
otherBRAM.freeSpace -= entry.length;
Expand All @@ -333,7 +395,7 @@ private void UpButton_Click(object sender, EventArgs e) {
selectedList = ref RightList;
}
else {
System.Windows.Forms.MessageBox.Show("Please select a file to move.");
MessageBox.Show("Please select a file to move.");
return;
}

Expand All @@ -360,7 +422,7 @@ private void DownButton_Click(object sender, EventArgs e) {
selectedList = ref RightList;
}
else {
System.Windows.Forms.MessageBox.Show("Please select a file to move.");
MessageBox.Show("Please select a file to move.");
return;
}

Expand Down Expand Up @@ -396,6 +458,11 @@ private void newBRAMToolStripMenuItem_Click(object sender, EventArgs e) {
}

private void clearAllToolStripMenuItem_Click(object sender, EventArgs e) {
if (leftBRAM.edited || rightBRAM.edited) {
if (MessageBox.Show("There are unsaved changes. Are you sure you want to clear?", "", MessageBoxButtons.YesNo) == DialogResult.No) {
return;
}
}
leftBRAM = new BRAM();
rightBRAM = new BRAM();
LeftList.Items.Clear();
Expand All @@ -404,6 +471,8 @@ private void clearAllToolStripMenuItem_Click(object sender, EventArgs e) {
RightAddress.Text = "";
LeftFreeSpace.Text = "";
RightFreeSpace.Text = "";
LeftEdited.Visible = false;
RightEdited.Visible = false;
}

// Misc
Expand All @@ -415,7 +484,7 @@ private void LeftList_SelectedIndexChanged(object sender, EventArgs e) {

private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
if (leftBRAM.edited || rightBRAM.edited) {
if (MessageBox.Show("There are unsaved changes. Are you sure you want to quit?", "BRAM Manager", MessageBoxButtons.YesNo) == DialogResult.No ){
if (MessageBox.Show("There are unsaved changes. Are you sure you want to quit?", "", MessageBoxButtons.YesNo) == DialogResult.No ){
e.Cancel = true;
}
}
Expand Down
Loading

0 comments on commit e41170e

Please sign in to comment.