2024-08-04 17:53:19 -05:00
|
|
|
|
using LANCommander.Launcher.Data;
|
|
|
|
|
|
using LANCommander.Launcher.Data.Models;
|
|
|
|
|
|
using LANCommander.Launcher.Models;
|
2024-05-28 00:54:09 -05:00
|
|
|
|
using LANCommander.SDK;
|
2024-09-16 20:51:48 -05:00
|
|
|
|
using LANCommander.SDK.Extensions;
|
2024-05-28 00:54:09 -05:00
|
|
|
|
using LANCommander.SDK.Helpers;
|
2024-09-11 00:24:34 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-08-26 20:18:54 -05:00
|
|
|
|
using System.Diagnostics;
|
2024-05-20 20:26:15 -05:00
|
|
|
|
|
2024-08-04 17:53:19 -05:00
|
|
|
|
namespace LANCommander.Launcher.Services
|
2024-05-20 20:26:15 -05:00
|
|
|
|
{
|
2024-05-21 20:51:09 -05:00
|
|
|
|
public class GameService : BaseDatabaseService<Game>
|
2024-05-20 20:26:15 -05:00
|
|
|
|
{
|
2025-01-15 02:28:11 -06:00
|
|
|
|
private readonly AuthenticationService AuthenticationService;
|
2024-08-26 20:18:54 -05:00
|
|
|
|
private readonly PlaySessionService PlaySessionService;
|
|
|
|
|
|
private readonly SaveService SaveService;
|
|
|
|
|
|
private readonly MessageBusService MessageBusService;
|
|
|
|
|
|
|
|
|
|
|
|
public Dictionary<Guid, Process> RunningProcesses = new Dictionary<Guid, Process>();
|
|
|
|
|
|
|
2024-06-12 01:51:14 -05:00
|
|
|
|
private Settings Settings { get; set; }
|
2024-05-28 00:54:09 -05:00
|
|
|
|
|
2024-07-08 20:07:32 -05:00
|
|
|
|
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-09-11 00:24:34 -05:00
|
|
|
|
public GameService(
|
|
|
|
|
|
DatabaseContext dbContext,
|
|
|
|
|
|
SDK.Client client,
|
|
|
|
|
|
ILogger<GameService> logger,
|
2025-01-15 02:28:11 -06:00
|
|
|
|
AuthenticationService authenticationService,
|
2024-09-11 00:24:34 -05:00
|
|
|
|
PlaySessionService playSessionService,
|
|
|
|
|
|
SaveService saveService,
|
|
|
|
|
|
MessageBusService messageBusService) : base(dbContext, client, logger)
|
2024-05-28 00:54:09 -05:00
|
|
|
|
{
|
2024-06-12 01:51:14 -05:00
|
|
|
|
Settings = SettingService.GetSettings();
|
2025-01-15 02:28:11 -06:00
|
|
|
|
AuthenticationService = authenticationService;
|
2024-08-26 20:18:54 -05:00
|
|
|
|
PlaySessionService = playSessionService;
|
|
|
|
|
|
SaveService = saveService;
|
|
|
|
|
|
MessageBusService = messageBusService;
|
2024-05-28 00:54:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-07 01:47:13 -05:00
|
|
|
|
public async Task UninstallAsync(Game game)
|
2024-06-10 23:29:19 -05:00
|
|
|
|
{
|
2024-09-16 20:51:48 -05:00
|
|
|
|
using (var operation = Logger.BeginOperation("Uninstalling game {GameTitle} ({GameId})", game.Title, game.Id))
|
2024-06-12 01:51:14 -05:00
|
|
|
|
{
|
2024-09-16 20:51:48 -05:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-01-27 00:04:42 -06:00
|
|
|
|
OnUninstall?.Invoke(game);
|
|
|
|
|
|
|
2024-09-16 20:51:48 -05:00
|
|
|
|
await Client.Games.UninstallAsync(game.InstallDirectory, game.Id);
|
2024-06-12 01:51:14 -05:00
|
|
|
|
|
2024-09-16 20:51:48 -05:00
|
|
|
|
game.InstallDirectory = null;
|
|
|
|
|
|
game.Installed = false;
|
|
|
|
|
|
game.InstalledOn = null;
|
|
|
|
|
|
game.InstalledVersion = null;
|
2024-06-12 01:51:14 -05:00
|
|
|
|
|
2025-01-19 00:03:21 -06:00
|
|
|
|
await UpdateAsync(game);
|
2024-06-12 01:51:14 -05:00
|
|
|
|
|
2024-09-16 20:51:48 -05:00
|
|
|
|
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
|
|
|
|
}
|
2024-08-26 20:18:54 -05:00
|
|
|
|
|
2024-09-05 00:48:04 -05:00
|
|
|
|
public async Task Run(Game game, SDK.Models.Action action)
|
2024-08-26 20:18:54 -05:00
|
|
|
|
{
|
2025-01-15 02:28:11 -06:00
|
|
|
|
Guid userId;
|
|
|
|
|
|
|
|
|
|
|
|
if (Client.IsConnected())
|
|
|
|
|
|
{
|
|
|
|
|
|
var profile = await Client.Profile.GetAsync();
|
|
|
|
|
|
|
|
|
|
|
|
userId = profile.Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
userId = AuthenticationService.GetUserId();
|
|
|
|
|
|
}
|
2024-08-26 20:18:54 -05:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-01-15 02:28:11 -06:00
|
|
|
|
var latestSession = await PlaySessionService.GetLatestSession(game.Id, userId);
|
2024-08-26 20:18:54 -05:00
|
|
|
|
|
2025-01-15 02:28:11 -06:00
|
|
|
|
await PlaySessionService.StartSession(game.Id, userId);
|
2024-08-26 20:18:54 -05:00
|
|
|
|
|
2024-09-05 00:48:04 -05:00
|
|
|
|
await Client.Games.RunAsync(game.InstallDirectory, game.Id, action, latestSession?.CreatedOn);
|
2024-08-26 20:18:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-09-11 00:24:34 -05:00
|
|
|
|
Logger?.LogError(ex, "Game failed to run");
|
2024-08-26 20:18:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
2025-01-15 02:28:11 -06:00
|
|
|
|
await PlaySessionService.EndSession(game.Id, userId);
|
2024-08-26 20:18:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-05-20 20:26:15 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|