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 {
public class JournalFile : IComparable<JournalFile>
{
private string fullpath = null;
private string part = null;
private int intpart = 0;
private DateTime datetime;
private DateTime normalised;
private List<Entry> entries = new List<Entry>();
public string FullPath { get; set; }
public int Part { get; set; }
public DateTime DateTime { get; set; }
public DateTime NormalisedDateTime { get; set; }
public List<Entry> entries = new List<Entry>();
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_notime = "yyyyMMdd";
public string FullPath => fullpath;
public static bool VerifyFile(string path) {
string filename = Path.GetFileName(path);
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) {
@ -35,32 +42,50 @@ namespace EDJournal {
}
var matches = fileregex.Matches(filename);
if (matches.Count < 1) {
throw new JournalException(string.Format("Invalid journal file: {0}", filename));
if (matches.Count == 0) {
matches = update11regex.Matches(filename);
if (matches.Count == 0) {
throw new JournalException(string.Format("invalid file format: {0}", filename));
}
}
this.fullpath = path;
this.part = matches[0].Groups[2].ToString();
this.intpart = int.Parse(part);
FullPath = path;
var groups = matches[0].Groups;
string part = groups[2].ToString();
string timestamp = groups[1].ToString();
if (!timestamp.Contains("T")) {
/* pre update 11 file
*/
// The ISO is not quite correct on journal filenames, so build
// 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(matches[0].Groups[1].ToString());
date.Append(timestamp);
date.Insert(8, "T"); // Add the "T" separating date and time
this.datetime = DateTime.ParseExact(date.ToString(), iso8601, CultureInfo.InvariantCulture);
this.normalised = DateTime.Parse(this.datetime.ToShortDateString());
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) {
if (datetime == null || other.datetime == null) {
if (DateTime == null || other.DateTime == null) {
return 0;
}
var comp = DateTime.Compare(datetime, other.datetime);
var comp = DateTime.Compare(DateTime, other.DateTime);
if (comp == 0) {
/* If we have the exact same datetime we compare by parts.
*/
return intpart - other.intpart;
return Part - other.Part;
} else {
return comp;
}
@ -70,18 +95,6 @@ namespace EDJournal {
SetFilename(path);
}
public DateTime Timestamp {
get { return datetime; }
}
public DateTime NormalisedTimestamp {
get { return normalised; }
}
public int Part {
get { return intpart; }
}
public IEnumerable<Entry> Entries {
get {
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
* 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)) {
string line = null;
while ((line = sr.ReadLine()) != null) {

View File

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