2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
|
|
|
|
|
using LANCommander.Server.Models;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using AutoMapper;
|
2025-12-11 02:06:04 -06:00
|
|
|
|
using LANCommander.SDK;
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using LANCommander.SDK.Extensions;
|
2025-02-04 02:31:23 -06:00
|
|
|
|
using LANCommander.Server.Services.Extensions;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-01-15 03:11:51 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-01-15 03:11:51 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public sealed class ScriptService(
|
|
|
|
|
|
ILogger<ScriptService> 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-11-16 12:31:25 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory) : BaseDatabaseService<Script>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
2023-01-15 03:11:51 -06:00
|
|
|
|
{
|
2025-02-04 02:31:23 -06:00
|
|
|
|
public override async Task<Script> AddAsync(Script script)
|
2025-02-01 02:21:34 -06:00
|
|
|
|
{
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using var context = await contextFactory.CreateDbContextAsync();
|
2026-07-02 20:01:04 -05:00
|
|
|
|
|
2025-02-05 01:32:00 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(script?.GameId);
|
2026-07-02 20:01:04 -05:00
|
|
|
|
await cache.RemoveByTagAsync("Scripts");
|
2025-02-05 01:32:00 -06:00
|
|
|
|
|
|
|
|
|
|
if (script.RedistributableId?.IsNullOrEmpty() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var games = await context
|
|
|
|
|
|
.Games
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
|
|
|
|
|
.Where(g => g.Redistributables.Any(r => r.Id == script.RedistributableId))
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
|
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
2025-02-23 08:49:43 -06:00
|
|
|
|
return await base.AddAsync(script, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Redistributable);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Server);
|
|
|
|
|
|
});
|
2025-02-04 02:31:23 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task<Script> UpdateAsync(Script script)
|
|
|
|
|
|
{
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using var context = await contextFactory.CreateDbContextAsync();
|
2026-07-02 20:01:04 -05:00
|
|
|
|
|
2025-02-05 01:32:00 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(script?.GameId);
|
2026-07-02 20:01:04 -05:00
|
|
|
|
await cache.RemoveByTagAsync("Scripts");
|
2025-02-05 01:32:00 -06:00
|
|
|
|
|
|
|
|
|
|
if (script.RedistributableId?.IsNullOrEmpty() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var games = await context
|
|
|
|
|
|
.Games
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
|
|
|
|
|
.Where(g => g.Redistributables.Any(r => r.Id == script.RedistributableId))
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
|
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
|
|
|
|
|
return await base.UpdateAsync(script, async context =>
|
2025-02-02 14:58:07 -06:00
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Redistributable);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Server);
|
|
|
|
|
|
});
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
|
|
|
|
|
public override async Task DeleteAsync(Script script)
|
|
|
|
|
|
{
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using var context = await contextFactory.CreateDbContextAsync();
|
2026-07-02 20:01:04 -05:00
|
|
|
|
|
2025-02-05 01:32:00 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(script?.GameId);
|
2026-07-02 20:01:04 -05:00
|
|
|
|
await cache.RemoveByTagAsync("Scripts");
|
2025-02-05 01:32:00 -06:00
|
|
|
|
|
|
|
|
|
|
if (script.RedistributableId?.IsNullOrEmpty() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var games = await context
|
|
|
|
|
|
.Games
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
|
|
|
|
|
.Where(g => g.Redistributables.Any(r => r.Id == script.RedistributableId))
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
|
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
|
|
|
|
|
await base.DeleteAsync(script);
|
|
|
|
|
|
}
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
2025-12-11 02:06:04 -06:00
|
|
|
|
public IEnumerable<Snippet> GetSnippets()
|
2023-01-26 20:42:33 -06:00
|
|
|
|
{
|
2026-01-20 00:46:53 -06:00
|
|
|
|
var storagePath = settingsProvider.CurrentValue.Server.Scripts.Snippets.StoragePath;
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(storagePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
// Set default storage path
|
|
|
|
|
|
storagePath = AppPaths.GetConfigPath("Snippets");
|
|
|
|
|
|
|
|
|
|
|
|
settingsProvider.Update(s =>
|
|
|
|
|
|
{
|
|
|
|
|
|
s.Server.Scripts.Snippets.StoragePath = storagePath;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 21:18:33 -06:00
|
|
|
|
var files = Directory.GetFiles(storagePath, "*.ps1", SearchOption.AllDirectories);
|
2023-01-26 20:42:33 -06:00
|
|
|
|
|
|
|
|
|
|
return files.Select(f =>
|
|
|
|
|
|
{
|
2026-03-04 21:18:33 -06:00
|
|
|
|
var split = f.Substring(storagePath.Length).TrimStart('/').Split(Path.DirectorySeparatorChar);
|
2023-01-26 20:42:33 -06:00
|
|
|
|
|
2025-12-11 02:06:04 -06:00
|
|
|
|
return new Snippet
|
2023-01-26 20:42:33 -06:00
|
|
|
|
{
|
|
|
|
|
|
Name = Path.GetFileNameWithoutExtension(f),
|
|
|
|
|
|
Group = split[1],
|
|
|
|
|
|
Content = File.ReadAllText(f)
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-06-28 23:02:35 -05:00
|
|
|
|
|
|
|
|
|
|
private string GetSnippetsStoragePath()
|
|
|
|
|
|
{
|
|
|
|
|
|
var storagePath = settingsProvider.CurrentValue.Server.Scripts.Snippets.StoragePath;
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(storagePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
storagePath = AppPaths.GetConfigPath("Snippets");
|
|
|
|
|
|
|
|
|
|
|
|
settingsProvider.Update(s =>
|
|
|
|
|
|
{
|
|
|
|
|
|
s.Server.Scripts.Snippets.StoragePath = storagePath;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(storagePath))
|
|
|
|
|
|
Directory.CreateDirectory(storagePath);
|
|
|
|
|
|
|
|
|
|
|
|
return storagePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetSnippetPath(string group, string name) =>
|
|
|
|
|
|
Path.Combine(GetSnippetsStoragePath(), group, $"{name}.ps1");
|
|
|
|
|
|
|
|
|
|
|
|
public Snippet GetSnippet(string group, string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = GetSnippetPath(group, name);
|
|
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
return new Snippet
|
|
|
|
|
|
{
|
|
|
|
|
|
Group = group,
|
|
|
|
|
|
Name = name,
|
|
|
|
|
|
Content = File.ReadAllText(path),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SaveSnippet(Snippet snippet)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = GetSnippetPath(snippet.Group, snippet.Name);
|
|
|
|
|
|
var directory = Path.GetDirectoryName(path);
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(directory))
|
|
|
|
|
|
Directory.CreateDirectory(directory);
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(path, snippet.Content ?? string.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteSnippet(string group, string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = GetSnippetPath(group, name);
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
}
|
2023-01-15 03:11:51 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|