summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/SkinJsonConverter.cs
blob: 016ea6d702aed05b8a341473bfd3910d7f0f37b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace MonoGame.Extended.Gui.Serialization;

public interface IGuiSkinService
{
    Skin Skin { get; set; }
}

public class SkinService : IGuiSkinService
{
    public Skin Skin { get; set; }
}

public class SkinJsonConverter : JsonConverter<Skin>
{
    private readonly ContentManager _contentManager;
    private readonly IGuiSkinService _skinService;
    private readonly Type[] _customControlTypes;

    public SkinJsonConverter(ContentManager contentManager, IGuiSkinService skinService, params Type[] customControlTypes)
    {
        _contentManager = contentManager;
        _skinService = skinService;
        _customControlTypes = customControlTypes;
    }

    /// <inheritdoc />
    public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Skin);

    /// <inheritdoc />
    public override Skin Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.String)
        {
            var assetName = reader.GetString();

            // TODO: Load this using the ContentManager instead.
            using (var stream = TitleContainer.OpenStream(assetName))
            {
                var skin = Skin.FromStream(_contentManager, stream, _customControlTypes);
                _skinService.Skin = skin;
                return skin;
            }

        }

        throw new InvalidOperationException($"{nameof(SkinJsonConverter)} can only convert from a string");
    }

    /// <inheritdoc />
    public override void Write(Utf8JsonWriter writer, Skin value, JsonSerializerOptions options) { }
}