2023-12-20 17:30:27 -06:00
|
|
|
|
using AutoMapper;
|
2025-01-25 00:18:52 -06:00
|
|
|
|
using LANCommander.SDK.Enums;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
|
|
|
|
|
using LANCommander.Server.Extensions;
|
|
|
|
|
|
using LANCommander.Server.Models;
|
|
|
|
|
|
using LANCommander.Server.Services;
|
2025-03-24 00:19:33 -05:00
|
|
|
|
using LANCommander.Server.Services.Abstractions;
|
|
|
|
|
|
using LANCommander.Server.Services.Enums;
|
2023-01-05 18:36:25 -06:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-12-04 19:59:20 -06:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2023-01-04 20:00:11 -06:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-01-08 18:33:12 -06:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-05-07 20:06:09 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-01-04 20:00:11 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Controllers.Api
|
2023-01-04 20:00:11 -06:00
|
|
|
|
{
|
2023-01-05 18:36:25 -06:00
|
|
|
|
[Authorize(AuthenticationSchemes = "Bearer")]
|
2023-01-04 20:00:11 -06:00
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public class GamesController : BaseApiController
|
2023-01-04 20:00:11 -06:00
|
|
|
|
{
|
2023-01-11 20:49:31 -06:00
|
|
|
|
private readonly GameService GameService;
|
2025-02-16 10:08:05 -06:00
|
|
|
|
private readonly ImportService<Game> ImportService;
|
2024-10-31 01:54:34 -05:00
|
|
|
|
private readonly LibraryService LibraryService;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
private readonly StorageLocationService StorageLocationService;
|
2024-10-01 17:56:58 -05:00
|
|
|
|
private readonly ArchiveService ArchiveService;
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
private readonly UserService UserService;
|
2025-02-23 15:39:06 -06:00
|
|
|
|
private readonly PlaySessionService PlaySessionService;
|
|
|
|
|
|
private readonly ServerService ServerService;
|
2025-03-24 00:19:33 -05:00
|
|
|
|
private readonly IEnumerable<IServerEngine> ServerEngines;
|
2024-05-07 20:06:09 -05:00
|
|
|
|
private readonly IFusionCache Cache;
|
2025-01-25 00:18:52 -06:00
|
|
|
|
private readonly IMapper Mapper;
|
2023-01-04 20:00:11 -06:00
|
|
|
|
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public GamesController(
|
|
|
|
|
|
ILogger<GamesController> logger,
|
2025-03-24 00:19:33 -05:00
|
|
|
|
IServiceProvider serviceProvider,
|
2024-08-28 20:33:59 -05:00
|
|
|
|
IFusionCache cache,
|
2025-01-25 00:18:52 -06:00
|
|
|
|
IMapper mapper,
|
2024-08-28 20:33:59 -05:00
|
|
|
|
GameService gameService,
|
2025-02-16 10:08:05 -06:00
|
|
|
|
ImportService<Game> importService,
|
2024-10-31 01:54:34 -05:00
|
|
|
|
LibraryService libraryService,
|
2024-10-11 01:09:12 -05:00
|
|
|
|
StorageLocationService storageLocationService,
|
2024-10-01 17:56:58 -05:00
|
|
|
|
ArchiveService archiveService,
|
2025-02-23 15:39:06 -06:00
|
|
|
|
UserService userService,
|
|
|
|
|
|
PlaySessionService playSessionService,
|
2025-03-24 00:19:33 -05:00
|
|
|
|
ServerService serverService) : base(logger)
|
2023-01-04 20:00:11 -06:00
|
|
|
|
{
|
2023-01-11 20:49:31 -06:00
|
|
|
|
GameService = gameService;
|
2025-02-16 10:08:05 -06:00
|
|
|
|
ImportService = importService;
|
2024-10-31 01:54:34 -05:00
|
|
|
|
LibraryService = libraryService;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
StorageLocationService = storageLocationService;
|
2024-10-01 17:56:58 -05:00
|
|
|
|
ArchiveService = archiveService;
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
UserService = userService;
|
2025-02-23 15:39:06 -06:00
|
|
|
|
PlaySessionService = playSessionService;
|
|
|
|
|
|
ServerService = serverService;
|
2025-03-24 00:19:33 -05:00
|
|
|
|
ServerEngines = serviceProvider.GetServices<IServerEngine>();
|
2024-05-07 20:06:09 -05:00
|
|
|
|
Cache = cache;
|
2025-01-25 00:18:52 -06:00
|
|
|
|
Mapper = mapper;
|
2023-01-04 20:00:11 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IEnumerable<SDK.Models.Game>> GetAsync()
|
2023-01-04 20:00:11 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var user = await UserService.GetAsync(User?.Identity?.Name);
|
|
|
|
|
|
var userLibrary = await LibraryService.GetByUserIdAsync(user.Id);
|
2024-05-07 20:06:09 -05:00
|
|
|
|
|
2025-01-23 00:25:11 -06:00
|
|
|
|
var mappedGames = await Cache.GetOrSetAsync<IEnumerable<SDK.Models.Game>>("Games", async _ => {
|
2024-09-17 00:18:39 -05:00
|
|
|
|
Logger?.LogDebug("Mapped games cache is empty, repopulating");
|
2024-11-11 18:30:31 -06:00
|
|
|
|
|
2025-02-21 17:24:08 -06:00
|
|
|
|
var games = await GameService
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.GetAsync<SDK.Models.Game>();
|
2024-11-11 18:30:31 -06:00
|
|
|
|
|
|
|
|
|
|
return games;
|
2025-02-09 01:37:31 -06:00
|
|
|
|
}, TimeSpan.MaxValue, tags: ["Games"]);
|
2024-11-11 18:30:31 -06:00
|
|
|
|
|
|
|
|
|
|
foreach (var mappedGame in mappedGames)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (userLibrary.Games != null)
|
|
|
|
|
|
mappedGame.InLibrary = userLibrary.Games.Any(g => g.Id == mappedGame.Id);
|
|
|
|
|
|
}
|
2024-02-16 00:22:01 -06:00
|
|
|
|
|
2024-10-16 02:06:02 -05:00
|
|
|
|
if (Settings.Roles.RestrictGamesByCollection && !User.IsInRole(RoleService.AdministratorRoleName))
|
2023-12-04 19:59:20 -06:00
|
|
|
|
{
|
2025-02-08 12:32:44 -06:00
|
|
|
|
var roles = await UserService.GetRolesAsync(user);
|
2024-06-14 14:35:15 -07:00
|
|
|
|
|
2024-11-11 18:30:31 -06:00
|
|
|
|
var accessibleCollectionIds = roles.SelectMany(r => r.Collections.Select(c => c.Id)).Distinct();
|
2023-12-04 19:59:20 -06:00
|
|
|
|
|
2024-11-11 18:30:31 -06:00
|
|
|
|
var accessibleGames = mappedGames.Where(g => g.Collections.Any(c => accessibleCollectionIds.Contains(c.Id)));
|
2024-06-14 14:35:15 -07:00
|
|
|
|
|
|
|
|
|
|
foreach (var game in accessibleGames)
|
|
|
|
|
|
{
|
2024-11-11 18:30:31 -06:00
|
|
|
|
game.Collections = game.Collections.Where(c => accessibleCollectionIds.Contains(c.Id));
|
2023-12-04 19:59:20 -06:00
|
|
|
|
}
|
2024-11-11 18:30:31 -06:00
|
|
|
|
|
|
|
|
|
|
return accessibleGames;
|
2024-02-16 00:22:01 -06:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-11-11 18:30:31 -06:00
|
|
|
|
return mappedGames;
|
2023-12-04 19:59:20 -06:00
|
|
|
|
}
|
2023-01-04 20:00:11 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<SDK.Models.Game> GetAsync(Guid id)
|
2023-01-04 20:00:11 -06:00
|
|
|
|
{
|
2025-01-23 00:25:11 -06:00
|
|
|
|
var game = await Cache.GetOrSetAsync<SDK.Models.Game>($"Games/{id}", async _ =>
|
2025-01-22 21:27:38 -06:00
|
|
|
|
{
|
|
|
|
|
|
return await GameService
|
2025-01-25 16:05:33 -06:00
|
|
|
|
.Include(g => g.Actions)
|
|
|
|
|
|
.Include(g => g.Archives)
|
|
|
|
|
|
.Include(g => g.BaseGame)
|
|
|
|
|
|
.Include(g => g.Categories)
|
|
|
|
|
|
.Include(g => g.Collections)
|
|
|
|
|
|
.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)
|
|
|
|
|
|
.Include(g => g.Tags)
|
2025-02-16 13:24:23 -06:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
2025-01-22 21:27:38 -06:00
|
|
|
|
.GetAsync<SDK.Models.Game>(id);
|
2025-02-09 01:37:31 -06:00
|
|
|
|
}, TimeSpan.MaxValue, tags: ["Games", $"Games/{id}"]);
|
2025-01-22 21:27:38 -06:00
|
|
|
|
|
|
|
|
|
|
return game;
|
2023-01-04 20:00:11 -06:00
|
|
|
|
}
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}/Manifest")]
|
2024-01-02 20:51:33 -06:00
|
|
|
|
public async Task<SDK.GameManifest> GetManifest(Guid id)
|
2023-01-14 15:09:45 -06:00
|
|
|
|
{
|
2025-01-23 00:25:11 -06:00
|
|
|
|
var manifest = await Cache.GetOrSetAsync($"Games/{id}/Manifest", async _ =>
|
2025-01-22 21:27:38 -06:00
|
|
|
|
{
|
|
|
|
|
|
return await GameService.GetManifestAsync(id);
|
2025-02-09 01:37:31 -06:00
|
|
|
|
}, tags: ["Games", $"Games/{id}"]);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-25 00:18:52 -06:00
|
|
|
|
[HttpGet("{id}/Actions")]
|
2025-01-25 01:48:37 -06:00
|
|
|
|
public async Task<IEnumerable<SDK.Models.Action>> GetActionsAsync(Guid id)
|
2025-01-25 00:18:52 -06:00
|
|
|
|
{
|
|
|
|
|
|
var actions = await Cache.GetOrSetAsync<IEnumerable<SDK.Models.Action>>($"Games/{id}/Actions", async _ =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var game = await GameService
|
|
|
|
|
|
.Query(q =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return q.Include(g => g.Actions)
|
|
|
|
|
|
.Include(g => g.DependentGames)
|
|
|
|
|
|
.ThenInclude(dg => dg.Actions)
|
|
|
|
|
|
.Include(g => g.Servers)
|
2025-02-16 13:24:23 -06:00
|
|
|
|
.ThenInclude(s => s.Actions);
|
2025-01-25 00:18:52 -06:00
|
|
|
|
})
|
2025-02-16 13:24:23 -06:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
2025-01-25 00:18:52 -06:00
|
|
|
|
.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
var actions = new List<Data.Models.Action>();
|
|
|
|
|
|
|
2025-02-21 17:41:19 -06:00
|
|
|
|
actions.AddRange(game.Actions.OrderBy(a => a.SortOrder));
|
|
|
|
|
|
actions.AddRange(game.DependentGames.Where(dg => dg.Type == GameType.Expansion || dg.Type == GameType.Mod).OrderBy(dg => String.IsNullOrWhiteSpace(dg.SortTitle) ? dg.Title : dg.SortTitle).SelectMany(dg => dg.Actions.OrderBy(a => a.SortOrder)));
|
2025-01-25 00:18:52 -06:00
|
|
|
|
actions.AddRange(game.Servers.SelectMany(s => s.Actions));
|
|
|
|
|
|
|
|
|
|
|
|
return Mapper.Map<IEnumerable<SDK.Models.Action>>(actions);
|
2025-02-09 01:37:31 -06:00
|
|
|
|
}, tags: ["Games", $"Games/{id}"]);
|
2025-01-25 00:18:52 -06:00
|
|
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-27 01:25:32 -06:00
|
|
|
|
[HttpGet("{id}/Addons")]
|
|
|
|
|
|
public async Task<IEnumerable<SDK.Models.Game>> GetAddonsAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var addons = await Cache.GetOrSetAsync($"Games/{id}/Addons", async _ =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return await GameService
|
|
|
|
|
|
.Include(g => g.Archives)
|
2025-02-16 13:24:23 -06:00
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.AsNoTracking()
|
2025-01-27 01:25:32 -06:00
|
|
|
|
.GetAsync<SDK.Models.Game>(g => g.BaseGameId == id && (g.Type == GameType.Expansion || g.Type == GameType.Mod));
|
2025-02-09 01:37:31 -06:00
|
|
|
|
}, tags: ["Games", $"Games/{id}"]);
|
2025-01-27 01:25:32 -06:00
|
|
|
|
|
|
|
|
|
|
return addons;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-23 15:39:06 -06:00
|
|
|
|
[HttpGet("{id}/Started")]
|
|
|
|
|
|
public async Task<IActionResult> StartedAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await UserService.GetAsync(User?.Identity?.Name);
|
|
|
|
|
|
var game = await GameService.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (game == null || user == null)
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
|
|
#region Start recording play session
|
|
|
|
|
|
var activeSessions = await PlaySessionService
|
|
|
|
|
|
.Include(ps => ps.Game)
|
|
|
|
|
|
.GetAsync(ps => ps.UserId == user.Id && ps.End == null);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var activeSession in activeSessions)
|
|
|
|
|
|
await PlaySessionService.EndSessionAsync(game.Id, activeSession.UserId);
|
|
|
|
|
|
|
|
|
|
|
|
await PlaySessionService.StartSessionAsync(game.Id, user.Id);
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Autostart Servers
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var servers = await ServerService.GetAsync(s =>
|
|
|
|
|
|
s.GameId == game.Id && s.Autostart && s.AutostartMethod == ServerAutostartMethod.OnPlayerActivity);
|
|
|
|
|
|
|
2025-03-24 00:19:33 -05:00
|
|
|
|
foreach (var serverEngine in ServerEngines)
|
2025-02-23 15:39:06 -06:00
|
|
|
|
{
|
2025-03-24 00:19:33 -05:00
|
|
|
|
foreach (var server in servers)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (serverEngine.IsManaging(server.Id) && (await serverEngine.GetStatusAsync(server.Id) == ServerProcessStatus.Stopped))
|
|
|
|
|
|
await serverEngine.StartAsync(server.Id);
|
|
|
|
|
|
}
|
2025-02-23 15:39:06 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Servers could not be autostarted");
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Run server scripts
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var servers = await ServerService
|
|
|
|
|
|
.GetAsync(s => s.GameId == game.Id);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var server in servers)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ServerService.RunGameStartedScriptsAsync(server.Id, user.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Server scripts could not run");
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}/Stopped")]
|
|
|
|
|
|
public async Task<IActionResult> StoppedAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await UserService.GetAsync(User?.Identity?.Name);
|
|
|
|
|
|
var game = await GameService.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (game == null || user == null)
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
|
|
await PlaySessionService.EndSessionAsync(game.Id, user.Id);
|
|
|
|
|
|
|
|
|
|
|
|
#region Run server scripts
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var servers = await ServerService
|
|
|
|
|
|
.GetAsync(s => s.GameId == game.Id);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var server in servers)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ServerService.RunGameStoppedScriptsAsync(server.Id, user.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Server scripts could not run");
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-25 01:48:37 -06:00
|
|
|
|
[HttpGet("{id}/CheckForUpdate")]
|
|
|
|
|
|
public async Task<bool> CheckForUpdateAsync(Guid id, string version)
|
|
|
|
|
|
{
|
|
|
|
|
|
var gameArchives = await ArchiveService.GetAsync(a => a.GameId == id);
|
|
|
|
|
|
|
|
|
|
|
|
var latestArchive = gameArchives.OrderByDescending(a => a.CreatedOn).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (latestArchive?.Version != version)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
else
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-20 12:15:47 -06:00
|
|
|
|
[AllowAnonymous]
|
2023-01-14 15:09:45 -06:00
|
|
|
|
[HttpGet("{id}/Download")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> DownloadAsync(Guid id)
|
2023-01-14 15:09:45 -06:00
|
|
|
|
{
|
2023-12-20 12:15:47 -06:00
|
|
|
|
if (!Settings.Archives.AllowInsecureDownloads && (User == null || User.Identity == null || !User.Identity.IsAuthenticated))
|
2024-09-17 00:18:39 -05:00
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError("User is not authorized to download game with ID {GameId}", id);
|
2023-12-20 12:15:47 -06:00
|
|
|
|
return Unauthorized();
|
2024-09-17 00:18:39 -05:00
|
|
|
|
}
|
2023-12-20 12:15:47 -06:00
|
|
|
|
|
2024-12-20 11:34:59 -06:00
|
|
|
|
var game = await GameService
|
|
|
|
|
|
.Include(g => g.Archives)
|
|
|
|
|
|
.GetAsync(id);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
|
|
|
|
|
if (game == null)
|
2024-09-17 00:18:39 -05:00
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError("Game not found with ID {GameId}", id);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
return NotFound();
|
2024-09-17 00:18:39 -05:00
|
|
|
|
}
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
|
|
|
|
|
if (game.Archives == null || game.Archives.Count == 0)
|
2024-09-17 00:18:39 -05:00
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError("No archives found for game with ID {GameId}", id);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
return NotFound();
|
2024-09-17 00:18:39 -05:00
|
|
|
|
}
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
|
|
|
|
|
var archive = game.Archives.OrderByDescending(a => a.CreatedOn).First();
|
|
|
|
|
|
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var filename = await ArchiveService.GetArchiveFileLocationAsync(archive);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
|
|
|
|
|
if (!System.IO.File.Exists(filename))
|
2024-09-17 00:18:39 -05:00
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError("No archive file exists for game with ID {GameId} at the expected path {FileName}", id, filename);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
return NotFound();
|
2024-09-17 00:18:39 -05:00
|
|
|
|
}
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
|
|
|
|
|
return File(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", $"{game.Title.SanitizeFilename()}.zip");
|
|
|
|
|
|
}
|
2024-08-12 19:33:26 -05:00
|
|
|
|
|
2024-10-16 02:06:02 -05:00
|
|
|
|
[Authorize(Roles = RoleService.AdministratorRoleName)]
|
2024-08-12 19:33:26 -05:00
|
|
|
|
[HttpPost("Import/{objectKey}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> ImportAsync(Guid objectKey)
|
2024-08-12 19:33:26 -05:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-02-16 10:08:05 -06:00
|
|
|
|
var game = await ImportService.ImportFromUploadArchiveAsync(objectKey);
|
2024-08-12 19:33:26 -05:00
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-09-17 00:18:39 -05:00
|
|
|
|
Logger?.LogError(ex, "Could not import game from upload");
|
2024-08-12 19:33:26 -05:00
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
2024-10-16 02:06:02 -05:00
|
|
|
|
[Authorize(Roles = RoleService.AdministratorRoleName)]
|
2024-10-01 17:56:58 -05:00
|
|
|
|
[HttpPost("UploadArchive")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> UploadArchiveAsync(SDK.Models.UploadArchiveRequest request)
|
2024-10-01 17:56:58 -05:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var storageLocation = await StorageLocationService.FirstOrDefaultAsync(l => request.StorageLocationId.HasValue ? l.Id == request.StorageLocationId.Value : l.Default);
|
|
|
|
|
|
var archive = await ArchiveService.FirstOrDefaultAsync(a => a.GameId == request.Id && a.Version == request.Version);
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var archivePath = await ArchiveService.GetArchiveFileLocationAsync(archive);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
|
|
|
|
|
if (archive != null)
|
|
|
|
|
|
{
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var existingArchivePath = await ArchiveService.GetArchiveFileLocationAsync(archive);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
|
|
|
|
|
System.IO.File.Delete(existingArchivePath);
|
|
|
|
|
|
|
|
|
|
|
|
archive.ObjectKey = request.ObjectKey.ToString();
|
|
|
|
|
|
archive.Changelog = request.Changelog;
|
|
|
|
|
|
archive.CompressedSize = new System.IO.FileInfo(archivePath).Length;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
archive.StorageLocation = storageLocation;
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
archive = await ArchiveService.UpdateAsync(archive);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
archive = new Archive()
|
|
|
|
|
|
{
|
|
|
|
|
|
ObjectKey = request.ObjectKey.ToString(),
|
|
|
|
|
|
Changelog = request.Changelog,
|
|
|
|
|
|
GameId = request.Id,
|
|
|
|
|
|
CompressedSize = new System.IO.FileInfo(archivePath).Length,
|
2024-10-11 01:09:12 -05:00
|
|
|
|
StorageLocation = storageLocation
|
2024-10-01 17:56:58 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
await ArchiveService.AddAsync(archive);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Could not upload game archive");
|
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-01-04 20:00:11 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|