ds5678 fd0f5fd014 Improve offline support
* Add a command line option for supplying local web files
* Consolodate references into a single file
* Resolves #1486
2024-09-07 09:12:15 -07:00

49 lines
1.2 KiB
C#

using System.Collections.Concurrent;
using System.Text.RegularExpressions;
namespace AssetRipper.GUI.Web;
public static partial class StaticContentLoader
{
private const string Prefix = "AssetRipper.GUI.Web.StaticContent.";
public static ConcurrentDictionary<string, byte[]> Cache { get; } = new();
public static async ValueTask<byte[]> Load(string path)
{
if (Cache.TryGetValue(path, out byte[]? result))
{
return result;
}
else
{
return await LoadInternal(path);
}
}
private static async Task<byte[]> LoadInternal(string path)
{
using Stream stream = typeof(StaticContentLoader).Assembly.GetManifestResourceStream(ToResourceName(path))
?? throw new NullReferenceException($"Could not load static file: {path}");
using MemoryStream memoryStream = new((int)stream.Length);
await stream.CopyToAsync(memoryStream);
byte[] result = memoryStream.ToArray();
Cache.TryAdd(path, result);
return result;
}
private static string ToResourceName(string path)
{
ArgumentException.ThrowIfNullOrEmpty(path);
string realPath = path[0] is '/' or '\\' ? path[1..] : path;
return Prefix + DirectorySeparator().Replace(realPath, ".");
}
[GeneratedRegex(@"[/\\]")]
private static partial Regex DirectorySeparator();
}