mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Implemented GeneralSettings
This commit is contained in:
parent
fcb4ca3df3
commit
f1214eb3ab
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using Artemis.DAL;
|
||||
using Artemis.InjectionModules;
|
||||
using Artemis.Settings;
|
||||
using Artemis.Utilities;
|
||||
@ -21,7 +22,7 @@ namespace Artemis
|
||||
public ArtemisBootstrapper()
|
||||
{
|
||||
// Start logging before anything else
|
||||
Logging.SetupLogging(General.Default.LogLevel);
|
||||
Logging.SetupLogging(SettingsProvider.Load<GeneralSettings>("GeneralSettings").LogLevel);
|
||||
|
||||
Initialize();
|
||||
BindSpecialValues();
|
||||
|
||||
@ -1,22 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Artemis.Settings;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
|
||||
namespace Artemis.DAL
|
||||
{
|
||||
public static class SettingsProvider
|
||||
{
|
||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private static readonly string SettingsFolder =
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\settings";
|
||||
|
||||
private static readonly List<IArtemisSettings> Settings = new List<IArtemisSettings>();
|
||||
|
||||
/// <summary>
|
||||
/// Loads settings with the given name from the filesystem
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public static T Load<T>(string name)
|
||||
public static T Load<T>(string name) where T : new()
|
||||
{
|
||||
if (!AreSettings(typeof(T)))
|
||||
throw new ArgumentException("Type doesn't implement IArtemisSettings");
|
||||
|
||||
throw new NotImplementedException();
|
||||
// Attempt to load from memory first
|
||||
var inMemory = Settings.FirstOrDefault(s => s.Name == name);
|
||||
if (inMemory != null)
|
||||
return (T) inMemory;
|
||||
|
||||
CheckSettings();
|
||||
|
||||
try
|
||||
{
|
||||
var loadSettings = (IArtemisSettings) JsonConvert
|
||||
.DeserializeObject<T>(File.ReadAllText(SettingsFolder + $@"\{name}.json"));
|
||||
if (loadSettings == null)
|
||||
SetToDefault(ref loadSettings);
|
||||
|
||||
Settings.Add(loadSettings);
|
||||
return (T) loadSettings;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error(e, "Couldn't load settings '{0}.json'", name);
|
||||
|
||||
// Not sure about this, I've seen prettier code
|
||||
var loadSettings = (IArtemisSettings) new T();
|
||||
SetToDefault(ref loadSettings);
|
||||
Settings.Add(loadSettings);
|
||||
return (T) loadSettings;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -25,25 +62,43 @@ namespace Artemis.DAL
|
||||
/// <param name="artemisSettings"></param>
|
||||
public static void Save(IArtemisSettings artemisSettings)
|
||||
{
|
||||
CheckSettings();
|
||||
|
||||
string json;
|
||||
// Should saving fail for whatever reason, catch the exception and log it
|
||||
// But DON'T touch the settings file.
|
||||
try
|
||||
{
|
||||
json = JsonConvert.SerializeObject(artemisSettings, Formatting.Indented);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error(e, "Couldn't save settings '{0}.json'", artemisSettings.Name);
|
||||
return;
|
||||
}
|
||||
|
||||
File.WriteAllText(SettingsFolder + $@"\{artemisSettings.Name}.json", json);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores the settings object to its default values
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetDefault<T>()
|
||||
/// <param name="settings"></param>
|
||||
public static void SetToDefault(ref IArtemisSettings settings)
|
||||
{
|
||||
if (!AreSettings(typeof(T)))
|
||||
throw new ArgumentException("Type doesn't implement IArtemisSettings");
|
||||
// Loading the object from an empty JSON object makes Json.NET use all the default values
|
||||
settings = (IArtemisSettings) JsonConvert.DeserializeObject("{}", settings.GetType());
|
||||
}
|
||||
|
||||
// Loading the object from an empty string makes Json.NET use all the default values
|
||||
return JsonConvert.DeserializeObject<T>("");
|
||||
private static void CheckSettings()
|
||||
{
|
||||
if (!Directory.Exists(SettingsFolder))
|
||||
Directory.CreateDirectory(SettingsFolder);
|
||||
}
|
||||
|
||||
private static bool AreSettings(Type t)
|
||||
{
|
||||
return t.IsAssignableFrom(typeof(IArtemisSettings));
|
||||
return typeof(IArtemisSettings).IsAssignableFrom(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,11 +2,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.DAL;
|
||||
using Artemis.DeviceProviders;
|
||||
using Artemis.Events;
|
||||
using Artemis.Services;
|
||||
using Artemis.Settings;
|
||||
using Caliburn.Micro;
|
||||
using MahApps.Metro.Controls.Dialogs;
|
||||
using Ninject;
|
||||
using Ninject.Extensions.Logging;
|
||||
@ -18,12 +18,13 @@ namespace Artemis.Managers
|
||||
/// </summary>
|
||||
public class DeviceManager
|
||||
{
|
||||
private readonly GeneralSettings _generalSettings;
|
||||
private readonly ILogger _logger;
|
||||
public event EventHandler<KeyboardChangedEventArgs> OnKeyboardChangedEvent;
|
||||
|
||||
public DeviceManager(ILogger logger, List<DeviceProvider> deviceProviders)
|
||||
{
|
||||
_logger = logger;
|
||||
_generalSettings = SettingsProvider.Load<GeneralSettings>("GeneralSettings");
|
||||
|
||||
KeyboardProviders = deviceProviders.Where(d => d.Type == DeviceType.Keyboard)
|
||||
.Cast<KeyboardProvider>().ToList();
|
||||
@ -46,17 +47,18 @@ namespace Artemis.Managers
|
||||
public KeyboardProvider ActiveKeyboard { get; set; }
|
||||
|
||||
public bool ChangingKeyboard { get; private set; }
|
||||
public event EventHandler<KeyboardChangedEventArgs> OnKeyboardChangedEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Enables the last keyboard according to the settings file
|
||||
/// </summary>
|
||||
public void EnableLastKeyboard()
|
||||
{
|
||||
_logger.Debug("Getting last keyboard: {0}", General.Default.LastKeyboard);
|
||||
if (string.IsNullOrEmpty(General.Default.LastKeyboard))
|
||||
_logger.Debug("Getting last keyboard: {0}", _generalSettings.LastKeyboard);
|
||||
if (string.IsNullOrEmpty(_generalSettings.LastKeyboard))
|
||||
return;
|
||||
|
||||
var keyboard = KeyboardProviders.FirstOrDefault(k => k.Name == General.Default.LastKeyboard);
|
||||
var keyboard = KeyboardProviders.FirstOrDefault(k => k.Name == _generalSettings.LastKeyboard);
|
||||
EnableKeyboard(keyboard);
|
||||
}
|
||||
|
||||
@ -69,7 +71,7 @@ namespace Artemis.Managers
|
||||
if (keyboardProvider == null)
|
||||
throw new ArgumentNullException(nameof(keyboardProvider));
|
||||
|
||||
if (ChangingKeyboard || ActiveKeyboard?.Name == keyboardProvider.Name)
|
||||
if (ChangingKeyboard || (ActiveKeyboard?.Name == keyboardProvider.Name))
|
||||
return;
|
||||
|
||||
_logger.Debug("Trying to enable keyboard: {0}", keyboardProvider.Name);
|
||||
@ -101,8 +103,8 @@ namespace Artemis.Managers
|
||||
|
||||
DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText);
|
||||
ActiveKeyboard = null;
|
||||
General.Default.LastKeyboard = null;
|
||||
General.Default.Save();
|
||||
_generalSettings.LastKeyboard = null;
|
||||
_generalSettings.Save();
|
||||
_logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name);
|
||||
ChangingKeyboard = false;
|
||||
return;
|
||||
@ -115,8 +117,8 @@ namespace Artemis.Managers
|
||||
await ActiveKeyboard.EnableAsync(dialog);
|
||||
EnableUsableDevices();
|
||||
|
||||
General.Default.LastKeyboard = ActiveKeyboard.Name;
|
||||
General.Default.Save();
|
||||
_generalSettings.LastKeyboard = ActiveKeyboard.Name;
|
||||
_generalSettings.Save();
|
||||
|
||||
RaiseKeyboardChangedEvent(new KeyboardChangedEventArgs(oldKeyboard, ActiveKeyboard));
|
||||
_logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name);
|
||||
@ -157,8 +159,8 @@ namespace Artemis.Managers
|
||||
|
||||
if (save)
|
||||
{
|
||||
General.Default.LastKeyboard = null;
|
||||
General.Default.Save();
|
||||
_generalSettings.LastKeyboard = null;
|
||||
_generalSettings.Save();
|
||||
}
|
||||
|
||||
RaiseKeyboardChangedEvent(new KeyboardChangedEventArgs(oldKeyboard, null));
|
||||
|
||||
@ -19,11 +19,13 @@ namespace Artemis.Managers
|
||||
private EffectModel _activeEffect;
|
||||
private LoopManager _waitLoopManager;
|
||||
private EffectModel _waitEffect;
|
||||
private readonly GeneralSettings _generalSettings;
|
||||
|
||||
public EffectManager(ILogger logger, DeviceManager deviceManager)
|
||||
{
|
||||
EffectModels = new List<EffectModel>();
|
||||
|
||||
_generalSettings = DAL.SettingsProvider.Load<GeneralSettings>("GeneralSettings");
|
||||
_logger = logger;
|
||||
_deviceManager = deviceManager;
|
||||
|
||||
@ -71,10 +73,10 @@ namespace Artemis.Managers
|
||||
/// <returns>Whether enabling was successful or not.</returns>
|
||||
public EffectModel GetLastEffect()
|
||||
{
|
||||
_logger.Debug("Getting last effect: {0}", General.Default.LastEffect);
|
||||
return General.Default.LastEffect == null
|
||||
_logger.Debug("Getting last effect: {0}", _generalSettings.LastEffect);
|
||||
return _generalSettings.LastEffect == null
|
||||
? null
|
||||
: EffectModels.FirstOrDefault(e => e.Name == General.Default.LastEffect);
|
||||
: EffectModels.FirstOrDefault(e => e.Name == _generalSettings.LastEffect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -149,8 +151,8 @@ namespace Artemis.Managers
|
||||
return;
|
||||
|
||||
// Non-game effects are stored as the new LastEffect.
|
||||
General.Default.LastEffect = ActiveEffect?.Name;
|
||||
General.Default.Save();
|
||||
_generalSettings.LastEffect = ActiveEffect?.Name;
|
||||
_generalSettings.Save();
|
||||
}
|
||||
|
||||
private void DeviceManagerOnOnKeyboardChangedEvent(object sender, KeyboardChangedEventArgs e)
|
||||
@ -180,11 +182,10 @@ namespace Artemis.Managers
|
||||
ActiveEffect.Dispose();
|
||||
ActiveEffect = null;
|
||||
|
||||
General.Default.LastEffect = null;
|
||||
General.Default.Save();
|
||||
_generalSettings.LastEffect = null;
|
||||
_generalSettings.Save();
|
||||
}
|
||||
|
||||
|
||||
_logger.Debug("Cleared active effect");
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Timers;
|
||||
using Artemis.DAL;
|
||||
using Artemis.Models;
|
||||
using Artemis.Modules.Effects.ProfilePreview;
|
||||
using Artemis.Settings;
|
||||
@ -44,7 +45,7 @@ namespace Artemis.Managers
|
||||
/// <param name="e"></param>
|
||||
private void SetupProfilePreview(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(General.Default.LastKeyboard) || _deviceManager.ChangingKeyboard ||
|
||||
if (string.IsNullOrEmpty(SettingsProvider.Load<GeneralSettings>("GeneralSettings").LastKeyboard) || _deviceManager.ChangingKeyboard ||
|
||||
ProfilePreviewModel == null)
|
||||
return;
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ namespace Artemis.Modules.Games.RocketLeague
|
||||
|
||||
private void SetVersionText()
|
||||
{
|
||||
if (!General.Default.EnablePointersUpdate)
|
||||
if (!DAL.SettingsProvider.Load<GeneralSettings>("GeneralSettings").EnablePointersUpdate)
|
||||
{
|
||||
VersionText = "Note: You disabled pointer updates, this could result in the " +
|
||||
"Rocket League effect not working after a game update.";
|
||||
|
||||
@ -13,6 +13,12 @@ namespace Artemis.Settings
|
||||
{
|
||||
public class GeneralSettings : IArtemisSettings
|
||||
{
|
||||
public GeneralSettings()
|
||||
{
|
||||
ThemeManager.AddAccent("CorsairYellow", new Uri("pack://application:,,,/Styles/Accents/CorsairYellow.xaml"));
|
||||
ApplyTheme();
|
||||
}
|
||||
|
||||
[DefaultValue("WindowsProfile")]
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
|
||||
public string LastEffect { get; set; }
|
||||
|
||||
@ -36,7 +36,7 @@ namespace Artemis.Utilities.GameState
|
||||
if (Running)
|
||||
return;
|
||||
|
||||
Port = General.Default.GamestatePort;
|
||||
Port = DAL.SettingsProvider.Load<GeneralSettings>("GeneralSettings").GamestatePort;
|
||||
|
||||
_listener = new HttpListener();
|
||||
_listener.Prefixes.Add($"http://localhost:{Port}/");
|
||||
|
||||
@ -7,6 +7,7 @@ using Artemis.Settings;
|
||||
using Artemis.Utilities.Memory;
|
||||
using Newtonsoft.Json;
|
||||
using Squirrel;
|
||||
using SettingsProvider = Artemis.DAL.SettingsProvider;
|
||||
|
||||
namespace Artemis.Utilities
|
||||
{
|
||||
@ -15,7 +16,7 @@ namespace Artemis.Utilities
|
||||
public static async void UpdateApp()
|
||||
{
|
||||
// Only update if the user allows it
|
||||
if (!General.Default.AutoUpdate)
|
||||
if (!SettingsProvider.Load<GeneralSettings>("GeneralSettings").AutoUpdate)
|
||||
return;
|
||||
|
||||
// TODO: Remove prerelease before releasing
|
||||
|
||||
@ -201,7 +201,8 @@ namespace Artemis.ViewModels.Flyouts
|
||||
|
||||
public void ResetSettings()
|
||||
{
|
||||
GeneralSettings = SettingsProvider.GetDefault<GeneralSettings>();
|
||||
IArtemisSettings generalSettings = GeneralSettings;
|
||||
SettingsProvider.SetToDefault(ref generalSettings);
|
||||
GeneralSettings.Save();
|
||||
NotifyOfPropertyChange(() => GeneralSettings);
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using Artemis.DAL;
|
||||
using Artemis.Events;
|
||||
using Artemis.Managers;
|
||||
using Artemis.Services;
|
||||
@ -31,8 +32,9 @@ namespace Artemis.ViewModels
|
||||
MainManager.EnableProgram();
|
||||
MainManager.OnEnabledChangedEvent += MainManagerOnOnEnabledChangedEvent;
|
||||
|
||||
Enabled = !General.Default.Suspended;
|
||||
if (General.Default.ShowOnStartup)
|
||||
var generalSettings = SettingsProvider.Load<GeneralSettings>("GeneralSettings");
|
||||
Enabled = !generalSettings.Suspended;
|
||||
if (generalSettings.ShowOnStartup)
|
||||
ShowWindow();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user