From 20f52e27071b6da95543f0ea8d72683670c586b8 Mon Sep 17 00:00:00 2001 From: Date: Mon, 7 May 2018 22:37:02 -0700 Subject: [PATCH] Allowed for custom types to be added in how to process a class multiple ways. Also added support for SDV.HUDMessages --- .../ModdedUtilitiesNetworking/Class1.cs | 39 ++++++++- .../Framework/CustomMultiplayer.cs | 18 +++- .../Framework/DataInfo.cs | 84 ------------------- .../MessagesExtentions.cs | 35 ++++++++ .../Features/Stardew/MessageFeatures.cs | 42 ++++++++++ .../Framework/Servers/CustomLidgrenServer.cs | 10 ++- .../ModdedUtilitiesNetworking.csproj | 2 + 7 files changed, 142 insertions(+), 88 deletions(-) create mode 100644 GeneralMods/ModdedUtilitiesNetworking/Framework/Extentions/StrardewValleyExtentions/MessagesExtentions.cs create mode 100644 GeneralMods/ModdedUtilitiesNetworking/Framework/Features/Stardew/MessageFeatures.cs diff --git a/GeneralMods/ModdedUtilitiesNetworking/Class1.cs b/GeneralMods/ModdedUtilitiesNetworking/Class1.cs index 41dd49d9..aed6092b 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Class1.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Class1.cs @@ -3,6 +3,7 @@ using ModdedUtilitiesNetworking.Framework; using ModdedUtilitiesNetworking.Framework.Clients; using ModdedUtilitiesNetworking.Framework.Delegates; using ModdedUtilitiesNetworking.Framework.Extentions; +using ModdedUtilitiesNetworking.Framework.Extentions.StrardewValleyExtentions; using ModdedUtilitiesNetworking.Framework.Servers; using StardewModdingAPI; using StardewValley; @@ -15,10 +16,25 @@ using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; +using ModdedUtilitiesNetworking.Framework.Features; using static ModdedUtilitiesNetworking.Framework.Delegates.DelegateInfo; namespace ModdedUtilitiesNetworking { + /* + *When adding a new supported type... + * 1.Make reader and writer overload functions + * 2.Add supported type to objectTypes list using addObjectType or addCustomObjectType + * + * When adding a new supported function... + * 1.Make sure the supported type is handled first! + * 2.Add in a function that will accept the object data and parse the data to do what you want. + * 3. Add funtion to possibleVoidFunctions + * + * + * TODO: + * Make CustomGalaxyClient work. + */ public class ModCore : Mod { public static CustomMultiplayer multiplayer; @@ -45,6 +61,7 @@ namespace ModdedUtilitiesNetworking multiplayer = new CustomMultiplayer(); possibleVoidFunctions.Add(displayMessageString, new voidFunc(displayMessage)); + possibleVoidFunctions.Add(Framework.Features.Stardew.MessageFeatures.FSTRING_SendHUDMessageWithIcon, Framework.Features.Stardew.MessageFeatures.SendHUDMessageWithIcon); initializeBasicTypes(); @@ -56,7 +73,7 @@ namespace ModdedUtilitiesNetworking if (e.KeyPressed==Microsoft.Xna.Framework.Input.Keys.K) { - multiplayer.sendModInfoReturnVoid(displayMessageString, typeof(String), (object)"My love is like fire."); + multiplayer.sendModInfoReturnVoid(Framework.Features.Stardew.MessageFeatures.FSTRING_SendHUDMessageWithIcon, MessagesExtentions.HUDMessageIconIdentifier, new HUDMessage("My love is like fire",1)); } } @@ -158,6 +175,7 @@ namespace ModdedUtilitiesNetworking addObjectType(monitor,typeof(List), new ReadWriter(new reader(GenericExtentions.ReadStringList), new writer(GenericExtentions.WriteStringList))); addObjectType(monitor, typeof(DataInfo), new ReadWriter(new reader(GenericExtentions.ReadDataInfo), new writer(GenericExtentions.WriteDataInfo))); + addObjectCustomType(monitor, MessagesExtentions.HUDMessageIconIdentifier, new ReadWriter(new reader(MessagesExtentions.ReadHUDMessageWithIcon), new writer(MessagesExtentions.WriteHUDMessageWithIcon))); } /// @@ -179,6 +197,25 @@ namespace ModdedUtilitiesNetworking } } + /// + /// Adds "types" using a string convention to be able to allow processing of the same class type in different ways. + /// + /// + /// + /// + public static void addObjectCustomType(IMonitor monitor, string t, ReadWriter readWriter) + { + if (objectTypes.ContainsKey(t.ToString()) == false) + { + objectTypes.Add(t, readWriter); + monitor.Log("Added custom supported type: " + t.ToString() + " to ModdedUtilitiesNetworking.", LogLevel.Info); + } + else + { + monitor.Log("Error custom adding supported type: " + t.ToString() + " to ModdedUtilitiesNetworking. Type already supported!", LogLevel.Alert); + } + } + /// /// Process all possible functions that can occur. /// diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs index bfa3c40e..e5f428cb 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs @@ -77,7 +77,7 @@ namespace ModdedUtilitiesNetworking.Framework } /// - /// Creates a net outgoing message that is written specifically to call a void function when sent. + /// Creates a net outgoing message that is written specifically to call a void function when sent. USed to specifiy types and specific ways to handle them. /// /// /// @@ -125,5 +125,21 @@ namespace ModdedUtilitiesNetworking.Framework ModCore.multiplayer.sendMessage(message); } + /// + /// 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. + /// + /// + /// + /// + public void sendModInfoReturnVoid(string uniqueID, string classType, object data) + { + Farmer f = Game1.player; + + OutgoingMessage message = ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f); + + ModCore.multiplayer.sendMessage(message); + } + } } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs index 17430f9e..69f61f0b 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs @@ -21,90 +21,6 @@ namespace ModdedUtilitiesNetworking.Framework this.data = Data; } - public TypeCode GetTypeCode() - { - throw new NotImplementedException(); - } - public bool ToBoolean(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public byte ToByte(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public char ToChar(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public decimal ToDecimal(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public double ToDouble(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public short ToInt16(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public int ToInt32(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public long ToInt64(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public sbyte ToSByte(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public float ToSingle(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public string ToString(IFormatProvider provider) - { - return this.ToString(); - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - return this.GetType(); - } - - public ushort ToUInt16(IFormatProvider provider) - { - ushort ushortValue1 = 53612; - return ushortValue1; - } - - public uint ToUInt32(IFormatProvider provider) - { - throw new NotImplementedException(); - } - - public ulong ToUInt64(IFormatProvider provider) - { - throw new NotImplementedException(); - } } } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Extentions/StrardewValleyExtentions/MessagesExtentions.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Extentions/StrardewValleyExtentions/MessagesExtentions.cs new file mode 100644 index 00000000..3c27bb83 --- /dev/null +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Extentions/StrardewValleyExtentions/MessagesExtentions.cs @@ -0,0 +1,35 @@ +using StardewValley; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ModdedUtilitiesNetworking.Framework.Extentions.StrardewValleyExtentions +{ + public static class MessagesExtentions + { + + public static string HUDMessageIconIdentifier= typeof(HUDMessage).ToString()+".MessageWithIcon"; + + public static HUDMessage ReadHUDMessageWithIcon(this BinaryReader reader) + { + string message = reader.ReadString(); + int messageType = reader.ReadInt32(); + + return new HUDMessage(message,messageType); + } + + + public static void WriteHUDMessageWithIcon(this BinaryWriter writer, object obj) + { + HUDMessage message =(HUDMessage) obj; + writer.WriteString(message.message); + writer.Write(message.whatType); + } + + + + } +} diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Features/Stardew/MessageFeatures.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Features/Stardew/MessageFeatures.cs new file mode 100644 index 00000000..2c96a43b --- /dev/null +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Features/Stardew/MessageFeatures.cs @@ -0,0 +1,42 @@ +using StardewValley; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ModdedUtilitiesNetworking.Framework.Features.Stardew +{ + public class MessageFeatures + { + + public static string FSTRING_SendHUDMessageWithIcon = "Omegasis.ModdedUtilitiesNetworking.Framework.Features.Stardew.MessageFeatures.SendHUDMessageWithIcon"; + + public static string FSTRING_DisplayConsoleMessageString = "Omegasis.ModdedUtilitiesNetworking.Framework.Features.Stardew.MessageFeatures.DisplayConsoleMessage"; + + + /// + /// Displays a HUD message to all clients and server containing this information. + /// + /// + public static void SendHUDMessageWithIcon(object data) + { + DataInfo obj = (DataInfo)data; + string type = obj.type; + HUDMessage message =(HUDMessage)obj.data; + Game1.addHUDMessage(message); + } + + + /// + /// Static Debug function. + /// + /// + public static void DisplayConsoleMessage(object param) + { + DataInfo dataInfo = (DataInfo)param; + string s = (string)dataInfo.data; + ModCore.monitor.Log(s); + } + } +} diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs index c2a14327..b02f0a54 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs @@ -197,8 +197,14 @@ namespace ModdedUtilitiesNetworking.Framework.Servers using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1)) { using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) { - message.Write(writer); - //OutgoingMessageBase.WriteFromMessage(message, writer); + if (message.MessageType != 20) + { + message.Write(writer); + } + else + { + OutgoingMessageBase.WriteFromMessage(message, writer); + } } } int num = (int)this.server.SendMessage(message1, connection, NetDeliveryMethod.ReliableOrdered); diff --git a/GeneralMods/ModdedUtilitiesNetworking/ModdedUtilitiesNetworking.csproj b/GeneralMods/ModdedUtilitiesNetworking/ModdedUtilitiesNetworking.csproj index 5e770318..322543ab 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/ModdedUtilitiesNetworking.csproj +++ b/GeneralMods/ModdedUtilitiesNetworking/ModdedUtilitiesNetworking.csproj @@ -55,7 +55,9 @@ + +