diff --git a/AutoTrackR2/HomePage.xaml.cs b/AutoTrackR2/HomePage.xaml.cs index cd264e6..318a63d 100644 --- a/AutoTrackR2/HomePage.xaml.cs +++ b/AutoTrackR2/HomePage.xaml.cs @@ -109,16 +109,13 @@ public partial class HomePage : UserControl }; // Ship - TrackREventDispatcher.InstancedInteriorEvent += (data) => { - if (data.OwnerGEID == LocalPlayerData.Username && data.Ship != null) - { + TrackREventDispatcher.JumpDriveStateChangedEvent += (shipName) => { Dispatcher.Invoke(() => { - PlayerShipTextBox.Text = data.Ship; + PlayerShipTextBox.Text = shipName; AdjustFontSize(PlayerShipTextBox); - LocalPlayerData.PlayerShip = data.Ship; + LocalPlayerData.PlayerShip = shipName; }); - } }; // Game Mode diff --git a/AutoTrackR2/LogEventHandlers/JumpDriveStateChangedEvent.cs b/AutoTrackR2/LogEventHandlers/JumpDriveStateChangedEvent.cs new file mode 100644 index 0000000..9955f7f --- /dev/null +++ b/AutoTrackR2/LogEventHandlers/JumpDriveStateChangedEvent.cs @@ -0,0 +1,27 @@ +using System.Text.RegularExpressions; + +namespace AutoTrackR2.LogEventHandlers; + +public class JumpDriveStateChangedEvent : ILogEventHandler +{ + public Regex Pattern { get; } + private Regex _cleanUpPattern = new Regex(@"(.+?)_\d+$"); + + public JumpDriveStateChangedEvent() + { + Pattern = new Regex(@"<Jump Drive State Changed>.*.adam: (?<ShipName>.*.) in"); + } + + public void Handle(LogEntry entry) + { + if (entry.Message is null) return; + var match = Pattern.Match(entry.Message); + if (!match.Success) return; + + match = _cleanUpPattern.Match(match.Groups["ShipName"].Value); + if (match.Success) + { + TrackREventDispatcher.OnJumpDriveStateChangedEvent(match.Groups[1].Value);; + } + } +} \ No newline at end of file diff --git a/AutoTrackR2/LogEventHandlers/RequestJumpFailedEvent.cs b/AutoTrackR2/LogEventHandlers/RequestJumpFailedEvent.cs new file mode 100644 index 0000000..c998685 --- /dev/null +++ b/AutoTrackR2/LogEventHandlers/RequestJumpFailedEvent.cs @@ -0,0 +1,27 @@ +using System.Text.RegularExpressions; + +namespace AutoTrackR2.LogEventHandlers; + +public class RequestJumpFailedEvent : ILogEventHandler +{ + public Regex Pattern { get; } + private Regex _cleanUpPattern = new Regex(@"(.+?)_\d+$"); + + public RequestJumpFailedEvent() + { + Pattern = new Regex(@"<Request Jump Failed>.*.adam: (?<ShipName>.*.) in"); + } + + public void Handle(LogEntry entry) + { + if (entry.Message is null) return; + var match = Pattern.Match(entry.Message); + if (!match.Success) return; + + match = _cleanUpPattern.Match(match.Groups["ShipName"].Value); + if (match.Success) + { + TrackREventDispatcher.OnJumpDriveStateChangedEvent(match.Groups[1].Value);; + } + } +} \ No newline at end of file diff --git a/AutoTrackR2/LogHandler.cs b/AutoTrackR2/LogHandler.cs index ce2f0ba..6cb61dd 100644 --- a/AutoTrackR2/LogHandler.cs +++ b/AutoTrackR2/LogHandler.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Diagnostics; +using System.IO; using System.Text.RegularExpressions; using AutoTrackR2.LogEventHandlers; @@ -13,11 +14,20 @@ public class LogEntry public required string? Message { get; set; } } + +enum GameProcessState +{ + NotRunning, + Running, + Unknown +} + public class LogHandler(string logPath) { private readonly string? _logPath = logPath; private FileStream? _fileStream; private StreamReader? _reader; + private GameProcessState _gameProcessState = GameProcessState.Unknown; private CancellationTokenSource cancellationToken = new CancellationTokenSource(); Thread? monitorThread; @@ -29,7 +39,9 @@ public class LogHandler(string logPath) new InstancedInteriorEvent(), new InArenaCommanderEvent(), new InPersistentUniverseEvent(), - new GameVersionEvent() + new GameVersionEvent(), + new JumpDriveStateChangedEvent(), + new RequestJumpFailedEvent() ]; // Initialize the LogHandler and run all startup handlers @@ -66,6 +78,7 @@ public class LogHandler(string logPath) // Parse a single line of the log file and run matching handlers private void HandleLogEntry(string line) { + Console.WriteLine(line); foreach (var handler in _eventHandlers) { var match = handler.Pattern.Match(line); @@ -85,10 +98,14 @@ public class LogHandler(string logPath) { while (!token.IsCancellationRequested) { + CheckGameProcessState(); + if (_reader?.ReadLine() is { } line) { - HandleLogEntry(line); - Console.WriteLine(line); + // start new thread to handle log entry + var thread = new Thread(() => HandleLogEntry(line)); + thread.Start(); + // Console.WriteLine(line); } else { @@ -98,4 +115,32 @@ public class LogHandler(string logPath) } Console.WriteLine("Monitor thread stopped"); } + + private void CheckGameProcessState() + { + // Check if the game process is running by window name + var process = Process.GetProcesses().FirstOrDefault(p => p.MainWindowTitle == "Star Citizen"); + + GameProcessState newGameProcessState = process != null ? GameProcessState.Running : GameProcessState.NotRunning; + + if (newGameProcessState == GameProcessState.Running && _gameProcessState == GameProcessState.NotRunning) + { + // Game process went from NotRunning to Running, so reload the Game.log file + Console.WriteLine("Game process started, reloading log file"); + + _reader?.Close(); + _fileStream?.Close(); + + // Wait for the log file to be written to + Thread.Sleep(3000); + + _fileStream = new FileStream(_logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + _reader = new StreamReader(_fileStream); + + // Skip all the startup junk + while (_reader.ReadLine() is { } line) { } + } + + _gameProcessState = newGameProcessState; + } } \ No newline at end of file diff --git a/AutoTrackR2/TrackREventDispatcher.cs b/AutoTrackR2/TrackREventDispatcher.cs index 8b3f12d..9aaab03 100644 --- a/AutoTrackR2/TrackREventDispatcher.cs +++ b/AutoTrackR2/TrackREventDispatcher.cs @@ -46,4 +46,12 @@ public static class TrackREventDispatcher { VehicleDestructionEvent?.Invoke(data); } + + // Jump Drive state has changed + // Todo: Add proper data for this event. Right now only ship name is used. + public static event Action<string>? JumpDriveStateChangedEvent; + public static void OnJumpDriveStateChangedEvent(string shipName) + { + JumpDriveStateChangedEvent?.Invoke(shipName); + } } \ No newline at end of file