Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 66 additions & 73 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,68 +76,15 @@ public Form1()

private void Settings_Click(object? sender, EventArgs e)
{
using (var dlg = new SettingsForm())
using (var dlg = new SettingsForm(backupManager, () => { BeginInvoke(() => CheckForMediaAndIdentify()); }))
{
if (dlg.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show("Settings saved.", "Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

// Additionally offer per-media assignment dialog
ShowMediaAssignmentDialog();
}

private void ShowMediaAssignmentDialog()
{
try
{
var drives = DriveInfo.GetDrives().Where(d => d.IsReady && d.DriveType == DriveType.Removable).ToList();
if (drives.Count == 0)
{
MessageBox.Show("No removable drives detected to assign.", "Assign Media", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

using (var frm = new Form { Text = "Assign Media to System", Width = 480, Height = 200, StartPosition = FormStartPosition.CenterParent })
{
var comboDrive = new ComboBox { Left = 10, Top = 10, Width = 420, DropDownStyle = ComboBoxStyle.DropDownList };
foreach (var d in drives) comboDrive.Items.Add(d.RootDirectory.FullName);
comboDrive.SelectedIndex = 0;
frm.Controls.Add(comboDrive);

var comboSystem = new ComboBox { Left = 10, Top = 45, Width = 300, DropDownStyle = ComboBoxStyle.DropDownList };
var systems = new[] { "NintendoDS", "GameBoyAdvance", "GameBoyColor", "GameBoy", "Unknown" };
foreach (var s in systems) comboSystem.Items.Add(s);
comboSystem.SelectedIndex = 0;
frm.Controls.Add(comboSystem);

var btnAssign = new Button { Text = "Assign", Left = 320, Top = 45, Width = 110 };
btnAssign.Click += (s, e) =>
{
var root = comboDrive.SelectedItem as string;
var system = comboSystem.SelectedItem as string;
if (!string.IsNullOrEmpty(root))
{
if (string.IsNullOrEmpty(system) || system == "Unknown")
SettingsManager.SetAssignedSystemForRoot(root, null);
else
SettingsManager.SetAssignedSystemForRoot(root, system);

MessageBox.Show($"Assigned {root} => {system}", "Assigned", MessageBoxButtons.OK, MessageBoxIcon.Information);
frm.DialogResult = DialogResult.OK;
frm.Close();
}
};
frm.Controls.Add(btnAssign);

frm.ShowDialog(this);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Assign Media Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// Previously opened a separate assignment dialog here; assignments are now managed inside SettingsForm.
}

private void UpdateLogoVisibility()
Expand Down Expand Up @@ -287,32 +234,78 @@ private void CheckForMediaAndIdentify()

if (unknownDrives.Count > 0)
{
// Show dialog to identify the media
using (var dlg = new SystemIdentificationDialog())
var drive = unknownDrives[0];
var rootPath = drive.RootDirectory.FullName;

// 1) check explicit assignments
var assigned = SettingsManager.GetAssignedSystemForRoot(rootPath);
if (!string.IsNullOrEmpty(assigned))
{
// If previously assigned, pre-select the system and skip dialog
var assigned = SettingsManager.GetAssignedSystemForRoot(unknownDrives[0].RootDirectory.FullName);
if (!string.IsNullOrEmpty(assigned))
lastDetectedUnknownSource = new GenericMediaSource(rootPath, assigned, assigned);
_lastCartColor = null;
RefreshStatus();
btnBackupNow.Enabled = true;
RefreshLogView();
UpdateLogoVisibility();
return;
}

// 2) auto-assign by volume label
try
{
if (SettingsManager.GetAutoAssignByVolumeLabel())
{
lastDetectedUnknownSource = new GenericMediaSource(
unknownDrives[0].RootDirectory.FullName,
assigned,
assigned
);
var vol = drive.VolumeLabel ?? string.Empty;
var rules = SettingsManager.GetVolumeLabelRules();
foreach (var kvp in rules)
{
if (!string.IsNullOrEmpty(kvp.Key) && vol.IndexOf(kvp.Key, StringComparison.OrdinalIgnoreCase) >= 0)
{
lastDetectedUnknownSource = new GenericMediaSource(rootPath, kvp.Value, kvp.Value);
SettingsManager.SetAssignedSystemForRoot(rootPath, kvp.Value);
RefreshStatus();
btnBackupNow.Enabled = true;
RefreshLogView();
UpdateLogoVisibility();
return;
}
}
}
}
catch { }

_lastCartColor = null;
RefreshStatus();
btnBackupNow.Enabled = true;
RefreshLogView();
UpdateLogoVisibility();
return;
// 3) auto-assign by marker files
try
{
if (SettingsManager.GetAutoAssignByMarkers())
{
var markers = SettingsManager.GetMarkerRules();
foreach (var kvp in markers)
{
var markerPath = Path.Combine(rootPath, kvp.Key);
if (File.Exists(markerPath))
{
lastDetectedUnknownSource = new GenericMediaSource(rootPath, kvp.Value, kvp.Value);
SettingsManager.SetAssignedSystemForRoot(rootPath, kvp.Value);
RefreshStatus();
btnBackupNow.Enabled = true;
RefreshLogView();
UpdateLogoVisibility();
return;
}
}
}
}
catch { }

// 4) fallback to manual identification dialog
using (var dlg = new SystemIdentificationDialog())
{
if (dlg.ShowDialog(this) == DialogResult.OK)
{
// Create GenericMediaSource with system type information
lastDetectedUnknownSource = new GenericMediaSource(
unknownDrives[0].RootDirectory.FullName,
rootPath,
dlg.SelectedSystemType, // Pass system type
dlg.SelectedSystemType // Use as volume label for display
);
Expand All @@ -328,7 +321,7 @@ private void CheckForMediaAndIdentify()
StoreMediaMetadata(dlg.SelectedSystemType, dlg.CartColor, dlg.CartNickname);

// Persist assignment for this media
SettingsManager.SetAssignedSystemForRoot(unknownDrives[0].RootDirectory.FullName, dlg.SelectedSystemType);
SettingsManager.SetAssignedSystemForRoot(rootPath, dlg.SelectedSystemType);

RefreshLogView();
UpdateLogoVisibility();
Expand Down
4 changes: 4 additions & 0 deletions GBACartBackup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<PackageReference Include="System.Management" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Tests\" />
</ItemGroup>

</Project>
20 changes: 18 additions & 2 deletions GBACartBackup/GenericMediaSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class GenericMediaSource : IMediaSource
{
private readonly string? root;
private readonly string? label;
private readonly SaveFileLocator.SystemType systemType;
private SaveFileLocator.SystemType systemType;

public GenericMediaSource()
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public string VolumeLabel

// Prefer a specific drive letter (E:) when present, as many users' cards mount there
var preferredLetter = "E:";
var preferred = drives.FirstOrDefault(d => string.Equals(Path.GetPathRoot(d.RootDirectory.FullName)?.TrimEnd('\\'), preferredLetter, StringComparison.OrdinalIgnoreCase));
var preferred = drives.FirstOrDefault(d => string.Equals(Path.GetPathRoot(d.RootDirectory.FullName)?.TrimEnd(Path.DirectorySeparatorChar), preferredLetter, StringComparison.OrdinalIgnoreCase));
if (preferred != null)
{
try { return preferred.RootDirectory.FullName; } catch { }
Expand Down Expand Up @@ -102,6 +102,22 @@ public string[] GetSaveFiles()
return SaveFileLocator.FindSaveFiles(r, systemType);
}

// Try to determine from persisted assignments if available
try
{
var assigned = SettingsManager.GetAssignedSystemForRoot(r);
if (!string.IsNullOrEmpty(assigned))
{
var parsed = SaveFileLocator.ParseSystemType(assigned);
if (parsed != SaveFileLocator.SystemType.Unknown)
{
systemType = parsed;
return SaveFileLocator.FindSaveFiles(r, systemType);
}
}
}
catch { }

// Fallback: return top-level files
return Directory.GetFiles(r, "*.*", SearchOption.TopDirectoryOnly);
}
Expand Down
Loading