implement new Update 11 journal file names

This commit is contained in:
Florian Stinglmayr 2022-03-15 18:08:09 +01:00
parent bace2f1db5
commit 1dd95aff82
2 changed files with 53 additions and 40 deletions

View File

@ -8,24 +8,31 @@ using System.Globalization;
namespace EDJournal { namespace EDJournal {
public class JournalFile : IComparable<JournalFile> public class JournalFile : IComparable<JournalFile>
{ {
private string fullpath = null; public string FullPath { get; set; }
private string part = null; public int Part { get; set; }
private int intpart = 0; public DateTime DateTime { get; set; }
private DateTime datetime; public DateTime NormalisedDateTime { get; set; }
private DateTime normalised;
private List<Entry> entries = new List<Entry>(); public List<Entry> entries = new List<Entry>();
private static Regex fileregex = new Regex("Journal\\.(\\d+)\\.(\\d+)\\.log"); private static Regex fileregex = new Regex("Journal\\.(\\d+)\\.(\\d+)\\.log");
private static Regex update11regex = new Regex("Journal\\.([^\\.]+)\\.(\\d+).log");
private static string iso8601 = "yyyyMMddTHHmmss"; private static string iso8601 = "yyyyMMddTHHmmss";
private static string iso8601_notime = "yyyyMMdd";
public string FullPath => fullpath;
public static bool VerifyFile(string path) { public static bool VerifyFile(string path) {
string filename = Path.GetFileName(path); string filename = Path.GetFileName(path);
var matches = fileregex.Matches(filename); var matches = fileregex.Matches(filename);
return matches.Count != 0; if (matches.Count != 0) {
return true;
}
matches = update11regex.Matches(filename);
if (matches.Count != 0) {
return true;
}
return false;
} }
private void SetFilename(string path) { private void SetFilename(string path) {
@ -35,32 +42,50 @@ namespace EDJournal {
} }
var matches = fileregex.Matches(filename); var matches = fileregex.Matches(filename);
if (matches.Count < 1) { if (matches.Count == 0) {
throw new JournalException(string.Format("Invalid journal file: {0}", filename)); matches = update11regex.Matches(filename);
if (matches.Count == 0) {
throw new JournalException(string.Format("invalid file format: {0}", filename));
}
} }
this.fullpath = path; FullPath = path;
this.part = matches[0].Groups[2].ToString(); var groups = matches[0].Groups;
this.intpart = int.Parse(part); string part = groups[2].ToString();
// The ISO is not quite correct on journal filenames, so build string timestamp = groups[1].ToString();
// a proper ISO 8601 date time stamp out of it.
var date = new StringBuilder(); if (!timestamp.Contains("T")) {
date.Append("20"); // Add the missing century in front. /* pre update 11 file
date.Append(matches[0].Groups[1].ToString()); */
date.Insert(8, "T"); // Add the "T" separating date and time
this.datetime = DateTime.ParseExact(date.ToString(), iso8601, CultureInfo.InvariantCulture); // The ISO is not quite correct on journal filenames, so build
this.normalised = DateTime.Parse(this.datetime.ToShortDateString()); // a proper ISO 8601 date time stamp out of it.
var date = new StringBuilder();
date.Append("20"); // Add the missing century in front.
date.Append(timestamp);
date.Insert(8, "T"); // Add the "T" separating date and time
timestamp = date.ToString();
} else {
/* post update 11 file, remove dashes
*/
timestamp = timestamp.Replace("-", "");
}
this.DateTime = DateTime.ParseExact(timestamp, iso8601, CultureInfo.InvariantCulture);
this.Part = int.Parse(part);
this.NormalisedDateTime = DateTime.Parse(DateTime.ToShortDateString());
} }
public int CompareTo(JournalFile other) { public int CompareTo(JournalFile other) {
if (datetime == null || other.datetime == null) { if (DateTime == null || other.DateTime == null) {
return 0; return 0;
} }
var comp = DateTime.Compare(datetime, other.datetime); var comp = DateTime.Compare(DateTime, other.DateTime);
if (comp == 0) { if (comp == 0) {
/* If we have the exact same datetime we compare by parts. /* If we have the exact same datetime we compare by parts.
*/ */
return intpart - other.intpart; return Part - other.Part;
} else { } else {
return comp; return comp;
} }
@ -70,18 +95,6 @@ namespace EDJournal {
SetFilename(path); SetFilename(path);
} }
public DateTime Timestamp {
get { return datetime; }
}
public DateTime NormalisedTimestamp {
get { return normalised; }
}
public int Part {
get { return intpart; }
}
public IEnumerable<Entry> Entries { public IEnumerable<Entry> Entries {
get { get {
if (entries == null || entries.Count == 0) { if (entries == null || entries.Count == 0) {
@ -101,7 +114,7 @@ namespace EDJournal {
/* This needs to be done this way, otherwise, if the game is still running the journal files cannot /* This needs to be done this way, otherwise, if the game is still running the journal files cannot
* be accessed. And it is very much convenient to generate logs while the game is still running. * be accessed. And it is very much convenient to generate logs while the game is still running.
*/ */
using (var fs = new FileStream(this.fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var fs = new FileStream(FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
using (var sr = new StreamReader(fs, Encoding.UTF8)) { using (var sr = new StreamReader(fs, Encoding.UTF8)) {
string line = null; string line = null;
while ((line = sr.ReadLine()) != null) { while ((line = sr.ReadLine()) != null) {

View File

@ -38,7 +38,7 @@ namespace EDJournal {
public JournalFile GetLastFile() { public JournalFile GetLastFile() {
var lastfile = Files var lastfile = Files
.OrderBy(x => x.Timestamp) .OrderBy(x => x.DateTime)
.Last() .Last()
; ;
return lastfile; return lastfile;