Added support to send a message to a specific farmer.

This commit is contained in:
2018-05-08 17:21:59 -07:00
parent 9c1314480c
commit 947e0c557f
5 changed files with 147 additions and 13 deletions

View File

@ -71,10 +71,12 @@ namespace ModdedUtilitiesNetworking
private void ControlEvents_KeyPressed(object sender, StardewModdingAPI.Events.EventArgsKeyPressed e)
{
/*
if (e.KeyPressed==Microsoft.Xna.Framework.Input.Keys.K)
{
multiplayer.sendModInfoReturnVoid(Framework.Features.Stardew.MessageFeatures.FSTRING_SendHUDMessageWithIcon, MessagesExtentions.HUDMessageIconIdentifier, new HUDMessage("My love is like fire",1),Framework.Enums.MessageTypes.messageTypes.SendToAll);
//multiplayer.sendMessage(Framework.Features.Stardew.MessageFeatures.FSTRING_SendHUDMessageWithIcon, MessagesExtentions.HUDMessageIconIdentifier, new HUDMessage("My love is like fire",1),Framework.Enums.MessageTypes.messageTypes.SendToSpecific,Game1.otherFarmers.ElementAt(0).Value);
}
*/
}
/// <summary>

View File

@ -34,7 +34,7 @@ namespace ModdedUtilitiesNetworking.Framework
/// Sends an outgoing message to appropriate players.
/// </summary>
/// <param name="message"></param>
public void sendMessage(OutgoingMessage message)
private void sendMessage(OutgoingMessage message)
{
if (Game1.server != null)
{
@ -104,6 +104,27 @@ namespace ModdedUtilitiesNetworking.Framework
return message;
}
public OutgoingMessage sendOutGoingMessageReturnVoid(string functionName, string objectParametersType, object data, Farmer source, Enums.MessageTypes.messageTypes sendingInfo, Farmer recipient)
{
byte bite = new byte();
if (sendingInfo == Enums.MessageTypes.messageTypes.SendOneWay) bite = Enums.MessageTypes.SendOneWay;
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToAll) bite = Enums.MessageTypes.SendToAll;
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToSpecific) bite = Enums.MessageTypes.SendToSpecific;
OutgoingMessage message = new OutgoingMessage(bite, source, makeDataArray(functionName, objectParametersType, data,recipient));
return message;
}
public OutgoingMessage sendOutGoingMessageReturnVoid(string functionName, Type objectParametersType, object data, Farmer source, Enums.MessageTypes.messageTypes sendingInfo, Farmer recipient)
{
byte bite = new byte();
if (sendingInfo == Enums.MessageTypes.messageTypes.SendOneWay) bite = Enums.MessageTypes.SendOneWay;
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToAll) bite = Enums.MessageTypes.SendToAll;
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToSpecific) bite = Enums.MessageTypes.SendToSpecific;
OutgoingMessage message = new OutgoingMessage(bite, source, makeDataArray(functionName, objectParametersType.ToString(), data, recipient));
return message;
}
public object[] makeDataArray(string functionName, string objectParametersType, object data)
{
@ -117,6 +138,30 @@ namespace ModdedUtilitiesNetworking.Framework
return obj;
}
public object[] makeDataArray(string functionName, string objectParametersType, object data, Farmer recipient)
{
DataInfo datainfo = new DataInfo(objectParametersType, data,recipient);
object[] obj = new object[3]
{
functionName,
typeof(DataInfo).ToString(),
datainfo,
};
return obj;
}
public object[] makeDataArray(string functionName, string objectParametersType, object data, long recipient)
{
DataInfo datainfo = new DataInfo(objectParametersType, data, recipient.ToString());
object[] obj = new object[3]
{
functionName,
typeof(DataInfo).ToString(),
datainfo,
};
return obj;
}
/// <summary>
/// Creates all of the necessary parameters for the outgoing message to be sent to the server/client on what to do and how to handle the data sent.
/// This message written will attempt to access a function that doesn't return anything. Essentially null.
@ -124,30 +169,65 @@ namespace ModdedUtilitiesNetworking.Framework
/// <param name="uniqueID"></param>
/// <param name="classType"></param>
/// <param name="data"></param>
public void sendModInfoReturnVoid(string uniqueID,Type classType,object data, Enums.MessageTypes.messageTypes sendingInfo)
public void sendMessage(string uniqueID, Type classType, object data, Enums.MessageTypes.messageTypes sendingInfo, Farmer recipient = null)
{
Farmer f = Game1.player;
if ((sendingInfo == Enums.MessageTypes.messageTypes.SendOneWay || sendingInfo == Enums.MessageTypes.messageTypes.SendToAll))
{
OutgoingMessage message = ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f, sendingInfo);
ModCore.multiplayer.sendMessage(message);
return;
}
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToSpecific && recipient != null)
{
OutgoingMessage message = ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f, sendingInfo, recipient);
ModCore.multiplayer.sendMessage(message);
return;
}
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToSpecific && recipient == null)
{
ModCore.monitor.Log("ERROR: Attempted to send a target specific message to a NULL recipient");
return;
}
}
/// <summary>
/// Creates all of the necessary parameters for the outgoing message to be sent to the server/client on what to do and how to handle the data sent.
/// This message written will attempt to access a function that doesn't return anything. Essentially null.
/// A way to send mod info across the net.
/// </summary>
/// <param name="uniqueID"></param>
/// <param name="classType"></param>
/// <param name="data"></param>
public void sendModInfoReturnVoid(string uniqueID, string classType, object data, Enums.MessageTypes.messageTypes sendingInfo)
/// <param name="sendingInfo"></param>
/// <param name="recipient"></param>
public void sendMessage(string uniqueID, string classType, object data, Enums.MessageTypes.messageTypes sendingInfo, Farmer recipient=null)
{
Farmer f = Game1.player;
if ((sendingInfo == Enums.MessageTypes.messageTypes.SendOneWay || sendingInfo == Enums.MessageTypes.messageTypes.SendToAll))
{
OutgoingMessage message = ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f, sendingInfo);
ModCore.multiplayer.sendMessage(message);
return;
}
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToSpecific && recipient!=null)
{
OutgoingMessage message = ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f, sendingInfo,recipient);
ModCore.multiplayer.sendMessage(message);
return;
}
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToSpecific && recipient == null)
{
ModCore.monitor.Log("ERROR: Attempted to send a target specific message to a NULL recipient");
return;
}
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using StardewValley;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -10,6 +11,7 @@ namespace ModdedUtilitiesNetworking.Framework
{
public string type;
public object data;
public string recipientID;
public DataInfo()
{
@ -19,8 +21,21 @@ namespace ModdedUtilitiesNetworking.Framework
{
this.type = Type;
this.data = Data;
this.recipientID = "";
}
public DataInfo(string Type, object Data, Farmer farmer)
{
this.type = Type;
this.data = Data;
this.recipientID = farmer.UniqueMultiplayerID.ToString();
}
public DataInfo(string Type, object Data, string uniqueID)
{
this.type = Type;
this.data = Data;
this.recipientID = uniqueID;
}
}
}

View File

@ -96,8 +96,8 @@ namespace ModdedUtilitiesNetworking.Framework.Extentions
{
String key=reader.ReadString();
object data = ModCore.processTypesToRead(reader, key);
DataInfo info = new DataInfo(key,data);
string ID = reader.ReadString();
DataInfo info = new DataInfo(key,data,ID);
return info;
}
@ -115,6 +115,7 @@ namespace ModdedUtilitiesNetworking.Framework.Extentions
writer.WriteString(dataInfo.type);
ModCore.monitor.Log("WRITE DATA INFO FUNCTION3: " + dataInfo.type);
ModCore.processTypesToWrite(writer, dataInfo.type, dataInfo.data);
writer.WriteString(dataInfo.recipientID);
}
//Can do custom classes here for reading and writing.

View File

@ -393,6 +393,25 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
object actualObject2 = ModCore.processTypesToRead(message.Reader, classType2);
ModCore.processVoidFunction(functionName2, actualObject2);
this.rebroadcastClientMessageToAllClients(message);
break;
case Enums.MessageTypes.SendToSpecific:
//Read in specific info.
object[] obj3 = message.Reader.ReadModdedInfoPacket();
string functionName3 = (string)obj3[0];
string classType3 = (string)obj3[1];
//Parse
object actualObject3 = ModCore.processTypesToRead(message.Reader, classType3);
DataInfo info = (DataInfo)actualObject3;
if (info.recipientID == Game1.player.UniqueMultiplayerID.ToString())
{
ModCore.processVoidFunction(functionName3, actualObject3);
}
else
{
this.sendMessageToSpecificClient(message, info);
}
break;
default:
ModCore.multiplayer.processIncomingMessage(message);
@ -416,7 +435,24 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
ModCore.monitor.Log("RESEND MESSAGE TO CLIENT!!!", StardewModdingAPI.LogLevel.Alert);
this.sendMessage(peerId, message1);
}
}
private void sendMessageToSpecificClient(IncomingMessage message, long farmerID)
{
byte messageType = Enums.MessageTypes.SendOneWay;
Farmer f = Game1.player;
object data = message.Data.Clone();
OutgoingMessage message1 = new OutgoingMessage(messageType, f, data);
this.sendMessage(farmerID, message1);
}
private void sendMessageToSpecificClient(IncomingMessage message, DataInfo info)
{
byte messageType = Enums.MessageTypes.SendOneWay;
Farmer f = Game1.player;
object data = message.Data.Clone();
OutgoingMessage message1 = new OutgoingMessage(messageType, f, data);
this.sendMessage(long.Parse(info.recipientID), message1);
}
private void rebroadcastClientMessage(IncomingMessage message)