From db7839e63f50da1614332f6470dd01976be9e542 Mon Sep 17 00:00:00 2001 From: DevTechProfile Date: Mon, 30 Dec 2019 16:31:56 +0100 Subject: [PATCH] overlay entry configuration with Json persistence --- .editorconfig | 4 + .../Overlay/IOverlayService.cs | 2 + .../CapFrameX.Overlay.csproj | 8 ++ .../OverlayEntryConfiguration.json | 44 +++++++ .../OverlayEntryPersistence.cs | 10 ++ .../CapFrameX.Overlay/OverlayEntryProvider.cs | 117 ++++++++++++++++-- .../CapFrameX.Overlay/OverlayEntryWrapper.cs | 2 + source/CapFrameX.Overlay/OverlayService.cs | 8 +- source/CapFrameX.Overlay/packages.config | 1 + source/CapFrameX.Test/CapFrameX.Test.csproj | 15 +++ .../Overlay/OverlayEntryPersistenceTest.cs | 78 ++++++++++++ source/CapFrameX.Test/packages.config | 2 + 12 files changed, 280 insertions(+), 11 deletions(-) create mode 100644 .editorconfig create mode 100644 source/CapFrameX.Overlay/OverlayConfiguration/OverlayEntryConfiguration.json create mode 100644 source/CapFrameX.Overlay/OverlayEntryPersistence.cs create mode 100644 source/CapFrameX.Test/Overlay/OverlayEntryPersistenceTest.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..bc332e48 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CA1031: Do not catch general exception types +dotnet_diagnostic.CA1031.severity = none diff --git a/source/CapFrameX.Contracts/Overlay/IOverlayService.cs b/source/CapFrameX.Contracts/Overlay/IOverlayService.cs index 557148a8..5c9cd96c 100644 --- a/source/CapFrameX.Contracts/Overlay/IOverlayService.cs +++ b/source/CapFrameX.Contracts/Overlay/IOverlayService.cs @@ -34,5 +34,7 @@ public interface IOverlayService void ResetHistory(); void AddRunToHistory(List captureData); + + void UpdateOverlayEntries(); } } diff --git a/source/CapFrameX.Overlay/CapFrameX.Overlay.csproj b/source/CapFrameX.Overlay/CapFrameX.Overlay.csproj index 903c4966..f054448b 100644 --- a/source/CapFrameX.Overlay/CapFrameX.Overlay.csproj +++ b/source/CapFrameX.Overlay/CapFrameX.Overlay.csproj @@ -49,6 +49,9 @@ MinimumRecommendedRules.ruleset + + ..\..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll + @@ -81,6 +84,7 @@ + @@ -110,7 +114,11 @@ + + Always + + \ No newline at end of file diff --git a/source/CapFrameX.Overlay/OverlayConfiguration/OverlayEntryConfiguration.json b/source/CapFrameX.Overlay/OverlayConfiguration/OverlayEntryConfiguration.json new file mode 100644 index 00000000..77a92c3b --- /dev/null +++ b/source/CapFrameX.Overlay/OverlayConfiguration/OverlayEntryConfiguration.json @@ -0,0 +1,44 @@ +{ + "OverlayEntries": [ + { + "Identifier": "CaptureServiceStatus", + "ShowOnOverlay": true, + "GroupName": "", + "Value": "Capture service ready...", + "ShowGraph": false, + "Color": "" + }, + { + "Identifier": "RunHistory", + "ShowOnOverlay": true, + "GroupName": "", + "Value": null, + "ShowGraph": false, + "Color": "" + }, + { + "Identifier": "CaptureTimer", + "ShowOnOverlay": true, + "GroupName": "Timer: ", + "Value": "0", + "ShowGraph": false, + "Color": "" + }, + { + "Identifier": "Framerate", + "ShowOnOverlay": true, + "GroupName": "", + "Value": 0.0, + "ShowGraph": false, + "Color": "" + }, + { + "Identifier": "Frametime", + "ShowOnOverlay": true, + "GroupName": "", + "Value": 0.0, + "ShowGraph": true, + "Color": "" + } + ] +} \ No newline at end of file diff --git a/source/CapFrameX.Overlay/OverlayEntryPersistence.cs b/source/CapFrameX.Overlay/OverlayEntryPersistence.cs new file mode 100644 index 00000000..2c0a05b2 --- /dev/null +++ b/source/CapFrameX.Overlay/OverlayEntryPersistence.cs @@ -0,0 +1,10 @@ +using CapFrameX.Contracts.Overlay; +using System.Collections.Generic; + +namespace CapFrameX.Overlay +{ + public class OverlayEntryPersistence + { + public List OverlayEntries { get; set; } + } +} diff --git a/source/CapFrameX.Overlay/OverlayEntryProvider.cs b/source/CapFrameX.Overlay/OverlayEntryProvider.cs index b2372821..327c78df 100644 --- a/source/CapFrameX.Overlay/OverlayEntryProvider.cs +++ b/source/CapFrameX.Overlay/OverlayEntryProvider.cs @@ -1,32 +1,133 @@ using CapFrameX.Contracts.Overlay; -using System; +using CapFrameX.Extensions; +using Newtonsoft.Json; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.IO; namespace CapFrameX.Overlay { public class OverlayEntryProvider : IOverlayEntryProvider { + private readonly Dictionary _identifierOverlayEntryDict + = new Dictionary(); + private List _overlayEntries; + public OverlayEntryProvider() { - + try + { + LoadOverlayEntriesFromJson(); + } + catch + { + SetOverlayEntryDefaults(); + } } public IOverlayEntry[] GetOverlayEntries() { - throw new NotImplementedException(); + return _overlayEntries.ToArray(); } public IOverlayEntry GetOverlayEntry(string identifier) { - throw new NotImplementedException(); + return _identifierOverlayEntryDict[identifier]; } public void MoveEntry(int sourceIndex, int targetIndex) { - throw new NotImplementedException(); + _overlayEntries.Move(sourceIndex, targetIndex); + } + + public bool SaveOverlayEntriesToJson() + { + try + { + var persistence = new OverlayEntryPersistence() + { + OverlayEntries = _overlayEntries + }; + + var json = JsonConvert.SerializeObject(persistence); + File.WriteAllText(@"OverlayConfiguration\OverlayEntryConfiguration.json", json); + + return true; + } + catch { return false; } + } + + private void LoadOverlayEntriesFromJson() + { + string json = File.ReadAllText(@"OverlayConfiguration\OverlayEntryConfiguration.json"); + _overlayEntries = new List(JsonConvert.DeserializeObject(json).OverlayEntries); + + foreach (var entry in _overlayEntries) + { + _identifierOverlayEntryDict.Add(entry.Identifier, entry); + } + } + + private void SetOverlayEntryDefaults() + { + _overlayEntries = new List + { + // CX + // CaptureServiceStatus + new OverlayEntryWrapper("CaptureServiceStatus") + { + ShowOnOverlay = true, + GroupName = string.Empty, + Value = "Capture service ready...", + ShowGraph = false, + Color = string.Empty + }, + + // RunHistory + new OverlayEntryWrapper("RunHistory") + { + ShowOnOverlay = true, + GroupName = string.Empty, + Value = default(object), + ShowGraph = false, + Color = string.Empty + }, + + // RunHistory + new OverlayEntryWrapper("CaptureTimer") + { + ShowOnOverlay = true, + GroupName = "Timer: ", + Value = "0", + ShowGraph = false, + Color = string.Empty + }, + + // RTSS + // Framerate + new OverlayEntryWrapper("Framerate") + { + ShowOnOverlay = true, + GroupName = "", + Value = 0d, + ShowGraph = false, + Color = string.Empty + }, + + // Frametime + new OverlayEntryWrapper("Frametime") + { + ShowOnOverlay = true, + GroupName = "", + Value = 0d, + ShowGraph = true, + Color = string.Empty + } + }; + + foreach (var entry in _overlayEntries) + { + _identifierOverlayEntryDict.Add(entry.Identifier, entry); + } } } } diff --git a/source/CapFrameX.Overlay/OverlayEntryWrapper.cs b/source/CapFrameX.Overlay/OverlayEntryWrapper.cs index 0af60d12..b9acf570 100644 --- a/source/CapFrameX.Overlay/OverlayEntryWrapper.cs +++ b/source/CapFrameX.Overlay/OverlayEntryWrapper.cs @@ -1,4 +1,5 @@ using CapFrameX.Contracts.Overlay; +using Newtonsoft.Json; using Prism.Mvvm; namespace CapFrameX.Overlay @@ -14,6 +15,7 @@ public class OverlayEntryWrapper : BindableBase, IOverlayEntry public string Identifier { get; } + [JsonIgnore] public string FormattedValue => _valueFormat == null ? Value.ToString() : string.Format(_valueFormat, Value); diff --git a/source/CapFrameX.Overlay/OverlayService.cs b/source/CapFrameX.Overlay/OverlayService.cs index b9a86a45..064309d7 100644 --- a/source/CapFrameX.Overlay/OverlayService.cs +++ b/source/CapFrameX.Overlay/OverlayService.cs @@ -85,7 +85,7 @@ public void StartCountdown(int seconds) SetCaptureTimerValue((int)t); if (t == 0) - OnCountdownFinished(); + SetShowCaptureTimer(false); }); } @@ -170,9 +170,11 @@ public void AddRunToHistory(List captureData) _pauseRefreshHeartBeat = false; } - private void OnCountdownFinished() + public void UpdateOverlayEntries() { - SetShowCaptureTimer(false); + _pauseRefreshHeartBeat = true; + SetOverlayEntries(_overlayEntryProvider?.GetOverlayEntries()); + _pauseRefreshHeartBeat = false; } private void SetShowCaptureTimer(bool show) diff --git a/source/CapFrameX.Overlay/packages.config b/source/CapFrameX.Overlay/packages.config index 66348262..c7d70966 100644 --- a/source/CapFrameX.Overlay/packages.config +++ b/source/CapFrameX.Overlay/packages.config @@ -1,5 +1,6 @@  + diff --git a/source/CapFrameX.Test/CapFrameX.Test.csproj b/source/CapFrameX.Test/CapFrameX.Test.csproj index 1014d0b1..5c0b05ac 100644 --- a/source/CapFrameX.Test/CapFrameX.Test.csproj +++ b/source/CapFrameX.Test/CapFrameX.Test.csproj @@ -60,6 +60,7 @@ + @@ -101,6 +102,10 @@ {a4559b99-7fcb-40ef-bf97-d293a2b7d6d3} CapFrameX.Extensions + + {624777C2-6AF6-46D5-A019-1285F699B8A7} + CapFrameX.Overlay + {adfbaece-b74f-4a80-87d1-84bc990527b0} CapFrameX.PresentMonInterface @@ -125,11 +130,21 @@ ..\..\packages\MSTest.TestFramework.2.0.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + ..\..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll + + + + + ..\..\packages\Prism.Core.7.0.0.396\lib\net45\Prism.dll + + ..\..\packages\System.Reactive.4.0.0\lib\net46\System.Reactive.dll + diff --git a/source/CapFrameX.Test/Overlay/OverlayEntryPersistenceTest.cs b/source/CapFrameX.Test/Overlay/OverlayEntryPersistenceTest.cs new file mode 100644 index 00000000..78f6bf17 --- /dev/null +++ b/source/CapFrameX.Test/Overlay/OverlayEntryPersistenceTest.cs @@ -0,0 +1,78 @@ +using CapFrameX.Contracts.Overlay; +using CapFrameX.Overlay; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; +using System.IO; + +namespace CapFrameX.Test.Overlay +{ + [TestClass] + public class OverlayEntryPersistenceTest + { + [TestMethod] + public void CreateJsonFile_InitialFile() + { + var persistence = new OverlayEntryPersistence + { + OverlayEntries = new System.Collections.Generic.List + { + // CX + // CaptureServiceStatus + new OverlayEntryWrapper("CaptureServiceStatus") + { + ShowOnOverlay = true, + GroupName = string.Empty, + Value = "Capture service ready...", + ShowGraph = false, + Color = string.Empty + }, + + // RunHistory + new OverlayEntryWrapper("RunHistory") + { + ShowOnOverlay = true, + GroupName = string.Empty, + Value = default(object), + ShowGraph = false, + Color = string.Empty + }, + + // RunHistory + new OverlayEntryWrapper("CaptureTimer") + { + ShowOnOverlay = true, + GroupName = "Timer: ", + Value = "0", + ShowGraph = false, + Color = string.Empty + }, + + // RTSS + // Framerate + new OverlayEntryWrapper("Framerate") + { + ShowOnOverlay = true, + GroupName = "", + Value = 0d, + ShowGraph = false, + Color = string.Empty + }, + + // Frametime + new OverlayEntryWrapper("Frametime") + { + ShowOnOverlay = true, + GroupName = "", + Value = 0d, + ShowGraph = true, + Color = string.Empty + } + } + }; + + File.WriteAllText( + @"..\..\..\..\CapFrameX.Overlay\OverlayConfiguration\OverlayEntryConfiguration.json", + JsonConvert.SerializeObject(persistence)); + } + } +} diff --git a/source/CapFrameX.Test/packages.config b/source/CapFrameX.Test/packages.config index fd7b8d25..9aafdd32 100644 --- a/source/CapFrameX.Test/packages.config +++ b/source/CapFrameX.Test/packages.config @@ -2,5 +2,7 @@ + + \ No newline at end of file