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.

This commit is contained in:
2018-05-07 05:41:16 -07:00
parent 62f07d731c
commit b3a9cb7b0a
9 changed files with 262 additions and 20 deletions

View File

@ -143,7 +143,8 @@ namespace ModdedUtilitiesNetworking
/// <param name="param"></param>
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.
/// </summary>
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<String>), 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
/// <param name="msg"></param>
/// <param name="key"></param>
/// <returns></returns>
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);
}
}

View File

@ -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;
}

View File

@ -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???");
}
}
/// <summary>
/// Sends an outgoing message to appropriate players.
/// </summary>
@ -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;
}

View File

@ -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();
}
}
}

View File

@ -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
}
}
/// <summary>
/// Read a data info file from a binary stream.
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Write a dataInfo file to binary.
/// </summary>
/// <param name="writer"></param>
/// <param name="obj"></param>
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
}

View File

@ -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
}
}
}));
}
}
}

View File

@ -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<Server>();
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:

View File

@ -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);
}

View File

@ -52,6 +52,7 @@
<Compile Include="Class1.cs" />
<Compile Include="Framework\Clients\CustomGalaxyClient.cs" />
<Compile Include="Framework\Clients\CustomLidgrenClient.cs" />
<Compile Include="Framework\DataInfo.cs" />
<Compile Include="Framework\Delegates\DelegateInfo.cs" />
<Compile Include="Framework\Extentions\GenericExtentions.cs" />
<Compile Include="Framework\Extentions\XNAExtentions.cs" />