Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor types #56

Merged
merged 60 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
1dc1755
refactor(types): make primitive types into type aliases
jonbarrow May 22, 2024
a76ede1
refactor(types): make Buffer into a type alias
jonbarrow May 22, 2024
2b0f14b
refactor(types): make QBuffer into a type alias
jonbarrow May 22, 2024
bd05699
refactor(types): make DateTime into a type alias
jonbarrow May 22, 2024
f8b1187
refactor(types): make PID into a type alias
jonbarrow May 22, 2024
8a848c5
refactor(types): make QResult into a type alias
jonbarrow May 22, 2024
2514f63
refactor(types): make String into a type alias
jonbarrow May 22, 2024
44581f1
fix(types): QBuffer.Equals should not be a pointer
jonbarrow May 22, 2024
cc2b19e
refactor(types): make QUUID into a type alias
jonbarrow May 22, 2024
fabfca0
fix(types): fix bad Copy methods
jonbarrow May 22, 2024
7bc3579
refactor(types): make primitive types into type aliases
jonbarrow May 22, 2024
2a775eb
refactor(types): make Buffer into a type alias
jonbarrow May 22, 2024
7998d14
refactor(types): make QBuffer into a type alias
jonbarrow May 22, 2024
d5feb92
refactor(types): make DateTime into a type alias
jonbarrow May 22, 2024
502b601
refactor(types): make PID into a type alias
jonbarrow May 22, 2024
feca72a
refactor(types): make QResult into a type alias
jonbarrow May 22, 2024
3c28fb7
refactor(types): make String into a type alias
jonbarrow May 22, 2024
c1aa55c
fix(types): QBuffer.Equals should not be a pointer
jonbarrow May 22, 2024
885121a
refactor(types): make QUUID into a type alias
jonbarrow May 22, 2024
446a730
fix(types): fix bad Copy methods
jonbarrow May 22, 2024
daceca2
Merge branch 'types-updates' of https://github.com/PretendoNetwork/ne…
jonbarrow Jun 2, 2024
8d306f4
refactor(types): make List into a type alias
jonbarrow Jun 2, 2024
943f141
fix(types): remove recursive String call in String type
jonbarrow Jun 2, 2024
1f8c5ff
fix(types): remove recursive String call in List type
jonbarrow Jun 2, 2024
3a90361
fix: update StationURLs handling in NewPRUDPConnection
jonbarrow Jun 4, 2024
99acae0
refactor(types): make Map into a type alias
jonbarrow Jun 4, 2024
832bd19
refactor(types): remove reliance on pointers
jonbarrow Jun 5, 2024
bfe9c9c
chore(types): remove old Value and LegacyValue methods on PID
jonbarrow Jun 7, 2024
ab4dbfc
chore(types): remove pointer from StationURL.SetPrincipalID
jonbarrow Jun 7, 2024
0e38401
fix(test): make secure endpoint secure again
jonbarrow Jun 10, 2024
7dafc92
fix(types): make StationURL check if map is nil before accessing
jonbarrow Jun 10, 2024
8271f9a
chore(types): rename EncodeToString to Format on StationURL
jonbarrow Jun 11, 2024
d668caa
fix(types): rename FromString to Parse on StationURL and reject unkno…
jonbarrow Jun 11, 2024
2c48000
feat(types): add support for custom parameters in StationURLs
jonbarrow Jun 11, 2024
d19d660
fix(types): make StationURL check if custom map is nil before accessing
jonbarrow Jun 11, 2024
7f5a097
refactor(types): only call ensureFields in StationURL.ExtractFrom
jonbarrow Jun 13, 2024
9373fd8
refactor(types): add StationURL.SetURL and URL, and more accurate imp…
jonbarrow Jun 13, 2024
2c2dd7a
fix(types): add url length checks for StationURL
jonbarrow Jun 13, 2024
88c51d6
fix(types): make StationURL.Format a pointer receiver
jonbarrow Jun 13, 2024
a1ca41e
fix(types): add address and port validation in StationURL
jonbarrow Jun 13, 2024
1bee30d
chore: update test secure server StationURL
jonbarrow Jun 13, 2024
a4b341d
chore(types): update Godoc comment on StationURL.URL
jonbarrow Jun 13, 2024
b57adeb
refactor: remove references to "Primitive" naming conventions
jonbarrow Jun 16, 2024
2309a6d
Merge branch 'master' into types-updates
jonbarrow Jul 4, 2024
182418f
feat: add struct tags to struct types
jonbarrow Jul 4, 2024
a0107a9
fix: AccountDetailsByPID signature to not take in a pointer
jonbarrow Jul 4, 2024
27cdd4c
Merge branch 'master' into types-updates
jonbarrow Jul 5, 2024
4fbbe2d
chore: fix indentation in DeriveKerberosKey from merge
jonbarrow Jul 5, 2024
37d01ad
Merge branch 'master' into types-updates
jonbarrow Jul 7, 2024
b36470e
feat: add CopyRef and Deref methods to RVType(Ptr)
jonbarrow Nov 16, 2024
fb784e4
Merge branch 'types-updates' of https://github.com/PretendoNetwork/ne…
jonbarrow Nov 16, 2024
d3b7a01
chore: make CopyRef do safer, full, copies
jonbarrow Nov 17, 2024
337fdcd
chore: add method comments to RVType and RVTypePtr interfaces
jonbarrow Nov 17, 2024
359eb43
refactor(types): simplify CopyRef() to use Copy() internally
jonbarrow Nov 17, 2024
2797330
chore: fix kerberos.go indentation
jonbarrow Nov 20, 2024
eee4fc9
fix(AnyDataHolder): Use CopyRef to get RVTypePtr of object data
DaniElectra Dec 24, 2024
10faf0a
Merge branch 'master' into types-updates
DaniElectra Jan 9, 2025
340b851
refactor: Replace AnyDataHolder with AnyObjectHolder
DaniElectra Jan 9, 2025
9c8f7bd
chore(AnyObjectHolder): Inline cast check
DaniElectra Jan 11, 2025
be12609
chore: Move DataHolder into its own file
DaniElectra Jan 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var logger = plogger.NewLogger()
func init() {
initResultCodes()

types.RegisterVariantType(1, types.NewPrimitiveS64(0))
types.RegisterVariantType(2, types.NewPrimitiveF64(0))
types.RegisterVariantType(3, types.NewPrimitiveBool(false))
types.RegisterVariantType(1, types.NewInt64(0))
types.RegisterVariantType(2, types.NewDouble(0))
types.RegisterVariantType(3, types.NewBool(false))
types.RegisterVariantType(4, types.NewString(""))
types.RegisterVariantType(5, types.NewDateTime(0))
types.RegisterVariantType(6, types.NewPrimitiveU64(0))
types.RegisterVariantType(6, types.NewUInt64(0))
}
4 changes: 2 additions & 2 deletions kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ func (ti *KerberosTicketInternalData) Decrypt(stream *ByteStreamIn, key []byte)
return fmt.Errorf("Failed to read Kerberos ticket internal data. %s", err.Error())
}

hash := md5.Sum(append(key, ticketKey.Value...))
hash := md5.Sum(append(key, *ticketKey...))
key = hash[:]

stream = NewByteStreamIn(data.Value, stream.LibraryVersions, stream.Settings)
stream = NewByteStreamIn(*data, stream.LibraryVersions, stream.Settings)
}

encryption := NewKerberosEncryption(key)
Expand Down
4 changes: 2 additions & 2 deletions prudp_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (pep *PRUDPEndPoint) readKerberosTicket(payload []byte) ([]byte, *types.PID
serverKey := DeriveKerberosKey(serverAccount.PID, []byte(serverAccount.Password))

ticket := NewKerberosTicketInternalData(pep.Server)
if err := ticket.Decrypt(NewByteStreamIn(ticketData.Value, pep.Server.LibraryVersions, pep.ByteStreamSettings()), serverKey); err != nil {
if err := ticket.Decrypt(NewByteStreamIn(*ticketData, pep.Server.LibraryVersions, pep.ByteStreamSettings()), serverKey); err != nil {
return nil, nil, 0, err
}

Expand All @@ -464,7 +464,7 @@ func (pep *PRUDPEndPoint) readKerberosTicket(payload []byte) ([]byte, *types.PID
sessionKey := ticket.SessionKey
kerberos := NewKerberosEncryption(sessionKey)

decryptedRequestData, err := kerberos.Decrypt(requestData.Value)
decryptedRequestData, err := kerberos.Decrypt(*requestData)
if err != nil {
return nil, nil, 0, err
}
Expand Down
6 changes: 3 additions & 3 deletions test/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func login(packet nex.PRUDPPacketInterface) {
panic(err)
}

sourceAccount, _ := accountDetailsByUsername(strUserName.Value)
sourceAccount, _ := accountDetailsByUsername(string(*strUserName))
targetAccount, _ := accountDetailsByUsername(secureServerAccount.Username)

retval := types.NewQResultSuccess(0x00010001)
Expand All @@ -72,8 +72,8 @@ func login(packet nex.PRUDPPacketInterface) {
strReturnMsg := types.NewString("Test Build")

pConnectionData.StationURL = types.NewStationURL("prudps:/address=192.168.1.98;port=60001;CID=1;PID=2;sid=1;stream=10;type=2")
pConnectionData.SpecialProtocols = types.NewList[*types.PrimitiveU8]()
pConnectionData.SpecialProtocols.Type = types.NewPrimitiveU8(0)
pConnectionData.SpecialProtocols = types.NewList[*types.UInt8]()
pConnectionData.SpecialProtocols.Type = types.NewUInt8(0)
pConnectionData.StationURLSpecialProtocols = types.NewStationURL("")
pConnectionData.Time = types.NewDateTime(0).Now()

Expand Down
16 changes: 8 additions & 8 deletions test/secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var secureEndpoint *nex.PRUDPEndPoint
type principalPreference struct {
types.Structure
*types.Data
ShowOnlinePresence *types.PrimitiveBool
ShowCurrentTitle *types.PrimitiveBool
BlockFriendRequests *types.PrimitiveBool
ShowOnlinePresence *types.Bool
ShowCurrentTitle *types.Bool
BlockFriendRequests *types.Bool
}

func (pp *principalPreference) WriteTo(writable types.Writable) {
Expand All @@ -31,7 +31,7 @@ func (pp *principalPreference) WriteTo(writable types.Writable) {
type comment struct {
types.Structure
*types.Data
Unknown *types.PrimitiveU8
Unknown *types.UInt8
Contents *types.String
LastChanged *types.DateTime
}
Expand Down Expand Up @@ -154,12 +154,12 @@ func updateAndGetAllInformation(packet nex.PRUDPPacketInterface) {
responseStream := nex.NewByteStreamOut(secureEndpoint.LibraryVersions(), secureEndpoint.ByteStreamSettings())

(&principalPreference{
ShowOnlinePresence: types.NewPrimitiveBool(true),
ShowCurrentTitle: types.NewPrimitiveBool(true),
BlockFriendRequests: types.NewPrimitiveBool(false),
ShowOnlinePresence: types.NewBool(true),
ShowCurrentTitle: types.NewBool(true),
BlockFriendRequests: types.NewBool(false),
}).WriteTo(responseStream)
(&comment{
Unknown: types.NewPrimitiveU8(0),
Unknown: types.NewUInt8(0),
Contents: types.NewString("Rewrite Test"),
LastChanged: types.NewDateTime(0),
}).WriteTo(responseStream)
Expand Down
20 changes: 11 additions & 9 deletions types/any_data_holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func RegisterDataHolderType(name string, rvType RVType) {
// Structure body, so the receiver can properly decode it.
type AnyDataHolder struct {
TypeName *String
Length1 *PrimitiveU32 // Length of ObjectData + Length2
Length2 *PrimitiveU32 // Length of ObjectData
Length1 *UInt32 // Length of ObjectData + Length2
Length2 *UInt32 // Length of ObjectData
ObjectData RVType
}

Expand Down Expand Up @@ -59,11 +59,13 @@ func (adh *AnyDataHolder) ExtractFrom(readable Readable) error {
return fmt.Errorf("Failed to read AnyDataHolder length 2. %s", err.Error())
}

if _, ok := AnyDataHolderObjects[adh.TypeName.Value]; !ok {
return fmt.Errorf("Unknown AnyDataHolder type: %s", adh.TypeName.Value)
typeName := string(*adh.TypeName)

if _, ok := AnyDataHolderObjects[typeName]; !ok {
return fmt.Errorf("Unknown AnyDataHolder type: %s", typeName)
}

adh.ObjectData = AnyDataHolderObjects[adh.TypeName.Value].Copy()
adh.ObjectData = AnyDataHolderObjects[typeName].Copy()

if err := adh.ObjectData.ExtractFrom(readable); err != nil {
return fmt.Errorf("Failed to read AnyDataHolder object data. %s", err.Error())
Expand All @@ -77,8 +79,8 @@ func (adh *AnyDataHolder) Copy() RVType {
copied := NewAnyDataHolder()

copied.TypeName = adh.TypeName.Copy().(*String)
copied.Length1 = adh.Length1.Copy().(*PrimitiveU32)
copied.Length2 = adh.Length2.Copy().(*PrimitiveU32)
copied.Length1 = adh.Length1.Copy().(*UInt32)
copied.Length2 = adh.Length2.Copy().(*UInt32)
copied.ObjectData = adh.ObjectData.Copy()

return copied
Expand Down Expand Up @@ -134,7 +136,7 @@ func (adh *AnyDataHolder) FormatToString(indentationLevel int) string {
func NewAnyDataHolder() *AnyDataHolder {
return &AnyDataHolder{
TypeName: NewString(""),
Length1: NewPrimitiveU32(0),
Length2: NewPrimitiveU32(0),
Length1: NewUInt32(0),
Length2: NewUInt32(0),
}
}
48 changes: 48 additions & 0 deletions types/bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package types

import "fmt"

// Bool is a type alias for the Go basic type bool for use as an RVType
type Bool bool

// WriteTo writes the Bool to the given writable
func (b Bool) WriteTo(writable Writable) {
writable.WritePrimitiveBool(bool(b))
}

// ExtractFrom extracts the Bool value from the given readable
func (b *Bool) ExtractFrom(readable Readable) error {
value, err := readable.ReadPrimitiveBool()
if err != nil {
return err
}

*b = Bool(value)
return nil
}

// Copy returns a pointer to a copy of the Bool. Requires type assertion when used
func (b Bool) Copy() RVType {
copy := b
return &copy
}

// Equals checks if the input is equal in value to the current instance
func (b Bool) Equals(o RVType) bool {
other, ok := o.(*Bool)
if !ok {
return false
}
return b == *other
}

// String returns a string representation of the Bool
func (b Bool) String() string {
return fmt.Sprintf("%t", b)
}

// NewBool returns a new Bool pointer
func NewBool(input bool) *Bool {
b := Bool(input)
return &b
}
36 changes: 19 additions & 17 deletions types/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package types

import (
"bytes"
"encoding/hex"
"fmt"
)

// Buffer is an implementation of rdv::Buffer.
// Wraps a primitive Go byte slice.
// Type alias of []byte.
// Same as QBuffer but with a uint32 length field.
type Buffer struct {
Value []byte
}
type Buffer []byte

// WriteTo writes the Buffer to the given writable
func (b *Buffer) WriteTo(writable Writable) {
length := len(b.Value)
func (b Buffer) WriteTo(writable Writable) {
length := len(b)

writable.WritePrimitiveUInt32LE(uint32(length))

if length > 0 {
writable.Write(b.Value)
writable.Write(b)
}
}

Expand All @@ -35,31 +34,34 @@ func (b *Buffer) ExtractFrom(readable Readable) error {
return fmt.Errorf("Failed to read NEX Buffer data. %s", err.Error())
}

b.Value = value

*b = Buffer(value)
return nil
}

// Copy returns a pointer to a copy of the Buffer. Requires type assertion when used
func (b *Buffer) Copy() RVType {
return NewBuffer(b.Value)
func (b Buffer) Copy() RVType {
copied := make(Buffer, len(b))
copy(copied, b)

return &copied
}

// Equals checks if the input is equal in value to the current instance
func (b *Buffer) Equals(o RVType) bool {
func (b Buffer) Equals(o RVType) bool {
if _, ok := o.(*Buffer); !ok {
return false
}

return bytes.Equal(b.Value, o.(*Buffer).Value)
return bytes.Equal(b, *o.(*Buffer))
}

// String returns a string representation of the struct
func (b *Buffer) String() string {
return fmt.Sprintf("%x", b.Value)
func (b Buffer) String() string {
return hex.EncodeToString(b)
}

// NewBuffer returns a new Buffer
func NewBuffer(data []byte) *Buffer {
return &Buffer{Value: data}
func NewBuffer(input []byte) *Buffer {
b := Buffer(input)
return &b
}
8 changes: 4 additions & 4 deletions types/class_version_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Contains version info for Structures used in verbose RMC messages.
type ClassVersionContainer struct {
Structure
ClassVersions *Map[*String, *PrimitiveU16]
ClassVersions *Map[*String, *UInt16]
}

// WriteTo writes the ClassVersionContainer to the given writable
Expand All @@ -25,7 +25,7 @@ func (cvc *ClassVersionContainer) ExtractFrom(readable Readable) error {
// Copy returns a pointer to a copy of the ClassVersionContainer. Requires type assertion when used
func (cvc *ClassVersionContainer) Copy() RVType {
copied := NewClassVersionContainer()
copied.ClassVersions = cvc.ClassVersions.Copy().(*Map[*String, *PrimitiveU16])
copied.ClassVersions = cvc.ClassVersions.Copy().(*Map[*String, *UInt16])

return copied
}
Expand Down Expand Up @@ -62,11 +62,11 @@ func (cvc *ClassVersionContainer) FormatToString(indentationLevel int) string {
// NewClassVersionContainer returns a new ClassVersionContainer
func NewClassVersionContainer() *ClassVersionContainer {
cvc := &ClassVersionContainer{
ClassVersions: NewMap[*String, *PrimitiveU16](),
ClassVersions: NewMap[*String, *UInt16](),
}

cvc.ClassVersions.KeyType = NewString("")
cvc.ClassVersions.ValueType = NewPrimitiveU16(0)
cvc.ClassVersions.ValueType = NewUInt16(0)

return cvc
}
Loading