Convert ModPreset to realm object

This commit is contained in:
Bartłomiej Dach 2022-07-23 17:25:11 +02:00
parent 998e3b74d6
commit cbabc4886c
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497

View File

@ -3,6 +3,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Game.Database;
using osu.Game.Online.API;
using Realms;
namespace osu.Game.Rulesets.Mods
{
@ -10,12 +16,18 @@ namespace osu.Game.Rulesets.Mods
/// A mod preset is a named collection of configured mods.
/// Presets are presented to the user in the mod select overlay for convenience.
/// </summary>
public class ModPreset
public class ModPreset : RealmObject, IHasGuidPrimaryKey, ISoftDelete
{
/// <summary>
/// The internal database ID of the preset.
/// </summary>
[PrimaryKey]
public Guid ID { get; set; } = Guid.NewGuid();
/// <summary>
/// The ruleset that the preset is valid for.
/// </summary>
public RulesetInfo RulesetInfo { get; set; } = null!;
public RulesetInfo Ruleset { get; set; } = null!;
/// <summary>
/// The name of the mod preset.
@ -30,6 +42,34 @@ namespace osu.Game.Rulesets.Mods
/// <summary>
/// The set of configured mods that are part of the preset.
/// </summary>
public ICollection<Mod> Mods { get; set; } = Array.Empty<Mod>();
[Ignored]
public ICollection<Mod> Mods
{
get
{
if (string.IsNullOrEmpty(ModsJson))
return Array.Empty<Mod>();
var apiMods = JsonConvert.DeserializeObject<IEnumerable<APIMod>>(ModsJson);
var ruleset = Ruleset.CreateInstance();
return apiMods.AsNonNull().Select(mod => mod.ToMod(ruleset)).ToArray();
}
set
{
var apiMods = value.Select(mod => new APIMod(mod)).ToArray();
ModsJson = JsonConvert.SerializeObject(apiMods);
}
}
/// <summary>
/// The set of configured mods that are part of the preset, serialised as a JSON blob.
/// </summary>
[MapTo("Mods")]
public string ModsJson { get; set; } = string.Empty;
/// <summary>
/// Whether the preset has been soft-deleted by the user.
/// </summary>
public bool DeletePending { get; set; }
}
}