2024-08-07 18:48:13 -05:00
|
|
|
|
using LANCommander.Launcher.Data;
|
2024-08-04 17:53:19 -05:00
|
|
|
|
using LANCommander.Launcher.Data.Models;
|
|
|
|
|
|
using LANCommander.Launcher.Models;
|
2024-08-07 18:48:13 -05:00
|
|
|
|
using LANCommander.Launcher.Services.Extensions;
|
2024-07-15 18:17:04 -05:00
|
|
|
|
using LANCommander.SDK;
|
2024-09-16 21:00:07 -05:00
|
|
|
|
using LANCommander.SDK.Extensions;
|
2024-07-15 17:41:02 -05:00
|
|
|
|
using LANCommander.SDK.Helpers;
|
|
|
|
|
|
using LANCommander.SDK.PowerShell;
|
2024-05-23 23:41:38 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-09-11 00:24:34 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-10-30 00:22:24 -05:00
|
|
|
|
using Steamworks.Ugc;
|
2024-05-23 18:38:13 -05:00
|
|
|
|
using System;
|
2024-05-20 20:26:15 -05:00
|
|
|
|
using System.Collections.Generic;
|
2024-05-25 15:40:31 -05:00
|
|
|
|
using System.Collections.ObjectModel;
|
2024-05-25 22:21:04 -05:00
|
|
|
|
using System.Diagnostics;
|
2024-05-20 20:26:15 -05:00
|
|
|
|
using System.Linq;
|
2024-05-23 18:38:13 -05:00
|
|
|
|
using System.Management.Automation.Language;
|
2024-05-20 20:26:15 -05:00
|
|
|
|
using System.Text;
|
2024-05-23 18:38:13 -05:00
|
|
|
|
using System.Text.RegularExpressions;
|
2024-05-20 20:26:15 -05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2024-08-04 17:53:19 -05:00
|
|
|
|
namespace LANCommander.Launcher.Services
|
2024-05-20 20:26:15 -05:00
|
|
|
|
{
|
2025-01-19 00:06:44 -06:00
|
|
|
|
public class LibraryService : BaseDatabaseService<Library>
|
2024-05-20 20:26:15 -05:00
|
|
|
|
{
|
2025-01-20 12:10:30 -06:00
|
|
|
|
private readonly AuthenticationService AuthenticationService;
|
2024-10-26 15:48:47 -05:00
|
|
|
|
private readonly InstallService InstallService;
|
2024-05-29 02:02:40 -05:00
|
|
|
|
private readonly GameService GameService;
|
2025-01-20 12:10:30 -06:00
|
|
|
|
private readonly UserService UserService;
|
2024-05-20 20:26:15 -05:00
|
|
|
|
|
2024-05-25 22:21:04 -05:00
|
|
|
|
public Dictionary<Guid, Process> RunningProcesses = new Dictionary<Guid, Process>();
|
2024-05-25 15:40:31 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public ObservableCollection<ListItem> Items { get; set; } = new ObservableCollection<ListItem>();
|
2024-06-28 03:50:21 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public delegate Task OnLibraryChangedHandler(IEnumerable<ListItem> items);
|
2024-05-29 02:02:40 -05:00
|
|
|
|
public event OnLibraryChangedHandler OnLibraryChanged;
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public delegate Task OnPreLibraryItemsFilteredHandler(IEnumerable<ListItem> items);
|
2024-07-13 12:55:42 -05:00
|
|
|
|
public event OnPreLibraryItemsFilteredHandler OnPreLibraryItemsFiltered;
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public delegate Task OnItemsFilteredHandler(IEnumerable<ListItem> items);
|
|
|
|
|
|
public event OnItemsFilteredHandler OnItemsFiltered;
|
2024-06-28 03:50:21 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public LibraryFilter Filter { get; set; } = new LibraryFilter();
|
2024-07-08 19:15:57 -05:00
|
|
|
|
|
2024-05-23 18:38:13 -05:00
|
|
|
|
public LibraryService(
|
2025-01-19 00:06:44 -06:00
|
|
|
|
DatabaseContext databaseContext,
|
2024-05-23 18:38:13 -05:00
|
|
|
|
SDK.Client client,
|
2024-09-11 00:24:34 -05:00
|
|
|
|
ILogger<LibraryService> logger,
|
2025-01-20 12:10:30 -06:00
|
|
|
|
AuthenticationService authenticationService,
|
2024-10-26 15:48:47 -05:00
|
|
|
|
InstallService installService,
|
2024-05-23 18:38:13 -05:00
|
|
|
|
GameService gameService,
|
2025-01-20 12:10:30 -06:00
|
|
|
|
UserService userService) : base(databaseContext, client, logger)
|
2024-05-20 20:26:15 -05:00
|
|
|
|
{
|
2025-01-20 12:10:30 -06:00
|
|
|
|
AuthenticationService = authenticationService;
|
2024-10-26 15:48:47 -05:00
|
|
|
|
InstallService = installService;
|
2024-05-29 02:02:40 -05:00
|
|
|
|
GameService = gameService;
|
2025-01-20 12:10:30 -06:00
|
|
|
|
UserService = userService;
|
2024-05-29 02:02:40 -05:00
|
|
|
|
|
2024-10-26 15:48:47 -05:00
|
|
|
|
InstallService.OnInstallComplete += InstallService_OnInstallComplete;
|
2024-10-30 00:22:24 -05:00
|
|
|
|
Filter.OnChanged += Filter_OnChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task Filter_OnChanged()
|
|
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
if (OnItemsFiltered != null)
|
|
|
|
|
|
await OnItemsFiltered.Invoke(Filter.ApplyFilter(Items));
|
2024-07-13 13:43:24 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-26 15:48:47 -05:00
|
|
|
|
private async Task InstallService_OnInstallComplete(Game game)
|
2024-05-29 02:02:40 -05:00
|
|
|
|
{
|
2024-07-13 12:55:42 -05:00
|
|
|
|
if (OnLibraryChanged != null)
|
2024-11-09 17:15:04 -06:00
|
|
|
|
await OnLibraryChanged.Invoke(Items);
|
2024-05-20 20:26:15 -05:00
|
|
|
|
}
|
2024-05-23 18:38:13 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public async Task<IEnumerable<ListItem>> RefreshItemsAsync()
|
2024-06-28 03:50:21 -05:00
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
Items = new ObservableCollection<ListItem>(await GetItemsAsync());
|
2024-06-28 03:50:21 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
if (OnItemsFiltered != null)
|
|
|
|
|
|
await OnItemsFiltered.Invoke(Filter.ApplyFilter(Items));
|
2024-10-30 00:22:24 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
return Items;
|
2024-06-28 03:50:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public IEnumerable<ListItem> GetItems<T>()
|
2024-06-28 03:50:21 -05:00
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
return Items.Where(i => i.DataItem is T).DistinctBy(i => i.Key);
|
2024-06-28 03:50:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-19 00:06:44 -06:00
|
|
|
|
public async Task<Library> GetByUserAsync(Guid userId)
|
|
|
|
|
|
{
|
2025-01-20 12:10:30 -06:00
|
|
|
|
var user = await Context
|
|
|
|
|
|
.Users
|
|
|
|
|
|
.Include(u => u.Library)
|
|
|
|
|
|
.ThenInclude(l => l.Games)
|
|
|
|
|
|
.FirstOrDefaultAsync(u => u.Id == userId);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
user = new User
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = userId,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
user = Context.Users.Add(user).Entity;
|
|
|
|
|
|
|
|
|
|
|
|
await Context.SaveChangesAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (user.Library == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
user.Library = new Library();
|
|
|
|
|
|
|
|
|
|
|
|
user = Context.Users.Update(user).Entity;
|
|
|
|
|
|
|
|
|
|
|
|
await Context.SaveChangesAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return user.Library;
|
2025-01-19 00:06:44 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-28 14:02:27 -06:00
|
|
|
|
public bool IsInstalled(Guid itemId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Items.Any(i => i.Key == itemId && (i.State == ListItemState.Installed || i.State == ListItemState.UpdateAvailable));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public async Task<IEnumerable<ListItem>> GetItemsAsync()
|
2024-05-23 23:41:38 -05:00
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
Items.Clear();
|
2024-09-11 18:59:40 -05:00
|
|
|
|
|
2024-09-17 18:14:40 -05:00
|
|
|
|
using (var op = Logger.BeginOperation(LogLevel.Trace, "Loading library items from local database"))
|
2024-09-11 18:59:40 -05:00
|
|
|
|
{
|
2025-01-20 20:32:51 -06:00
|
|
|
|
var games = await Context
|
|
|
|
|
|
.Games
|
2025-01-26 15:25:41 -06:00
|
|
|
|
.AsSplitQuery()
|
2025-01-20 20:32:51 -06:00
|
|
|
|
.Where(g => g.Libraries.Any(l => l.UserId == AuthenticationService.GetUserId()))
|
|
|
|
|
|
.Include(g => g.Media)
|
|
|
|
|
|
.Include(g => g.Platforms)
|
|
|
|
|
|
.Include(g => g.Collections)
|
|
|
|
|
|
.Include(g => g.Genres)
|
|
|
|
|
|
.Include(g => g.Engine)
|
|
|
|
|
|
.Include(g => g.Publishers)
|
|
|
|
|
|
.Include(g => g.Developers)
|
|
|
|
|
|
.Include(g => g.Tags)
|
|
|
|
|
|
.Include(g => g.MultiplayerModes)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
Filter.Populate(games);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in games.Select(g => new ListItem(g)).OrderByTitle(g => !String.IsNullOrWhiteSpace(g.SortName) ? g.SortName : g.Name))
|
2024-09-16 21:00:07 -05:00
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
Items.Add(item);
|
2024-09-16 21:00:07 -05:00
|
|
|
|
}
|
2024-09-25 20:27:48 -05:00
|
|
|
|
|
|
|
|
|
|
op.Complete();
|
2024-09-11 18:59:40 -05:00
|
|
|
|
}
|
2024-07-12 18:22:17 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
return Items;
|
2024-07-12 18:22:17 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public IEnumerable<ListItem> GetFilteredItems()
|
2024-07-12 18:22:17 -05:00
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
return Filter.ApplyFilter(Items);
|
2024-05-23 23:41:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public ListItem GetItem(Guid key)
|
2024-06-28 03:50:21 -05:00
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
var item = Items.FirstOrDefault(i => i.Key == key);
|
2024-06-28 03:50:21 -05:00
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
2024-11-09 17:15:04 -06:00
|
|
|
|
item = Items.FirstOrDefault(i => i.Key == key);
|
2024-06-28 03:50:21 -05:00
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public async Task<ListItem> GetItemAsync(ListItem libraryItem)
|
2024-06-02 17:23:36 -05:00
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
return await GetItemAsync(libraryItem.Key);
|
2024-08-01 17:21:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
public async Task<ListItem> GetItemAsync(Guid key)
|
2024-08-01 17:21:44 -05:00
|
|
|
|
{
|
2025-01-19 00:06:44 -06:00
|
|
|
|
var game = await Context.Games
|
2025-01-26 15:25:41 -06:00
|
|
|
|
.AsSplitQuery()
|
2025-01-12 21:49:07 -06:00
|
|
|
|
.Include(g => g.Collections)
|
|
|
|
|
|
.Include(g => g.Developers)
|
|
|
|
|
|
.Include(g => g.Genres)
|
|
|
|
|
|
.Include(g => g.Publishers)
|
|
|
|
|
|
.Include(g => g.Tags)
|
|
|
|
|
|
.Include(g => g.PlaySessions)
|
|
|
|
|
|
.Include(g => g.Engine)
|
|
|
|
|
|
.Include(g => g.Platforms)
|
|
|
|
|
|
.Include(g => g.Media)
|
|
|
|
|
|
.Include(g => g.MultiplayerModes)
|
|
|
|
|
|
.FirstOrDefaultAsync(g => g.Id == key);
|
2024-06-02 17:23:36 -05:00
|
|
|
|
|
2024-11-09 17:15:04 -06:00
|
|
|
|
return new ListItem(game);
|
2024-06-02 17:23:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-15 23:45:51 -06:00
|
|
|
|
public async Task AddToLibraryAsync(Game game)
|
2024-11-12 02:35:28 -06:00
|
|
|
|
{
|
2025-01-20 12:10:30 -06:00
|
|
|
|
var library = await GetByUserAsync(AuthenticationService.GetUserId());
|
|
|
|
|
|
|
2025-02-15 23:45:51 -06:00
|
|
|
|
if (!library.Games.Any(g => g.Id == game.Id))
|
2025-01-20 12:10:30 -06:00
|
|
|
|
{
|
2025-02-15 23:45:51 -06:00
|
|
|
|
library.Games.Add(game);
|
|
|
|
|
|
|
|
|
|
|
|
await UpdateAsync(library);
|
2025-01-20 12:10:30 -06:00
|
|
|
|
}
|
2024-11-12 02:35:28 -06:00
|
|
|
|
|
2025-02-15 23:45:51 -06:00
|
|
|
|
if (!Items.Any(i => i.Key == game.Id))
|
|
|
|
|
|
{
|
|
|
|
|
|
Items.Add(new ListItem(game));
|
2024-11-12 02:35:28 -06:00
|
|
|
|
|
2025-02-15 23:45:51 -06:00
|
|
|
|
await LibraryChanged();
|
2025-02-06 19:47:09 -06:00
|
|
|
|
|
2025-02-15 23:45:51 -06:00
|
|
|
|
if (OnItemsFiltered != null)
|
|
|
|
|
|
await OnItemsFiltered.Invoke(Filter.ApplyFilter(Items));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task AddToLibraryAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var localGame = await GameService.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
await AddToLibraryAsync(localGame);
|
|
|
|
|
|
|
|
|
|
|
|
await Client.Library.AddToLibrary(id);
|
2024-11-12 02:35:28 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task RemoveFromLibraryAsync(Guid id)
|
|
|
|
|
|
{
|
2025-01-19 00:03:21 -06:00
|
|
|
|
var localGame = await GameService.GetAsync(id);
|
2025-01-20 12:10:30 -06:00
|
|
|
|
var library = await GetByUserAsync(AuthenticationService.GetUserId());
|
2024-11-12 02:35:28 -06:00
|
|
|
|
|
2025-01-20 12:10:30 -06:00
|
|
|
|
library.Games.Remove(localGame);
|
|
|
|
|
|
|
|
|
|
|
|
await UpdateAsync(library);
|
|
|
|
|
|
|
|
|
|
|
|
await Client.Library.RemoveFromLibrary(id);
|
2025-02-06 19:47:09 -06:00
|
|
|
|
|
|
|
|
|
|
var itemToRemove = Items.FirstOrDefault(i => i.Key == id);
|
|
|
|
|
|
|
|
|
|
|
|
if (itemToRemove != null)
|
|
|
|
|
|
Items.Remove(itemToRemove);
|
2024-11-12 02:35:28 -06:00
|
|
|
|
|
|
|
|
|
|
await LibraryChanged();
|
2025-02-06 19:47:09 -06:00
|
|
|
|
|
|
|
|
|
|
if (OnItemsFiltered != null)
|
|
|
|
|
|
await OnItemsFiltered.Invoke(Filter.ApplyFilter(Items));
|
2024-11-12 02:35:28 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-10 23:29:19 -05:00
|
|
|
|
public async Task LibraryChanged()
|
2024-05-29 02:02:40 -05:00
|
|
|
|
{
|
2024-07-13 12:55:42 -05:00
|
|
|
|
if (OnLibraryChanged != null)
|
2024-11-09 17:15:04 -06:00
|
|
|
|
await OnLibraryChanged.Invoke(Items);
|
2024-07-13 12:55:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task FilterChanged()
|
|
|
|
|
|
{
|
2024-11-09 17:15:04 -06:00
|
|
|
|
Items = new ObservableCollection<ListItem>(await GetItemsAsync());
|
2024-05-29 02:02:40 -05:00
|
|
|
|
}
|
2024-05-20 20:26:15 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|