diff --git a/VRDash/MainWindow.xaml.cs b/VRDash/MainWindow.xaml.cs index 4636dfa..3f1f1e2 100644 --- a/VRDash/MainWindow.xaml.cs +++ b/VRDash/MainWindow.xaml.cs @@ -1,8 +1,10 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -33,6 +35,7 @@ namespace VRDash { Logger.Setup(); this.GetLogger().Info("VDash is starting up..."); + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; InitializeComponent(); //Connect to VR runtime... @@ -63,6 +66,34 @@ namespace VRDash } }; + try + { + if (File.Exists(ProfileManager.ConfigLocation)) + { + var profile = ProfileManager.LoadProfile(); + LoadProfile(profile); + this.GetLogger().Info($"Default profile loaded. {profile.OverlayConfigs.Count()} overlays loaded from disk."); + } + } + catch(Exception ex) + { + this.GetLogger().Error($"Unable to load profile from disk. Profile location: {ProfileManager.ConfigLocation}", ex); + MessageBox.Show($"There was an error while trying to load the default profile: {ex.Message}.", "Error Loading Default Profile", MessageBoxButton.OK, MessageBoxImage.Error); + } + + Task saveTask = Task.Factory.StartNew(() => + { + //Update profile on disk every ten seconds. Shitty way of doing things, but it is what it is. + while (true) + { + if (!ProfileManager.IsProfileSaveUpToDate()) + { + ProfileManager.SaveProfile(); + this.GetLogger().Info("Updated profile saved to disk."); + } + Thread.Sleep(10000); + } + }); #region Debug @@ -92,6 +123,11 @@ namespace VRDash #endregion } + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + this.GetLogger().Fatal("Unhandled AppDomain exception.", (Exception)e.ExceptionObject); + } + private void LoadProfile(Profile profile) { //Dispose of all overlays and clear list. diff --git a/VRDash/Overlay/Actions/BringWindowToFrontAction.cs b/VRDash/Overlay/Actions/BringWindowToFrontAction.cs index cb0a6cd..22adac9 100644 --- a/VRDash/Overlay/Actions/BringWindowToFrontAction.cs +++ b/VRDash/Overlay/Actions/BringWindowToFrontAction.cs @@ -35,7 +35,7 @@ namespace VRDash.Overlay.Actions if (!Enabled) { return; } if (!intOverlay.GetType().IsAssignableFrom(typeof(WindowOverlay))) { - Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); + //Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); return; } diff --git a/VRDash/Overlay/Actions/MaximizeWindowAction.cs b/VRDash/Overlay/Actions/MaximizeWindowAction.cs index 606d255..b4bb363 100644 --- a/VRDash/Overlay/Actions/MaximizeWindowAction.cs +++ b/VRDash/Overlay/Actions/MaximizeWindowAction.cs @@ -18,7 +18,7 @@ namespace VRDash.Overlay.Actions if (!Enabled) { return; } if (!intOverlay.GetType().IsAssignableFrom(typeof(WindowOverlay))) { - Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); + //Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); return; } diff --git a/VRDash/Overlay/Actions/MinimizeWIndowAction.cs b/VRDash/Overlay/Actions/MinimizeWIndowAction.cs index 3473f5c..c6abc5e 100644 --- a/VRDash/Overlay/Actions/MinimizeWIndowAction.cs +++ b/VRDash/Overlay/Actions/MinimizeWIndowAction.cs @@ -17,7 +17,7 @@ namespace VRDash.Overlay.Actions if (!Enabled) { return; } if (!intOverlay.GetType().IsAssignableFrom(typeof(WindowOverlay))) { - Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); + //Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); return; } diff --git a/VRDash/Overlay/Actions/ResizeWindowAction.cs b/VRDash/Overlay/Actions/ResizeWindowAction.cs index ab2df79..11d2a87 100644 --- a/VRDash/Overlay/Actions/ResizeWindowAction.cs +++ b/VRDash/Overlay/Actions/ResizeWindowAction.cs @@ -28,7 +28,7 @@ namespace VRDash.Overlay.Actions if (!Enabled) { return; } if (!intOverlay.GetType().IsAssignableFrom(typeof(WindowOverlay))) { - Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); + //Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); return; } diff --git a/VRDash/Overlay/Actions/RestoreWindowAction.cs b/VRDash/Overlay/Actions/RestoreWindowAction.cs index efaffb1..eb71292 100644 --- a/VRDash/Overlay/Actions/RestoreWindowAction.cs +++ b/VRDash/Overlay/Actions/RestoreWindowAction.cs @@ -17,7 +17,7 @@ namespace VRDash.Overlay.Actions if (!Enabled) { return; } if (!intOverlay.GetType().IsAssignableFrom(typeof(WindowOverlay))) { - Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); + //Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); return; } diff --git a/VRDash/Overlay/MonitorOverlay.cs b/VRDash/Overlay/MonitorOverlay.cs index 0dab8af..2a19661 100644 --- a/VRDash/Overlay/MonitorOverlay.cs +++ b/VRDash/Overlay/MonitorOverlay.cs @@ -206,7 +206,7 @@ namespace VRDash.Overlay if (!succ) { - Console.WriteLine("SetCursorPos failed."); + this.GetLogger().Warn($"[{Config.Name}] SetCursorPos failed."); } break; @@ -381,13 +381,13 @@ namespace VRDash.Overlay } else { - Console.WriteLine("Error getting coords to draw cursor."); + this.GetLogger().Warn($"[{Config.Name}] Error getting coords to draw cursor."); } } else { string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; - Console.WriteLine($"Error getting cursor info: {errorMessage}"); + this.GetLogger().Warn($"[{Config.Name}] Error getting cursor info: {errorMessage}"); } #endregion @@ -428,7 +428,7 @@ namespace VRDash.Overlay HmdVector2_t vecWindowSize = new HmdVector2_t() { v0 = data.Width, v1 = data.Height }; OpenVR.Overlay.SetOverlayMouseScale(m_OverlayHandle, ref vecWindowSize); - Console.WriteLine("Updated!"); + this.GetLogger().Info($"[{Config.Name}] Monitor resized, texture updated."); } @@ -488,7 +488,7 @@ namespace VRDash.Overlay if (result != EVROverlayError.None) { - Console.WriteLine($"Error rendering: {result}"); + this.GetLogger().Warn($"Error rendering: {result}"); } device.ImmediateContext.UnmapSubresource(screenTexture, 0); diff --git a/VRDash/Overlay/WindowOverlay.cs b/VRDash/Overlay/WindowOverlay.cs index a6becea..42352e9 100644 --- a/VRDash/Overlay/WindowOverlay.cs +++ b/VRDash/Overlay/WindowOverlay.cs @@ -168,7 +168,7 @@ namespace VRDash.Overlay if (IsCreated()) { destroyInternal(); - Console.WriteLine($"Destroyed {Config.Key} because it's window does not exist."); + this.GetLogger().Info($"Destroyed {Config.Key} because it's window does not exist."); } Thread.Sleep(100); @@ -265,7 +265,7 @@ namespace VRDash.Overlay if (!succ) { - Console.WriteLine("ClientToScreen failed."); + this.GetLogger().Warn($"[{Config.Name}] ClientToScreen failed."); } @@ -273,7 +273,7 @@ namespace VRDash.Overlay if (!succ) { - Console.WriteLine("SetCursorPos failed."); + this.GetLogger().Warn($"[{Config.Name}] SetCursorPos failed."); } } break; @@ -460,13 +460,13 @@ namespace VRDash.Overlay } else { - Console.WriteLine("Error getting coords to draw cursor."); + this.GetLogger().Warn($"[{Config.Name}] Error getting coords to draw cursor."); } } else { string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; - Console.WriteLine($"Error getting cursor info: {errorMessage}"); + this.GetLogger().Warn($"[{Config.Name}] Error getting cursor info: {errorMessage}"); } #endregion @@ -507,7 +507,7 @@ namespace VRDash.Overlay HmdVector2_t vecWindowSize = new HmdVector2_t() { v0 = data.Width, v1 = data.Height }; OpenVR.Overlay.SetOverlayMouseScale(m_OverlayHandle, ref vecWindowSize); - Console.WriteLine("Updated!"); + this.GetLogger().Info($"[{Config.Name}] Window resized, texture updated."); } @@ -567,7 +567,7 @@ namespace VRDash.Overlay if (result != EVROverlayError.None) { - Console.WriteLine($"Error rendering: {result}"); + this.GetLogger().Warn($"[{Config.Name}] Error rendering: {result}"); } device.ImmediateContext.UnmapSubresource(screenTexture, 0); diff --git a/VRDash/ProfileManager.cs b/VRDash/ProfileManager.cs index 8d4ec3b..efa6e7a 100644 --- a/VRDash/ProfileManager.cs +++ b/VRDash/ProfileManager.cs @@ -36,14 +36,31 @@ namespace VRDash SaveProfile(ConfigLocation); } + public string GetLoadedProfileJson() + { + IOverlayConfig[] selection = MainWindow.OverlayManager.Overlays.Select(x => x.Overlay.OverlayConfig).ToArray(); + var profile = new Profile(selection); + var json = JsonConvert.SerializeObject(profile, m_Settings); + return json; + } + + public string GetDefaultProfileJson() + { + var json = File.ReadAllText(ConfigLocation); + return json; + } + + public bool IsProfileSaveUpToDate() + { + return GetLoadedProfileJson() == GetDefaultProfileJson(); + } + /// /// Saves config to ConfigLocation /// public void SaveProfile(string location) { - IOverlayConfig[] selection = MainWindow.OverlayManager.Overlays.Select(x => x.Overlay.OverlayConfig).ToArray(); - var profile = new Profile(selection); - var json = JsonConvert.SerializeObject(profile, m_Settings); + var json = GetLoadedProfileJson(); File.WriteAllText(location, json); }