From c26169f53ab1cc8b082d6cf30fad7c8034d85366 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 10 Feb 2025 19:40:28 +0000 Subject: [PATCH] BundleSerializer --- Libraries/Core/Loaders/BundleLoader.cs | 4 +- .../Routers/Serializers/BundleSerializer.cs | 38 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Libraries/Core/Loaders/BundleLoader.cs b/Libraries/Core/Loaders/BundleLoader.cs index 0c4e733c..96ca7a27 100644 --- a/Libraries/Core/Loaders/BundleLoader.cs +++ b/Libraries/Core/Loaders/BundleLoader.cs @@ -82,9 +82,9 @@ namespace Core.Loaders return result; } - public BundleInfo GetBundle(string bundleKey) + public BundleInfo? GetBundle(string bundleKey) { - return _cloner.Clone(_bundles[bundleKey]); + return _bundles.GetValueOrDefault(bundleKey); } public void AddBundles(string modPath) diff --git a/Libraries/Core/Routers/Serializers/BundleSerializer.cs b/Libraries/Core/Routers/Serializers/BundleSerializer.cs index e7ea2b03..7cf66025 100644 --- a/Libraries/Core/Routers/Serializers/BundleSerializer.cs +++ b/Libraries/Core/Routers/Serializers/BundleSerializer.cs @@ -1,5 +1,39 @@ -namespace Core.Routers.Serializers; +using Core.DI; +using Core.Loaders; +using Core.Models.Utils; +using Core.Utils; +using SptCommon.Annotations; -public class BundleSerializer +namespace Core.Routers.Serializers; + +[Injectable] +public class BundleSerializer( + ISptLogger logger, + BundleLoader bundleLoader, + HttpFileUtil httpFileUtil +) : ISerializer { + public void Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body) + { + var key = req.Path.Value.Split("/bundle/")[1]; + var bundle = bundleLoader.GetBundle(key); + if (bundle == null) + { + return; + } + + logger.Info($"[BUNDLE]: {req.Path.Value}"); + if (bundle.ModPath == null) + { + logger.Error($"Mod: {key} lacks a modPath property, skipped loading"); + return; + } + + httpFileUtil.SendFile(resp, $"{bundle.ModPath}/bundles/{bundle.FileName}"); + } + + public bool CanHandle(string route) + { + return route == "BUNDLE"; + } }