using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace StardewModdingAPI.Framework.Networking
{
/// The metadata for a mod message.
internal class ModMessageModel
{
/*********
** Accessors
*********/
/****
** Origin
****/
/// The unique ID of the player who broadcast the message.
public long FromPlayerID { get; }
/// The unique ID of the mod which broadcast the message.
public string FromModID { get; }
/****
** Destination
****/
/// The players who should receive the message.
public long[]? ToPlayerIDs { get; init; }
/// The mods which should receive the message, or null for all mods.
public string[]? ToModIDs { get; }
/// A message type which receiving mods can use to decide whether it's the one they want to handle, like SetPlayerLocation. This doesn't need to be globally unique, since mods should check the originating mod ID.
public string Type { get; }
/// The custom mod data being broadcast.
public JToken Data { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The unique ID of the player who broadcast the message.
/// The unique ID of the mod which broadcast the message.
/// The players who should receive the message, or null for all players.
/// The mods which should receive the message, or null for all mods.
/// A message type which receiving mods can use to decide whether it's the one they want to handle, like SetPlayerLocation. This doesn't need to be globally unique, since mods should check the originating mod ID.
/// The custom mod data being broadcast.
[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;
}
/// Construct an instance.
/// The message to clone.
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;
}
}
}