Updated ModNet to be compatable with SDV 1.3.10, which adds bug fixes for sprite errors.

This commit is contained in:
2018-05-10 00:16:05 -07:00
parent 0fb7e729c1
commit 651cd8a26b
5 changed files with 214 additions and 64 deletions

View File

@ -117,7 +117,7 @@ namespace ModdedUtilitiesNetworking
ModCore.monitor.Log("Custom multiplayer binding success!"); ModCore.monitor.Log("Custom multiplayer binding success!");
} }
if(Game1.client !=null && multiplayerSet == false) if (Game1.client != null && multiplayerSet ==false )
{ {
if(Game1.client is LidgrenClient) if(Game1.client is LidgrenClient)
{ {
@ -131,7 +131,7 @@ namespace ModdedUtilitiesNetworking
var address = (string)finfo.GetValue(Game1.client); var address = (string)finfo.GetValue(Game1.client);
Game1.client.disconnect(true); //Disconnect old client Game1.client.disconnect(true); //Disconnect old client
CustomLidgrenClient client = new CustomLidgrenClient(address); LidgrenClient client = new CustomLidgrenClient(address);
Game1.client = client; Game1.client = client;
client.connect(); //Connect new client. client.connect(); //Connect new client.
multiplayerSet = true; multiplayerSet = true;

View File

@ -236,7 +236,7 @@ namespace ModdedUtilitiesNetworking.Framework.Clients
this.connectionMessage = message.Reader.ReadString(); this.connectionMessage = message.Reader.ReadString();
return; return;
} }
ModCore.multiplayer.baseProcessMessage(message); ModCore.multiplayer.processIncomingMessage(message);
//Packet signiture for functions that return nothing. //Packet signiture for functions that return nothing.

View File

@ -7,6 +7,7 @@ using StardewValley.Menus;
using StardewValley.Network; using StardewValley.Network;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using System.Text; using System.Text;
@ -22,8 +23,49 @@ namespace ModdedUtilitiesNetworking.Framework
public CustomMultiplayer() public CustomMultiplayer()
{ {
this.hasConnectedOnce = new List<long>(); this.hasConnectedOnce = new List<long>();
} }
public override void writeObjectFull<T>(BinaryWriter writer, NetRoot<T> root, long? peer)
{
try
{
root.CreateConnectionPacket(writer, peer);
}
catch(Exception err)
{
}
}
public override void readObjectDelta<T>(BinaryReader reader, NetRoot<T> root)
{
try
{
root.Read(reader);
}
catch(Exception err)
{
}
}
public override void receiveWorldState(BinaryReader msg)
{
this.readObjectDelta<IWorldState>(msg, Game1.netWorldState);
if (Game1.IsServer)
return;
int num1 = Game1.timeOfDay;
Game1.netWorldState.Value.WriteToGame1();
int num2 = Game1.timeOfDay;
if (num1 == num2 || Game1.currentLocation == null || Game1.newDaySync != null)
return;
Game1.performTenMinuteClockUpdate();
}
public override bool isClientBroadcastType(byte messageType) public override bool isClientBroadcastType(byte messageType)
{ {
return true; return true;
@ -31,7 +73,94 @@ namespace ModdedUtilitiesNetworking.Framework
public override void processIncomingMessage(IncomingMessage msg) public override void processIncomingMessage(IncomingMessage msg)
{ {
base.processIncomingMessage(msg); if (msg.MessageType <= 19)
{
switch (msg.MessageType)
{
case 0:
NetFarmerRoot netFarmerRoot = this.farmerRoot(msg.Reader.ReadInt64());
this.readObjectDelta<Farmer>(msg.Reader, (NetRoot<Farmer>)netFarmerRoot);
break;
case 2:
this.receivePlayerIntroduction(msg.Reader);
break;
case 3:
this.readActiveLocation(msg, false);
break;
case 4:
int eventId = msg.Reader.ReadInt32();
int tileX = msg.Reader.ReadInt32();
int tileY = msg.Reader.ReadInt32();
if (Game1.CurrentEvent != null)
break;
this.readWarp(msg.Reader, tileX, tileY, (Action)(() =>
{
Farmer farmerActor = (msg.SourceFarmer.NetFields.Root as NetRoot<Farmer>).Clone().Value;
farmerActor.currentLocation = Game1.currentLocation;
farmerActor.completelyStopAnimatingOrDoingAction();
farmerActor.hidden.Value = false;
Event eventById = Game1.currentLocation.findEventById(eventId, farmerActor);
Game1.currentLocation.startEvent(eventById);
farmerActor.Position = Game1.player.Position;
}));
break;
case 6:
GameLocation gameLocation = this.readLocation(msg.Reader);
if (gameLocation == null)
break;
this.readObjectDelta<GameLocation>(msg.Reader, gameLocation.Root);
break;
case 7:
GameLocation location = this.readLocation(msg.Reader);
if (location == null)
break;
location.temporarySprites.AddRange((IEnumerable<TemporaryAnimatedSprite>)this.readSprites(msg.Reader, location));
break;
case 8:
NPC character = this.readNPC(msg.Reader);
GameLocation targetLocation = this.readLocation(msg.Reader);
if (character == null || targetLocation == null)
break;
Game1.warpCharacter(character, targetLocation, BinaryReaderWriterExtensions.ReadVector2(msg.Reader));
break;
case 10:
this.receiveChatMessage(msg.SourceFarmer, BinaryReaderWriterExtensions.ReadEnum<LocalizedContentManager.LanguageCode>(msg.Reader), msg.Reader.ReadString());
break;
case 12:
try
{
this.receiveWorldState(msg.Reader);
}
catch(Exception err)
{
}
break;
case 13:
this.receiveTeamDelta(msg.Reader);
break;
case 14:
this.receiveNewDaySync(msg);
break;
case 15:
string messageKey = msg.Reader.ReadString();
string[] args = new string[(int)msg.Reader.ReadByte()];
for (int index = 0; index < args.Length; ++index)
args[index] = msg.Reader.ReadString();
this.receiveChatInfoMessage(msg.SourceFarmer, messageKey, args);
break;
case 17:
this.receiveFarmerGainExperience(msg);
break;
case 18:
this.parseServerToClientsMessage(msg.Reader.ReadString());
break;
case 19:
this.playerDisconnected(msg.SourceFarmer.UniqueMultiplayerID);
break;
}
}
if (msg.MessageType == 20) if (msg.MessageType == 20)
{ {
ModCore.monitor.Log("CUSTOM FUNCTION???"); ModCore.monitor.Log("CUSTOM FUNCTION???");

View File

@ -51,7 +51,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
{ {
get get
{ {
return Enumerable.Sum<Server>((IEnumerable<Server>)this.servers, (Func<Server, int>)(s => s.connectionsCount)); return Enumerable.Sum<Server>((IEnumerable<Server>) this.servers, (Func<Server, int>) (s => s.connectionsCount));
} }
} }
@ -75,7 +75,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
if (server.getUserName(farmerId) != null) if (server.getUserName(farmerId) != null)
return server.getUserName(farmerId); return server.getUserName(farmerId);
} }
return (string)null; return (string) null;
} }
protected void initialize() protected void initialize()
@ -104,6 +104,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
foreach (Server server in this.servers) foreach (Server server in this.servers)
{ {
if (server == null) continue;
server.receiveMessages(); server.receiveMessages();
} }
if (!this.isGameAvailable()) if (!this.isGameAvailable())
@ -116,7 +117,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
public void sendMessage(long peerId, OutgoingMessage message) public void sendMessage(long peerId, OutgoingMessage message)
{ {
foreach (Server server in this.servers) foreach (Server server in this.servers)
if (server is CustomLidgrenServer) if (server is CustomLidgrenServer || server is LidgrenServer)
{ {
server.sendMessage(peerId, message); server.sendMessage(peerId, message);
} }
@ -136,7 +137,18 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
public bool canOfferInvite() public bool canOfferInvite()
{ {
return Enumerable.Aggregate<bool, bool>(Enumerable.Select<Server, bool>((IEnumerable<Server>)this.servers, (Func<Server, bool>)(s => s.canOfferInvite())), false, (Func<bool, bool, bool>)((a, b) => a | b)); if (this.servers == null)
{
ModCore.monitor.Log("WAIT WHY IS THIS HAPPENING????");
}
foreach(var v in this.servers)
{
if (v.canOfferInvite() == true) continue;
else return false;
}
return true;
//return Enumerable.Aggregate<bool, bool>(Enumerable.Select<Server, bool>((IEnumerable<Server>)this.servers, (Func<Server, bool>)(s => s.canOfferInvite())), false, (Func<bool, bool, bool>)((a, b) => a | b));
} }
public void offerInvite() public void offerInvite()
@ -177,12 +189,12 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
{ {
Console.WriteLine("Starting server. Protocol version: " + ModCore.multiplayer.protocolVersion); Console.WriteLine("Starting server. Protocol version: " + ModCore.multiplayer.protocolVersion);
this.initialize(); this.initialize();
if ((NetFieldBase<Farmer, NetRef<Farmer>>)Game1.serverHost == (NetRef<Farmer>)null) if (Game1.serverHost.Value == null)
Game1.serverHost = new NetFarmerRoot(); Game1.serverHost = new NetFarmerRoot();
Game1.serverHost.Value = Game1.player; Game1.serverHost.Value = Game1.player;
Game1.serverHost.MarkClean(); Game1.serverHost.MarkClean();
Game1.serverHost.Clock.InterpolationTicks = ModCore.multiplayer.defaultInterpolationTicks; Game1.serverHost.Clock.InterpolationTicks = ModCore.multiplayer.defaultInterpolationTicks;
if ((NetFieldBase<IWorldState, NetRef<IWorldState>>)Game1.netWorldState == (NetRef<IWorldState>)null) if (Game1.netWorldState.Value == (NetRef<IWorldState>)null)
Game1.netWorldState = new NetRoot<IWorldState>((IWorldState)new NetWorldState()); Game1.netWorldState = new NetRoot<IWorldState>((IWorldState)new NetWorldState());
Game1.netWorldState.Clock.InterpolationTicks = 0; Game1.netWorldState.Clock.InterpolationTicks = 0;
Game1.netWorldState.Value.UpdateFromGame1(); Game1.netWorldState.Value.UpdateFromGame1();
@ -209,6 +221,8 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
} }
} }
public void playerDisconnected(long disconnectee) public void playerDisconnected(long disconnectee)
{ {
Farmer sourceFarmer = (Farmer)null; Farmer sourceFarmer = (Farmer)null;
@ -217,6 +231,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
if (sourceFarmer == null) if (sourceFarmer == null)
return; return;
if (this.hasPlayerDisconnectedOnce.Contains(disconnectee)) if (this.hasPlayerDisconnectedOnce.Contains(disconnectee))
{ {
OutgoingMessage message = new OutgoingMessage((byte)19, sourceFarmer, new object[0]); OutgoingMessage message = new OutgoingMessage((byte)19, sourceFarmer, new object[0]);
@ -230,6 +245,15 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
{ {
this.hasPlayerDisconnectedOnce.Add(disconnectee); this.hasPlayerDisconnectedOnce.Add(disconnectee);
} }
/*
OutgoingMessage message = new OutgoingMessage((byte)19, sourceFarmer, new object[0]);
foreach (long peerId in (IEnumerable<long>)Game1.otherFarmers.Keys)
{
if (peerId != disconnectee)
this.sendMessage(peerId, message);
}
*/
} }
@ -455,7 +479,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
OutgoingMessage message1 = new OutgoingMessage(messageType, f, data); OutgoingMessage message1 = new OutgoingMessage(messageType, f, data);
foreach (long peerId in (IEnumerable<long>)Game1.otherFarmers.Keys) foreach (long peerId in (IEnumerable<long>)Game1.otherFarmers.Keys)
{ {
ModCore.monitor.Log("RESEND MESSAGE TO CLIENT!!!", StardewModdingAPI.LogLevel.Alert);
this.sendMessage(peerId, message1); this.sendMessage(peerId, message1);
} }
} }

View File

@ -14,6 +14,38 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
{ {
class CustomLidgrenServer : Server class CustomLidgrenServer : Server
{ {
///Save this for later
protected override void playerDisconnected(long disconnectee)
{
this.gameServer.playerDisconnected(disconnectee);
this.introductionsSent.Remove(this.peers[disconnectee]);
this.peers.RemoveLeft(disconnectee);
}
protected void sendMessage(NetConnection connection, OutgoingMessage message)
{
NetOutgoingMessage message1 = this.server.CreateMessage();
using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1))
{
using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) {
if (message.MessageType < 20)
{
message.Write(writer);
}
else
{
OutgoingMessageBase.WriteFromMessage(message, writer);
}
}
}
int num = (int)this.server.SendMessage(message1, connection, NetDeliveryMethod.ReliableOrdered);
}
private HashSet<NetConnection> introductionsSent = new HashSet<NetConnection>(); private HashSet<NetConnection> introductionsSent = new HashSet<NetConnection>();
private Bimap<long, NetConnection> peers = new Bimap<long, NetConnection>(); private Bimap<long, NetConnection> peers = new Bimap<long, NetConnection>();
public const int defaultPort = 24642; public const int defaultPort = 24642;
@ -84,8 +116,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
NetIncomingMessage netIncomingMessage; NetIncomingMessage netIncomingMessage;
while ((netIncomingMessage = this.server.ReadMessage()) != null) while ((netIncomingMessage = this.server.ReadMessage()) != null)
{ {
int i = (int)netIncomingMessage.MessageType;
//ModCore.monitor.Log("Message Type: " + i.ToString());
switch (netIncomingMessage.MessageType) switch (netIncomingMessage.MessageType)
{ {
case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.WarningMessage:
@ -114,11 +144,10 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
netIncomingMessage.SenderConnection.Deny(); netIncomingMessage.SenderConnection.Deny();
break; break;
case NetIncomingMessageType.Data: case NetIncomingMessageType.Data:
//ModCore.monitor.Log("GOT DATA~~");
this.parseDataMessageFromClient(netIncomingMessage); this.parseDataMessageFromClient(netIncomingMessage);
break; break;
default: default:
//ModCore.monitor.Log(netIncomingMessage.ToString()); Game1.debugOutput = netIncomingMessage.ToString();
break; break;
} }
this.server.Recycle(netIncomingMessage); this.server.Recycle(netIncomingMessage);
@ -150,28 +179,15 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
case NetConnectionStatus.Disconnecting: case NetConnectionStatus.Disconnecting:
if (!this.peers.ContainsRight(message.SenderConnection)) if (!this.peers.ContainsRight(message.SenderConnection))
break; break;
//Forces a de-sync with money.
this.playerDisconnected(this.peers[message.SenderConnection]); this.playerDisconnected(this.peers[message.SenderConnection]);
break; break;
} }
} }
protected override void playerDisconnected(long disconnectee)
{
this.gameServer.playerDisconnected(disconnectee);
this.introductionsSent.Remove(this.peers[disconnectee]);
this.peers.RemoveLeft(disconnectee);
}
private void parseDataMessageFromClient(NetIncomingMessage dataMsg) private void parseDataMessageFromClient(NetIncomingMessage dataMsg)
{ {
//ModCore.monitor.Log("DATA MSG: "+dataMsg.ToString());
NetConnection peer = dataMsg.SenderConnection; NetConnection peer = dataMsg.SenderConnection;
using (IncomingMessage message = new IncomingMessage()) using (IncomingMessage message = new IncomingMessage())
{ {
@ -182,7 +198,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
while ((long)dataMsg.LengthBits - dataMsg.Position >= 8L) while ((long)dataMsg.LengthBits - dataMsg.Position >= 8L)
{ {
message.Read(reader); message.Read(reader);
int type = message.MessageType;
if (this.peers.ContainsLeft(message.FarmerID) && this.peers[message.FarmerID] == peer) if (this.peers.ContainsLeft(message.FarmerID) && this.peers[message.FarmerID] == peer)
this.gameServer.processIncomingMessage(message); this.gameServer.processIncomingMessage(message);
else if ((int)message.MessageType == 2) else if ((int)message.MessageType == 2)
@ -203,24 +218,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
this.sendMessage(this.peers[peerId], message); this.sendMessage(this.peers[peerId], message);
} }
protected void sendMessage(NetConnection connection, OutgoingMessage message)
{
NetOutgoingMessage message1 = this.server.CreateMessage();
using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1))
{
using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) {
if (message.MessageType < 20)
{
message.Write(writer);
}
else
{
OutgoingMessageBase.WriteFromMessage(message, writer);
}
}
}
int num = (int)this.server.SendMessage(message1, connection, NetDeliveryMethod.ReliableOrdered);
}
public override void setLobbyData(string key, string value) public override void setLobbyData(string key, string value)
{ {