EDBGS/EDPlayerJournal/PlayerJournal.cs
Florian Stinglmayr d6842115c5 no longer completely fail on one wrong entry
U17 might have caused some journal bugs so don't fail immediately on one wrong entry, instead keep them in an Errors collection for later processing
2023-10-25 11:30:34 +02:00

96 lines
2.4 KiB
C#

using EDPlayerJournal.Entries;
namespace EDPlayerJournal;
public class PlayerJournal {
public static string DefaultPath = "%UserProfile%\\Saved Games\\Frontier Developments\\Elite Dangerous";
private List<JournalFile> journalfiles = new List<JournalFile>();
private string? basepath = null;
public string? Location => basepath;
public PlayerJournal() {
Initialise(DefaultPath);
}
public PlayerJournal(string path) {
Initialise(path);
}
private void Initialise(string path) {
basepath = Environment.ExpandEnvironmentVariables(path);
ScanFiles();
}
public List<Exception> AllErrors {
get {
return Files.SelectMany(x => x.Errors).ToList();
}
}
public List<JournalFile> Files {
get { return journalfiles; }
}
public void Open() {
if (!Directory.Exists(basepath)) {
throw new JournalException("Invalid base path, path could not be found");
}
this.ScanFiles();
}
public JournalFile GetLastFile() {
var lastfile = Files
.OrderBy(x => x.DateTime)
.Last()
;
return lastfile;
}
public void ScanFiles() {
if (basepath == null) {
return;
}
var files = Directory.EnumerateFiles(basepath);
journalfiles.Clear();
foreach (var file in files) {
string full = Path.Combine(basepath, file);
try {
JournalFile journalfile = new JournalFile(full);
journalfiles.Add(journalfile);
} catch (JournalException) {
/* invalid journal file, or one of the other json files in there */
continue;
}
}
journalfiles.Sort();
}
public Entry? FindMostRecent(string entry) {
var entries = journalfiles
.OrderByDescending(x => x.DateTime)
.ToArray()
;
if (entries == null || entries.Length == 0) {
return null;
}
foreach(JournalFile file in entries) {
Entry found = file.Entries
.OrderByDescending(x => x.Timestamp)
.First(x => x.Event == entry)
;
if (found != null) {
return found;
}
}
return null;
}
}