mirror of
https://github.com/BubbaGumpShrump/AutoTrackR2.git
synced 2025-05-23 09:05:31 +00:00
Add more ship detection events. Add threading to log handling
This commit is contained in:
parent
0e4419e5cc
commit
d817b097ab
5 changed files with 114 additions and 10 deletions
AutoTrackR2
|
@ -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
|
||||
|
|
27
AutoTrackR2/LogEventHandlers/JumpDriveStateChangedEvent.cs
Normal file
27
AutoTrackR2/LogEventHandlers/JumpDriveStateChangedEvent.cs
Normal file
|
@ -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);;
|
||||
}
|
||||
}
|
||||
}
|
27
AutoTrackR2/LogEventHandlers/RequestJumpFailedEvent.cs
Normal file
27
AutoTrackR2/LogEventHandlers/RequestJumpFailedEvent.cs
Normal file
|
@ -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);;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue