Skip to content

Commit

Permalink
Merge pull request #33 from shutterbug2000/subscription-protocol
Browse files Browse the repository at this point in the history
Add Subscription Protocol
  • Loading branch information
jonbarrow authored Oct 4, 2023
2 parents bd89924 + d6aca0d commit 7e85097
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 0 deletions.
39 changes: 39 additions & 0 deletions subscription/create_my_subscription_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// CreateMySubscriptionData sets the CreateMySubscriptionData handler function
func (protocol *SubscriptionProtocol) CreateMySubscriptionData(handler func(err error, client *nex.Client, callID uint32, unk uint64, content []byte)) {
protocol.createMySubscriptionDataHandler = handler
}

func (protocol *SubscriptionProtocol) handleCreateMySubscriptionData(packet nex.PacketInterface) {
if protocol.createMySubscriptionDataHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::CreateMySubscriptionData not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()
parameters := request.Parameters()

parametersStream := nex.NewStreamIn(parameters, protocol.Server)
unk, err := parametersStream.ReadUInt64LE()
if err != nil {
go protocol.createMySubscriptionDataHandler(fmt.Errorf("Failed to read param from parameters. %s", err.Error()), client, callID, 0, nil)
return
}

//This is done since the server doesn't need to care about the data here (it's game-specific), so we just pass it along to store however the handler wants
content := parametersStream.ReadRemaining()
go protocol.createMySubscriptionDataHandler(nil, client, callID, unk, content)
}
29 changes: 29 additions & 0 deletions subscription/get_active_player_subscription_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// GetActivePlayerSubscriptionData sets the GetActivePlayerSubscriptionData handler function
func (protocol *SubscriptionProtocol) GetActivePlayerSubscriptionData(handler func(err error, client *nex.Client, callID uint32)) {
protocol.getActivePlayerSubscriptionDataHandler = handler
}

func (protocol *SubscriptionProtocol) handleGetActivePlayerSubscriptionData(packet nex.PacketInterface) {
if protocol.getActivePlayerSubscriptionDataHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::GetActivePlayerSubscriptionData not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()

go protocol.getActivePlayerSubscriptionDataHandler(nil, client, callID)
}
29 changes: 29 additions & 0 deletions subscription/get_friend_subscription_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// GetFriendSubscriptionData sets GetFriendSubscriptionData Unk1 handler function
func (protocol *SubscriptionProtocol) GetFriendSubscriptionData(handler func(err error, client *nex.Client, callID uint32)) {
protocol.getFriendSubscriptionDataHandler = handler
}

func (protocol *SubscriptionProtocol) handleGetFriendSubscriptionData(packet nex.PacketInterface) {
if protocol.getFriendSubscriptionDataHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::GetFriendSubscriptionData not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()

go protocol.getFriendSubscriptionDataHandler(nil, client, callID)
}
29 changes: 29 additions & 0 deletions subscription/get_privacy_levels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// GetPrivacyLevels sets the GetPrivacyLevels handler function
func (protocol *SubscriptionProtocol) GetPrivacyLevels(handler func(err error, client *nex.Client, callID uint32)) {
protocol.getPrivacyLevelsHandler = handler
}

func (protocol *SubscriptionProtocol) handleGetPrivacyLevels(packet nex.PacketInterface) {
if protocol.getPrivacyLevelsHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::GetPrivacyLevels not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()

go protocol.getPrivacyLevelsHandler(nil, client, callID)
}
36 changes: 36 additions & 0 deletions subscription/get_subscription_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// GetSubscriptionData sets the GetSubscriptionData handler function
func (protocol *SubscriptionProtocol) GetSubscriptionData(handler func(err error, client *nex.Client, callID uint32, pids []uint32)) {
protocol.getSubscriptionDataHandler = handler
}

func (protocol *SubscriptionProtocol) handleGetSubscriptionData(packet nex.PacketInterface) {
if protocol.getSubscriptionDataHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::GetSubscriptionData not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()
parameters := request.Parameters()

parametersStream := nex.NewStreamIn(parameters, protocol.Server)
pids, err := parametersStream.ReadListUInt32LE()
if err != nil {
go protocol.getSubscriptionDataHandler(nil, client, callID, nil)
}

go protocol.getSubscriptionDataHandler(nil, client, callID, pids)
}
29 changes: 29 additions & 0 deletions subscription/get_target_subscription_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// GetTargetSubscriptionData sets the GetTargetSubscriptionData handler function
func (protocol *SubscriptionProtocol) GetTargetSubscriptionData(handler func(err error, client *nex.Client, callID uint32)) {
protocol.getTargetSubscriptionDataHandler = handler
}

func (protocol *SubscriptionProtocol) handleGetTargetSubscriptionData(packet nex.PacketInterface) {
if protocol.getTargetSubscriptionDataHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::GetTargetSubscriptionData not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()

go protocol.getTargetSubscriptionDataHandler(nil, client, callID)
}
112 changes: 112 additions & 0 deletions subscription/protocol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

const (
// ProtocolID is the protocol ID for the Subscription protocol
ProtocolID = 0x75

// MethodCreateMySubscriptionData is the method ID for the method CreateMySubscriptionDataID
MethodCreateMySubscriptionData = 0x1

// MethodUpdateMySubscriptionData is the method ID for the method UpdateMySubscriptionData
MethodUpdateMySubscriptionData = 0x2

// MethodClearMySubscriptionData is the method ID for the method ClearMySubscriptionDataID
MethodClearMySubscriptionData = 0x3

// MethodAddTarget is the method ID for the method AddTarget
MethodAddTarget = 0x4

// MethodDeleteTarget is the method ID for the method DeleteTarget
MethodDeleteTarget = 0x5

// MethodClearTarget is the method ID for the method ClearTarget
MethodClearTarget = 0x6

// MethodGetFriendSubscriptionData is the method ID for the method GetFriendSubscriptionData
MethodGetFriendSubscriptionData = 0x7

// MethodGetTargetSubscriptionData is the method ID for the method GetTargetSubscriptionData
MethodGetTargetSubscriptionData = 0x8

// MethodGetActivePlayerSubscriptionData is the method ID for the method GetActivePlayerSubscriptionData
MethodGetActivePlayerSubscriptionData = 0x9

// MethodGetSubscriptionData is the method ID for the method GetSubscriptionData
MethodGetSubscriptionData = 0xA

// MethodReplaceTargetAndGetSubscriptionData is the method ID for the method ReplaceTargetAndGetSubscriptionData
MethodReplaceTargetAndGetSubscriptionData = 0xB

// MethodSetPrivacyLevel is the method ID for the method SetPrivacyLevel
MethodSetPrivacyLevel = 0xC

// MethodGetPrivacyLevel is the method ID for the method GetPrivacyLevel
MethodGetPrivacyLevel = 0xD

// MethodGetSubscriptionUserFriendList is the method ID for the method GetSubscriptionUserFriendList
MethodGetSubscriptionUserFriendList = 0xE

// MethodGetPrivacyLevels is the method ID for the method GetPrivacyLevels
MethodGetPrivacyLevels = 0xF
)

// SubscriptionProtocol handles the Subscription nex protocol
type SubscriptionProtocol struct {
Server *nex.Server
createMySubscriptionDataHandler func(err error, client *nex.Client, callID uint32, unk uint64, content []byte)
updateMySubscriptionDataHandler func(err error, client *nex.Client, callID uint32, unk uint32, content []byte)
getFriendSubscriptionDataHandler func(err error, client *nex.Client, callID uint32)
getTargetSubscriptionDataHandler func(err error, client *nex.Client, callID uint32)
getActivePlayerSubscriptionDataHandler func(err error, client *nex.Client, callID uint32)
getSubscriptionDataHandler func(err error, client *nex.Client, callID uint32, pids []uint32)
replaceTargetAndGetSubscriptionDataHandler func(err error, client *nex.Client, callID uint32)
getPrivacyLevelsHandler func(err error, client *nex.Client, callID uint32)
}

// Setup initializes the protocol
func (protocol *SubscriptionProtocol) Setup() {
protocol.Server.On("Data", func(packet nex.PacketInterface) {
request := packet.RMCRequest()

if ProtocolID == request.ProtocolID() {
switch request.MethodID() {
case MethodCreateMySubscriptionData:
go protocol.handleCreateMySubscriptionData(packet)
case MethodUpdateMySubscriptionData:
go protocol.handleUpdateMySubscriptionData(packet)
case MethodGetFriendSubscriptionData:
go protocol.handleGetFriendSubscriptionData(packet)
case MethodGetTargetSubscriptionData:
go protocol.handleGetTargetSubscriptionData(packet)
case MethodGetActivePlayerSubscriptionData:
go protocol.handleGetActivePlayerSubscriptionData(packet)
case MethodGetSubscriptionData:
go protocol.handleGetSubscriptionData(packet)
case MethodReplaceTargetAndGetSubscriptionData:
go protocol.handleReplaceTargetAndGetSubscriptionData(packet)
case MethodGetPrivacyLevels:
go protocol.handleGetPrivacyLevels(packet)
default:
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
fmt.Printf("Unsupported Subscription method ID: %#v\n", request.MethodID())
}
}
})
}

// NewSubscriptionProtocol returns a new SubscriptionProtocol
func NewSubscriptionProtocol(server *nex.Server) *SubscriptionProtocol {
protocol := &SubscriptionProtocol{Server: server}

protocol.Setup()

return protocol
}
29 changes: 29 additions & 0 deletions subscription/replace_target_and_get_subscription_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// ReplaceTargetAndGetSubscriptionData sets the ReplaceTargetAndGetSubscriptionData handler function
func (protocol *SubscriptionProtocol) ReplaceTargetAndGetSubscriptionData(handler func(err error, client *nex.Client, callID uint32)) {
protocol.replaceTargetAndGetSubscriptionDataHandler = handler
}

func (protocol *SubscriptionProtocol) handleReplaceTargetAndGetSubscriptionData(packet nex.PacketInterface) {
if protocol.replaceTargetAndGetSubscriptionDataHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::ReplaceTargetAndGetSubscriptionData not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()

go protocol.replaceTargetAndGetSubscriptionDataHandler(nil, client, callID)
}
39 changes: 39 additions & 0 deletions subscription/update_my_subscription_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package subscription implements the Subscription NEX protocol
package subscription

import (
"fmt"

nex "github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-go/globals"
)

// UpdateMySubscriptionData sets the UpdateMySubscriptionData handler function
func (protocol *SubscriptionProtocol) UpdateMySubscriptionData(handler func(err error, client *nex.Client, callID uint32, unk uint32, content []byte)) {
protocol.updateMySubscriptionDataHandler = handler
}

func (protocol *SubscriptionProtocol) handleUpdateMySubscriptionData(packet nex.PacketInterface) {
if protocol.updateMySubscriptionDataHandler == nil {
fmt.Println("[Warning] SubscriptionProtocol::UpdateMySubscriptionData not implemented")
go globals.RespondError(packet, ProtocolID, nex.Errors.Core.NotImplemented)
return
}

client := packet.Sender()
request := packet.RMCRequest()

callID := request.CallID()
parameters := request.Parameters()

parametersStream := nex.NewStreamIn(parameters, protocol.Server)
unk, err := parametersStream.ReadUInt32LE()
if err != nil {
go protocol.updateMySubscriptionDataHandler(fmt.Errorf("Failed to read param from parameters. %s", err.Error()), client, callID, 0, nil)
return
}

//This is done since the server doesn't need to care about the data here (it's game-specific), so we just pass it along to store however the handler wants
content := parametersStream.ReadRemaining()
go protocol.updateMySubscriptionDataHandler(nil, client, callID, unk, content)
}

0 comments on commit 7e85097

Please sign in to comment.