private void parseDataMessageFromClient(NetIncomingMessage dataMsg) { NetConnection peer = dataMsg.SenderConnection; using (IncomingMessage message = new IncomingMessage()) { using (NetBufferReadStream bufferReadStream = new NetBufferReadStream((NetBuffer)dataMsg)) { using (BinaryReader reader = new BinaryReader((Stream)bufferReadStream)) { while ((long)dataMsg.LengthBits - dataMsg.Position >= 8L) { message.Read(reader); if (this.peers.ContainsLeft(message.FarmerID) && this.peers[message.FarmerID] == peer) { this.gameServer.processIncomingMessage(message); } else if ((int)message.MessageType == 2) { NetFarmerRoot farmer = ModCore.multiplayer.readFarmer(message.Reader); this.gameServer.checkFarmhandRequest("", farmer, (msg => this.sendMessage(peer, msg)), (Action)(() => this.peers[farmer.Value.UniqueMultiplayerID] = peer)); } } } } } }
protected override void parseDataMessageFromClient(NetIncomingMessage rawMessage) { // add hook to call multiplayer core NetConnection peer = rawMessage.SenderConnection; using IncomingMessage message = new IncomingMessage(); using Stream readStream = new NetBufferReadStream(rawMessage); using BinaryReader reader = new BinaryReader(readStream); while (rawMessage.LengthBits - rawMessage.Position >= 8) { message.Read(reader); NetConnection connection = rawMessage.SenderConnection; // don't pass rawMessage into context because it gets reused this.OnProcessingMessage(message, outgoing => this.sendMessage(connection, outgoing), () => { if (this.peers.ContainsLeft(message.FarmerID) && this.peers[message.FarmerID] == peer) { this.gameServer.processIncomingMessage(message); } else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader); this.gameServer.checkFarmhandRequest("", this.getConnectionId(rawMessage.SenderConnection), farmer, msg => this.sendMessage(peer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = peer); } }); } }
public bool ParseDataMessageFromClient(NetIncomingMessage rawMessage) { // add hook to call multiplayer core NetConnection peer = rawMessage.SenderConnection; using (IncomingMessage message = new IncomingMessage()) using (Stream readStream = (Stream)this.NetBufferReadStreamConstructor.Invoke(new object[] { rawMessage })) using (BinaryReader reader = new BinaryReader(readStream)) { while (rawMessage.LengthBits - rawMessage.Position >= 8) { message.Read(reader); this.OnProcessingMessage(this, rawMessage, message, () => { if (this.Peers.ContainsLeft(message.FarmerID) && this.Peers[message.FarmerID] == peer) { this.gameServer.processIncomingMessage(message); } else if (message.MessageType == Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.ReadFarmer(message.Reader); this.gameServer.checkFarmhandRequest("", farmer, msg => this.SendMessage(peer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = peer); } }); } } return(false); }
protected virtual void onReceiveMessage(GalaxyID peer, Stream messageStream) { if (bandwidthLogger != null) { bandwidthLogger.RecordBytesDown(messageStream.Length); } using (IncomingMessage message = new IncomingMessage()) { using (BinaryReader reader = new BinaryReader(messageStream)) { message.Read(reader); if (peers.ContainsLeft(message.FarmerID) && peers[message.FarmerID] == peer.ToUint64()) { gameServer.processIncomingMessage(message); } else if (message.MessageType == 2) { NetFarmerRoot farmer = Game1.multiplayer.readFarmer(message.Reader); GalaxyID capturedPeer = new GalaxyID(peer.ToUint64()); gameServer.checkFarmhandRequest(createUserID(peer), getConnectionId(peer), farmer, delegate(OutgoingMessage msg) { sendMessage(capturedPeer, msg); }, delegate { peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64(); }); } } } }
public static bool Prefix(NetFarmerRoot f) { if (Game1.IsServer) { ModEntry.BRGame.ProcessPlayerJoin(f); return(new AutoKicker().ProcessPlayerJoin(f)); } return(true); }
public void checkFarmhandRequest(string userID, NetFarmerRoot farmer, Action <OutgoingMessage> sendMessage, Action approve) { if (farmer.Value == null) { rejectFarmhandRequest(userID, farmer, sendMessage); return; } long id = farmer.Value.UniqueMultiplayerID; Action check = delegate() { Farmer originalFarmhand = this.findOriginalFarmhand(farmer.Value); if (originalFarmhand == null) { Memory.instance.Monitor.Log("Rejected request for farmhand " + id + ": doesn't exist"); rejectFarmhandRequest(userID, farmer, sendMessage); return; } if (!authCheck(userID, originalFarmhand)) { Memory.instance.Monitor.Log("Rejected request for farmhand " + id + ": authorization failure"); rejectFarmhandRequest(userID, farmer, sendMessage); return; } if ((Game1.otherFarmers.ContainsKey(id) && !Memory.multiplayer.isDisconnecting(id)) || Game1.serverHost.Value.UniqueMultiplayerID == id) { Memory.instance.Monitor.Log("Rejected request for farmhand " + id + ": already in use"); rejectFarmhandRequest(userID, farmer, sendMessage); return; } if (findCabin(farmer.Value).isInventoryOpen()) { Memory.instance.Monitor.Log("Rejected request for farmhand " + id + ": inventory in use"); rejectFarmhandRequest(userID, farmer, sendMessage); return; } Memory.instance.Monitor.Log("Approved request for farmhand " + id); approve(); Memory.multiplayer.addPlayer(farmer); Memory.multiplayer.broadcastPlayerIntroduction(farmer); sendServerIntroduction(id); updateLobbyData(); }; if (!whenGameAvailable(check)) { Memory.instance.Monitor.Log("Postponing request for farmhand " + id); sendMessage(new OutgoingMessage(11, Game1.player, new object[] { "Strings\\UI:Client_WaitForHostAvailability" })); } }
public override void addPlayer(NetFarmerRoot f) { long uniqueMultiplayerId = f.Value.UniqueMultiplayerID; //Surpresses the first connected chat info message on non modded clients. if (this.hasConnectedOnce.Contains(uniqueMultiplayerId)) { base.addPlayer(f); } else { this.hasConnectedOnce.Add(uniqueMultiplayerId); } }
public void checkFarmhandRequest(string userID, NetFarmerRoot farmer, Action <OutgoingMessage> sendMessage, Action approve) { if (farmer.Value == null) { this.rejectFarmhandRequest(userID, farmer, sendMessage); } else { long id = farmer.Value.UniqueMultiplayerID; if (this.whenGameAvailable((Action)(() => { Farmer originalFarmhand = this.findOriginalFarmhand(farmer.Value); if (originalFarmhand == null) { Console.WriteLine("Rejected request for farmhand " + (object)id + ": doesn't exist"); this.rejectFarmhandRequest(userID, farmer, sendMessage); } else if (!this.authCheck(userID, originalFarmhand)) { Console.WriteLine("Rejected request for farmhand " + (object)id + ": authorization failure"); this.rejectFarmhandRequest(userID, farmer, sendMessage); } else if (Game1.otherFarmers.ContainsKey(id) && !ModCore.multiplayer.isDisconnecting(id) || Game1.serverHost.Value.UniqueMultiplayerID == id) { Console.WriteLine("Rejected request for farmhand " + (object)id + ": already in use"); this.rejectFarmhandRequest(userID, farmer, sendMessage); } else { Console.WriteLine("Approved request for farmhand " + (object)id); approve(); ModCore.multiplayer.addPlayer(farmer); ModCore.multiplayer.broadcastPlayerIntroduction(farmer); this.sendServerIntroduction(id); this.updateLobbyData(); } }))) { return; } Console.WriteLine("Postponing request for farmhand " + (object)id); sendMessage(new OutgoingMessage((byte)11, Game1.player, new object[1] { (object)"Strings\\UI:Client_WaitForHostAvailability" })); } }
public void ProcessPlayerJoin(NetFarmerRoot farmerRoot) { Console.WriteLine($"(Game) Player joining..."); Storm.SendPhasesData(farmerRoot.Value.UniqueMultiplayerID); //If you run immediately it does nothing Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(1000); //TODO: reduce/increase maybe? NetworkUtility.WarpFarmer(farmerRoot.Value, new TileLocation("Forest", 100, 20)); }); if (IsGameInProgress && alivePlayers.Count <= 1) { //Restart the game, or nothing will ever happen HandleWin(null, null); } }
public static bool Prefix(NetFarmerRoot f) { if (f.Value.Name.Length == 0) { //Don't create a cabin unless there is an elevator building bool elevatorBuildingExists = false; foreach (Building building in Game1.getFarm().buildings) { if (CabinHelper.IsElevatorBuilding(building)) { elevatorBuildingExists = true; break; } } if (!elevatorBuildingExists) { return(true); } //A new player has joined. If there is less then 10 spots availible, mark up until 10 int emptyPlaces = 0; foreach (Farmer player in Game1.getAllFarmhands()) { if (player.Name.Length == 0) { emptyPlaces++; } } Console.WriteLine($"Generating {10 - emptyPlaces} new cabins"); if (emptyPlaces < 10) { for (int i = 0; i <= 10 - emptyPlaces; i++) //Make up to 10 { CabinHelper.AddNewCabin(Game1.random.Next(1, 4)); } } } return(true); }
internal static bool Prefix(GameServer __instance, string userID, NetFarmerRoot farmer, Action <OutgoingMessage> sendMessage, Action approve) { if (!ultiplayer || farmer.Value.homeLocation.Value != "FarmHouse") { return(true); } long id = farmer.Value.UniqueMultiplayerID; approve(); Multiplayer multiplayer = (Multiplayer)typeof(Game1).GetField("multiplayer", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); farmer.Value.currentLocation = Game1.getLocationFromName("FarmHouse"); farmer.Value.Position = new Vector2(640f, 320f); multiplayer.addPlayer(farmer); multiplayer.broadcastPlayerIntroduction(farmer); __instance.sendServerIntroduction(id); __instance.updateLobbyData(); return(false); }
protected override void receiveServerIntroduction(BinaryReader msg) { Game1.otherFarmers.Roots[Game1.player.UniqueMultiplayerID] = (NetRoot <Farmer>)(Game1.player.NetFields.Root as NetFarmerRoot); NetFarmerRoot netFarmerRoot = ModCore.multiplayer.readFarmer(msg); long uniqueMultiplayerId = netFarmerRoot.Value.UniqueMultiplayerID; Game1.serverHost = netFarmerRoot; Game1.serverHost.Value.teamRoot = ModCore.multiplayer.readObjectFull <FarmerTeam>(msg); Game1.otherFarmers.Roots.Remove(uniqueMultiplayerId); Game1.otherFarmers.Roots.Add(uniqueMultiplayerId, (NetRoot <Farmer>)netFarmerRoot); Game1.player.teamRoot = Game1.serverHost.Value.teamRoot; Game1.netWorldState = ModCore.multiplayer.readObjectFull <IWorldState>(msg); Game1.netWorldState.Clock.InterpolationTicks = 0; Game1.netWorldState.Value.WriteToGame1(); this.setUpGame(); if (Game1.chatBox == null) { return; } //Game1.chatBox.listPlayers(); }
protected override void onReceiveMessage(GalaxyID peer, Stream messageStream) { using (IncomingMessage message = new IncomingMessage()) using (BinaryReader reader = new BinaryReader(messageStream)) { message.Read(reader); this.OnProcessingMessage(message, outgoing => this.sendMessage(peer, outgoing), () => { if (this.peers.ContainsLeft(message.FarmerID) && (long)this.peers[message.FarmerID] == (long)peer.ToUint64()) { this.gameServer.processIncomingMessage(message); } else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader); GalaxyID capturedPeer = new GalaxyID(peer.ToUint64()); this.gameServer.checkFarmhandRequest(Convert.ToString(peer.ToUint64()), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64()); } }); } }
protected override void onReceiveMessage(GalaxyID peer, Stream messageStream) { using IncomingMessage message = new IncomingMessage(); using BinaryReader reader = new BinaryReader(messageStream); message.Read(reader); ulong peerID = peer.ToUint64(); // note: GalaxyID instances get reused, so need to store the underlying ID instead this.OnProcessingMessage(message, outgoing => this.SendMessageToPeerID(peerID, outgoing), () => { if (this.peers.ContainsLeft(message.FarmerID) && (long)this.peers[message.FarmerID] == (long)peerID) { this.gameServer.processIncomingMessage(message); } else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader); GalaxyID capturedPeer = new GalaxyID(peerID); this.gameServer.checkFarmhandRequest(Convert.ToString(peerID), this.getConnectionId(peer), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64()); } }); }
protected override void saveFarmhand(NetFarmerRoot farmhand) { CallProtectedIntercepted("saveFarmhand", new object[] { farmhand }); }
public bool ProcessPlayerJoin(NetFarmerRoot farmer) { var pl = ModEntry.BRGame.ModHelper.Multiplayer.GetConnectedPlayer(farmer.Value.UniqueMultiplayerID); if (!pl.HasSmapi) { Console.WriteLine("Kicking because does not have SMAPI"); SendChatMessageToPlayerWithoutMod(farmer.Value.UniqueMultiplayerID, "Read the instruction page before joining."); Game1.server.sendMessage(farmer.Value.UniqueMultiplayerID, new OutgoingMessage((byte)19, farmer.Value.UniqueMultiplayerID, new object[0])); Game1.server.playerDisconnected(farmer.Value.UniqueMultiplayerID); Game1.otherFarmers.Remove(farmer.Value.UniqueMultiplayerID); return(false); } Console.WriteLine($"Player joined with mods:"); foreach (IMultiplayerPeerMod mod in pl.Mods) { Console.WriteLine($" - '{mod.ID}' / '{mod.Name}' / '{mod.Version}'"); if (!AntiCheat.IsLegal(mod)) { Console.WriteLine($"^ found illegal mod"); SendChatMessageToPlayerWithoutMod(farmer.Value.UniqueMultiplayerID, $"Mod {mod.Name} is not permitted."); Game1.server.sendMessage(farmer.Value.UniqueMultiplayerID, new OutgoingMessage((byte)19, farmer.Value.UniqueMultiplayerID, new object[0])); Game1.server.playerDisconnected(farmer.Value.UniqueMultiplayerID); Game1.otherFarmers.Remove(farmer.Value.UniqueMultiplayerID); return(false); } } Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(ModEntry.Config.TimeInMillisecondsBetweenPlayerJoiningAndServerExpectingTheirVersionNumber); if (playersToVersions.TryGetValue(farmer.Value.UniqueMultiplayerID, out var theirVersion)) { //Item1 = major, Item2 = minor, Item3 = sha var tp = GetMyVersion(); var major = tp.Item1; var minor = tp.Item2; var sha = tp.Item3; if (theirVersion.Item1 != major || theirVersion.Item2 != minor || !theirVersion.Item3.SequenceEqual(sha)) { //Kick them for wrong version playersToVersions.Remove(farmer.Value.UniqueMultiplayerID); KickPlayer(farmer.Value, $"Incompatible mod version. You need {major}.{minor}"); return; } } else { //They don't have the mod installed or they have glitched (or really slow internet?) Console.WriteLine("Kicking"); //KickPlayer(farmer.Value, $"Could not connect in time"); SendChatMessageToPlayerWithoutMod(farmer.Value.UniqueMultiplayerID, "Could not connect in time"); Game1.server.sendMessage(farmer.Value.UniqueMultiplayerID, new OutgoingMessage((byte)19, farmer.Value.UniqueMultiplayerID, new object[0])); Game1.server.playerDisconnected(farmer.Value.UniqueMultiplayerID); Game1.otherFarmers.Remove(farmer.Value.UniqueMultiplayerID); return; } }); return(true); }
public void saveFarmhand(NetFarmerRoot farmhand) { farmhand.CloneInto(this.farmhand); resetFarmhandState(); }
private void rejectFarmhandRequest(string userID, NetFarmerRoot farmer, Action <OutgoingMessage> sendMessage) { sendAvailableFarmhands(userID, sendMessage); Memory.instance.Monitor.Log("Rejected request for farmhand " + ((farmer.Value != null) ? farmer.Value.UniqueMultiplayerID.ToString() : "???")); }
private void rejectFarmhandRequest(string userID, NetFarmerRoot farmer, Action <OutgoingMessage> sendMessage) { this.sendAvailableFarmhands(userID, sendMessage); Console.WriteLine("Rejected request for farmhand " + (farmer.Value != null ? farmer.Value.UniqueMultiplayerID.ToString() : "???")); }
public override void processIncomingMessage(IncomingMessage 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) { ModCore.monitor.Log("CUSTOM FUNCTION???"); } }
public override void addPlayer(NetFarmerRoot f) { intercepted.addPlayer(f); }
private static void AddPlayer_Postfix(Multiplayer __instance, ref NetFarmerRoot f) { __instance.PlayerConnected(f.Value); }
public override void broadcastPlayerIntroduction(NetFarmerRoot farmerRoot) { intercepted.broadcastPlayerIntroduction(farmerRoot); }