2025-08-02 00:22:16 -05:00
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
|
using AutoMapper;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using LANCommander.Server.Services.Extensions;
|
2023-01-14 15:09:45 -06:00
|
|
|
|
using LANCommander.SDK;
|
2023-12-14 19:09:00 -06:00
|
|
|
|
using LANCommander.SDK.Enums;
|
2024-05-07 23:08:22 -05:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using LANCommander.Server.Services.Models;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public class GameService(
|
|
|
|
|
|
ILogger<GameService> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IMapper mapper,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory,
|
2025-11-17 22:09:03 -06:00
|
|
|
|
ArchiveService archiveService,
|
2025-09-24 20:58:23 -05:00
|
|
|
|
MediaService mediaService,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SDK.Services.ScriptClient scriptClient) : BaseDatabaseService<Game>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task<Game> AddAsync(Game entity)
|
2024-05-07 23:08:22 -05:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
return await base.AddAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Actions);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Archives);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.BaseGame);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Categories);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Collections);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.CustomFields);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Developers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Engine);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Genres);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Keys);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Libraries);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Media);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.MultiplayerModes);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Pages);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Platforms);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Publishers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Redistributables);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.SavePaths);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Scripts);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Tags);
|
|
|
|
|
|
});
|
2024-05-07 23:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task<ExistingEntityResult<Game>> AddMissingAsync(Expression<Func<Game, bool>> predicate, Game entity)
|
2024-05-07 23:08:22 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(entity.Id);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
return await base.AddMissingAsync(predicate, entity);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task<Game> UpdateAsync(Game entity)
|
2024-05-07 23:08:22 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(entity.Id);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
|
2025-02-09 14:52:26 -06:00
|
|
|
|
if (entity.Media != null)
|
|
|
|
|
|
foreach (var media in entity.Media.Where(m => m.Id == Guid.Empty && String.IsNullOrWhiteSpace(m.Crc32)).ToList())
|
|
|
|
|
|
entity.Media.Remove(media);
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
|
|
|
|
|
var update = await base.UpdateAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Actions);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Archives);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.BaseGame);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Categories);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Collections);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.CustomFields);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Developers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Engine);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Genres);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Keys);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Libraries);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Media);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.MultiplayerModes);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Pages);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Platforms);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Publishers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Redistributables);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.SavePaths);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Scripts);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Tags);
|
|
|
|
|
|
});
|
2024-09-30 20:19:02 -05:00
|
|
|
|
|
2025-02-02 14:58:07 -06:00
|
|
|
|
return update;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
2023-01-09 01:35:08 -06:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task DeleteAsync(Game game)
|
2023-01-09 01:35:08 -06:00
|
|
|
|
{
|
2024-11-25 22:29:56 -06:00
|
|
|
|
game = await Include(
|
|
|
|
|
|
g => g.Archives,
|
|
|
|
|
|
g => g.Media)
|
|
|
|
|
|
.GetAsync(game.Id);
|
|
|
|
|
|
|
2025-02-09 14:52:26 -06:00
|
|
|
|
if (game.Archives != null)
|
|
|
|
|
|
foreach (var archive in game.Archives.ToList())
|
2025-11-17 22:09:03 -06:00
|
|
|
|
await archiveService.DeleteAsync(archive);
|
2023-09-01 11:40:29 -05:00
|
|
|
|
|
2025-02-09 14:52:26 -06:00
|
|
|
|
if (game.Media != null)
|
|
|
|
|
|
foreach (var media in game.Media.ToList())
|
|
|
|
|
|
await mediaService.DeleteAsync(media);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
2025-02-09 14:52:26 -06:00
|
|
|
|
await base.DeleteAsync(game);
|
2023-01-09 01:35:08 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-23 02:17:06 -05:00
|
|
|
|
public async Task<ICollection<Game>> GetAddonsAsync(Game game)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await GetAsync(g => g.AddonTypes.Contains(g.Type));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-29 18:24:27 -06:00
|
|
|
|
public async Task<SDK.Models.Manifest.Game> GetManifestAsync(Guid id)
|
2023-01-14 15:09:45 -06:00
|
|
|
|
{
|
2025-01-25 12:46:24 -06:00
|
|
|
|
var game = await Query(q =>
|
|
|
|
|
|
{
|
2025-01-30 19:21:02 -06:00
|
|
|
|
return q
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
2025-01-25 12:46:24 -06:00
|
|
|
|
.Include(g => g.Actions)
|
|
|
|
|
|
.Include(g => g.Archives)
|
|
|
|
|
|
.Include(g => g.BaseGame)
|
|
|
|
|
|
.Include(g => g.Categories)
|
|
|
|
|
|
.Include(g => g.Collections)
|
2025-01-30 19:21:02 -06:00
|
|
|
|
.Include(g => g.CustomFields)
|
2025-01-25 12:46:24 -06:00
|
|
|
|
.Include(g => g.DependentGames)
|
|
|
|
|
|
.Include(g => g.Developers)
|
|
|
|
|
|
.Include(g => g.Engine)
|
|
|
|
|
|
.Include(g => g.Genres)
|
|
|
|
|
|
.Include(g => g.Media)
|
|
|
|
|
|
.Include(g => g.MultiplayerModes)
|
|
|
|
|
|
.Include(g => g.Platforms)
|
|
|
|
|
|
.Include(g => g.Publishers)
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
2025-03-13 00:35:58 -05:00
|
|
|
|
.Include(g => g.SavePaths)
|
2025-08-08 13:27:09 +02:00
|
|
|
|
.Include(g => g.Scripts)
|
2025-01-25 12:46:24 -06:00
|
|
|
|
.Include(g => g.Tags);
|
2025-08-06 19:37:31 -05:00
|
|
|
|
}).GetAsync(id);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
2025-08-06 19:37:31 -05:00
|
|
|
|
return GetManifest(game);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-29 18:24:27 -06:00
|
|
|
|
public SDK.Models.Manifest.Game GetManifest(Game game)
|
2025-08-06 19:37:31 -05:00
|
|
|
|
{
|
|
|
|
|
|
if (game == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2025-11-29 18:24:27 -06:00
|
|
|
|
var manifest = mapper.Map<SDK.Models.Manifest.Game>(game);
|
2025-08-06 19:37:31 -05:00
|
|
|
|
|
|
|
|
|
|
return manifest;
|
2024-02-16 00:22:01 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-29 20:10:35 -05:00
|
|
|
|
public async Task<GameCustomField> GetCustomFieldAsync(Guid id, string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
var game = await AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.Include(g => g.CustomFields)
|
|
|
|
|
|
.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
return game.CustomFields.FirstOrDefault(c => c.Name == name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<GameCustomField> SetCustomFieldAsync(Guid id, string name, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
var game = await AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.Include(g => g.CustomFields)
|
|
|
|
|
|
.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (game.CustomFields.Any(c => c.Name == name))
|
|
|
|
|
|
foreach (var customField in game.CustomFields.Where(c => c.Name == name))
|
|
|
|
|
|
customField.Value = value;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
game.CustomFields.Add(new GameCustomField());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return await GetCustomFieldAsync(id, name);
|
|
|
|
|
|
}
|
2025-08-02 00:22:16 -05:00
|
|
|
|
|
2025-11-20 22:04:20 -06:00
|
|
|
|
public async Task<Archive> GetLatestArchiveAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var game = await AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.Include(g => g.Archives)
|
|
|
|
|
|
.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
var latestArchive = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
return latestArchive;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetVersionAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var latestArchive = await GetLatestArchiveAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
return latestArchive?.Version ?? String.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-02 00:22:16 -05:00
|
|
|
|
public async Task PackageAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var game = await AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.Include(g => g.Archives)
|
|
|
|
|
|
.Include(g => g.CustomFields)
|
|
|
|
|
|
.Include(g => g.Scripts)
|
|
|
|
|
|
.GetAsync(id);
|
2025-08-02 23:27:27 -05:00
|
|
|
|
|
|
|
|
|
|
logger?.LogInformation("Packaging game {GameTitle}", game.Title);
|
2025-08-02 00:22:16 -05:00
|
|
|
|
|
|
|
|
|
|
var latestArchive = game.Archives?.OrderByDescending(a => a.CreatedOn).FirstOrDefault();
|
|
|
|
|
|
var storageLocationId = latestArchive?.StorageLocationId;
|
|
|
|
|
|
|
|
|
|
|
|
if (game.Scripts?.Any(s => s.Type == ScriptType.Package) ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var script in game.Scripts.Where(s => s.Type == ScriptType.Package))
|
|
|
|
|
|
{
|
2025-09-24 20:58:23 -05:00
|
|
|
|
var package = await scriptClient.RunPackageScriptAsync(mapper.Map<SDK.Models.Script>(script), mapper.Map<SDK.Models.Game>(game));
|
2025-08-02 00:22:16 -05:00
|
|
|
|
|
|
|
|
|
|
var archive = new Archive
|
|
|
|
|
|
{
|
|
|
|
|
|
Version = package.Version,
|
|
|
|
|
|
GameId = game.Id,
|
|
|
|
|
|
ObjectKey = Guid.NewGuid().ToString(),
|
|
|
|
|
|
LastVersion = latestArchive,
|
|
|
|
|
|
StorageLocationId = storageLocationId.GetValueOrDefault(),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-17 22:09:03 -06:00
|
|
|
|
archive = await archiveService.AddAsync(archive);
|
2025-08-02 00:22:16 -05:00
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(package.Path))
|
|
|
|
|
|
{
|
2025-11-17 22:09:03 -06:00
|
|
|
|
var destination = await archiveService.GetArchiveFileLocationAsync(archive);
|
2025-08-02 00:22:16 -05:00
|
|
|
|
|
|
|
|
|
|
ZipFile.CreateFromDirectory(package.Path, destination);
|
2025-08-02 23:27:27 -05:00
|
|
|
|
|
|
|
|
|
|
logger?.LogInformation("Successfully packaged {GameTitle} and created new archive with version number {GameVersion}", game.Title, archive.Version);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
logger?.LogError("Could not package game {GameTitle}, the path {Path} could not be found", game.Title, package.Path);
|
|
|
|
|
|
|
2025-11-17 22:09:03 -06:00
|
|
|
|
await archiveService.DeleteAsync(archive);
|
2025-08-02 00:22:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-02 23:27:27 -05:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
logger?.LogWarning("Could not package game {GameTitle}, no packaging scripts are defined", game.Title);
|
|
|
|
|
|
}
|
2025-08-02 00:22:16 -05:00
|
|
|
|
}
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|