diff --git a/JournalFile.cs b/JournalFile.cs index 5a82341..bfae462 100644 --- a/JournalFile.cs +++ b/JournalFile.cs @@ -19,6 +19,8 @@ namespace EDJournal { private static string iso8601 = "yyyyMMddTHHmmss"; private static string iso8601_notime = "yyyyMMdd"; + public string FullPath => fullpath; + public static bool VerifyFile(string path) { string filename = Path.GetFileName(path); diff --git a/JournalStream.cs b/JournalStream.cs index 0e61bbf..05e0ed3 100644 --- a/JournalStream.cs +++ b/JournalStream.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Collections.Generic; using System.IO; @@ -26,11 +27,28 @@ namespace EDJournal { Open(); } + private void AddFileToStreams(string path, bool seekend = false) { + if (!streams.ContainsKey(path) && JournalFile.VerifyFile(path)) { + var filestream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + streams[path] = new StreamReader(filestream); + + if (seekend) { + streams[path].BaseStream.Seek(0, SeekOrigin.End); + } + } + } + public void Open() { if (watcher != null) { return; } + if (journal.Files.Count > 0) { + JournalFile lastfile = journal.GetLastFile(); + /* add last file to stream */ + AddFileToStreams(lastfile.FullPath, true); + } + watcher = new FileSystemWatcher(journal.Location); watcher.NotifyFilter = NotifyFilters.FileName | @@ -53,16 +71,11 @@ namespace EDJournal { } private void Watcher_Created(object sender, FileSystemEventArgs e) { - if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) { - streams[e.FullPath] = new StreamReader(e.FullPath); - } + AddFileToStreams(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); - } + AddFileToStreams(e.FullPath); var stream = streams[e.FullPath]; if (stream == null) { diff --git a/PlayerJournal.cs b/PlayerJournal.cs index 348840f..3576753 100644 --- a/PlayerJournal.cs +++ b/PlayerJournal.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Collections.Generic; using System.IO; @@ -21,6 +22,7 @@ namespace EDJournal { private void Initialise(string path) { basepath = Environment.ExpandEnvironmentVariables(path); + ScanFiles(); } public List Files { @@ -34,6 +36,16 @@ namespace EDJournal { this.ScanFiles(); } + public JournalFile GetLastFile() { + var lastfile = Files + .GroupBy(x => x.Timestamp) + .Last() + .ToArray() + .Last() + ; + return lastfile; + } + public void ScanFiles() { var files = Directory.EnumerateFiles(basepath);