mirror of
https://github.com/ppy/osu.git
synced 2025-02-15 23:42:55 +08:00
Move extensions methods into own class
This commit is contained in:
parent
6736db327a
commit
dd50b5870e
@ -2,16 +2,8 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System.Threading;
|
||||
using AutoMapper;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Framework.Statistics;
|
||||
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
|
||||
@ -170,49 +162,4 @@ 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);
|
||||
}
|
||||
}
|
||||
|
60
osu.Game/Database/RealmExtensions.cs
Normal file
60
osu.Game/Database/RealmExtensions.cs
Normal file
@ -0,0 +1,60 @@
|
||||
// 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user