From d3a2db3d0ade270b008d666c43353e42a209cf4a Mon Sep 17 00:00:00 2001 From: KatterMaw Date: Fri, 22 Sep 2023 17:48:48 +0500 Subject: [PATCH] Apply changes to profile runner when it's edited --- .../Scoring/HotKeyProfileRunner.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/SightKeeper.Services/Scoring/HotKeyProfileRunner.cs b/SightKeeper.Services/Scoring/HotKeyProfileRunner.cs index 26d78e06..4431ae2b 100644 --- a/SightKeeper.Services/Scoring/HotKeyProfileRunner.cs +++ b/SightKeeper.Services/Scoring/HotKeyProfileRunner.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Numerics; using System.Reactive.Disposables; +using System.Reactive.Linq; using CommunityToolkit.Diagnostics; using Serilog; using SharpHook.Native; @@ -16,11 +17,12 @@ namespace SightKeeper.Services.Scoring; public sealed class HotKeyProfileRunner : ProfileRunner { - public HotKeyProfileRunner(StreamDetector streamDetector, MouseMover mouseMover, SharpHookHotKeyManager hotKeyManager) + public HotKeyProfileRunner(StreamDetector streamDetector, MouseMover mouseMover, SharpHookHotKeyManager hotKeyManager, ProfileEditor profileEditor) { _streamDetector = streamDetector; _mouseMover = mouseMover; _hotKeyManager = hotKeyManager; + _profileEditor = profileEditor; _streamDetector.ObservableDetection.Subscribe(HandleDetection).DisposeWithEx(_constructorDisposables); } @@ -37,6 +39,17 @@ public void Run(Profile profile) _currentProfileDisposables = new CompositeDisposable(); _hotKeyManager.Register(MouseButton.Button4, () => _streamDetector.IsEnabled = true, () => _streamDetector.IsEnabled = false) .DisposeWithEx(_currentProfileDisposables); + _profileEditor.ProfileEdited.Where(editedProfile => editedProfile == profile).Subscribe(OnProfileEdited) + .DisposeWithEx(_currentProfileDisposables); + } + + private void OnProfileEdited(Profile profile) + { + _itemClassesIndexes = profile.ItemClasses.ToDictionary( + profileItemClass => profileItemClass.ItemClass, + profileItemClass => profileItemClass.Index); + _streamDetector.Weights = profile.Weights; + _streamDetector.ProbabilityThreshold = profile.DetectionThreshold; } private void HandleDetection(ImmutableList items) @@ -96,6 +109,7 @@ public void Stop() private readonly StreamDetector _streamDetector; private readonly MouseMover _mouseMover; private readonly SharpHookHotKeyManager _hotKeyManager; + private readonly ProfileEditor _profileEditor; private Profile? _currentProfile; private CompositeDisposable? _currentProfileDisposables; private Dictionary? _itemClassesIndexes;