Skip to content

Commit

Permalink
enhance: 异步复制文件
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Feb 25, 2024
1 parent fca33aa commit 10dcfe7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ private string storageRoot()
return storage.GetFullPath(".");
}

public Task<string?> ExportSingleTask(BeatmapSetInfo setInfo, string targetFile, string? desti)
{
string? val = ExportFileSingle(setInfo, targetFile, desti);
return Task.FromResult(val);
}

public string? ExportFileSingle(BeatmapSetInfo setInfo, string targetFile, string? desti)
{
try
Expand Down Expand Up @@ -89,12 +95,13 @@ private string storageRoot()
if (!parent.Exists)
parent.Create();

Logger.Log($"Trying copy {path} to {desti}");
//Logger.Log($"Trying copy {path} to {desti}");

if (File.Exists(desti))
{
File.Delete(desti);
Logger.Log("File already exists! Replacing...");
//File.Delete(desti);
//Logger.Log("File already exists! Skipping...");
return desti;
}

File.Copy(path, desti);
Expand All @@ -111,16 +118,7 @@ private string getRealmedFileName(string hash)
{
string str = Path.Combine(hash[..1], hash[..2], hash);

Logger.Log("File is located at " + str);
//Logger.Log("File is located at " + str);
return str;
}

private void buildTree()
{
var allBeatmaps = realm.All<BeatmapSetInfo>();

foreach (BeatmapSetInfo setInfo in allBeatmaps)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Database;
Expand Down Expand Up @@ -43,7 +42,19 @@ private void load(Bindable<WorkingBeatmap> globalBeatmap)

private string staticRoot()
{
return storage.GetFullPath("static", true);
string? path = storage.GetFullPath("gosu_statics", true);

try
{
if (!Path.Exists(path))
Directory.CreateDirectory(path);
}
catch (Exception e)
{
Logging.LogError(e, "Unable to create statics directory");
}

return path;
}

protected override void LoadComplete()
Expand Down Expand Up @@ -101,7 +112,7 @@ private void onBeatmapChanged(WorkingBeatmap newBeatmap)
{
Hub.GetDataRoot().UpdateBeatmap(newBeatmap);

Logger.Log($"~BACKGROUND IS {newBeatmap.Metadata.BackgroundFile}");
//Logger.Log($"~BACKGROUND IS {newBeatmap.Metadata.BackgroundFile}");
updateFileSupporters(newBeatmap.BeatmapSetInfo, newBeatmap);

this.onModsChanged(this.mods.Value);
Expand Down Expand Up @@ -132,30 +143,46 @@ private void updateFileSupporters(BeatmapSetInfo setInfo, WorkingBeatmap beatmap
if (directAccessor == null)
return;

string root = staticRoot();

if (directAccessor == null) return;
if (beatmap.Metadata.BackgroundFile == null)
{
var dataRoot = Hub.GetDataRoot();
string defaultVal = "_default.png";

string? final = directAccessor.ExportFileSingle(setInfo, beatmap.Metadata.BackgroundFile, $"{root}/cover_{beatmap.GetHashCode()}");
dataRoot.MenuValues.GosuBeatmapInfo.Path.BackgroundPath = defaultVal;
dataRoot.MenuValues.GosuBeatmapInfo.Path.BgPath = defaultVal;
return;
}

if (final == null) return;
string root = staticRoot();

Logger.Log("~~~PUSH TO GOSU!");
var dataRoot = Hub.GetDataRoot();
Task.Run(async () =>
{
string? final = await directAccessor.ExportSingleTask(setInfo, beatmap.Metadata.BackgroundFile, $"{root}/{beatmap.BeatmapSetInfo.OnlineID}_{beatmap.Metadata.BackgroundFile.GetHashCode()}")
.ConfigureAwait(false);

string boardcast = final.Replace(root, "").Replace("/", "");
Logger.Log("~~~BOARDCAST IS " + boardcast);
dataRoot.MenuValues.GosuBeatmapInfo.Path.BackgroundPath = boardcast;
dataRoot.MenuValues.GosuBeatmapInfo.Path.BgPath = boardcast;
if (final == null) return;

try
{
var server = Hub.GetWsLoader()?.Server;
server?.AddStaticContent(staticRoot(), "/Songs");
}
catch (Exception e)
{
Logging.LogError(e, "Unable to add cache");
}
this.Schedule(() =>
{
//Logger.Log("~~~PUSH TO GOSU!");
var dataRoot = Hub.GetDataRoot();

string boardcast = final.Replace(root, "").Replace("/", "");
//Logger.Log("~~~BOARDCAST IS " + boardcast);
dataRoot.MenuValues.GosuBeatmapInfo.Path.BackgroundPath = boardcast;
dataRoot.MenuValues.GosuBeatmapInfo.Path.BgPath = boardcast;

try
{
var server = Hub.GetWsLoader()?.Server;
server?.RemoveStaticContent(staticRoot());
server?.AddStaticContent(staticRoot(), "/Songs");
}
catch (Exception e)
{
Logging.LogError(e, "Unable to add cache");
}
});
});
}
}

0 comments on commit 10dcfe7

Please sign in to comment.