diff --git a/Libraries/Core/Helpers/HttpServerHelper.cs b/Libraries/Core/Helpers/HttpServerHelper.cs index 80cb4976..1add8b18 100644 --- a/Libraries/Core/Helpers/HttpServerHelper.cs +++ b/Libraries/Core/Helpers/HttpServerHelper.cs @@ -22,9 +22,9 @@ public class HttpServerHelper(ConfigServer configServer) { "txt", "text/plain" } }; - public string GetMimeText(string key) + public string? GetMimeText(string key) { - return mime[key]; + return mime.GetValueOrDefault(key); } /** diff --git a/Libraries/Core/Loaders/BundleLoader.cs b/Libraries/Core/Loaders/BundleLoader.cs index 01bb98c2..29e413ef 100644 --- a/Libraries/Core/Loaders/BundleLoader.cs +++ b/Libraries/Core/Loaders/BundleLoader.cs @@ -7,33 +7,51 @@ using SptCommon.Annotations; namespace Core.Loaders { + /* + { + "ModPath" : "/user/mods/Mod3", + "FileName" : "assets/content/weapons/usable_items/item_bottle/textures/client_assets.bundle", + "Bundle" : { + "key" : "assets/content/weapons/usable_items/item_bottle/textures/client_assets.bundle", + "dependencyKeys" : [ ] + }, + "Crc" : 1030040371, + "Dependencies" : [ ] + } */ public class BundleInfo { public string? ModPath { get; + set; } public string FileName { get; + set; } public BundleManifestEntry Bundle { get; + set; } public uint Crc { get; + set; } public List Dependencies { get; + set; } + public BundleInfo() {} + public BundleInfo( string modPath, BundleManifestEntry bundle, @@ -95,7 +113,8 @@ namespace Core.Loaders public void AddBundles(string modPath) { - // modPath should be relative to the server exe - /user/mods/Mod3/ + // modPath should be relative to the server exe - ./user/mods/Mod3 + // TODO: make sure the mod is passing a path that is relative from the server exe var modBundlesJson = _fileUtil.ReadFile(Path.Join(Directory.GetCurrentDirectory(), modPath, "bundles.json")); var modBundles = _jsonUtil.Deserialize(modBundlesJson); @@ -103,9 +122,9 @@ namespace Core.Loaders foreach (var bundleManifest in bundleManifestArr) { - var relativeModPath = modPath.Replace('\\', '/'); // /\\/g, "/" - replaces all instances of \\ with / + var relativeModPath = modPath.Replace('\\', '/'); - var bundleLocalPath = Path.Join(relativeModPath, "bundles", bundleManifest.Key); + var bundleLocalPath = Path.Join(relativeModPath, "bundles", bundleManifest.Key).Replace('\\', '/'); if (!_bundleHashCacheService.CalculateAndMatchHash(bundleLocalPath)) { @@ -138,7 +157,8 @@ public record BundleManifest public record BundleManifestEntry { [JsonPropertyName("key")] - public string Key { + public string Key + { get; set; } @@ -150,3 +170,4 @@ public record BundleManifestEntry set; } } + diff --git a/Libraries/Core/Routers/Serializers/BundleSerializer.cs b/Libraries/Core/Routers/Serializers/BundleSerializer.cs index 7cf66025..08568d62 100644 --- a/Libraries/Core/Routers/Serializers/BundleSerializer.cs +++ b/Libraries/Core/Routers/Serializers/BundleSerializer.cs @@ -29,7 +29,8 @@ public class BundleSerializer( return; } - httpFileUtil.SendFile(resp, $"{bundle.ModPath}/bundles/{bundle.FileName}"); + var bundlePath = Path.Join(bundle.ModPath, "/bundles/", bundle.FileName); + httpFileUtil.SendFile(resp, bundlePath); } public bool CanHandle(string route) diff --git a/Libraries/Core/Services/BundleHashCacheService.cs b/Libraries/Core/Services/BundleHashCacheService.cs index b87201ff..762b9eb0 100644 --- a/Libraries/Core/Services/BundleHashCacheService.cs +++ b/Libraries/Core/Services/BundleHashCacheService.cs @@ -14,7 +14,6 @@ namespace Core.Services protected readonly Dictionary _bundleHashes = new Dictionary(); protected const string _bundleHashCachePath = "./user/cache/"; protected const string _cacheName = "bundleHashCache.json"; - private readonly string _currentDirectory = Directory.GetCurrentDirectory(); public BundleHashCacheService( ISptLogger logger, @@ -52,27 +51,25 @@ namespace Core.Services _logger.Debug($"Bundle: {bundlePath} hash stored in: ${_bundleHashCachePath}"); } - public bool CalculateAndMatchHash(string relativeBundlePath) + public bool CalculateAndMatchHash(string BundlePath) { - var absolutePath = Path.Join(_currentDirectory, relativeBundlePath); - return MatchWithStoredHash(relativeBundlePath, CalculateHash(absolutePath)); + return MatchWithStoredHash(BundlePath, CalculateHash(BundlePath)); } - public void CalculateAndStoreHash(string relativeBundlePath) + public void CalculateAndStoreHash(string BundlePath) { - var absolutePath = Path.Join(_currentDirectory, relativeBundlePath); - StoreValue(relativeBundlePath, CalculateHash(absolutePath)); + StoreValue(BundlePath, CalculateHash(BundlePath)); } - public uint CalculateHash(string absoluteBundlePath) + public uint CalculateHash(string BundlePath) { - var fileData = _fileUtil.ReadFile(absoluteBundlePath); + var fileData = _fileUtil.ReadFile(BundlePath); return _hashUtil.GenerateCrc32ForData(fileData); } - public bool MatchWithStoredHash(string relativeBundlePath, uint hash) + public bool MatchWithStoredHash(string BundlePath, uint hash) { - return GetStoredValue(relativeBundlePath) == hash; + return GetStoredValue(BundlePath) == hash; } } } diff --git a/Libraries/Core/Utils/HttpFileUtil.cs b/Libraries/Core/Utils/HttpFileUtil.cs index a4ecf8ef..bf579d93 100644 --- a/Libraries/Core/Utils/HttpFileUtil.cs +++ b/Libraries/Core/Utils/HttpFileUtil.cs @@ -20,7 +20,5 @@ public class HttpFileUtil var type = string.IsNullOrWhiteSpace(mimePath) ? _httpServerHelper.GetMimeText("txt") : mimePath; resp.Headers.Append("Content-Type", type); resp.SendFileAsync(filePath, CancellationToken.None).Wait(); - // maybe the above is correct? - // await pipeline(fs.createReadStream(filePath), resp); } } diff --git a/ModExamples/12Bundle/Bundle.cs b/ModExamples/12Bundle/Bundle.cs index c221e554..25dbb6ab 100644 --- a/ModExamples/12Bundle/Bundle.cs +++ b/ModExamples/12Bundle/Bundle.cs @@ -10,7 +10,6 @@ public class Bundle : IPostDBLoadMod { private readonly BundleLoader _bundleLoader; - public Bundle( BundleLoader bundleLoader) { @@ -19,6 +18,11 @@ public class Bundle : IPostDBLoadMod public void PostDBLoad() { - _bundleLoader.AddBundles("/user/mods/Mod3"); + // must be a relative path. + // for example "./user/mods/Mod3" + // . being the server.exe or root directory + // follow all the way to your mods folder name + // you will be only changing from "./user/mods/" onwards + _bundleLoader.AddBundles("./user/mods/Mod3"); } }