2025-11-29 18:24:27 -06:00
|
|
|
|
using LANCommander.Launcher.Models;
|
2024-09-11 00:24:34 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-11-29 18:24:27 -06:00
|
|
|
|
using LANCommander.Launcher.Services.Import;
|
|
|
|
|
|
using LANCommander.Launcher.Services.Import.Factories;
|
2025-12-05 21:24:08 -06:00
|
|
|
|
using LANCommander.SDK;
|
2025-11-29 18:24:27 -06:00
|
|
|
|
using LANCommander.SDK.Services;
|
2025-01-12 02:54:41 -06:00
|
|
|
|
using Collection = LANCommander.Launcher.Data.Models.Collection;
|
|
|
|
|
|
using Company = LANCommander.Launcher.Data.Models.Company;
|
|
|
|
|
|
using Engine = LANCommander.Launcher.Data.Models.Engine;
|
|
|
|
|
|
using Genre = LANCommander.Launcher.Data.Models.Genre;
|
|
|
|
|
|
using MultiplayerMode = LANCommander.Launcher.Data.Models.MultiplayerMode;
|
|
|
|
|
|
using Platform = LANCommander.Launcher.Data.Models.Platform;
|
|
|
|
|
|
using Tag = LANCommander.Launcher.Data.Models.Tag;
|
2024-05-28 00:24:29 -05:00
|
|
|
|
|
2024-08-04 17:53:19 -05:00
|
|
|
|
namespace LANCommander.Launcher.Services
|
2024-05-28 00:24:29 -05:00
|
|
|
|
{
|
2025-09-24 20:58:23 -05:00
|
|
|
|
public class ImportService(
|
|
|
|
|
|
ILogger<ImportService> logger,
|
2025-11-29 18:24:27 -06:00
|
|
|
|
ImportContextFactory importContextFactory,
|
|
|
|
|
|
GameClient gameClient,
|
|
|
|
|
|
LibraryClient libraryClient) : BaseService(logger)
|
2024-05-28 00:24:29 -05:00
|
|
|
|
{
|
2025-02-06 20:54:03 -06:00
|
|
|
|
private ImportProgress _importProgress = new();
|
|
|
|
|
|
public ImportProgress Progress => _importProgress;
|
|
|
|
|
|
|
2025-12-05 21:24:08 -06:00
|
|
|
|
public AsyncEventHandler<ImportStatusUpdate> OnImportStarted;
|
|
|
|
|
|
public AsyncEventHandler<ImportStatusUpdate> OnImportStatusUpdate;
|
|
|
|
|
|
public AsyncEventHandler<ImportStatusUpdate> OnImportComplete;
|
|
|
|
|
|
public AsyncEventHandler<ImportStatusUpdate> OnImportError;
|
2024-06-11 02:05:15 -05:00
|
|
|
|
|
2024-11-16 01:44:53 -06:00
|
|
|
|
private IEnumerable<Collection> Collections;
|
|
|
|
|
|
private IEnumerable<Company> Companies;
|
|
|
|
|
|
private IEnumerable<Engine> Engines;
|
|
|
|
|
|
private IEnumerable<Genre> Genres;
|
|
|
|
|
|
private IEnumerable<Platform> Platforms;
|
|
|
|
|
|
private IEnumerable<Tag> Tags;
|
|
|
|
|
|
private IEnumerable<MultiplayerMode> MultiplayerModes;
|
|
|
|
|
|
|
2024-05-28 00:24:29 -05:00
|
|
|
|
public async Task ImportAsync()
|
|
|
|
|
|
{
|
2025-12-05 21:24:08 -06:00
|
|
|
|
await ImportLibraryAsync();
|
2024-11-16 01:44:53 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-20 12:10:30 -06:00
|
|
|
|
public async Task ImportLibraryAsync()
|
2024-11-12 02:35:28 -06:00
|
|
|
|
{
|
2025-11-29 18:24:27 -06:00
|
|
|
|
var remoteLibrary = await libraryClient.GetAsync();
|
2025-09-05 02:00:59 -05:00
|
|
|
|
|
2025-02-06 20:54:03 -06:00
|
|
|
|
Logger?.LogInformation("Starting library import");
|
2024-09-11 02:05:24 -05:00
|
|
|
|
|
2025-11-29 18:24:27 -06:00
|
|
|
|
var importContext = importContextFactory.Create();
|
2025-12-05 21:24:08 -06:00
|
|
|
|
|
|
|
|
|
|
importContext.OnImportStarted = OnImportStarted;
|
|
|
|
|
|
importContext.OnImportComplete = OnImportComplete;
|
|
|
|
|
|
importContext.OnImportError = OnImportError;
|
|
|
|
|
|
importContext.OnImportStatusUpdate = OnImportStatusUpdate;
|
2025-01-12 02:54:41 -06:00
|
|
|
|
|
2025-11-29 18:24:27 -06:00
|
|
|
|
foreach (var game in remoteLibrary)
|
2024-09-11 02:05:24 -05:00
|
|
|
|
{
|
2025-01-12 02:54:41 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-11-29 18:24:27 -06:00
|
|
|
|
var manifest = await gameClient.GetManifestAsync(game.Id);
|
2025-01-12 02:54:41 -06:00
|
|
|
|
|
2025-11-29 18:24:27 -06:00
|
|
|
|
await importContext.AddAsync(manifest);
|
2025-01-12 02:54:41 -06:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-11-29 18:24:27 -06:00
|
|
|
|
Logger?.LogError(ex, "Could not add game with ID {GameId} to import queue", game.Id);
|
2024-05-28 00:24:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-29 18:24:27 -06:00
|
|
|
|
await importContext.ImportQueueAsync();
|
2024-05-28 00:24:29 -05:00
|
|
|
|
}
|
2025-11-29 19:31:43 -06:00
|
|
|
|
|
|
|
|
|
|
public async Task ImportGameAsync(Guid gameId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var importContext = importContextFactory.Create();
|
|
|
|
|
|
|
|
|
|
|
|
var manifest = await gameClient.GetManifestAsync(gameId);
|
|
|
|
|
|
|
|
|
|
|
|
await importContext.AddAsync(manifest);
|
|
|
|
|
|
await importContext.ImportQueueAsync();
|
|
|
|
|
|
}
|
2024-05-28 00:24:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|