From b3a9cb7b0ab75736691c754a9779c74f2f11cb9b Mon Sep 17 00:00:00 2001 From: Date: Mon, 7 May 2018 05:41:16 -0700 Subject: [PATCH] Updated network utils to be able to sync a custom class that can hold info to a different sync type of data. This allows for users to add in custom data types now. --- .../ModdedUtilitiesNetworking/Class1.cs | 22 +++- .../Framework/Clients/CustomLidgrenClient.cs | 23 +++- .../Framework/CustomMultiplayer.cs | 29 ++++- .../Framework/DataInfo.cs | 110 ++++++++++++++++++ .../Framework/Extentions/GenericExtentions.cs | 32 ++++- .../Framework/Messages/OutgoingMessageBase.cs | 49 ++++++++ .../Framework/Servers/CustomGameServer.cs | 5 +- .../Framework/Servers/CustomLidgrenServer.cs | 11 +- .../ModdedUtilitiesNetworking.csproj | 1 + 9 files changed, 262 insertions(+), 20 deletions(-) create mode 100644 GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs diff --git a/GeneralMods/ModdedUtilitiesNetworking/Class1.cs b/GeneralMods/ModdedUtilitiesNetworking/Class1.cs index a400fa0b..41dd49d9 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Class1.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Class1.cs @@ -143,7 +143,8 @@ namespace ModdedUtilitiesNetworking /// public static void displayMessage(object param) { - string s =(string) param; + DataInfo dataInfo = (DataInfo)param; + string s = (string)dataInfo.data; monitor.Log(s); } @@ -151,9 +152,11 @@ namespace ModdedUtilitiesNetworking /// Initialize basic supported types. /// public static void initializeBasicTypes() - { + { + addObjectType(monitor, typeof(string), new ReadWriter(new reader(GenericExtentions.ReadString), new writer(GenericExtentions.WriteString))); addObjectType(monitor,typeof(String), new ReadWriter(new reader(GenericExtentions.ReadString), new writer(GenericExtentions.WriteString))); 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))); } @@ -195,7 +198,7 @@ namespace ModdedUtilitiesNetworking /// /// /// - public static object processTypes(BinaryReader msg,string key) + public static object processTypesToRead(BinaryReader msg,string key) { foreach(var v in objectTypes) { @@ -209,6 +212,19 @@ namespace ModdedUtilitiesNetworking return null; } + public static void processTypesToWrite(BinaryWriter msg, string key,object data) + { + foreach (var v in objectTypes) + { + if (v.Key == key) + { + ModCore.monitor.Log("PROCESS TYPES TO WRITE FUNCTION: "+v.Key + " " + data.ToString()); + v.Value.write(msg,data); + return; + } + } + monitor.Log("Error: type not found: " + key, LogLevel.Error); + } } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs index 24560034..fa105f9f 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading.Tasks; using Netcode; using ModdedUtilitiesNetworking.Framework.Extentions; +using ModdedUtilitiesNetworking.Framework.Messages; namespace ModdedUtilitiesNetworking.Framework.Clients { @@ -142,9 +143,24 @@ namespace ModdedUtilitiesNetworking.Framework.Clients using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1)) { using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) - message.Write(writer); + { + if (message.MessageType != 20) + { + message.Write(writer); + } + else + { + OutgoingMessageBase.WriteFromMessage(message, writer); + } + } + + } + + int num = (int)this.client.SendMessage(message1, NetDeliveryMethod.ReliableOrdered); + if (num == (int)NetSendResult.Sent) + { + ModCore.monitor.Log("DONE Writing message from client!"); } - int num = (int)this.client.SendMessage(message1, NetDeliveryMethod.ReliableOrdered); } private void parseDataMessageFromServer(NetIncomingMessage dataMsg) @@ -167,6 +183,7 @@ namespace ModdedUtilitiesNetworking.Framework.Clients protected override void processIncomingMessage(IncomingMessage message) { + ModCore.monitor.Log("PROCESS PACKET" + ((int)message.MessageType).ToString()); base.processIncomingMessage(message); //Packet signiture for functions that return nothing. @@ -176,7 +193,7 @@ namespace ModdedUtilitiesNetworking.Framework.Clients object[] obj = message.Reader.ReadModdedInfoPacket(); string functionName = (string)obj[0]; string classType = (string)obj[1]; - object actualObject = ModCore.processTypes(message.Reader, classType); + object actualObject = ModCore.processTypesToRead(message.Reader, classType); ModCore.processVoidFunction(functionName, actualObject); return; } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs index 9ffd0c8c..bfa3c40e 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs @@ -1,4 +1,5 @@ -using ModdedUtilitiesNetworking.Framework.Messages; +using ModdedUtilitiesNetworking.Framework.Clients; +using ModdedUtilitiesNetworking.Framework.Messages; using ModdedUtilitiesNetworking.Framework.Servers; using StardewValley; using StardewValley.Network; @@ -20,6 +21,15 @@ namespace ModdedUtilitiesNetworking.Framework return true; } + public override void processIncomingMessage(IncomingMessage msg) + { + base.processIncomingMessage(msg); + if (msg.MessageType == 20) + { + ModCore.monitor.Log("CUSTOM FUNCTION???"); + } + } + /// /// Sends an outgoing message to appropriate players. /// @@ -35,7 +45,17 @@ namespace ModdedUtilitiesNetworking.Framework } if (Game1.client != null) { - Game1.client.sendMessage(message); + if (Game1.client is CustomLidgrenClient) { + (Game1.client as CustomLidgrenClient).sendMessage(message); + return; + } + if (Game1.client is CustomGalaxyClient) + { + (Game1.client as CustomGalaxyClient).sendMessage(message); + return; + } + ModCore.monitor.Log("Error sending server message!!!"); + } } @@ -79,11 +99,12 @@ namespace ModdedUtilitiesNetworking.Framework public object[] makeDataArray(string functionName, string objectParametersType, object data) { + DataInfo datainfo = new DataInfo(objectParametersType, data); object[] obj = new object[3] { functionName, - objectParametersType, - data + typeof(DataInfo).ToString(), + datainfo, }; return obj; } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs new file mode 100644 index 00000000..17430f9e --- /dev/null +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/DataInfo.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ModdedUtilitiesNetworking.Framework +{ + public class DataInfo + { + public string type; + public object data; + + public DataInfo() + { + } + + public DataInfo(string Type,object Data) + { + this.type = Type; + 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/GenericExtentions.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Extentions/GenericExtentions.cs index 7c157829..10f0a88d 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Extentions/GenericExtentions.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Extentions/GenericExtentions.cs @@ -21,7 +21,7 @@ namespace ModdedUtilitiesNetworking.Framework.Extentions public static void WriteString(this BinaryWriter writer, object str) { - writer.WriteString((string)str); + writer.Write((string)str); } @@ -87,6 +87,36 @@ namespace ModdedUtilitiesNetworking.Framework.Extentions } } + /// + /// Read a data info file from a binary stream. + /// + /// + /// + public static DataInfo ReadDataInfo(this BinaryReader reader) + { + String key=reader.ReadString(); + object data = ModCore.processTypesToRead(reader, key); + + DataInfo info = new DataInfo(key,data); + return info; + } + + /// + /// Write a dataInfo file to binary. + /// + /// + /// + public static void WriteDataInfo(this BinaryWriter writer, object obj) + { + + + DataInfo dataInfo = (DataInfo)obj; + + writer.WriteString(dataInfo.type); + ModCore.monitor.Log("WRITE DATA INFO FUNCTION3: " + dataInfo.type); + ModCore.processTypesToWrite(writer, dataInfo.type, dataInfo.data); + } + //Can do custom classes here for reading and writing. //That way it will be better to save/load data } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Messages/OutgoingMessageBase.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Messages/OutgoingMessageBase.cs index 47093365..f97f20d2 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Messages/OutgoingMessageBase.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Messages/OutgoingMessageBase.cs @@ -1,4 +1,5 @@ using Microsoft.Xna.Framework; +using ModdedUtilitiesNetworking.Framework.Extentions; using Netcode; using StardewValley; using StardewValley.Network; @@ -137,6 +138,54 @@ namespace ModdedUtilitiesNetworking.Framework.Messages { return new OutgoingMessage(this.messageType, this.farmerID, this.data); } + + public static void WriteFromMessage(OutgoingMessage message,BinaryWriter writer) + { + writer.Write(message.MessageType); + writer.Write(message.FarmerID); + object[] data = message.Data.ToArray(); + BinaryReaderWriterExtensions.WriteSkippable(writer, (Action)(() => + { + foreach (object enumValue in data) + { + if (enumValue is Vector2) + { + writer.Write(((Vector2)enumValue).X); + writer.Write(((Vector2)enumValue).Y); + } + else if (enumValue is Guid) + writer.Write(((Guid)enumValue).ToByteArray()); + else if (enumValue is byte[]) + writer.Write((byte[])enumValue); + else if (enumValue is bool) + writer.Write((bool)enumValue ? (byte)1 : (byte)0); + else if (enumValue is byte) + writer.Write((byte)enumValue); + else if (enumValue is int) + writer.Write((int)enumValue); + else if (enumValue is short) + writer.Write((short)enumValue); + else if (enumValue is float) + writer.Write((float)enumValue); + else if (enumValue is long) + writer.Write((long)enumValue); + else if (enumValue is string) + writer.Write((string)enumValue); + else if (enumValue is string[]) + { + string[] strArray = (string[])enumValue; + writer.Write((byte)strArray.Length); + for (int index = 0; index < strArray.Length; ++index) + writer.Write(strArray[index]); + } + else + { + ModCore.processTypesToWrite(writer, (string)data[1], data[2]); //writer, stringType, data + } + } + })); + + } } } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs index f9688ae1..587ecbcc 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs @@ -1,4 +1,5 @@ using Microsoft.Xna.Framework; +using ModdedUtilitiesNetworking.Framework.Clients; using ModdedUtilitiesNetworking.Framework.Extentions; using Netcode; using StardewValley; @@ -24,7 +25,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers public CustomGameServer() { this.servers = new List(); - this.servers.Add(ModCore.multiplayer.InitServer((Server)new LidgrenServer((IGameServer)this))); + this.servers.Add(ModCore.multiplayer.InitServer((Server)new CustomLidgrenServer((IGameServer)this))); ModCore.monitor.Log("Custom Lidgren Server Created"); ModCore.monitor.Log("Custom Game Server Created"); if (Program.sdk.Networking == null) @@ -382,7 +383,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers object[] obj = message.Reader.ReadModdedInfoPacket(); string functionName = (string)obj[0]; string classType = (string)obj[1]; - object actualObject = ModCore.processTypes(message.Reader, classType); + object actualObject = ModCore.processTypesToRead(message.Reader, classType); ModCore.processVoidFunction(functionName, actualObject); break; default: diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs index 4ff5ed85..c2a14327 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs @@ -1,4 +1,5 @@ using Lidgren.Network; +using ModdedUtilitiesNetworking.Framework.Messages; using ModdedUtilitiesNetworking.Framework.Network; using StardewValley; using StardewValley.Network; @@ -154,12 +155,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers } } - protected override void playerDisconnected(long disconnectee) - { - base.playerDisconnected(disconnectee); - this.introductionsSent.Remove(this.peers[disconnectee]); - this.peers.RemoveLeft(disconnectee); - } private void parseDataMessageFromClient(NetIncomingMessage dataMsg) { @@ -201,8 +196,10 @@ namespace ModdedUtilitiesNetworking.Framework.Servers NetOutgoingMessage message1 = this.server.CreateMessage(); using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1)) { - using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) + using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) { message.Write(writer); + //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 3d5d8e60..5e770318 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/ModdedUtilitiesNetworking.csproj +++ b/GeneralMods/ModdedUtilitiesNetworking/ModdedUtilitiesNetworking.csproj @@ -52,6 +52,7 @@ +