using System; using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.FileProviders; namespace Supercell.Laser.Server.SCID { internal static class SCIDAssets { private const string ManifestName = "AssetManifest.json"; public static void Register(WebApplication app) { string root = Path.Combine(AppContext.BaseDirectory, "SCID", "Assets"); if (!Directory.Exists(root)) { root = Path.Combine(Directory.GetCurrentDirectory(), "SCID", "Assets"); if (!Directory.Exists(root)) { root = Path.Combine(AppContext.BaseDirectory, "Assets"); if (!Directory.Exists(root)) { root = Path.Combine(Directory.GetCurrentDirectory(), "Assets"); if (!Directory.Exists(root)) return; } } } try { PhysicalFileProvider provider = new(root); app.Lifetime.ApplicationStopped.Register(provider.Dispose); app.UseStaticFiles(CreateOptions(provider, PathString.Empty)); app.UseStaticFiles(CreateOptions(provider, new PathString("/assets"))); app.UseStaticFiles(CreateOptions(provider, new PathString("/RemoteAssets"))); app.UseStaticFiles(CreateOptions(provider, new PathString("/assets/RemoteAssets"))); SCIDStart.Log($"[SCID] Static assets registered from: {root}"); } catch (Exception ex) { SCIDStart.Log($"[SCID] Asset provider error: {ex.Message}"); } } private static StaticFileOptions CreateOptions(IFileProvider provider, PathString requestPath) { return new StaticFileOptions { FileProvider = provider, RequestPath = requestPath, ServeUnknownFileTypes = true, DefaultContentType = "application/octet-stream", OnPrepareResponse = context => { HttpResponse response = context.Context.Response; response.Headers["X-Content-Type-Options"] = "nosniff"; response.Headers.CacheControl = string.Equals( context.File.Name, ManifestName, StringComparison.OrdinalIgnoreCase) ? "no-cache" : "public, max-age=86400"; } }; } } }