// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. 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(); c.CreateMap(); }).CreateMapper(); public static List Detach(this List items) where T : RealmObject { var list = new List(); foreach (var obj in items) list.Add(obj.Detach()); return list; } public static T Detach(this T obj) where T : RealmObject { if (!obj.IsManaged) return obj; var detached = mapper.Map(obj); //typeof(RealmObject).GetField("_realm", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.SetValue(detached, null); return detached; } public static Live Wrap(this T obj, IRealmFactory contextFactory) where T : RealmObject, IHasGuidPrimaryKey => new Live(obj, contextFactory); public static Live WrapAsUnmanaged(this T obj) where T : RealmObject, IHasGuidPrimaryKey => new Live(obj, null); } }