// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using AutoMapper; using osu.Game.Beatmaps; using osu.Game.Configuration; using osu.Game.Input.Bindings; using osu.Game.IO; using osu.Game.Rulesets; using osu.Game.Scoring; using osu.Game.Skinning; using Realms; namespace osu.Game.Database { public static class RealmExtensions { private static readonly IMapper mapper = new MapperConfiguration(c => { c.ShouldMapField = fi => false; c.ShouldMapProperty = pi => pi.SetMethod != null && pi.SetMethod.IsPublic; c.CreateMap(); c.CreateMap(); c.CreateMap(); c.CreateMap(); c.CreateMap() .ForMember(s => s.Beatmaps, d => d.MapFrom(s => s.Beatmaps)) .ForMember(s => s.Files, d => d.MapFrom(s => s.Files)) .MaxDepth(2); c.CreateMap(); c.CreateMap(); c.CreateMap(); c.CreateMap(); c.CreateMap(); c.CreateMap(); }).CreateMapper(); /// /// Create a detached copy of the each item in the collection. /// /// A list of managed s to detach. /// The type of object. /// A list containing non-managed copies of provided items. public static List Detach(this IEnumerable items) where T : RealmObject { var list = new List(); foreach (var obj in items) list.Add(obj.Detach()); return list; } /// /// Create a detached copy of the each item in the list. /// /// The managed to detach. /// The type of object. /// A non-managed copy of provided item. Will return the provided item if already detached. public static T Detach(this T item) where T : RealmObject { if (!item.IsManaged) return item; return mapper.Map(item); } /// /// Wrap a managed instance of a realm object in a . /// /// The item to wrap. /// A factory to retrieve realm contexts from. /// The type of object. /// A wrapped instance of the provided item. public static Live Wrap(this T item, IRealmFactory contextFactory) where T : RealmObject, IHasGuidPrimaryKey => new Live(item, contextFactory); /// /// Wrap an unmanaged instance of a realm object in a . /// /// The item to wrap. /// The type of object. /// A wrapped instance of the provided item. /// Throws if the provided item is managed. public static Live WrapAsUnmanaged(this T item) where T : RealmObject, IHasGuidPrimaryKey { if (item.IsManaged) throw new ArgumentException("Provided item must not be managed", nameof(item)); return new Live(item, null); } } }