Skip to content
Draft
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
33 changes: 33 additions & 0 deletions ACViewer/Commands/RelayCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Windows.Input;

namespace ACViewer.Commands
{
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute;

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}

public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute(parameter);
}

public void Execute(object parameter)
{
_execute(parameter);
}
}
}
6 changes: 6 additions & 0 deletions ACViewer/Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ public class Config
public BackgroundColors BackgroundColors { get; set; } = new BackgroundColors();
public string Theme { get; set; }
public Mouse Mouse { get; set; } = new Mouse();
public KeyBindingConfig KeyBindingConfig { get; set; }

public Config()
{
KeyBindingConfig = new KeyBindingConfig();
}
}
}
164 changes: 142 additions & 22 deletions ACViewer/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
// File: ACViewer/Config/ConfigManager.cs
using System;
using System.Collections.Generic;
using System.IO;

using System.Windows;
using ACViewer.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace ACViewer.Config
{
public static class ConfigManager
{
private static readonly string Filename = "ACViewer.json";

private static readonly string KeybindingsFile = "keybindings.json";
private static Config config { get; set; }

public static Config Config
Expand All @@ -17,41 +21,44 @@ public static Config Config
{
if (config == null)
config = new Config();

return config;
}
}

public static Config Snapshot { get; set; }

public static void SaveConfig()

private static JsonSerializerSettings GetSerializerSettings()
{
var settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;

var json = JsonConvert.SerializeObject(config, settings);

File.WriteAllText(Filename, json);
return new JsonSerializerSettings
{
Formatting = Formatting.Indented,
TypeNameHandling = TypeNameHandling.Auto,
Converters = new JsonConverter[]
{
new StringEnumConverter()
}
};
}

public static Config Snapshot { get; set; }

public static void LoadConfig()
{
config = ReadConfig();
}

public static Config ReadConfig()
{
if (!File.Exists(Filename)) return null;
if (!File.Exists(Filename))
return null;

var json = File.ReadAllText(Filename);

var _config = JsonConvert.DeserializeObject<Config>(json);

if (_config == null)
{
Console.WriteLine($"ConfigManager.LoadConfig() - failed to parse {Filename}");
return null;
}

return _config;
}

Expand All @@ -69,12 +76,125 @@ public static bool HasDBInfo
{
get
{
return config != null && config.Database != null && !string.IsNullOrWhiteSpace(config.Database.Host) &&
config.Database.Port > 0 &&
!string.IsNullOrWhiteSpace(config.Database.DatabaseName) &&
!string.IsNullOrWhiteSpace(config.Database.Username) &&
!string.IsNullOrWhiteSpace(config.Database.Password);
return config != null &&
config.Database != null &&
!string.IsNullOrWhiteSpace(config.Database.Host) &&
config.Database.Port > 0 &&
!string.IsNullOrWhiteSpace(config.Database.DatabaseName) &&
!string.IsNullOrWhiteSpace(config.Database.Username) &&
!string.IsNullOrWhiteSpace(config.Database.Password);
}
}

public static bool SaveKeyBindings()
{
try
{
var bindings = Config.KeyBindingConfig;
System.Diagnostics.Debug.WriteLine($"SaveKeyBindings - ToggleZLevel: {bindings.ToggleZLevel.MainKey} (Value: {bindings.ToggleZLevel.KeyValue})");

// Create verification copy
var verifyConfig = new KeyBindingConfig();
CopyBindings(bindings, verifyConfig);

var settings = GetSerializerSettings();
var json = JsonConvert.SerializeObject(verifyConfig, settings);

// Test deserialize
var testConfig = JsonConvert.DeserializeObject<KeyBindingConfig>(json, settings);
System.Diagnostics.Debug.WriteLine($"Test deserialize ToggleZLevel: {testConfig.ToggleZLevel.MainKey} (Value: {testConfig.ToggleZLevel.KeyValue})");

// Verify both the key enum and the numeric value match
if (testConfig.ToggleZLevel.MainKey != bindings.ToggleZLevel.MainKey ||
testConfig.ToggleZLevel.KeyValue != bindings.ToggleZLevel.KeyValue)
{
throw new Exception($"Verification failed - key mismatch after serialization test." +
$" Expected {bindings.ToggleZLevel.MainKey} ({bindings.ToggleZLevel.KeyValue})," +
$" got {testConfig.ToggleZLevel.MainKey} ({testConfig.ToggleZLevel.KeyValue})");
}

File.WriteAllText(KeybindingsFile, json);
SaveConfig();
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error saving keybindings: {ex}");
return false;
}
}

private static void CopyBindings(KeyBindingConfig source, KeyBindingConfig target)
{
target.MoveForward = source.MoveForward.Clone();
target.MoveBackward = source.MoveBackward.Clone();
target.StrafeLeft = source.StrafeLeft.Clone();
target.StrafeRight = source.StrafeRight.Clone();
target.MoveUp = source.MoveUp.Clone();
target.MoveDown = source.MoveDown.Clone();
target.ToggleZLevel = source.ToggleZLevel.Clone();
target.IncreaseZLevel = source.IncreaseZLevel.Clone();
target.DecreaseZLevel = source.DecreaseZLevel.Clone();

target.CustomBindings = new Dictionary<string, GameKeyBinding>();
foreach (var kvp in source.CustomBindings)
{
target.CustomBindings[kvp.Key] = kvp.Value.Clone();
}
}

public static void SaveConfig()
{
try
{
var settings = GetSerializerSettings();
var json = JsonConvert.SerializeObject(Config, settings);
File.WriteAllText(Filename, json);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error saving config: {ex}");
throw;
}
}

public static bool LoadKeyBindings()
{
try
{
if (!File.Exists(KeybindingsFile))
{
Config.KeyBindingConfig = new KeyBindingConfig();
Config.KeyBindingConfig.SetDefaults();
return true;
}

var json = File.ReadAllText(KeybindingsFile);
System.Diagnostics.Debug.WriteLine($"Loading keybindings: {json}");

var settings = GetSerializerSettings();
var bindings = JsonConvert.DeserializeObject<KeyBindingConfig>(json, settings);

if (bindings != null)
{
// Create a new config and copy values to ensure clean state
var newConfig = new KeyBindingConfig();
CopyBindings(bindings, newConfig);
newConfig.ValidateConfig();

Config.KeyBindingConfig = newConfig;
return true;
}

return false;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error loading keybindings: {ex}");
Config.KeyBindingConfig = new KeyBindingConfig();
Config.KeyBindingConfig.SetDefaults();
return false;
}
}
}
}
}
Loading