fix bundle sending

This commit is contained in:
CWX
2025-02-10 20:38:12 +00:00
parent aabd9cef6c
commit 7d9e7f0727
6 changed files with 43 additions and 22 deletions
+2 -2
View File
@@ -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);
}
/**
+25 -4
View File
@@ -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<string> 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<BundleManifest>(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;
}
}
@@ -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)
@@ -14,7 +14,6 @@ namespace Core.Services
protected readonly Dictionary<string, uint> _bundleHashes = new Dictionary<string, uint>();
protected const string _bundleHashCachePath = "./user/cache/";
protected const string _cacheName = "bundleHashCache.json";
private readonly string _currentDirectory = Directory.GetCurrentDirectory();
public BundleHashCacheService(
ISptLogger<BundleHashCacheService> 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;
}
}
}
-2
View File
@@ -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);
}
}
+6 -2
View File
@@ -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");
}
}