1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:20:04 +08:00

Add basic method to migrate beatmaps to the new JSON format

This commit is contained in:
smoogipoo 2017-12-07 03:04:36 +09:00
parent ed5f7e5353
commit b6b26cfe25
3 changed files with 66 additions and 1 deletions

View File

@ -545,6 +545,31 @@ namespace osu.Game.Beatmaps
return beatmapSet;
}
public void UpdateContent(BeatmapInfo beatmapInfo, Stream newData)
{
// let's only allow one concurrent update at a time for now.
var context = createContext();
using (var transaction = context.BeginTransaction())
{
// create local stores so we can isolate and thread safely, and share a context/transaction.
var setInfo = beatmapInfo.BeatmapSet;
var existingSetFileInfo = setInfo.Files.First(f => f.FileInfo.Hash == beatmapInfo.Hash);
var existingFileInfo = existingSetFileInfo.FileInfo;
existingSetFileInfo.FileInfo = files.Add(newData);
files.Dereference(existingFileInfo);
beatmapInfo.Hash = newData.ComputeSHA2Hash();
beatmapInfo.MD5Hash = newData.ComputeMD5Hash();
context.Update(existingSetFileInfo);
context.Update(beatmapInfo);
context.SaveChanges(transaction);
}
}
/// <summary>
/// Returns a list of all usable <see cref="BeatmapSetInfo"/>s.
/// </summary>

View File

@ -16,7 +16,7 @@ namespace osu.Game.IO.Serialization
public static T Deserialize<T>(this string objString) => JsonConvert.DeserializeObject<T>(objString, CreateGlobalSettings());
public static void DeserializeInto<T>(this string objString, T target) => JsonConvert.PopulateObject(objString, target, CreateGlobalSettings());
public static void DeserializeInto<T>(this string objString, T target) => JsonConvert.DeserializeAnonymousType(objString, target, CreateGlobalSettings());
public static T DeepClone<T>(this T obj) where T : IJsonSerializable => Deserialize<T>(Serialize(obj));

View File

@ -1,12 +1,15 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Graphics.UserInterface;
using osu.Game.IO.Serialization;
namespace osu.Game.Overlays.Settings.Sections.Maintenance
{
@ -15,6 +18,7 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance
private TriangleButton importButton;
private TriangleButton deleteButton;
private TriangleButton restoreButton;
private TriangleButton migrateButton;
protected override string Header => "General";
@ -55,6 +59,42 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance
}).ContinueWith(t => Schedule(() => restoreButton.Enabled.Value = true));
}
},
migrateButton = new SettingsButton
{
Text = "Migrate all beatmaps to the new format",
Action = () =>
{
migrateButton.Enabled.Value = false;
Task.Run(() =>
{
var usableSets = beatmaps.GetAllUsableBeatmapSets();
foreach (var set in usableSets)
{
foreach (var beatmap in set.Beatmaps)
{
var working = beatmaps.GetWorkingBeatmap(beatmap);
using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms))
{
try
{
sw.Write(working.Beatmap.Serialize());
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
sw.Flush();
ms.Position = 0;
beatmaps.UpdateContent(beatmap, ms);
}
}
}
}).ContinueWith(t => Schedule(() => migrateButton.Enabled.Value = true));
}
}
};
}
}