From 8472964d1c500194286f0a19286fa8b01fb0987a Mon Sep 17 00:00:00 2001 From: Slim Debug Date: Wed, 18 May 2022 15:42:15 +0200 Subject: [PATCH 1/3] improved file read logic after file changes --- HuntMmrReader/Models/HuntReader.cs | 70 +++++++++---------- .../ViewModels/MainWindowViewModel.cs | 4 +- HuntMmrReader/Views/MainWindow.xaml | 5 +- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/HuntMmrReader/Models/HuntReader.cs b/HuntMmrReader/Models/HuntReader.cs index 7e79f72..eb9979d 100644 --- a/HuntMmrReader/Models/HuntReader.cs +++ b/HuntMmrReader/Models/HuntReader.cs @@ -4,8 +4,8 @@ using System.Globalization; using System.IO; using System.Linq; -using System.Threading; using System.Threading.Tasks; +using System.Timers; using System.Xml.Linq; using HuntMmrReader.DesignHelper; @@ -15,26 +15,26 @@ internal class HuntReader : ObservableObject, IDisposable { internal const ushort MaxPlayers = 12; internal const ushort MaxTeams = MaxPlayers; - - private readonly TimeSpan _readTimeout; - - private readonly BlockingCollection<(DateTime modifiedDateTime, bool alreadyTried)> _readXmlQueue = - new(new ConcurrentQueue<(DateTime modifieDateTime, bool alreadyTried)>()); + private readonly TimeSpan _readDelay; + private readonly Timer _readTimer; private readonly object _syncLastReadTimeObject = new(); private readonly object _syncPathObject = new(); private readonly ConcurrentBag _teams = new(); + private DateTime _lastModificationTime; private DateTime _lastReadTime; + private bool _readSinceLastModification; private FileSystemWatcher? _watcher; - internal HuntReader(TimeSpan readTimeout) + internal HuntReader(TimeSpan readDelay) { + _readSinceLastModification = false; + _readDelay = readDelay; _lastReadTime = DateTime.Now; - _readTimeout = readTimeout; - new Thread(ProcessFileChanges) - { - IsBackground = true - }.Start(); + _lastModificationTime = _lastReadTime; + _readTimer = new Timer {AutoReset = true, Interval = readDelay.TotalMilliseconds}; + _readTimer.Elapsed += ReadTimerElapsed; + _readTimer.Start(); } internal string? FilePath { get; private set; } @@ -57,34 +57,28 @@ internal DateTime LastReadTime public void Dispose() { - _readXmlQueue.Dispose(); + _readTimer.Dispose(); _watcher?.Dispose(); } - - private void ProcessFileChanges() + private void ReadTimerElapsed(object? sender, ElapsedEventArgs e) { - while (true) + if (string.IsNullOrEmpty(FilePath) || _lastModificationTime.Add(_readDelay) >= DateTime.Now || + _readSinceLastModification) + return; + try { - var (modifiedDateTime, alreadyTried) = _readXmlQueue.Take(); - if (string.IsNullOrEmpty(FilePath)) - return; - try - { - Thread.Sleep(_readTimeout); - Read(FilePath); - LastReadTime = modifiedDateTime; - } - catch (Exception e) - { - if (!alreadyTried) - _readXmlQueue.Add((DateTime.Now, true)); - else - OnExceptionRaised(e); - } + Read(FilePath); + LastReadTime = DateTime.Now; + _readSinceLastModification = true; + } + catch (Exception ex) + { + OnExceptionRaised(ex); } } + private IEnumerable GetPlayersFromDoc(XContainer doc) { var teams = new List(); @@ -219,6 +213,13 @@ private void HandlePath(string path, IEnumerable players) foreach (var player in players) _teams.Add(player); OnPropertyChanged(nameof(Teams)); LastReadTime = DateTime.Now; + PrepareFileWatcher(path); + + SetPath(path); + } + + internal void PrepareFileWatcher(string path) + { var parentDirectory = Directory.GetParent(path); var filename = Path.GetFileName(path); if (parentDirectory == default || string.IsNullOrEmpty(filename)) @@ -234,8 +235,6 @@ private void HandlePath(string path, IEnumerable players) { _watcher.Path = parentDirectory.FullName; } - - SetPath(path); } internal void Read(string path) @@ -285,7 +284,8 @@ internal void SetPath(string? path) private void Watcher_Changed(object sender, FileSystemEventArgs e) { - _readXmlQueue.Add((DateTime.Now, false)); + _lastModificationTime = DateTime.Now; + _readSinceLastModification = false; } internal event EventHandler? ExceptionRaised; diff --git a/HuntMmrReader/ViewModels/MainWindowViewModel.cs b/HuntMmrReader/ViewModels/MainWindowViewModel.cs index ad57ce5..b545e2e 100644 --- a/HuntMmrReader/ViewModels/MainWindowViewModel.cs +++ b/HuntMmrReader/ViewModels/MainWindowViewModel.cs @@ -37,7 +37,7 @@ public MainWindowViewModel() OpenFolderCommand = new RelayCommand(OpenFolder, CheckIfFileExists); ClipboardCopyCommand = new RelayCommand(CopyToClipboard); AboutCommand = new RelayCommand(OpenAbout); - _reader = new HuntReader(TimeSpan.FromSeconds(3)); + _reader = new HuntReader(TimeSpan.FromSeconds(20)); _reader.PropertyChanged += Reader_PropertyChanged; _reader.ExceptionRaised += Reader_ExceptionRaised; _filePath = ""; @@ -45,6 +45,8 @@ public MainWindowViewModel() try { FilePath = Settings.Default.LastPath; + if (CheckIfFileExists(FilePath)) + _reader.PrepareFileWatcher(FilePath); } catch (SettingsPropertyNotFoundException) { diff --git a/HuntMmrReader/Views/MainWindow.xaml b/HuntMmrReader/Views/MainWindow.xaml index 4422f66..1e43ce0 100644 --- a/HuntMmrReader/Views/MainWindow.xaml +++ b/HuntMmrReader/Views/MainWindow.xaml @@ -86,14 +86,13 @@ - + CommandParameter="{Binding SelectedDisplayException.StackTrace}" /> Date: Wed, 18 May 2022 15:58:03 +0200 Subject: [PATCH 2/3] Update README.md --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7dfef99..f87de7f 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,20 @@ This tool let's you read out your exact MMR as a number. 1. Download the [newest release version](https://github.com/slimDebug/HuntMmrReader/releases/latest) of the unt MMR Reader. Refer to [Downloading](#Downloading) if you are unsure what version to download. 2. Extract the zip whereever you like. I recommend using [7-zip](https://www.7-zip.org/), but other utilities should work without problems as well. 3. Open the **_HuntMmrReader.exe_** file. -4. Click _Settings_ in the menu tab and click _Edit Path..._.![grafik](https://user-images.githubusercontent.com/66317138/167859972-eeff99ab-2166-4906-9480-7f4ae567e862.png) +4. Click _Settings_ in the menu tab and click _Edit Path..._.![grafik](https://user-images.githubusercontent.com/66317138/169058102-f069f162-1813-4978-b266-dab1a82ea01c.png) 5. Select the _attributes.xml_ file from your **Hunt: Showdown** installation folder. The file should be located in **_SteamLibrary\steamapps\common\Hunt Showdown\user\profiles\default\attributes.xml_** 6. Hit the open button. -7. You should see something like this:![grafik](https://user-images.githubusercontent.com/66317138/167861050-40446d3a-2315-4b10-84cb-2e5c31bc499e.png) -8. Congratulations. You can see the MMR of all teams/players of your most recent match (_luckily for us this information is saved locally_). +7. You should see something like this:![example_screenshot](https://user-images.githubusercontent.com/66317138/169058676-93fed089-2124-45ba-8d7f-d0b265660542.PNG) +8. Congratulations. You can see the MMR of all teams/players of you +r most recent match (_luckily for us this information is saved locally_). + +## Reporting errors + +If an error occurs and you would like to report it, please oben a GitHub issue or on the [steam guide](https://steamcommunity.com/sharedfiles/filedetails/?id=2806779825) page.\ +Errors can be seen in the marked box here:\ + +\It's important that you also provide a Stack trace with the error you're reporting. You can obtain a Stack trace by right clicking the error and pressing the _Copy Stack trace to clipboard_ option. ## Downloading From 1b2f727af8aa606e9f7891afbda7cfeca68cbb1a Mon Sep 17 00:00:00 2001 From: slimDebug <66317138+slimDebug@users.noreply.github.com> Date: Wed, 18 May 2022 16:10:02 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f87de7f..e43a9ca 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ r most recent match (_luckily for us this information is saved locally_). If an error occurs and you would like to report it, please oben a GitHub issue or on the [steam guide](https://steamcommunity.com/sharedfiles/filedetails/?id=2806779825) page.\ Errors can be seen in the marked box here:\ - -\It's important that you also provide a Stack trace with the error you're reporting. You can obtain a Stack trace by right clicking the error and pressing the _Copy Stack trace to clipboard_ option. +![grafik](https://user-images.githubusercontent.com/66317138/169061726-cc95afd3-db82-4931-8a2d-347f9fbffdca.png) +It's important that you also provide a Stack trace with the error you're reporting. You can obtain a Stack trace by right clicking the error and pressing the _Copy Stack trace to clipboard_ option. ## Downloading