Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement better logic for ApplyStateChange in skin editor #23344

Merged
merged 12 commits into from
May 3, 2023
Prev Previous commit
Next Next commit
Trim comments
Leaving only the ones that add anything useful and do not restate the
code verbatim.
  • Loading branch information
bdach committed May 3, 2023
commit 1d4d31e35c8b85eb5104734a25b971ec8c827ad3
8 changes: 3 additions & 5 deletions osu.Game/Overlays/SkinEditor/SkinEditorChangeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override void ApplyStateChange(byte[] previousState, byte[] newState)
SerialisedDrawableInfo[] skinnableInfos = deserializedContent.ToArray();
ISerialisableDrawable[] targetComponents = firstTarget.Components.ToArray();

// Store components based on type for later lookup
// Store components based on type for later reuse
var componentsPerTypeLookup = new Dictionary<Type, Queue<Drawable>>();

foreach (ISerialisableDrawable component in targetComponents)
Expand All @@ -73,7 +73,6 @@ protected override void ApplyStateChange(byte[] previousState, byte[] newState)
componentsOfSameType.Enqueue((Drawable)component);
}

// Remove all components
for (int i = targetComponents.Length - 1; i >= 0; i--)
firstTarget.Remove(targetComponents[i], false);

Expand All @@ -87,23 +86,22 @@ protected override void ApplyStateChange(byte[] previousState, byte[] newState)
continue;
}

// Wherever possible, attempt to reuse existing component instances.
if (componentsOfSameType.TryDequeue(out Drawable? component))
{
// Re-use unused component
component.ApplySerialisedInfo(skinnableInfo);
}
else
{
// Create new one
component = skinnableInfo.CreateInstance();
}

firstTarget.Add((ISerialisableDrawable)component);
}

// Dispose components which were not reused.
foreach ((Type _, Queue<Drawable> typeComponents) in componentsPerTypeLookup)
{
// Dispose extra components
foreach (var component in typeComponents)
component.Dispose();
}
Expand Down