LANCommander/LANCommander.Client/Services/LibraryService.cs

134 lines
4.7 KiB
C#
Raw Normal View History

using LANCommander.Client.Data;
using LANCommander.Client.Data.Models;
2024-05-23 23:41:38 -05:00
using LANCommander.Client.Models;
using Microsoft.EntityFrameworkCore;
2024-06-05 19:24:59 -05:00
using Photino.NET;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2024-05-25 22:21:04 -05:00
using System.Diagnostics;
using System.Linq;
using System.Management.Automation.Language;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace LANCommander.Client.Services
{
public class LibraryService : BaseService
{
private readonly SDK.Client Client;
2024-05-29 02:02:40 -05:00
private readonly DownloadService DownloadService;
private readonly CollectionService CollectionService;
2024-05-29 02:02:40 -05:00
private readonly GameService GameService;
2024-06-03 19:06:25 -05:00
private readonly PlaySessionService PlaySessionService;
private readonly RedistributableService RedistributableService;
2024-06-05 19:24:59 -05:00
private readonly PhotinoWindow Window;
2024-05-25 22:21:04 -05:00
public Dictionary<Guid, Process> RunningProcesses = new Dictionary<Guid, Process>();
2024-05-29 02:02:40 -05:00
public delegate void OnLibraryChangedHandler();
public event OnLibraryChangedHandler OnLibraryChanged;
public LibraryService(
SDK.Client client,
2024-05-29 02:02:40 -05:00
DownloadService downloadService,
CollectionService collectionService,
GameService gameService,
2024-06-03 19:06:25 -05:00
PlaySessionService playSessionService,
2024-06-05 19:24:59 -05:00
PhotinoWindow window,
2024-05-29 02:02:40 -05:00
RedistributableService redistributableService) : base()
{
Client = client;
2024-05-29 02:02:40 -05:00
DownloadService = downloadService;
CollectionService = collectionService;
2024-05-29 02:02:40 -05:00
GameService = gameService;
2024-06-03 19:06:25 -05:00
PlaySessionService = playSessionService;
2024-06-05 19:24:59 -05:00
Window = window;
RedistributableService = redistributableService;
2024-05-29 02:02:40 -05:00
DownloadService.OnInstallComplete += DownloadService_OnInstallComplete;
}
private async Task DownloadService_OnInstallComplete()
2024-05-29 02:02:40 -05:00
{
OnLibraryChanged?.Invoke();
}
2024-05-23 23:41:38 -05:00
public async Task<IEnumerable<LibraryItem>> GetLibraryItemsAsync()
{
var items = new List<LibraryItem>();
var collections = await CollectionService.Get();
items.AddRange(collections.Select(c => new LibraryItem(c)));
var redistributables = await RedistributableService.Get();
foreach (var redistributable in redistributables)
items.Add(new LibraryItem(redistributable));
return items;
}
public async Task<LibraryItem> GetLibraryItemAsync(LibraryItem libraryItem)
{
// Assume for now it's a game
var game = await GameService.Get(libraryItem.Key);
return new LibraryItem(game);
}
2024-05-29 02:02:40 -05:00
public async Task Install(LibraryItem libraryItem)
{
var game = libraryItem.DataItem as Game;
2024-05-29 02:02:40 -05:00
await DownloadService.Add(game);
}
2024-05-29 02:02:40 -05:00
public async Task Uninstall(LibraryItem libraryItem)
{
var game = libraryItem.DataItem as Game;
await Task.Run(() => Client.Games.Uninstall(game.InstallDirectory, game.Id));
game.InstallDirectory = null;
game.Installed = false;
game.InstalledVersion = null;
await GameService.Update(game);
OnLibraryChanged?.Invoke();
}
2024-05-25 22:21:04 -05:00
2024-06-01 22:47:56 -05:00
public async Task Run(Game game, SDK.Models.Action action)
2024-05-25 22:21:04 -05:00
{
var process = new Process();
2024-06-05 19:24:59 -05:00
var monitor = Window.Monitors.First(m => m.MonitorArea.X == 0 && m.MonitorArea.Y == 0);
Client.Actions.AddVariable("DisplayWidth", monitor.MonitorArea.Width.ToString());
Client.Actions.AddVariable("DisplayHeight", monitor.MonitorArea.Height.ToString());
// Client.Actions.AddVariable("DisplayRefreshRate", ((int)DeviceDisplay.Current.MainDisplayInfo.RefreshRate).ToString());
2024-05-25 22:21:04 -05:00
process.StartInfo.Arguments = Client.Actions.ExpandVariables(action.Arguments, game.InstallDirectory, skipSlashes: true);
process.StartInfo.FileName = Client.Actions.ExpandVariables(action.Path, game.InstallDirectory);
process.StartInfo.WorkingDirectory = Client.Actions.ExpandVariables(action.WorkingDirectory, game.InstallDirectory);
process.StartInfo.UseShellExecute = true;
if (String.IsNullOrWhiteSpace(action.WorkingDirectory))
process.StartInfo.WorkingDirectory = game.InstallDirectory;
2024-06-03 19:06:25 -05:00
var userId = Guid.NewGuid();
await PlaySessionService.StartSession(game.Id, userId);
2024-05-25 22:21:04 -05:00
process.Start();
2024-06-03 19:06:25 -05:00
await process.WaitForExitAsync();
await PlaySessionService.EndSession(game.Id, userId);
2024-05-25 22:21:04 -05:00
}
}
}