LANCommander/LANCommander.Launcher.Services/GameService.cs

120 lines
3.9 KiB
C#
Raw Permalink Normal View History

using LANCommander.Launcher.Data;
using LANCommander.Launcher.Data.Models;
using LANCommander.Launcher.Models;
using LANCommander.SDK;
using LANCommander.SDK.Extensions;
using LANCommander.SDK.Helpers;
2024-09-11 00:24:34 -05:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
namespace LANCommander.Launcher.Services
{
public class GameService(
DatabaseContext dbContext,
ILogger<GameService> logger,
AuthenticationService authenticationService,
PlaySessionService playSessionService,
SDK.Client client,
IServiceProvider serviceProvider) : BaseDatabaseService<Game>(dbContext, logger)
{
public Dictionary<Guid, Process> RunningProcesses = new Dictionary<Guid, Process>();
public delegate Task OnUninstallCompleteHandler(Game game);
public event OnUninstallCompleteHandler OnUninstallComplete;
2025-01-27 00:04:42 -06:00
public delegate Task OnUninstallHandler(Game game);
public event OnUninstallHandler OnUninstall;
2024-08-07 01:47:13 -05:00
public async Task UninstallAsync(Game game)
2024-06-10 23:29:19 -05:00
{
using (var operation = Logger.BeginOperation("Uninstalling game {GameTitle} ({GameId})", game.Title, game.Id))
{
try
{
2025-01-27 00:04:42 -06:00
OnUninstall?.Invoke(game);
await client.Games.UninstallAsync(game.InstallDirectory, game.Id);
if (game.BaseGameId.HasValue)
{
var libraryService = serviceProvider.GetService<LibraryService>();
var isInstalled = await libraryService!.IsInstalledAsync(game.BaseGameId.Value);
if (!isInstalled)
{
var baseGame = await GetAsync(game.BaseGameId.Value);
await client.Games.UninstallAsync(game.InstallDirectory, baseGame?.Id ?? game.BaseGameId.Value);
ClearGameState(baseGame!, skipAddons: true);
}
}
ClearGameState(game);
await UpdateAsync(game);
OnUninstallComplete?.Invoke(game);
operation.Complete();
}
catch (Exception ex)
{
Logger?.LogError(ex, "Game {GameTitle} ({GameId}) could not be uninstalled", game.Title, game.Id);
}
2024-08-07 01:47:13 -05:00
}
2024-06-10 23:29:19 -05:00
}
public async Task Run(Game game, SDK.Models.Manifest.Action action)
{
2025-01-15 02:28:11 -06:00
Guid userId;
if (client.Connection.IsConnected())
2025-01-15 02:28:11 -06:00
{
var profile = await client.Profile.GetAsync();
2025-01-15 02:28:11 -06:00
userId = profile.Id;
}
else
{
userId = authenticationService.GetUserId();
2025-01-15 02:28:11 -06:00
}
try
{
var latestSession = await playSessionService.GetLatestSession(game.Id, userId);
await playSessionService.StartSession(game.Id, userId);
await client.Games.RunAsync(game.InstallDirectory, game.Id, action, latestSession?.CreatedOn);
}
catch (Exception ex)
{
2024-09-11 00:24:34 -05:00
Logger?.LogError(ex, "Game failed to run");
}
finally
{
await playSessionService.EndSession(game.Id, userId);
}
}
protected void ClearGameState(Game game, bool skipAddons = false)
{
if (game == null)
return;
game.InstallDirectory = null;
game.Installed = false;
game.InstalledOn = null;
game.InstalledVersion = null;
if (!skipAddons)
{
foreach (var addon in (game.DependentGames ?? []))
{
ClearGameState(addon);
}
}
}
}
}