From 1abeb7af936cea0bd6fbb0f7f90fb7dd938fe75a Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Wed, 25 Aug 2021 12:16:28 +0200 Subject: [PATCH] add a journal stream to get new events --- Journal/Entry.cs | 7 ++++ Journal/JournalFile.cs | 7 ++++ Journal/JournalStream.cs | 87 ++++++++++++++++++++++++++++++++++++++++ Journal/PlayerJournal.cs | 2 + nonabgs.csproj | 1 + 5 files changed, 104 insertions(+) create mode 100644 Journal/JournalStream.cs diff --git a/Journal/Entry.cs b/Journal/Entry.cs index 86a27ea..d6c3c15 100644 --- a/Journal/Entry.cs +++ b/Journal/Entry.cs @@ -84,5 +84,12 @@ namespace NonaBGS.Journal { public JObject JSON { get { return this.json; } } + + public override string ToString() { + if (json == null) { + return ""; + } + return json.ToString(); + } } } diff --git a/Journal/JournalFile.cs b/Journal/JournalFile.cs index 2b35776..dc10c9b 100644 --- a/Journal/JournalFile.cs +++ b/Journal/JournalFile.cs @@ -20,6 +20,13 @@ namespace NonaBGS.Journal private static string iso8601 = "yyyyMMddTHHmmss"; private static string iso8601_notime = "yyyyMMdd"; + public static bool VerifyFile(string path) { + string filename = Path.GetFileName(path); + + var matches = fileregex.Matches(filename); + return matches.Count != 0; + } + private void SetFilename(string path) { string filename = Path.GetFileName(path); if (!File.Exists(path) || filename == null) { diff --git a/Journal/JournalStream.cs b/Journal/JournalStream.cs new file mode 100644 index 0000000..4d76a1e --- /dev/null +++ b/Journal/JournalStream.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NonaBGS.Journal; +using System.IO; + +namespace NonaBGS.Journal { + public class JournalStream { + private PlayerJournal journal = null; + private FileSystemWatcher watcher = null; + + private Dictionary streams = new Dictionary(); + + public delegate void NewJournalEntryDelegate(Entry entry); + + public event NewJournalEntryDelegate NewJournalEntry; + + public PlayerJournal Journal { + get => journal; + set => journal = value; + } + + public JournalStream() { + } + + public JournalStream(PlayerJournal journal) { + this.journal = journal; + Open(); + } + + public void Open() { + if (watcher != null) { + return; + } + + watcher = new FileSystemWatcher(journal.Location); + + watcher.NotifyFilter = NotifyFilters.FileName | + NotifyFilters.LastWrite | + NotifyFilters.Size; + + watcher.Changed += Watcher_Changed; + watcher.Created += Watcher_Created; + + watcher.Filter = "*.log"; + watcher.EnableRaisingEvents = true; + } + + public void Close() { + if (watcher != null) { + watcher.EnableRaisingEvents = false; + } + watcher = null; + streams.Clear(); + } + + private void Watcher_Created(object sender, FileSystemEventArgs e) { + if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) { + streams[e.FullPath] = new StreamReader(e.FullPath); + } + } + + private void Watcher_Changed(object sender, FileSystemEventArgs e) { + if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) { + var filestream = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + streams[e.FullPath] = new StreamReader(filestream); + } + + var stream = streams[e.FullPath]; + if (stream == null) { + return; + } + + string line; + + while ((line = stream.ReadLine()) != null) { + try { + Entry entry = Entry.Parse(line); + NewJournalEntry?.Invoke(entry); + } catch (Exception) { + } + } + } + } +} diff --git a/Journal/PlayerJournal.cs b/Journal/PlayerJournal.cs index 24f09ff..850ae30 100644 --- a/Journal/PlayerJournal.cs +++ b/Journal/PlayerJournal.cs @@ -11,6 +11,8 @@ namespace NonaBGS.Journal { private List journalfiles = new List(); private string basepath = null; + public string Location => basepath; + public PlayerJournal() { Initialise(DefaultPath); } diff --git a/nonabgs.csproj b/nonabgs.csproj index 5168049..8630c45 100644 --- a/nonabgs.csproj +++ b/nonabgs.csproj @@ -77,6 +77,7 @@ +