/// <summary>
        /// When the mod receives data from a peer connection.
        /// </summary>
        /// <param name="bb">The content of the message.</param>
        /// <param name="msg">The message type.</param>
        /// <param name="buffer">The <see cref="MessageBuffer" /> that received the message.</param>
        public override void NetReceive(BinBuffer bb, int msg, MessageBuffer buffer)
        {
            base.NetReceive(bb, msg, buffer);

            // commonly used vars
            int id;

            switch ((NetMessages)msg)
            {
                case NetMessages.StartWraithInvasion:
                    // todo
                    break;
                case NetMessages.SetMusicBox:
                    // todo
                    break;
                case NetMessages.RequestTiles:
                    NetMessage.SendTileSquare(bb.ReadInt(), bb.ReadInt(), bb.ReadInt(), bb.ReadInt());
                    break;
                case NetMessages.RequestCustomSlots:
                    BinBuffer itemB = new BinBuffer();

                    itemB.Write (Main.myPlayer);
                    itemB.WriteX(MWorld.localAccessories);
                    itemB.Write (MWorld.localTome);

                    itemB.Pos = 0;

                    NetHelper.SendModData(AvalonMod.Instance, NetMessages.SendCustomSlots, bb.ReadInt(), -1, itemB.ReadBytes());
                    break;
                case NetMessages.SendCustomSlots:
                    id = bb.ReadInt();

                    for (int i = 0; i < AvalonMod.ExtraSlots; i++)
                        MWorld.accessories[id][i] = bb.ReadItem();

                    MWorld.tomes[id] = bb.ReadItem();
                    MWorld.managers[id] = SkillManager.FromItem(MWorld.tomes[id]);
                    break;
                case NetMessages.ActivateSkill:
                    id = bb.ReadInt();

                    if (MWorld.managers[id] == null)
                        MWorld.managers[id] = SkillManager.FromItem(MWorld.tomes[id]);

                    if (MWorld.managers[id] != null)
                        MWorld.managers[id].Activate(Main.player[id]); // id is both client id and player id.
                    break;
            }
        }
Exemple #2
0
        public override void NetReceive(BinBuffer bb, int msg, MessageBuffer buffer)
        {
            base.NetReceive(bb, msg, buffer);

            switch ((InternalNetMessages)msg)
            {
                case InternalNetMessages.SyncRandom_Sync:
                    SyncedRandom.GetCached(bb.ReadInt()).NextDouble();
                    break;
                case InternalNetMessages.SyncRandom_CTOR:
                    {
                        string group = bb.ReadString();
                        int seed = bb.ReadInt();

                        SyncedRandom.rands[group] = seed;
                        SyncedRandom.refs[group]++;
                    }
                    break;
                case InternalNetMessages.SyncRandom_DTOR:
                    {
                        int seed = bb.ReadInt();
                        string group = null;

                        foreach (var kvp in SyncedRandom.rands)
                            if (kvp.Value == seed)
                                group = kvp.Key;

                        if (group == null)
                            return;

                        SyncedRandom.refs[group]--;
                        if (SyncedRandom.refs[group] <= 0)
                        {
                            SyncedRandom.rands.Remove(group);
                            SyncedRandom.refs.Remove(group);
                            SyncedRandom.RemoveCached(seed);
                        }
                    }
                    break;
            }
        }
		internal bool InvokeServerChat(MessageBuffer buffer, int who, string text)
		{
			ServerChatEventArgs args = new ServerChatEventArgs
			{
				Buffer = buffer,
				Who = who,
				Text = text
			};

			this.ServerChat.Invoke(args);
			return args.Handled;
		}
		internal bool InvokeNetGetData(ref byte msgId, MessageBuffer buffer, ref int index, ref int length)
		{
			if (Main.netMode == 2)
			{
				switch ((PacketTypes) msgId)
				{
					case PacketTypes.ConnectRequest:
						if (this.InvokeServerConnect(buffer.whoAmI))
						{
							Netplay.Clients[buffer.whoAmI].PendingTermination = true;
							return true;
						}

						break;
					case PacketTypes.ContinueConnecting2:
						if (this.InvokeServerJoin(buffer.whoAmI))
						{
							Netplay.Clients[buffer.whoAmI].PendingTermination = true;
							return true;
						}

						break;
					case PacketTypes.ChatText:
						var text = "";
						using(var stream = new MemoryStream(buffer.readBuffer))
						{
							stream.Position = index;
							using(var reader = new BinaryReader(stream))
							{
								reader.ReadByte();
								reader.ReadRGB();
								text = reader.ReadString();
							}
						}

						if (this.InvokeServerChat(buffer, buffer.whoAmI, @text))
									return true;

						break;

					case PacketTypes.ClientUUID:
						byte[] uuid = new byte[length - 5];
						Buffer.BlockCopy(buffer.readBuffer, index + 4, uuid, 0, length - 5);
						SHA512 shaM = new SHA512Managed();
						var result = shaM.ComputeHash(uuid);
						Netplay.Clients[buffer.whoAmI].ClientUUID = result.Aggregate("", (s, b) => s + b.ToString("X2"));;
						return true;
				}
			}

			GetDataEventArgs args = new GetDataEventArgs
			{
				MsgID = (PacketTypes)msgId, 
				Msg = buffer, 
				Index = index, 
				Length = length
			};

			this.NetGetData.Invoke(args);

			msgId = (byte)args.MsgID;
			index = args.Index;
			length = args.Length;
			return args.Handled;
		}
		internal bool InvokeNetGetData(ref byte msgId, MessageBuffer buffer, ref int index, ref int length)
		{
			if (Main.netMode != 2 && msgId == (byte)PacketTypes.ChatText && length > 3)
			{
				byte playerID = buffer.readBuffer[index];
				Color color = new Color(buffer.readBuffer[index + 1] << 16, buffer.readBuffer[index + 2] << 8, buffer.readBuffer[index + 3]);
				string @string = Encoding.UTF8.GetString(buffer.readBuffer, index + 4, length - 5);
				this.InvokeClientChatReceived(playerID, color, @string);
			}

			if (Main.netMode == 2)
			{
				switch ((PacketTypes) msgId)
				{
					case PacketTypes.ConnectRequest:
						if (this.InvokeServerConnect(buffer.whoAmI))
						{
							Netplay.serverSock[buffer.whoAmI].kill = true;
							return true;
						}

						break;
					case PacketTypes.ContinueConnecting2:
						if (this.InvokeServerJoin(buffer.whoAmI))
						{
							Netplay.serverSock[buffer.whoAmI].kill = true;
							return true;
						}

						break;
					case PacketTypes.ChatText:
						string @string = Encoding.UTF8.GetString(buffer.readBuffer, index + 4, length - 5);
						if (this.InvokeServerChat(buffer, buffer.whoAmI, @string))
							return true;

						break;

					case PacketTypes.ClientUUID:
						byte[] uuid = new byte[length - 5];
						Buffer.BlockCopy(buffer.readBuffer, index + 4, uuid, 0, length - 5);
						SHA512 shaM = new SHA512Managed();
						var result = shaM.ComputeHash(uuid);
						Netplay.serverSock[buffer.whoAmI].clientUUID = result.Aggregate("", (s, b) => s + b.ToString("X2"));;
						return true;

						break;
				}
			}

			GetDataEventArgs args = new GetDataEventArgs
			{
				MsgID = (PacketTypes)msgId, 
				Msg = buffer, 
				Index = index, 
				Length = length
			};

			this.NetGetData.Invoke(args);

			msgId = (byte)args.MsgID;
			index = args.Index;
			length = args.Length;
			return args.Handled;
		}