1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 14:12:56 +08:00

Add basic write support via automapper

This commit is contained in:
Dean Herbert 2022-01-11 18:23:04 +09:00
parent 80eee6d7b0
commit f986c3ebd4

View File

@ -19,7 +19,33 @@ namespace osu.Game.Database
{
public static class RealmObjectExtensions
{
private static readonly IMapper mapper = new MapperConfiguration(c =>
private static readonly IMapper write_mapper = new MapperConfiguration(c =>
{
c.ShouldMapField = fi => false;
// If we want to limit this further, we can avoid mapping properties with no setter that are not IList<>.
// Takes a bit of effort to determine whether this is the case though, see https://stackoverflow.com/questions/951536/how-do-i-tell-whether-a-type-implements-ilist
c.ShouldMapProperty = pi => pi.SetMethod?.IsPublic == true;
c.CreateMap<RealmKeyBinding, RealmKeyBinding>();
c.CreateMap<BeatmapMetadata, BeatmapMetadata>();
c.CreateMap<BeatmapDifficulty, BeatmapDifficulty>();
c.CreateMap<RulesetInfo, RulesetInfo>();
c.CreateMap<ScoreInfo, ScoreInfo>();
c.CreateMap<RealmUser, RealmUser>();
c.CreateMap<RealmFile, RealmFile>();
c.CreateMap<RealmNamedFileUsage, RealmNamedFileUsage>();
c.CreateMap<BeatmapInfo, BeatmapInfo>();
c.CreateMap<BeatmapSetInfo, BeatmapSetInfo>();
c.AddGlobalIgnore(nameof(RealmObjectBase.ObjectSchema));
c.ForAllMaps((a, b) =>
{
b.PreserveReferences();
b.MaxDepth(2);
});
}).CreateMapper();
private static readonly IMapper read_mapper = new MapperConfiguration(c =>
{
c.ShouldMapField = fi => false;
// If we want to limit this further, we can avoid mapping properties with no setter that are not IList<>.
@ -36,6 +62,7 @@ namespace osu.Game.Database
c.CreateMap<RealmNamedFileUsage, RealmNamedFileUsage>();
c.CreateMap<BeatmapInfo, BeatmapInfo>();
c.CreateMap<BeatmapSetInfo, BeatmapSetInfo>();
c.AddGlobalIgnore(nameof(RealmObjectBase.ObjectSchema));
c.ForAllMaps((a, b) =>
{
@ -77,7 +104,12 @@ namespace osu.Game.Database
if (!item.IsManaged)
return item;
return mapper.Map<T>(item);
return read_mapper.Map<T>(item);
}
public static void CopyChangesToRealm<T>(this T source, T destination) where T : RealmObjectBase
{
write_mapper.Map(source, destination);
}
public static List<ILive<T>> ToLiveUnmanaged<T>(this IEnumerable<T> realmList)