mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 10:33:30 +08:00
61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||
|
// See the LICENCE file in the repository root for full licence text.
|
||
|
|
||
|
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<BeatmapDifficulty, BeatmapDifficulty>();
|
||
|
c.CreateMap<BeatmapInfo, BeatmapInfo>();
|
||
|
c.CreateMap<BeatmapMetadata, BeatmapMetadata>();
|
||
|
c.CreateMap<BeatmapSetFileInfo, BeatmapSetFileInfo>();
|
||
|
|
||
|
c.CreateMap<BeatmapSetInfo, BeatmapSetInfo>()
|
||
|
.ForMember(s => s.Beatmaps, d => d.MapFrom(s => s.Beatmaps))
|
||
|
.ForMember(s => s.Files, d => d.MapFrom(s => s.Files))
|
||
|
.MaxDepth(2);
|
||
|
|
||
|
c.CreateMap<DatabasedKeyBinding, DatabasedKeyBinding>();
|
||
|
c.CreateMap<RealmKeyBinding, RealmKeyBinding>();
|
||
|
c.CreateMap<DatabasedSetting, DatabasedSetting>();
|
||
|
c.CreateMap<FileInfo, FileInfo>();
|
||
|
c.CreateMap<ScoreFileInfo, ScoreFileInfo>();
|
||
|
c.CreateMap<SkinInfo, SkinInfo>();
|
||
|
c.CreateMap<RulesetInfo, RulesetInfo>();
|
||
|
}).CreateMapper();
|
||
|
|
||
|
public static T Detach<T>(this T obj) where T : RealmObject
|
||
|
{
|
||
|
if (!obj.IsManaged)
|
||
|
return obj;
|
||
|
|
||
|
var detached = mapper.Map<T>(obj);
|
||
|
|
||
|
//typeof(RealmObject).GetField("_realm", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.SetValue(detached, null);
|
||
|
|
||
|
return detached;
|
||
|
}
|
||
|
|
||
|
public static Live<T> Wrap<T>(this T obj, IRealmFactory contextFactory)
|
||
|
where T : RealmObject, IHasGuidPrimaryKey => new Live<T>(obj, contextFactory);
|
||
|
|
||
|
public static Live<T> WrapAsUnmanaged<T>(this T obj)
|
||
|
where T : RealmObject, IHasGuidPrimaryKey => new Live<T>(obj, null);
|
||
|
}
|
||
|
}
|