summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Networking/ModMessageModel.cs
blob: 01672714a5aeb4ec9d44624a2135f72e897554d9 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace StardewModdingAPI.Framework.Networking
{
    /// <summary>The metadata for a mod message.</summary>
    internal class ModMessageModel
    {
        /*********
        ** Accessors
        *********/
        /****
        ** Origin
        ****/
        /// <summary>The unique ID of the player who broadcast the message.</summary>
        public long FromPlayerID { get; }

        /// <summary>The unique ID of the mod which broadcast the message.</summary>
        public string FromModID { get; }

        /****
        ** Destination
        ****/
        /// <summary>The players who should receive the message.</summary>
        public long[]? ToPlayerIDs { get; init; }

        /// <summary>The mods which should receive the message, or <c>null</c> for all mods.</summary>
        public string[]? ToModIDs { get; }

        /// <summary>A message type which receiving mods can use to decide whether it's the one they want to handle, like <c>SetPlayerLocation</c>. This doesn't need to be globally unique, since mods should check the originating mod ID.</summary>
        public string Type { get; }

        /// <summary>The custom mod data being broadcast.</summary>
        public JToken Data { get; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="fromPlayerID">The unique ID of the player who broadcast the message.</param>
        /// <param name="fromModID">The unique ID of the mod which broadcast the message.</param>
        /// <param name="toPlayerIDs">The players who should receive the message, or <c>null</c> for all players.</param>
        /// <param name="toModIDs">The mods which should receive the message, or <c>null</c> for all mods.</param>
        /// <param name="type">A message type which receiving mods can use to decide whether it's the one they want to handle, like <c>SetPlayerLocation</c>. This doesn't need to be globally unique, since mods should check the originating mod ID.</param>
        /// <param name="data">The custom mod data being broadcast.</param>
        [JsonConstructor]
        public ModMessageModel(long fromPlayerID, string fromModID, long[]? toPlayerIDs, string[]? toModIDs, string type, JToken data)
        {
            this.FromPlayerID = fromPlayerID;
            this.FromModID = fromModID;
            this.ToPlayerIDs = toPlayerIDs;
            this.ToModIDs = toModIDs;
            this.Type = type;
            this.Data = data;
        }

        /// <summary>Construct an instance.</summary>
        /// <param name="message">The message to clone.</param>
        public ModMessageModel(ModMessageModel message)
        {
            this.FromPlayerID = message.FromPlayerID;
            this.FromModID = message.FromModID;
            this.ToPlayerIDs = message.ToPlayerIDs?.ToArray();
            this.ToModIDs = message.ToModIDs?.ToArray();
            this.Type = message.Type;
            this.Data = message.Data;
        }
    }
}