Enable saving settings to disk

This commit is contained in:
2025-08-31 21:31:23 +02:00
parent 87bd2132e5
commit 69fd02e331
5 changed files with 163 additions and 5 deletions

3
.gitignore vendored
View File

@@ -1,4 +1,5 @@
.idea
obj
bin
.vs
.vs
packages

View File

@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@@ -37,8 +37,39 @@
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=9.0.0.8, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.9.0.8\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.IO.Pipelines, Version=9.0.0.8, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Pipelines.9.0.8\lib\net462\System.IO.Pipelines.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=9.0.0.8, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.9.0.8\lib\net462\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=9.0.0.8, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.9.0.8\lib\net462\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@@ -84,6 +115,7 @@
<EmbeddedResource Include="WindowPanelForm.resx">
<DependentUpon>WindowPanelForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@@ -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<Settings>(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();

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.8" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.IO.Pipelines" version="9.0.8" targetFramework="net48" />
<package id="System.Memory" version="4.5.5" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="9.0.8" targetFramework="net48" />
<package id="System.Text.Json" version="9.0.8" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>