Пример #1
0
func (this *PacketResultCodec) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetResult := encode.(*PacketResult)
	err = packet.WriteInt32(writer, util, packetResult.SequenceId)
	if err != nil {
		return
	}
	err = packet.WriteUint8(writer, util, packetResult.StatusCode)
	if packetResult.StatusCode == STATUS_SUCCESS {
		if packetResult.Result.Id() < 0 {
			err = errors.New(fmt.Sprintf("Encode, Request Id is below zero: %d", packetResult.Result.Id()))
			return
		}
		if packetResult.Result.Id() >= len(requestCodecs) {
			err = errors.New(fmt.Sprintf("Encode, Request Id is above maximum: %d", packetResult.Result.Id()))
			return
		}
		buffer := &bytes.Buffer{}
		codec := resultCodecs[packetResult.Result.Id()]
		if codec == nil {
			err = errors.New(fmt.Sprintf("Encode, Request Id does not have a codec: %d", packetResult.Result.Id()))
			return
		}
		err = codec.Encode(buffer, util, packetResult.Result)
		if err != nil {
			return
		}
		payload := buffer.Bytes()
		err = packet.WriteUint16(writer, util, uint16(len(payload)))
		if err != nil {
			return
		}
		_, err = writer.Write(payload)
	}
	return
}
func (this *packetServerLoginEncryptResponseCodec17) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetServerLoginEncryptResponse := encode.(*PacketServerLoginEncryptResponse)
	err = packet.WriteUint16(writer, uint16(len(packetServerLoginEncryptResponse.SharedSecret)))
	if err != nil {
		return
	}
	_, err = writer.Write(packetServerLoginEncryptResponse.SharedSecret)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, uint16(len(packetServerLoginEncryptResponse.VerifyToken)))
	if err != nil {
		return
	}
	_, err = writer.Write(packetServerLoginEncryptResponse.VerifyToken)
	return
}
Пример #3
0
func (this *RequestAsServerCodec) Encode(writer io.Writer, util []byte, request Request) (err error) {
	requestAsServer := request.(*RequestAsServer)
	err = packet.WriteString(writer, util, requestAsServer.Address)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, requestAsServer.Port)
	return
}
func (this *packetClientLoginEncryptRequestCodec17) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetClientLoginEncryptRequest := encode.(*PacketClientLoginEncryptRequest)
	err = packet.WriteString(writer, util, packetClientLoginEncryptRequest.ServerId)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, uint16(len(packetClientLoginEncryptRequest.PublicKey)))
	if err != nil {
		return
	}
	_, err = writer.Write(packetClientLoginEncryptRequest.PublicKey)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, uint16(len(packetClientLoginEncryptRequest.VerifyToken)))
	if err != nil {
		return
	}
	_, err = writer.Write(packetClientLoginEncryptRequest.VerifyToken)
	return
}
func (this *requestAsProxyCodec) Encode(writer io.Writer, util []byte, request Request) (err error) {
	requestAsProxy := request.(*RequestAsProxy)
	err = packet.WriteString(writer, util, requestAsProxy.Address)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, requestAsProxy.Port)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, util, requestAsProxy.Motd)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, util, requestAsProxy.Version)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, requestAsProxy.MaxPlayers)
	return
}
Пример #6
0
func (this *resultGetPlayersCodec) Encode(writer io.Writer, result Result) (err error) {
	resultGetPlayers := result.(*ResultGetPlayers)
	err = packet.WriteBool(writer, resultGetPlayers.List)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, resultGetPlayers.CurrentPlayers)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, resultGetPlayers.MaxPlayers)
	if resultGetPlayers.List {
		var i uint16
		for i = 0; i < resultGetPlayers.CurrentPlayers; i++ {
			if err != nil {
				return
			}
			err = packet.WriteString(writer, resultGetPlayers.Players[i])
		}
	}
	return
}
func (this *packetServerPluginMessageCodec17) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetServerPluginMessage := encode.(*PacketServerPluginMessage)
	err = packet.WriteString(writer, util, packetServerPluginMessage.Channel)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, uint16(len(packetServerPluginMessage.Data)))
	if err != nil {
		return
	}
	_, err = writer.Write(packetServerPluginMessage.Data)
	return
}
Пример #8
0
func (this *requestMessageCodec) Encode(writer io.Writer, request Request) (err error) {
	requestMessage := request.(*RequestMessage)
	err = packet.WriteUint16(writer, uint16(len(requestMessage.Recipients)))
	for i := 0; i < len(requestMessage.Recipients); i++ {
		if err != nil {
			return
		}
		err = packet.WriteString(writer, requestMessage.Recipients[i])
	}
	if err != nil {
		return
	}
	err = packet.WriteString(writer, requestMessage.Channel)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, uint16(len(requestMessage.Message)))
	if err != nil {
		return
	}
	_, err = writer.Write(requestMessage.Message)
	return
}
Пример #9
0
func (this *packetMessageEventCodec) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetMessageEvent := encode.(*PacketMessageEvent)
	err = packet.WriteString(writer, packetMessageEvent.Sender)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, packetMessageEvent.Channel)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, uint16(len(packetMessageEvent.Payload)))
	if err != nil {
		return
	}
	_, err = writer.Write(packetMessageEvent.Payload)
	return
}
Пример #10
0
func (this *PacketServerHandshakeCodec) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetServerHandshake := encode.(*PacketServerHandshake)
	err = packet.WriteVarInt(writer, util, packetServerHandshake.ProtocolVersion)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, util, packetServerHandshake.ServerAddress)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, packetServerHandshake.ServerPort)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, util, packetServerHandshake.State)
	return
}
Пример #11
0
func (this *resultGetDetailsCodec) Encode(writer io.Writer, result Result) (err error) {
	resultGetDetails := result.(*ResultGetDetails)
	err = packet.WriteString(writer, resultGetDetails.Ip)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, resultGetDetails.Port)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, resultGetDetails.Motd)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, resultGetDetails.Version)
	return
}
Пример #12
0
func (this *PacketClientTeamsCodec) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetClientTeams := encode.(*PacketClientTeams)
	err = packet.WriteString(writer, util, packetClientTeams.Name)
	if err != nil {
		return
	}
	err = packet.WriteInt8(writer, util, packetClientTeams.Action)
	if packetClientTeams.Action == 0 || packetClientTeams.Action == 2 {
		if err != nil {
			return
		}
		err = packet.WriteString(writer, util, packetClientTeams.DisplayName)
		if err != nil {
			return
		}
		err = packet.WriteString(writer, util, packetClientTeams.Prefix)
		if err != nil {
			return
		}
		err = packet.WriteString(writer, util, packetClientTeams.Suffix)
		if err != nil {
			return
		}
		err = packet.WriteInt8(writer, util, packetClientTeams.FriendlyFire)
	}
	if packetClientTeams.Action == 0 || packetClientTeams.Action == 3 || packetClientTeams.Action == 4 {
		if err != nil {
			return
		}
		err = packet.WriteUint16(writer, util, uint16(len(packetClientTeams.Players)))
		for i := 0; i < len(packetClientTeams.Players); i++ {
			if err != nil {
				return
			}
			err = packet.WriteString(writer, util, packetClientTeams.Players[i])
		}
	}
	return
}
Пример #13
0
func (this *packetServerEventCodec) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetServerEvent := encode.(*PacketServerEvent)
	err = packet.WriteBool(writer, packetServerEvent.Add)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, packetServerEvent.Server)
	if packetServerEvent.Add {
		if err != nil {
			return
		}
		err = packet.WriteString(writer, packetServerEvent.SecurityKey)
		if err != nil {
			return
		}
		err = packet.WriteString(writer, packetServerEvent.Address)
		if err != nil {
			return
		}
		err = packet.WriteUint16(writer, packetServerEvent.Port)
	}
	return
}
Пример #14
0
func (this *packetRequestCodec) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetRequest := encode.(*PacketRequest)
	err = packet.WriteInt32(writer, packetRequest.SequenceId)
	if err != nil {
		return
	}
	err = packet.WriteUint8(writer, uint8(packetRequest.Request.Id()))
	if err != nil {
		return
	}
	if packetRequest.Request.Id() < 0 {
		err = errors.New(fmt.Sprintf("Encode, Request Id is below zero: %d", packetRequest.Request.Id()))
		return
	}
	if packetRequest.Request.Id() >= len(requestCodecs) {
		err = errors.New(fmt.Sprintf("Encode, Request Id is above maximum: %d", packetRequest.Request.Id()))
		return
	}
	buffer := new(bytes.Buffer)
	codec := requestCodecs[packetRequest.Request.Id()]
	if codec == nil {
		err = errors.New(fmt.Sprintf("Encode, Request Id does not have a codec: %d", packetRequest.Request.Id()))
		return
	}
	err = codec.Encode(buffer, packetRequest.Request)
	if err != nil {
		return
	}
	payload := buffer.Bytes()
	err = packet.WriteUint16(writer, uint16(len(payload)))
	if err != nil {
		return
	}
	_, err = writer.Write(payload)
	return
}