Skip to content

Commit 1812350

Browse files
committed
refactor(detect): Attempt to find game exe if JSON config not found
1 parent 1d21408 commit 1812350

File tree

1 file changed

+97
-15
lines changed

1 file changed

+97
-15
lines changed

Hi3Helper.Plugin.Wuwa/Management/WuwaGameManager.cs

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,30 +290,79 @@ public override void LoadConfig()
290290
string filePath = Path.Combine(CurrentGameInstallPath, "app-game-config.json");
291291
FileInfo fileInfo = new(filePath);
292292

293-
if (!fileInfo.Exists)
293+
if (fileInfo.Exists)
294294
{
295-
SharedStatic.InstanceLogger.LogWarning(
296-
"[WuwaGameManager::LoadConfig] File app-game-config.json doesn't exist on dir: {Dir}",
297-
CurrentGameInstallPath);
298-
return;
299-
}
300-
301-
try
302-
{
303-
using FileStream fileStream = fileInfo.OpenRead();
295+
try
296+
{
297+
using FileStream fileStream = fileInfo.OpenRead();
304298
#if USELIGHTWEIGHTJSONPARSER
305299
CurrentGameConfig = WuwaGameLauncherConfig.ParseFrom(fileStream);
306300
#else
307301
CurrentGameConfigNode = JsonNode.Parse(fileStream) as JsonObject ?? new JsonObject();
308302
#endif
309-
SharedStatic.InstanceLogger.LogTrace(
310-
"[WuwaGameManager::LoadConfig] Loaded app-game-config.json from directory: {Dir}",
303+
SharedStatic.InstanceLogger.LogTrace(
304+
"[WuwaGameManager::LoadConfig] Loaded app-game-config.json from directory: {Dir}",
305+
CurrentGameInstallPath);
306+
return;
307+
}
308+
catch (Exception ex)
309+
{
310+
SharedStatic.InstanceLogger.LogError(
311+
"[WuwaGameManager::LoadConfig] Cannot load app-game-config.json! Reason: {Exception}", ex);
312+
// fallthrough: attempt recovery/write if possible
313+
}
314+
}
315+
else
316+
{
317+
SharedStatic.InstanceLogger.LogWarning(
318+
"[WuwaGameManager::LoadConfig] File app-game-config.json doesn't exist on dir: {Dir}",
311319
CurrentGameInstallPath);
312-
}
320+
}
321+
322+
// If the file is missing (or failed to parse), attempt to detect an existing install by executable
323+
// and persist a config so manual-locate flow behaves like installer.
324+
try
325+
{
326+
string exePath = Path.Combine(CurrentGameInstallPath, CurrentGameExecutableByPreset);
327+
if (File.Exists(exePath))
328+
{
329+
SharedStatic.InstanceLogger.LogInformation(
330+
"[WuwaGameManager::LoadConfig] Found executable at {Exe}. Attempting to initialize API and persist app-game-config.json.",
331+
exePath);
332+
333+
try
334+
{
335+
// Ensure API metadata available for SaveConfig (best-effort; swallow errors)
336+
InitAsyncInner(true, CancellationToken.None).GetAwaiter().GetResult();
337+
}
338+
catch (Exception ex)
339+
{
340+
SharedStatic.InstanceLogger.LogWarning(
341+
"[WuwaGameManager::LoadConfig] InitAsyncInner failed (continuing): {Err}", ex.Message);
342+
}
343+
344+
try
345+
{
346+
SaveConfig();
347+
SharedStatic.InstanceLogger.LogInformation(
348+
"[WuwaGameManager::LoadConfig] Persisted app-game-config.json for manual-located installation.");
349+
}
350+
catch (Exception ex)
351+
{
352+
SharedStatic.InstanceLogger.LogWarning(
353+
"[WuwaGameManager::LoadConfig] Failed to persist app-game-config.json: {Err}", ex.Message);
354+
}
355+
}
356+
else
357+
{
358+
SharedStatic.InstanceLogger.LogTrace(
359+
"[WuwaGameManager::LoadConfig] No executable found at {Exe}; skipping auto-save.", exePath);
360+
}
361+
}
313362
catch (Exception ex)
314363
{
315-
SharedStatic.InstanceLogger.LogError(
316-
"[WuwaGameManager::LoadConfig] Cannot load app-game-config.json! Reason: {Exception}", ex);
364+
SharedStatic.InstanceLogger.LogWarning(
365+
"[WuwaGameManager::LoadConfig] Recovery attempt failed: {Err}", ex.Message);
317366
}
318367
}
319368

@@ -332,12 +381,45 @@ public override void SaveConfig()
332381
ApiGameConfigResponse?.KeyFileCheckList?[2] ??
333382
Path.GetFileNameWithoutExtension(CurrentGameExecutableByPreset));
334383
#endif
384+
335385
if (CurrentGameVersion == GameVersion.Empty)
336386
{
337387
SharedStatic.InstanceLogger.LogWarning(
338388
"[WuwaGameManager::SaveConfig] Current version returns 0.0.0! Overwrite the version to current provided version by API, {VersionApi}",
339389
ApiGameVersion);
340390
CurrentGameVersion = ApiGameVersion;
341391
}
392+
393+
// Persist to disk (write app-game-config.json)
394+
try
395+
{
396+
string configPath = Path.Combine(CurrentGameInstallPath, "app-game-config.json");
397+
Directory.CreateDirectory(CurrentGameInstallPath);
398+
399+
var writerOptions = new JsonWriterOptions
400+
{
401+
Indented = true,
402+
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
403+
};
404+
405+
#if !USELIGHTWEIGHTJSONPARSER
406+
using (var fs = new FileStream(configPath, FileMode.Create, FileAccess.Write, FileShare.None))
407+
using (var writer = new Utf8JsonWriter(fs, writerOptions))
408+
{
409+
// JsonObject supports WriteTo(Utf8JsonWriter)
410+
CurrentGameConfigNode.WriteTo(writer);
411+
writer.Flush();
412+
}
413+
SharedStatic.InstanceLogger.LogInformation("[WuwaGameManager::SaveConfig] Wrote app-game-config.json to {Path}", configPath);
414+
#else
415+
// If lightweight parser is used, add equivalent persistence here
416+
SharedStatic.InstanceLogger.LogWarning("[WuwaGameManager::SaveConfig] Lightweight JSON parser enabled: persistence not implemented.");
417+
#endif
418+
}
419+
catch (Exception ex)
420+
{
421+
SharedStatic.InstanceLogger.LogWarning(
422+
"[WuwaGameManager::SaveConfig] Failed to write app-game-config.json: {Exception}", ex);
423+
}
342424
}
343425
}

0 commit comments

Comments
 (0)