Move `StoragePath` implementation to an extension method

This commit is contained in:
Dean Herbert 2021-11-19 16:07:55 +09:00
parent c6c6b04f2b
commit 59e763467f
23 changed files with 58 additions and 37 deletions

View File

@ -16,6 +16,7 @@
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Notifications;
@ -363,15 +364,15 @@ public async Task TestImportCorruptThenImport()
var files = osu.Dependencies.Get<FileStore>();
long originalLength;
using (var stream = files.Storage.GetStream(firstFile.FileInfo.StoragePath))
using (var stream = files.Storage.GetStream(firstFile.FileInfo.GetStoragePath()))
originalLength = stream.Length;
using (var stream = files.Storage.GetStream(firstFile.FileInfo.StoragePath, FileAccess.Write, FileMode.Create))
using (var stream = files.Storage.GetStream(firstFile.FileInfo.GetStoragePath(), FileAccess.Write, FileMode.Create))
stream.WriteByte(0);
var importedSecondTime = await LoadOszIntoOsu(osu);
using (var stream = files.Storage.GetStream(firstFile.FileInfo.StoragePath))
using (var stream = files.Storage.GetStream(firstFile.FileInfo.GetStoragePath()))
Assert.AreEqual(stream.Length, originalLength, "Corruption was not fixed on second import");
// check the newly "imported" beatmap is actually just the restored previous import. since it matches hash.

View File

@ -14,6 +14,7 @@
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO.Archives;
using osu.Game.Models;
using osu.Game.Overlays.Notifications;
@ -348,15 +349,15 @@ public void TestImportCorruptThenImport()
var firstFile = imported.Files.First();
long originalLength;
using (var stream = storage.GetStream(firstFile.File.StoragePath))
using (var stream = storage.GetStream(firstFile.File.GetStoragePath()))
originalLength = stream.Length;
using (var stream = storage.GetStream(firstFile.File.StoragePath, FileAccess.Write, FileMode.Create))
using (var stream = storage.GetStream(firstFile.File.GetStoragePath(), FileAccess.Write, FileMode.Create))
stream.WriteByte(0);
var importedSecondTime = await LoadOszIntoStore(importer, realmFactory.Context);
using (var stream = storage.GetStream(firstFile.File.StoragePath))
using (var stream = storage.GetStream(firstFile.File.GetStoragePath()))
Assert.AreEqual(stream.Length, originalLength, "Corruption was not fixed on second import");
// check the newly "imported" beatmap is actually just the restored previous import. since it matches hash.

View File

@ -6,6 +6,7 @@
using System.Linq;
using NUnit.Framework;
using osu.Framework.Logging;
using osu.Game.Extensions;
using osu.Game.Models;
using osu.Game.Stores;
@ -28,7 +29,7 @@ public void TestImportFile()
realm.Write(() => files.Add(testData, realm));
Assert.True(files.Storage.Exists("0/05/054edec1d0211f624fed0cbca9d4f9400b0e491c43742af2c5b0abebf0c990d8"));
Assert.True(files.Storage.Exists(realm.All<RealmFile>().First().StoragePath));
Assert.True(files.Storage.Exists(realm.All<RealmFile>().First().GetStoragePath()));
});
}
@ -74,7 +75,7 @@ public void TestDontPurgeReferenced()
Logger.Log($"Import complete at {timer.ElapsedMilliseconds}");
string path = file.StoragePath;
string path = file.GetStoragePath();
Assert.True(realm.All<RealmFile>().Any());
Assert.True(files.Storage.Exists(path));
@ -98,7 +99,7 @@ public void TestPurgeUnreferenced()
var file = realm.Write(() => files.Add(new MemoryStream(new byte[] { 0, 1, 2, 3 }), realm));
string path = file.StoragePath;
string path = file.GetStoragePath();
Assert.True(realm.All<RealmFile>().Any());
Assert.True(files.Storage.Exists(path));

View File

@ -18,6 +18,7 @@
using osu.Framework.Testing;
using osu.Game.Beatmaps.Formats;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO;
using osu.Game.IO.Archives;
using osu.Game.Rulesets;
@ -384,7 +385,7 @@ private List<BeatmapInfo> createBeatmapDifficulties(List<BeatmapSetFileInfo> fil
foreach (var file in files.Where(f => f.Filename.EndsWith(".osu", StringComparison.OrdinalIgnoreCase)))
{
using (var raw = Files.Store.GetStream(file.FileInfo.StoragePath))
using (var raw = Files.Store.GetStream(file.FileInfo.GetStoragePath()))
using (var ms = new MemoryStream()) // we need a memory stream so we can seek
using (var sr = new LineBufferedReader(ms))
{

View File

@ -8,6 +8,7 @@
using JetBrains.Annotations;
using osu.Framework.Testing;
using osu.Game.Database;
using osu.Game.Extensions;
namespace osu.Game.Beatmaps
{
@ -61,7 +62,7 @@ public int? OnlineID
/// The path returned is relative to the user file storage.
/// </summary>
/// <param name="filename">The name of the file to get the storage path of.</param>
public string GetPathForFile(string filename) => Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.FileInfo.StoragePath;
public string GetPathForFile(string filename) => Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.FileInfo.GetStoragePath();
public override string ToString() => Metadata?.ToString() ?? base.ToString();

View File

@ -325,7 +325,7 @@ protected virtual string ComputeHash(TModel item)
foreach (TFileModel file in hashableFiles)
{
using (Stream s = Files.Store.GetStream(file.FileInfo.StoragePath))
using (Stream s = Files.Store.GetStream(file.FileInfo.GetStoragePath()))
s.CopyTo(hashable);
}
@ -480,7 +480,7 @@ public virtual void ExportModelTo(TModel model, Stream outputStream)
using (var archive = ZipArchive.Create())
{
foreach (var file in model.Files)
archive.AddEntry(file.Filename, Files.Storage.GetStream(file.FileInfo.StoragePath));
archive.AddEntry(file.Filename, Files.Storage.GetStream(file.FileInfo.GetStoragePath()));
archive.SaveTo(outputStream);
}

View File

@ -1,8 +1,10 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.IO;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.IO;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Rulesets;
using osu.Game.Scoring;
@ -14,6 +16,13 @@ namespace osu.Game.Extensions
{
public static class ModelExtensions
{
/// <summary>
/// Get the relative path in osu! storage for this file.
/// </summary>
/// <param name="fileInfo">The file info.</param>
/// <returns>A relative file path.</returns>
public static string GetStoragePath(this IFileInfo fileInfo) => Path.Combine(fileInfo.Hash.Remove(1), fileInfo.Hash.Remove(2), fileInfo.Hash);
/// <summary>
/// Returns a user-facing string representing the <paramref name="model"/>.
/// </summary>

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.IO;
using osu.Game.Database;
namespace osu.Game.IO
@ -12,8 +11,6 @@ public class FileInfo : IHasPrimaryKey, IFileInfo
public string Hash { get; set; }
public string StoragePath => Path.Combine(Hash.Remove(1), Hash.Remove(2), Hash);
public int ReferenceCount { get; set; }
}
}

View File

@ -12,6 +12,7 @@
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Database;
using osu.Game.Extensions;
namespace osu.Game.IO
{
@ -47,7 +48,7 @@ public FileInfo Add(Stream data, bool reference = true)
var info = existing ?? new FileInfo { Hash = hash };
string path = info.StoragePath;
string path = info.GetStoragePath();
// we may be re-adding a file to fix missing store entries.
bool requiresCopy = !Storage.Exists(path);
@ -120,7 +121,7 @@ public override void Cleanup()
{
try
{
Storage.Delete(f.StoragePath);
Storage.Delete(f.GetStoragePath());
context.FileInfo.Remove(f);
}
catch (Exception e)

View File

@ -52,7 +52,7 @@ public class RealmBeatmapSet : RealmObject, IHasGuidPrimaryKey, IHasRealmFiles,
/// The path returned is relative to the user file storage.
/// </summary>
/// <param name="filename">The name of the file to get the storage path of.</param>
public string? GetPathForFile(string filename) => Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.File.StoragePath;
public string? GetPathForFile(string filename) => Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.File.GetStoragePath();
public bool Equals(RealmBeatmapSet? other)
{

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.IO;
using osu.Framework.Testing;
using osu.Game.IO;
using Realms;
@ -16,7 +15,5 @@ public class RealmFile : RealmObject, IFileInfo
{
[PrimaryKey]
public string Hash { get; set; } = string.Empty;
public string StoragePath => Path.Combine(Hash.Remove(1), Hash.Remove(2), Hash);
}
}

View File

@ -6,6 +6,7 @@
using System.Linq;
using ManagedBass;
using osu.Framework.Audio.Callbacks;
using osu.Game.Extensions;
using osu.Game.Rulesets.Edit.Checks.Components;
namespace osu.Game.Rulesets.Edit.Checks
@ -31,7 +32,7 @@ public IEnumerable<Issue> Run(BeatmapVerifierContext context)
foreach (var file in beatmapSet.Files)
{
using (Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.StoragePath))
using (Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.GetStoragePath()))
{
if (data == null)
continue;

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using osu.Game.Extensions;
using osu.Game.Rulesets.Edit.Checks.Components;
namespace osu.Game.Rulesets.Edit.Checks
@ -22,7 +23,7 @@ public IEnumerable<Issue> Run(BeatmapVerifierContext context)
foreach (var file in beatmapSet.Files)
{
using (Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.StoragePath))
using (Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.GetStoragePath()))
{
if (data?.Length == 0)
yield return new IssueTemplateZeroBytes(this).Create(file.Filename);

View File

@ -5,6 +5,7 @@
using System.Linq;
using osu.Framework.IO.Stores;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
using osu.Game.Rulesets;
using osu.Game.Scoring.Legacy;
@ -16,7 +17,7 @@ public LegacyDatabasedScore(ScoreInfo score, RulesetStore rulesets, BeatmapManag
{
ScoreInfo = score;
string replayFilename = score.Files.FirstOrDefault(f => f.Filename.EndsWith(".osr", StringComparison.InvariantCultureIgnoreCase))?.FileInfo.StoragePath;
string replayFilename = score.Files.FirstOrDefault(f => f.Filename.EndsWith(".osr", StringComparison.InvariantCultureIgnoreCase))?.FileInfo.GetStoragePath();
if (replayFilename == null)
return;

View File

@ -13,6 +13,7 @@
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO.Archives;
using osu.Game.Rulesets;
using osu.Game.Scoring.Legacy;
@ -77,7 +78,7 @@ public override void ExportModelTo(ScoreInfo model, Stream outputStream)
if (file == null)
return;
using (var inputStream = Files.Storage.GetStream(file.FileInfo.StoragePath))
using (var inputStream = Files.Storage.GetStream(file.FileInfo.GetStoragePath()))
inputStream.CopyTo(outputStream);
}

View File

@ -7,6 +7,7 @@
using osu.Framework.Extensions;
using osu.Framework.IO.Stores;
using osu.Game.Database;
using osu.Game.Extensions;
namespace osu.Game.Skinning
{
@ -35,7 +36,7 @@ protected override IEnumerable<string> GetFilenames(string name)
}
private string getPathForFile(string filename) =>
source.Files.Find(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.FileInfo.StoragePath;
source.Files.Find(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.FileInfo.GetStoragePath();
public override IEnumerable<string> GetAvailableResources() => source.Files.Select(f => f.Filename);
}

View File

@ -15,6 +15,7 @@
using osu.Framework.Graphics.Textures;
using osu.Framework.Logging;
using osu.Game.Audio;
using osu.Game.Extensions;
using osu.Game.IO;
using osu.Game.Screens.Play.HUD;
@ -63,7 +64,7 @@ protected Skin(SkinInfo skin, IStorageResourceProvider resources, [CanBeNull] St
if (fileInfo == null)
continue;
byte[] bytes = resources?.Files.Get(fileInfo.FileInfo.StoragePath);
byte[] bytes = resources?.Files.Get(fileInfo.FileInfo.GetStoragePath());
if (bytes == null)
continue;
@ -93,7 +94,7 @@ protected virtual void ParseConfigurationStream(Stream stream)
private Stream getConfigurationStream()
{
string path = SkinInfo.Files.SingleOrDefault(f => f.Filename.Equals(@"skin.ini", StringComparison.OrdinalIgnoreCase))?.FileInfo.StoragePath;
string path = SkinInfo.Files.SingleOrDefault(f => f.Filename.Equals(@"skin.ini", StringComparison.OrdinalIgnoreCase))?.FileInfo.GetStoragePath();
if (string.IsNullOrEmpty(path))
return null;

View File

@ -215,7 +215,7 @@ private void updateSkinIniMetadata(SkinInfo item)
{
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))
{
using (var existingStream = Files.Storage.GetStream(existingFile.FileInfo.StoragePath))
using (var existingStream = Files.Storage.GetStream(existingFile.FileInfo.GetStoragePath()))
using (var sr = new StreamReader(existingStream))
{
string line;

View File

@ -18,6 +18,7 @@
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO;
using osu.Game.IO.Archives;
using osu.Game.Models;
@ -197,7 +198,7 @@ private List<RealmBeatmap> createBeatmapDifficulties(IList<RealmNamedFileUsage>
foreach (var file in files.Where(f => f.Filename.EndsWith(".osu", StringComparison.OrdinalIgnoreCase)))
{
using (var memoryStream = new MemoryStream(Files.Store.Get(file.File.StoragePath))) // we need a memory stream so we can seek
using (var memoryStream = new MemoryStream(Files.Store.Get(file.File.GetStoragePath()))) // we need a memory stream so we can seek
{
IBeatmap decoded;
using (var lineReader = new LineBufferedReader(memoryStream, true))

View File

@ -15,6 +15,7 @@
using osu.Framework.Platform;
using osu.Framework.Threading;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO.Archives;
using osu.Game.Models;
using osu.Game.Overlays.Notifications;
@ -305,7 +306,7 @@ protected virtual string ComputeHash(TModel item, ArchiveReader? reader = null)
foreach (RealmNamedFileUsage file in item.Files.Where(f => HashableFileTypes.Any(ext => f.Filename.EndsWith(ext, StringComparison.OrdinalIgnoreCase))).OrderBy(f => f.Filename))
{
using (Stream s = Files.Store.GetStream(file.File.StoragePath))
using (Stream s = Files.Store.GetStream(file.File.GetStoragePath()))
s.CopyTo(hashable);
}

View File

@ -10,6 +10,7 @@
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.Models;
using Realms;
@ -64,7 +65,7 @@ private void copyToStore(RealmFile file, Stream data)
{
data.Seek(0, SeekOrigin.Begin);
using (var output = Storage.GetStream(file.StoragePath, FileAccess.Write))
using (var output = Storage.GetStream(file.GetStoragePath(), FileAccess.Write))
data.CopyTo(output);
data.Seek(0, SeekOrigin.Begin);
@ -72,7 +73,7 @@ private void copyToStore(RealmFile file, Stream data)
private bool checkFileExistsAndMatchesHash(RealmFile file)
{
string path = file.StoragePath;
string path = file.GetStoragePath();
// we may be re-adding a file to fix missing store entries.
if (!Storage.Exists(path))
@ -100,7 +101,7 @@ public void Cleanup()
try
{
Storage.Delete(file.StoragePath);
Storage.Delete(file.GetStoragePath());
realm.Remove(file);
}
catch (Exception e)

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -9,6 +10,7 @@
using osu.Framework.Graphics.Textures;
using osu.Framework.Graphics.Video;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
namespace osu.Game.Storyboards.Drawables
{
@ -29,7 +31,7 @@ public DrawableStoryboardVideo(StoryboardVideo video)
[BackgroundDependencyLoader(true)]
private void load(IBindable<WorkingBeatmap> beatmap, TextureStore textureStore)
{
string path = beatmap.Value.BeatmapSetInfo?.Files.Find(f => f.Filename.Equals(Video.Path, StringComparison.OrdinalIgnoreCase))?.FileInfo.StoragePath;
string path = beatmap.Value.BeatmapSetInfo?.Files.FirstOrDefault(f => f.Filename.Equals(Video.Path, StringComparison.OrdinalIgnoreCase))?.File.GetStoragePath();
if (path == null)
return;

View File

@ -8,6 +8,7 @@
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
using osu.Game.Skinning;
using osu.Game.Storyboards.Drawables;
@ -95,7 +96,7 @@ public DrawableStoryboard CreateDrawable(IWorkingBeatmap working = null) =>
public Drawable CreateSpriteFromResourcePath(string path, TextureStore textureStore)
{
Drawable drawable = null;
string storyboardPath = BeatmapInfo.BeatmapSet?.Files.Find(f => f.Filename.Equals(path, StringComparison.OrdinalIgnoreCase))?.FileInfo.StoragePath;
string storyboardPath = BeatmapInfo.BeatmapSet?.Files.Find(f => f.Filename.Equals(path, StringComparison.OrdinalIgnoreCase))?.FileInfo.GetStoragePath();
if (!string.IsNullOrEmpty(storyboardPath))
drawable = new Sprite { Texture = textureStore.Get(storyboardPath) };