mirror of
https://github.com/BubbaGumpShrump/AutoTrackR2.git
synced 2025-05-04 17:45:31 +00:00
Added Initialization Waits
This will help prevent reading the old game.log on boot.
This commit is contained in:
parent
e88876a620
commit
ad773f6790
2 changed files with 115 additions and 22 deletions
AutoTrackR2
|
@ -25,6 +25,9 @@ public partial class HomePage : UserControl
|
|||
private bool _isLogHandlerRunning = false;
|
||||
private int _counter = 1;
|
||||
private System.Timers.Timer _counterTimer;
|
||||
private bool _isInitializing = false;
|
||||
private System.Timers.Timer? _initializationTimer;
|
||||
private bool _wasStarCitizenRunningOnStart = false;
|
||||
|
||||
public HomePage()
|
||||
{
|
||||
|
@ -42,6 +45,18 @@ public partial class HomePage : UserControl
|
|||
AdjustFontSize(KillTallyTextBox);
|
||||
AddKillHistoryKillsToUI();
|
||||
|
||||
// Check if Star Citizen is already running
|
||||
_wasStarCitizenRunningOnStart = IsStarCitizenRunning();
|
||||
if (_wasStarCitizenRunningOnStart)
|
||||
{
|
||||
UpdateStatusIndicator(true);
|
||||
InitializeLogHandler();
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateStatusIndicator(false);
|
||||
}
|
||||
|
||||
// Initialize and start the status check timer
|
||||
_statusCheckTimer = new System.Timers.Timer(1000); // Check every second
|
||||
_statusCheckTimer.Elapsed += CheckStarCitizenStatus;
|
||||
|
@ -51,16 +66,6 @@ public partial class HomePage : UserControl
|
|||
_counterTimer = new System.Timers.Timer(1000); // Update every second
|
||||
_counterTimer.Elapsed += UpdateCounter;
|
||||
_counterTimer.Start();
|
||||
|
||||
// Check if Star Citizen is already running and initialize accordingly
|
||||
if (IsStarCitizenRunning())
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
UpdateStatusIndicator(true);
|
||||
InitializeLogHandler(); // Then initialize the log handler
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckStarCitizenStatus(object? sender, ElapsedEventArgs e)
|
||||
|
@ -68,14 +73,44 @@ public partial class HomePage : UserControl
|
|||
bool isRunning = IsStarCitizenRunning();
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
UpdateStatusIndicator(isRunning);
|
||||
if (_isInitializing)
|
||||
{
|
||||
return; // Don't update status while initializing
|
||||
}
|
||||
|
||||
if (isRunning)
|
||||
{
|
||||
if (!_isLogHandlerRunning)
|
||||
{
|
||||
// Game is running, start log monitoring and read initial states
|
||||
InitializeLogHandler();
|
||||
if (_wasStarCitizenRunningOnStart)
|
||||
{
|
||||
// Game was already running on start, initialize immediately
|
||||
UpdateStatusIndicator(true);
|
||||
InitializeLogHandler();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Game started after app launch, use initialization delay
|
||||
_isInitializing = true;
|
||||
UpdateStatusIndicator(false, true); // Set to yellow for initialization
|
||||
|
||||
_initializationTimer = new System.Timers.Timer(20000); // 20 seconds
|
||||
_initializationTimer.Elapsed += (sender, e) =>
|
||||
{
|
||||
_isInitializing = false;
|
||||
_initializationTimer?.Stop();
|
||||
_initializationTimer?.Dispose();
|
||||
_initializationTimer = null;
|
||||
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
// Game is running, start log monitoring and read initial states
|
||||
UpdateStatusIndicator(true);
|
||||
InitializeLogHandler();
|
||||
});
|
||||
};
|
||||
_initializationTimer.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -96,16 +131,23 @@ public partial class HomePage : UserControl
|
|||
_logHandler?.StopMonitoring();
|
||||
_isLogHandlerRunning = false;
|
||||
}
|
||||
|
||||
UpdateStatusIndicator(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void UpdateStatusIndicator(bool isRunning)
|
||||
private void UpdateStatusIndicator(bool isRunning, bool isInitializing = false)
|
||||
{
|
||||
if (isRunning)
|
||||
if (isInitializing)
|
||||
{
|
||||
StatusLight.Fill = new SolidColorBrush(Colors.Yellow);
|
||||
StatusText.Text = "TrackR\nInitializing";
|
||||
}
|
||||
else if (isRunning)
|
||||
{
|
||||
StatusLight.Fill = new SolidColorBrush(Colors.Green);
|
||||
StatusText.Text = "TrackR\nActive";
|
||||
StatusText.Text = "TrackR\nRunning";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -30,8 +30,11 @@ public class LogHandler
|
|||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private GameProcessState _gameProcessState = GameProcessState.NotRunning;
|
||||
private bool _isMonitoring = false;
|
||||
private bool _isInitializing = false;
|
||||
private System.Timers.Timer? _initializationTimer;
|
||||
|
||||
public bool IsMonitoring => _isMonitoring;
|
||||
public bool IsInitializing => _isInitializing;
|
||||
|
||||
// Handlers that should be run on every log entry
|
||||
// Overlap with _startupEventHandlers is fine
|
||||
|
@ -62,6 +65,37 @@ public class LogHandler
|
|||
throw new FileNotFoundException("Log file not found", _logPath);
|
||||
}
|
||||
|
||||
// Check if Star Citizen is running
|
||||
if (!IsStarCitizenRunning())
|
||||
{
|
||||
StartInitializationDelay();
|
||||
return;
|
||||
}
|
||||
|
||||
InitializeLogHandler();
|
||||
}
|
||||
|
||||
private void StartInitializationDelay()
|
||||
{
|
||||
_isInitializing = true;
|
||||
_initializationTimer = new System.Timers.Timer(20000); // 20 seconds
|
||||
_initializationTimer.Elapsed += (sender, e) =>
|
||||
{
|
||||
_isInitializing = false;
|
||||
_initializationTimer?.Stop();
|
||||
_initializationTimer?.Dispose();
|
||||
_initializationTimer = null;
|
||||
|
||||
if (IsStarCitizenRunning())
|
||||
{
|
||||
InitializeLogHandler();
|
||||
}
|
||||
};
|
||||
_initializationTimer.Start();
|
||||
}
|
||||
|
||||
private void InitializeLogHandler()
|
||||
{
|
||||
_fileStream = new FileStream(_logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
_reader = new StreamReader(_fileStream);
|
||||
|
||||
|
@ -75,6 +109,11 @@ public class LogHandler
|
|||
StartMonitoring();
|
||||
}
|
||||
|
||||
private bool IsStarCitizenRunning()
|
||||
{
|
||||
return Process.GetProcessesByName("StarCitizen").Length > 0;
|
||||
}
|
||||
|
||||
public void StartMonitoring()
|
||||
{
|
||||
if (_isMonitoring) return;
|
||||
|
@ -156,14 +195,26 @@ public class LogHandler
|
|||
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");
|
||||
// Game process went from NotRunning to Running, wait 20 seconds before reloading
|
||||
Console.WriteLine("Game process started, waiting 20 seconds before initializing...");
|
||||
_isInitializing = true;
|
||||
|
||||
_reader?.Close();
|
||||
_fileStream?.Close();
|
||||
_initializationTimer = new System.Timers.Timer(20000); // 20 seconds
|
||||
_initializationTimer.Elapsed += (sender, e) =>
|
||||
{
|
||||
_isInitializing = false;
|
||||
_initializationTimer?.Stop();
|
||||
_initializationTimer?.Dispose();
|
||||
_initializationTimer = null;
|
||||
|
||||
_fileStream = new FileStream(_logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
_reader = new StreamReader(_fileStream);
|
||||
Console.WriteLine("Initialization delay complete, reloading log file");
|
||||
_reader?.Close();
|
||||
_fileStream?.Close();
|
||||
|
||||
_fileStream = new FileStream(_logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
_reader = new StreamReader(_fileStream);
|
||||
};
|
||||
_initializationTimer.Start();
|
||||
}
|
||||
_gameProcessState = newGameProcessState;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue