Skip to content

Commit

Permalink
Debugger: Auto-save debug workspace periodically to prevent data loss…
Browse files Browse the repository at this point in the history
… if a crash occurs
  • Loading branch information
SourMesen committed Dec 26, 2023
1 parent f545e41 commit 4168583
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions UI/Debugger/Breakpoints/BreakpointManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mesen.Interop;
using Mesen.Debugger.Utilities;
using Mesen.Interop;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -64,7 +65,9 @@ public static void AddBreakpoints(List<Breakpoint> breakpoints)

public static void RemoveBreakpoint(Breakpoint bp)
{
_breakpoints.Remove(bp);
if(_breakpoints.Remove(bp)) {
DebugWorkspaceManager.AutoSave();
}
RefreshBreakpoints(bp);
}

Expand All @@ -80,6 +83,7 @@ public static void AddBreakpoint(Breakpoint bp)
{
if(!_breakpoints.Contains(bp)) {
_breakpoints.Add(bp);
DebugWorkspaceManager.AutoSave();
}
RefreshBreakpoints(bp);
}
Expand Down Expand Up @@ -149,6 +153,7 @@ public static bool EnableDisableBreakpoint(AddressInfo info, CpuType cpuType)
Breakpoint? breakpoint = BreakpointManager.GetMatchingBreakpoint(info, cpuType);
if(breakpoint != null) {
breakpoint.Enabled = !breakpoint.Enabled;
DebugWorkspaceManager.AutoSave();
RefreshBreakpoints();
return true;
}
Expand Down
10 changes: 10 additions & 0 deletions UI/Debugger/Utilities/DebugWorkspaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ public static void Save(bool releaseWorkspace = false)
_workspace = null;
}
}

private static DateTime _previousAutoSave = DateTime.MinValue;
public static void AutoSave()
{
//Automatically save when changing a label/breakpoint/watch to avoid losing progress if a crash occurs
if((DateTime.Now - _previousAutoSave).TotalSeconds >= 60) {
_workspace?.Save(_path, _romInfo.CpuTypes);
_previousAutoSave = DateTime.Now;
}
}
}

public class CpuDebugWorkspace
Expand Down
1 change: 1 addition & 0 deletions UI/Debugger/Views/BreakpointListView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private void OnCellClick(DataBoxCell cell)
}
}

DebugWorkspaceManager.AutoSave();
BreakpointManager.RefreshBreakpoints();
}
}
Expand Down
2 changes: 2 additions & 0 deletions UI/Debugger/WatchManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia.Media;
using Mesen.Config;
using Mesen.Debugger.Labels;
using Mesen.Debugger.Utilities;
using Mesen.Interop;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
Expand Down Expand Up @@ -226,6 +227,7 @@ public void UpdateWatch(int index, string expression)
}
WatchChanged?.Invoke(null, EventArgs.Empty);
}
DebugWorkspaceManager.AutoSave();
}

public void RemoveWatch(params int[] indexes)
Expand Down
1 change: 1 addition & 0 deletions UI/Debugger/Windows/BreakpointEditWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Mesen.Controls;
using Mesen.Debugger.Utilities;
using Mesen.Debugger.ViewModels;
using Mesen.Utilities;
using System;
Expand Down
2 changes: 2 additions & 0 deletions UI/Debugger/Windows/LabelEditWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Mesen.Debugger.Labels;
using Mesen.Debugger.Utilities;
using Mesen.Debugger.ViewModels;
using Mesen.Interop;
using Mesen.Utilities;
Expand Down Expand Up @@ -58,6 +59,7 @@ public static async void EditLabel(CpuType cpuType, Control parent, CodeLabel la
model.Commit();
LabelManager.DeleteLabel(label, false);
LabelManager.SetLabel(copy ?? label, true);
DebugWorkspaceManager.AutoSave();
}

model.Dispose();
Expand Down

0 comments on commit 4168583

Please sign in to comment.