Skip to content

Commit

Permalink
Refactor skin migration to allow mutating multiple layouts at once
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Nov 25, 2024
1 parent 8dca69e commit f1b5686
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions osu.Game/Skinning/Skin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ protected Skin(SkinInfo skin, IStorageResourceProvider? resources, IResourceStor
Logger.Error(ex, "Failed to load skin configuration.");
}
}

if (layoutInfos.Values.Any())
{
int version = layoutInfos.Values.Min(l => l.Version);

for (int i = version + 1; i <= SkinLayoutInfo.LATEST_VERSION; i++)
applyMigration(i);

foreach (var layout in layoutInfos.Values)
{
layout.Version = SkinLayoutInfo.LATEST_VERSION;

foreach (var kvp in layout.DrawableInfo.ToArray())
{
foreach (var di in kvp.Value)
{
if (!isValidDrawable(di))
layout.DrawableInfo[kvp.Key] = kvp.Value.Where(i => i.Type != di.Type).ToArray();
}
}
}
}
}

protected virtual IResourceStore<TextureUpload> CreateTextureLoaderStore(IStorageResourceProvider resources, IResourceStore<byte[]> storage)
Expand Down Expand Up @@ -244,20 +266,6 @@ public void UpdateDrawableTarget(SkinnableContainer targetContainer)
Logger.Log($"Ferrying {deserializedContent.Count()} components in {target} to global section of new {nameof(SkinLayoutInfo)} format");
}

for (int i = layout.Version + 1; i <= SkinLayoutInfo.LATEST_VERSION; i++)
applyMigration(layout, target, i);

layout.Version = SkinLayoutInfo.LATEST_VERSION;

foreach (var kvp in layout.DrawableInfo.ToArray())
{
foreach (var di in kvp.Value)
{
if (!isValidDrawable(di))
layout.DrawableInfo[kvp.Key] = kvp.Value.Where(i => i.Type != di.Type).ToArray();
}
}

return layout;
}

Expand All @@ -275,16 +283,18 @@ private bool isValidDrawable(SerialisedDrawableInfo di)
return true;
}

private void applyMigration(SkinLayoutInfo layout, GlobalSkinnableContainers target, int version)
private void applyMigration(int version)
{
switch (version)
{
case 1:
{
// Combo counters were moved out of the global HUD components into per-ruleset.
// This is to allow some rulesets to customise further (ie. mania and catch moving the combo to within their play area).
if (target != GlobalSkinnableContainers.MainHUDComponents ||
!layout.TryGetDrawableInfo(null, out var globalHUDComponents) ||
if (!layoutInfos.TryGetValue(GlobalSkinnableContainers.MainHUDComponents, out var hudLayout))
break;

if (!hudLayout.TryGetDrawableInfo(null, out var globalHUDComponents) ||
resources == null)
break;

Expand All @@ -293,13 +303,13 @@ private void applyMigration(SkinLayoutInfo layout, GlobalSkinnableContainers tar
c.Type.Name == nameof(DefaultComboCounter) ||
c.Type.Name == nameof(ArgonComboCounter)).ToArray();

layout.Update(null, globalHUDComponents.Except(comboCounters).ToArray());
hudLayout.Update(null, globalHUDComponents.Except(comboCounters).ToArray());

resources.RealmAccess.Run(r =>
{
foreach (var ruleset in r.All<RulesetInfo>())
{
layout.Update(ruleset, layout.TryGetDrawableInfo(ruleset, out var rulesetHUDComponents)
hudLayout.Update(ruleset, hudLayout.TryGetDrawableInfo(ruleset, out var rulesetHUDComponents)
? rulesetHUDComponents.Concat(comboCounters).ToArray()
: comboCounters);
}
Expand Down

0 comments on commit f1b5686

Please sign in to comment.