2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00

Handle anciliary files.

This commit is contained in:
Xjph 2022-06-09 21:10:35 -02:30
parent 3896621092
commit 750cb5bec4

View File

@ -193,6 +193,18 @@ namespace Observatory
private Dictionary<string, int> currentLine;
private LogMonitorState currentState = LogMonitorState.Idle; // Change via #SetLogMonitorState
private bool firstStartMonitor = true;
private string[] EventsWithAncillaryFile = new string[]
{
"Cargo",
"NavRoute",
"Market",
"Outfitting",
"Shipyard",
"Backpack",
"FCMaterials",
"ModuleInfo",
"ShipLocker"
};
#endregion
@ -295,6 +307,16 @@ namespace Observatory
return readErrors;
}
private JournalEventArgs DeserializeToEventArgs(string eventType, string line)
{
var eventClass = journalTypes[eventType];
MethodInfo journalRead = typeof(JournalReader).GetMethod(nameof(JournalReader.ObservatoryDeserializer));
MethodInfo journalGeneric = journalRead.MakeGenericMethod(eventClass);
object entry = journalGeneric.Invoke(null, new object[] { line });
return new JournalEventArgs() { journalType = eventClass, journalEvent = entry };
}
private void DeserializeAndInvoke(string line)
{
var eventType = JournalUtilities.GetEventType(line);
@ -303,14 +325,32 @@ namespace Observatory
eventType = "JournalBase";
}
var eventClass = journalTypes[eventType];
MethodInfo journalRead = typeof(JournalReader).GetMethod(nameof(JournalReader.ObservatoryDeserializer));
MethodInfo journalGeneric = journalRead.MakeGenericMethod(eventClass);
object entry = journalGeneric.Invoke(null, new object[] { line });
var journalEvent = new JournalEventArgs() { journalType = eventClass, journalEvent = entry };
var handler = JournalEntry;
var journalEvent = DeserializeToEventArgs(eventType, line);
JournalEntry?.Invoke(this, journalEvent);
handler?.Invoke(this, journalEvent);
// Files are only valid if realtime, otherwise they will be stale or empty.
if (!currentState.HasFlag(LogMonitorState.Batch) && EventsWithAncillaryFile.Contains(eventType))
{
HandleAncillaryFile(eventType);
}
}
private void HandleAncillaryFile(string eventType)
{
string filename = eventType == "ModuleInfo"
? "ModulesInfo.json" // Just FDev things
: eventType + ".json";
// I have no idea what order Elite writes these files or if they're already written
// by the time the journal updates.
// Brief sleep to ensure the content is updated before we read it.
System.Threading.Thread.Sleep(50);
string fileContent = File.ReadAllText(journalWatcher.Path + Path.DirectorySeparatorChar + filename);
var fileObject = DeserializeToEventArgs(eventType + "File", fileContent);
JournalEntry?.Invoke(this, fileObject);
}