Files
SPT-Server-Build/Libraries/SPTarkov.Server.Web/SPTWeb.cs
T
Jesse 687b4f7a49 Add blazor & MVC Support (#602)
* Add initial code for Razor pages support

* Remove finalizer

* Try fully loading blazor

This is most likely entirely broken because of a rebase now

* UseSptBlazor after app.Use

* Fix up StaticWebAsset loading, add MudBlazor

* Implement page

* Update comment

* Replaced existing status page with razor

* Track background video in LFS

* Update attributes

* Improved status page theming

* Fix up wwwroot publish folder to SPT_Data/wwwroot

* Added name to page

* Remove unnecessary code

* Begin fixing up MVC & Blazor for modding

* Update TestMod

* Cleanup todo

* Further work out mod support

* Re-order initialization and use logger

* Rename library to SPTarkov.Server.Web

---------

Co-authored-by: Chomp <dev@dev.sp-tarkov.com>
Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com>
2025-10-02 19:03:27 +00:00

77 lines
2.4 KiB
C#

using Microsoft.Extensions.FileProviders;
using MudBlazor.Services;
using SPTarkov.Server.Core.Models.Spt.Mod;
using SPTarkov.Server.Web.Components;
namespace SPTarkov.Server.Web;
public static class SPTWeb
{
internal static IEnumerable<SptMod> SptWebMods = [];
public static void InitializeSptBlazor(this WebApplicationBuilder builder, IReadOnlyList<SptMod> sptMods)
{
SptWebMods = sptMods.Where(mod => mod.ModMetadata is IModWebMetadata).ToList();
builder.WebHost.UseStaticWebAssets();
builder.Services.AddMudServices();
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
var mvcBuilder = builder.Services.AddControllers();
foreach (var assembly in SptWebMods.SelectMany(mod => mod.Assemblies))
{
mvcBuilder.AddApplicationPart(assembly);
}
}
public static void UseSptBlazor(this WebApplication app)
{
var logger = app.Services.GetRequiredService<ILogger<App>>();
app.UseAntiforgery();
#if DEBUG
//MS currently has a bug where streaming video doesn't work properly in debug, unless you use this
//Issue: https://github.com/dotnet/aspnetcore/issues/63320
app.UseStaticFiles();
#else
app.MapStaticAssets();
#endif
var razorBuilder = app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
foreach (var mod in SptWebMods)
{
foreach (var assembly in mod.Assemblies)
{
razorBuilder.AddAdditionalAssemblies(assembly);
}
var modAssembly = mod.ModMetadata.GetType().Assembly;
var location = Path.GetDirectoryName(modAssembly.Location);
if (!string.IsNullOrEmpty(location) && Directory.Exists(Path.Combine(location, "wwwroot")))
{
var modAssemblyName = modAssembly.GetName().Name;
logger.LogDebug(
"Mod {modName} has a wwwroot, mapping to /{modAssemblyName}/",
mod.ModMetadata.Name,
modAssembly.GetName().Name
);
app.UseStaticFiles(
new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(location, "wwwroot")),
RequestPath = $"/{modAssembly.GetName().Name}",
}
);
}
}
app.MapControllers();
}
}