From 289cbe36a8cb687535e10d962da0422816f923e5 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Mon, 22 Jul 2024 19:49:23 +0800 Subject: [PATCH] =?UTF-8?q?misc:=20=E5=A4=84=E7=90=86gosu=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=AF=B7=E6=B1=82=E6=97=B6=E7=9B=B4=E6=8E=A5=E4=BB=8E?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=AF=BB=E6=89=BE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Feature/Gosumemory/GosuCompatInjector.cs | 20 ++--- .../Gosumemory/Tracker/BeatmapTracker.cs | 20 ----- .../Feature/Gosumemory/Web/GosuSession.cs | 67 ++++++++++++++++- .../Feature/Gosumemory/Web/WebSocketLoader.cs | 73 +++++++++++-------- 4 files changed, 114 insertions(+), 66 deletions(-) diff --git a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/GosuCompatInjector.cs b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/GosuCompatInjector.cs index 9c6d957..3c181ef 100644 --- a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/GosuCompatInjector.cs +++ b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/GosuCompatInjector.cs @@ -67,7 +67,7 @@ private void setupGosuStatics(WebSocketLoader.GosuServer server) Logging.Log("Setting up statics..."); var staticsStorage = globalStorage.GetStorageForDirectory("gosu_statics"); - string? staticPath = staticsStorage.GetFullPath("."); + string? staticPath = staticsStorage.GetFullPath(".", true); if (staticPath == null || !Directory.Exists(staticPath)) { @@ -75,22 +75,16 @@ private void setupGosuStatics(WebSocketLoader.GosuServer server) return; } - server.SetStorage(globalStorage); + var cacheStorage = globalStorage.GetStorageForDirectory("gosu_caches"); + string? cachePath = cacheStorage.GetFullPath(".", true); - DirectoryInfo dirInfo = new DirectoryInfo(staticPath); - - if (!dirInfo.Exists) + if (cachePath == null || !Directory.Exists(cachePath)) { - var newDirInfo = Directory.CreateDirectory(staticPath); - - if (!newDirInfo.Exists) - { - Logging.Log("Unable to create statics directory, skipping..."); - return; - } + Logging.Log("Cache directory is null or non-exist!"); + return; } - server.AddStaticContent(staticPath); + server.SetStorage(globalStorage, cacheStorage); Logging.Log("Done setting up static content!"); } diff --git a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Tracker/BeatmapTracker.cs b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Tracker/BeatmapTracker.cs index 1358016..c7d5d1b 100644 --- a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Tracker/BeatmapTracker.cs +++ b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Tracker/BeatmapTracker.cs @@ -231,9 +231,6 @@ private void updateFileSupporters(BeatmapSetInfo setInfo, WorkingBeatmap beatmap beatmap.BeatmapInfo.BeatmapSet?.Metadata.AudioFile ?? "", audioFileDesti).ConfigureAwait(false); - // Await for statics refresh - await Task.Run(updateStatics).ConfigureAwait(false); - // Update! this.Schedule(() => { @@ -280,9 +277,6 @@ private void clearCache(string cacheRoot) foreach (var fileInfo in dirInfo.GetFiles()) fileInfo.Delete(); - - var server = Hub.GetWsLoader()?.Server; - server?.ClearStaticContent(); } catch (Exception e) { @@ -295,18 +289,4 @@ private Guid genGuidFrom(string str) byte[] hash = MD5.HashData(Encoding.Unicode.GetBytes(str)); return new Guid(hash); } - - private void updateStatics() - { - try - { - var server = Hub.GetWsLoader()?.Server; - server?.ClearStaticContent(); - server?.AddStaticContent(staticRoot(), "/Songs"); - } - catch (Exception e) - { - Logging.LogError(e, "Unable to add cache"); - } - } } diff --git a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/GosuSession.cs b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/GosuSession.cs index dc99d85..6a55f1a 100644 --- a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/GosuSession.cs +++ b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/GosuSession.cs @@ -27,6 +27,8 @@ protected override void OnReceivedRequest(HttpRequest request) if (!path.EndsWith("/ws", StringComparison.Ordinal) && !path.EndsWith("/json", StringComparison.Ordinal)) { + Logging.Log("Received FILE? request " + request.Url); + HttpResponse response = new HttpResponse(); response.SetBegin(200); @@ -66,17 +68,70 @@ protected override void OnReceivedRequest(HttpRequest request) string storagePath = Path.Combine("gosu_statics", urlPath); // 目标存储的绝对位置 - string targetLocalStorage = storage.GetFullPath(storagePath); + string targetFilePath = storage.GetFullPath(storagePath); + + Logging.Log("URLPath is " + urlPath); + + // 处理Songs + if (urlPath.StartsWith("Songs", StringComparison.Ordinal)) + { + string[] split = urlPath.Split("/", 2); + var fileResponse = new HttpResponse(); + + if (split.Length < 2) + { + Logging.Log("Illegal argument, not processing..."); + fileResponse.SetBegin(400); + this.SendResponse(fileResponse); + + return; + } + + string fileName = split[1].Split("?", 2)[0]; + + byte[] content = gosuServer.FindStaticOrAsset(fileName) ?? new byte[] { }; + + if (content.Length == 0) + { + response.SetBegin(404); + this.SendResponse(response); + + return; + } + + fileResponse.SetBegin(200); + response.SetHeader("Cache-Control", stringAndClear) + .SetHeader("Access-Control-Allow-Origin", "*"); + response.SetBody(content); + + this.SendResponse(response); + return; + } // 如果要访问文件, 那么不要进行处理 - if (File.Exists(targetLocalStorage)) + if (File.Exists(targetFilePath)) { - base.OnReceivedRequest(request); + var fileResponse = new HttpResponse(); + byte[] content = gosuServer.FindStaticOrAsset(targetFilePath) ?? new byte[] { }; + + if (content.Length == 0) + { + fileResponse.SetBegin(404); + this.SendResponse(response); + return; + } + + fileResponse.SetBegin(200); + response.SetHeader("Cache-Control", stringAndClear) + .SetHeader("Access-Control-Allow-Origin", "*"); + response.SetBody(File.ReadAllBytes(targetFilePath)); + + this.SendResponse(response); return; } // 只当目录存在时遍历其中的内容 - if (Path.Exists(targetLocalStorage)) + if (Path.Exists(targetFilePath)) { try { @@ -107,9 +162,13 @@ protected override void OnReceivedRequest(HttpRequest request) response.SetBody(htmlCode); this.SendResponse(response); + + Logging.Log("Sending " + response.Body); } else { + Logging.Log("Received WS OR JSON request " + request.Url); + base.OnReceivedRequest(request); } } diff --git a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/WebSocketLoader.cs b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/WebSocketLoader.cs index 5201417..d28ffec 100644 --- a/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/WebSocketLoader.cs +++ b/osu.Game.Rulesets.IGPlayer/Feature/Gosumemory/Web/WebSocketLoader.cs @@ -1,8 +1,8 @@ using System; +using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Sockets; -using System.Runtime.CompilerServices; using NetCoreServer; using osu.Framework.Allocation; using osu.Framework.Graphics.Containers; @@ -106,6 +106,35 @@ protected override void Dispose(bool isDisposing) public GosuServer? Server; + public partial class AssetManager + { + private readonly List storages = new List(); + + public AssetManager(List storages) + { + SetStorages(storages); + } + + public void SetStorages(List newList) + { + storages.Clear(); + storages.AddRange(newList); + } + + public byte[] FindAsset(string relativePath) + { + foreach (var storage in storages) + { + if (!storage.Exists(relativePath)) continue; + + string fullPath = storage.GetFullPath(relativePath); + return File.ReadAllBytes(fullPath); + } + + return []; + } + } + public partial class GosuServer : WsServer { public GosuServer(IPAddress address, int port) @@ -113,16 +142,20 @@ public GosuServer(IPAddress address, int port) { } - private Storage? storage; + public readonly AssetManager AssetManager = new AssetManager(new List()); - public void SetStorage(Storage storage) + private Storage staticsStorage; + + public void SetStorage(Storage statics, Storage caches) { - this.storage = storage; + this.staticsStorage = statics; + + AssetManager.SetStorages([statics, caches]); } public Storage? GetStorage() { - return this.storage; + return this.staticsStorage; } protected override TcpSession CreateSession() { return new GosuSession(this); } @@ -138,32 +171,14 @@ public void AddCustomHandler(string path, string urlPath, FileCache.InsertHandle this.Cache.InsertPath(path, urlPath, "*.*", timeout, handler); } - /** - * From decompiled HttpServer#AddStaticContent(...) - */ - public new void AddStaticContent(string path, string prefix = "/", string filter = "*.*", TimeSpan? timeout = null) + public byte[]? FindStaticOrAsset(string path) { - timeout ??= TimeSpan.FromHours(1.0); - - this.Cache.InsertPath(path, prefix, filter, timeout.Value, handler); - - static bool handler(FileCache cache, string key, byte[] value, TimeSpan timespan) - { - var response = new HttpResponse(); - response.SetBegin(200); - response.SetContentType(Path.GetExtension(key)); - - var interpolatedStringHandler = new DefaultInterpolatedStringHandler(8, 1); - interpolatedStringHandler.AppendLiteral("max-age="); - interpolatedStringHandler.AppendFormatted(timespan.Seconds); - - string stringAndClear = interpolatedStringHandler.ToStringAndClear(); - response.SetHeader("Cache-Control", stringAndClear); - response.SetHeader("Access-Control-Allow-Origin", "*"); - response.SetBody(value); + return this.AssetManager.FindAsset(path); + } - return cache.Add(key, response.Cache.Data, timespan); - } + public new void AddStaticContent(string path, string prefix = "/", string filter = "*.*", TimeSpan? timeout = null) + { + throw new Exception("Deprecated operation"); } } }