Skip to content

Commit b767682

Browse files
committed
Adjust Core Plugin changes
1 parent f161c64 commit b767682

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

Hi3Helper.Plugin.HBR/Exports.GameLaunch.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
using Microsoft.Extensions.Logging;
1112

1213
namespace Hi3Helper.Plugin.HBR;
1314

@@ -24,14 +25,22 @@ public override async Task<bool> LaunchGameFromGameManagerCoreAsync(GameManagerE
2425
using (process)
2526
{
2627
process.Start();
27-
process.PriorityBoostEnabled = isRunBoosted;
28-
process.PriorityClass = processPriority;
28+
29+
try
30+
{
31+
process.PriorityBoostEnabled = isRunBoosted;
32+
process.PriorityClass = processPriority;
33+
}
34+
catch (Exception e)
35+
{
36+
InstanceLogger.LogError(e, "[Seraphim::LaunchGameFromGameManagerCoreAsync()] An error has occurred while trying to set process priority, Ignoring!");
37+
}
2938

3039
CancellationTokenSource gameLogReaderCts = new CancellationTokenSource();
3140
CancellationTokenSource coopCts = CancellationTokenSource.CreateLinkedTokenSource(token, gameLogReaderCts.Token);
3241

3342
// Run game log reader (Create a new thread)
34-
_ = ReadGameLog(context, coopCts.Token);
43+
_ = ReadGameLog(context, process, coopCts.Token);
3544

3645
// ReSharper disable once PossiblyMistakenUseOfCancellationToken
3746
await process.WaitForExitAsync(token);
@@ -42,16 +51,19 @@ public override async Task<bool> LaunchGameFromGameManagerCoreAsync(GameManagerE
4251
}
4352

4453
/// <inheritdoc/>
45-
public override bool IsGameRunningCore(GameManagerExtension.RunGameFromGameManagerContext context, out bool isGameRunning)
54+
public override bool IsGameRunningCore(GameManagerExtension.RunGameFromGameManagerContext context, out bool isGameRunning, out DateTime gameStartTime)
4655
{
4756
isGameRunning = false;
57+
gameStartTime = default;
58+
4859
if (!TryGetGameExecutablePath(context, out string? gameExecutablePath))
4960
{
5061
return false;
5162
}
5263

5364
using Process? process = FindExecutableProcess(gameExecutablePath);
5465
isGameRunning = process != null;
66+
gameStartTime = process?.StartTime ?? default;
5567

5668
return true;
5769
}
@@ -75,9 +87,11 @@ public override async Task<bool> WaitRunningGameCoreAsync(GameManagerExtension.R
7587
}
7688

7789
/// <inheritdoc/>
78-
public override bool KillRunningGameCore(GameManagerExtension.RunGameFromGameManagerContext context, out bool wasGameRunning)
90+
public override bool KillRunningGameCore(GameManagerExtension.RunGameFromGameManagerContext context, out bool wasGameRunning, out DateTime gameStartTime)
7991
{
8092
wasGameRunning = false;
93+
gameStartTime = default;
94+
8195
if (!TryGetGameExecutablePath(context, out string? gameExecutablePath))
8296
{
8397
return false;
@@ -90,6 +104,7 @@ public override bool KillRunningGameCore(GameManagerExtension.RunGameFromGameMan
90104
}
91105

92106
wasGameRunning = true;
107+
gameStartTime = process.StartTime;
93108
process.Kill();
94109
return true;
95110
}
@@ -166,7 +181,7 @@ private static bool TryGetGameProcessFromContext(GameManagerExtension.RunGameFro
166181
return true;
167182
}
168183

169-
private static async Task ReadGameLog(GameManagerExtension.RunGameFromGameManagerContext context, CancellationToken token)
184+
private static async Task ReadGameLog(GameManagerExtension.RunGameFromGameManagerContext context, Process process, CancellationToken token)
170185
{
171186
if (context is not { PresetConfig: PluginPresetConfigBase presetConfig })
172187
{
@@ -184,6 +199,13 @@ private static async Task ReadGameLog(GameManagerExtension.RunGameFromGameManage
184199

185200
string gameLogPath = Path.Combine(gameAppDataPath, gameLogFileName);
186201

202+
// Make artificial delay and read the game log if the window is already spawned.
203+
while (!token.IsCancellationRequested &&
204+
process.MainWindowHandle == nint.Zero)
205+
{
206+
await Task.Delay(250, token);
207+
}
208+
187209
int retry = 5;
188210
while (!File.Exists(gameLogPath) && retry >= 0)
189211
{
@@ -197,11 +219,12 @@ private static async Task ReadGameLog(GameManagerExtension.RunGameFromGameManage
197219
return;
198220
}
199221

200-
var printCallback = context.PrintGameLogCallback;
222+
GameManagerExtension.PrintGameLog? printCallback = context.PrintGameLogCallback;
201223

202-
await using FileStream fileStream = File.Open(gameLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
224+
await using FileStream fileStream = File.Open(gameLogPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
203225
using StreamReader reader = new StreamReader(fileStream);
204226

227+
fileStream.Position = 0;
205228
while (!token.IsCancellationRequested)
206229
{
207230
while (await reader.ReadLineAsync(token) is { } line)

0 commit comments

Comments
 (0)