From 69fd02e33143e0cc08a93e0165cd5b25813d863a Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 31 Aug 2025 21:31:23 +0200 Subject: [PATCH] Enable saving settings to disk --- .gitignore | 3 +- DD2Switcher/App.config | 12 +++- DD2Switcher/DD2Switcher.csproj | 32 ++++++++++ DD2Switcher/Program.cs | 108 ++++++++++++++++++++++++++++++++- DD2Switcher/packages.config | 13 ++++ 5 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 DD2Switcher/packages.config diff --git a/.gitignore b/.gitignore index d33a901..7cfbe22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea obj bin -.vs \ No newline at end of file +.vs +packages diff --git a/DD2Switcher/App.config b/DD2Switcher/App.config index 8a99d30..5d2e92b 100644 --- a/DD2Switcher/App.config +++ b/DD2Switcher/App.config @@ -1,6 +1,14 @@ - + - + + + + + + + + + diff --git a/DD2Switcher/DD2Switcher.csproj b/DD2Switcher/DD2Switcher.csproj index ef91bc4..d7ee25d 100644 --- a/DD2Switcher/DD2Switcher.csproj +++ b/DD2Switcher/DD2Switcher.csproj @@ -37,8 +37,39 @@ false + + ..\packages\Microsoft.Bcl.AsyncInterfaces.9.0.8\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll + + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + ..\packages\System.IO.Pipelines.9.0.8\lib\net462\System.IO.Pipelines.dll + + + ..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Text.Encodings.Web.9.0.8\lib\net462\System.Text.Encodings.Web.dll + + + ..\packages\System.Text.Json.9.0.8\lib\net462\System.Text.Json.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + @@ -84,6 +115,7 @@ WindowPanelForm.cs + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/DD2Switcher/Program.cs b/DD2Switcher/Program.cs index 907459c..6095b5f 100644 --- a/DD2Switcher/Program.cs +++ b/DD2Switcher/Program.cs @@ -1,15 +1,25 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; +using System.Text.Json; namespace DD2Switcher { + public class Settings { + public int FirstIndex { get; set; } = -1; + public int LastIndex { get; set; } = -1; + public Keys SequenceKeybind { get; set; } = Keys.F1; + } + internal static class Program { private static int NumProc = 19; private static Process[] windows = new Process[NumProc]; + private static string settingsPath = + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "DD2Switcher.json"); // Public access to tracked windows for the settings form public static Process[] GetTrackedWindows() { @@ -59,8 +69,9 @@ namespace DD2Switcher { CurrentSequenceIndex++; Console.WriteLine($"Advanced to index {CurrentSequenceIndex}"); if (CurrentSequenceIndex > LastIndex) { - // End of sequence - Console.WriteLine("End of sequence reached, exiting sequence mode"); + // End of sequence - tab back to first + Console.WriteLine("End of sequence reached, tabbing back to first window"); + TabTo(FirstIndex + 1); ExitSequenceMode(); } else { Console.WriteLine($"Tabbing to index {CurrentSequenceIndex + 1}"); @@ -80,6 +91,72 @@ namespace DD2Switcher { return CurrentState != SequenceState.INACTIVE; } + private static void LoadSettings() { + try { + if (File.Exists(settingsPath)) { + string json = File.ReadAllText(settingsPath); + var settings = JsonSerializer.Deserialize(json); + FirstIndex = settings.FirstIndex; + LastIndex = settings.LastIndex; + SequenceKeybind = settings.SequenceKeybind; + Console.WriteLine( + $"Loaded settings: First={FirstIndex}, Last={LastIndex}, Keybind={SequenceKeybind}"); + } + } catch (Exception ex) { + Console.WriteLine($"Error loading settings: {ex.Message}"); + } + } + + private static void SaveSettings() { + try { + var settings = + new Settings { FirstIndex = FirstIndex, LastIndex = LastIndex, SequenceKeybind = SequenceKeybind }; + string json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText(settingsPath, json); + Console.WriteLine($"Saved settings: First={FirstIndex}, Last={LastIndex}, Keybind={SequenceKeybind}"); + } catch (Exception ex) { + Console.WriteLine($"Error saving settings: {ex.Message}"); + } + } + + private static int FindFirstNonNullWindow() { + for (int i = 0; i < NumProc; i++) { + if (windows[i] != null && windows[i].MainWindowHandle != IntPtr.Zero) { + return i; + } + } + return -1; + } + + private static int FindLastNonNullWindow() { + for (int i = NumProc - 1; i >= 0; i--) { + if (windows[i] != null && windows[i].MainWindowHandle != IntPtr.Zero) { + return i; + } + } + return -1; + } + + private static int FindNextNonNullWindow(int startIndex) { + for (int i = startIndex; i < NumProc; i++) { + if (windows[i] != null && windows[i].MainWindowHandle != IntPtr.Zero) { + return i; + } + } + return -1; + } + + private static void SetDefaultFirstLastIndices() { + if (FirstIndex == -1) { + FirstIndex = FindFirstNonNullWindow(); + Console.WriteLine($"Set default FirstIndex to: {FirstIndex}"); + } + if (LastIndex == -1) { + LastIndex = FindLastNonNullWindow(); + Console.WriteLine($"Set default LastIndex to: {LastIndex}"); + } + } + public static void UpdateSequenceHotkey(Keys newKey) { Console.WriteLine($"UpdateSequenceHotkey called with new key: {newKey} (code: {(int)newKey})"); Console.WriteLine($"Old SequenceKeybind before update: {SequenceKeybind} (code: {(int)SequenceKeybind})"); @@ -95,6 +172,7 @@ namespace DD2Switcher { Console.WriteLine($"Registered new hotkey ID: {sequenceHotkeyId} for key: {newKey}"); SequenceKeybind = newKey; Console.WriteLine($"New SequenceKeybind after update: {SequenceKeybind} (code: {(int)SequenceKeybind})"); + SaveSettings(); } // Static properties for first/last selection persistence @@ -293,6 +371,10 @@ namespace DD2Switcher { PushHistory(firstNullIndex); ActiveIndex = firstNullIndex; Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}"); + + // Set default first/last indices if not set + SetDefaultFirstLastIndices(); + SaveSettings(); } private static void TrackProcess() { @@ -371,6 +453,25 @@ namespace DD2Switcher { CleanWindows(); Console.WriteLine($"Tab to window at index {index}"); + // Find the next non-null window if the target is null + int originalIndex = index; + while (index < NumProc && (windows[index] == null || windows[index].MainWindowHandle == IntPtr.Zero)) { + index++; + } + if (index >= NumProc) { + // Try from the beginning + index = 0; + while (index < originalIndex && + (windows[index] == null || windows[index].MainWindowHandle == IntPtr.Zero)) { + index++; + } + } + + if (index >= NumProc || windows[index] == null || windows[index].MainWindowHandle == IntPtr.Zero) { + Console.WriteLine("No valid windows found to tab to"); + return; + } + var window = windows[index]; Console.WriteLine( $"Window at index {index}: {(window == null ? "NULL" : window.ProcessName + " PID:" + window.Id)}"); @@ -454,6 +555,9 @@ namespace DD2Switcher { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); + // Load settings + LoadSettings(); + var processes = Process.GetProcesses(); var currentProcess = Process.GetCurrentProcess(); diff --git a/DD2Switcher/packages.config b/DD2Switcher/packages.config new file mode 100644 index 0000000..71a95f9 --- /dev/null +++ b/DD2Switcher/packages.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file