summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Networking/MultiplayerPeer.cs
blob: b4e39379994bd4737a19b00818a2d854224395e8 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using StardewValley.Network;

namespace StardewModdingAPI.Framework.Networking
{
    /// <summary>Metadata about a connected player.</summary>
    internal class MultiplayerPeer : IMultiplayerPeer
    {
        /*********
        ** Fields
        *********/
        /// <summary>A method which sends a message to the peer.</summary>
        private readonly Action<OutgoingMessage> SendMessageImpl;


        /*********
        ** Accessors
        *********/
        /// <summary>The player's unique ID.</summary>
        public long PlayerID { get; }

        /// <summary>Whether this is a connection to the host player.</summary>
        public bool IsHost { get; }

        /// <summary>Whether the player has SMAPI installed.</summary>
        public bool HasSmapi => this.ApiVersion != null;

        /// <summary>The player's OS platform, if <see cref="HasSmapi"/> is true.</summary>
        public GamePlatform? Platform { get; }

        /// <summary>The installed version of Stardew Valley, if <see cref="HasSmapi"/> is true.</summary>
        public ISemanticVersion GameVersion { get; }

        /// <summary>The installed version of SMAPI, if <see cref="HasSmapi"/> is true.</summary>
        public ISemanticVersion ApiVersion { get; }

        /// <summary>The installed mods, if <see cref="HasSmapi"/> is true.</summary>
        public IEnumerable<IMultiplayerPeerMod> Mods { get; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="playerID">The player's unique ID.</param>
        /// <param name="model">The metadata to copy.</param>
        /// <param name="sendMessage">A method which sends a message to the peer.</param>
        /// <param name="isHost">Whether this is a connection to the host player.</param>
        public MultiplayerPeer(long playerID, RemoteContextModel model, Action<OutgoingMessage> sendMessage, bool isHost)
        {
            this.PlayerID = playerID;
            this.IsHost = isHost;
            if (model != null)
            {
                this.Platform = model.Platform;
                this.GameVersion = model.GameVersion;
                this.ApiVersion = model.ApiVersion;
                this.Mods = model.Mods.Select(mod => new MultiplayerPeerMod(mod)).ToArray();
            }
            this.SendMessageImpl = sendMessage;
        }

        /// <summary>Get metadata for a mod installed by the player.</summary>
        /// <param name="id">The unique mod ID.</param>
        /// <returns>Returns the mod info, or <c>null</c> if the player doesn't have that mod.</returns>
        public IMultiplayerPeerMod GetMod(string id)
        {
            if (string.IsNullOrWhiteSpace(id) || this.Mods == null || !this.Mods.Any())
                return null;

            id = id.Trim();
            return this.Mods.FirstOrDefault(mod => mod.ID != null && mod.ID.Equals(id, StringComparison.InvariantCultureIgnoreCase));
        }

        /// <summary>Send a message to the given peer, bypassing the game's normal validation to allow messages before the connection is approved.</summary>
        /// <param name="message">The message to send.</param>
        public void SendMessage(OutgoingMessage message)
        {
            this.SendMessageImpl(message);
        }
    }
}