Skip to content

Commit 0ef0621

Browse files
authored
Merge pull request #3 from Michelprogram/refacto
Refacto
2 parents d1418d3 + f465e92 commit 0ef0621

35 files changed

Lines changed: 1284 additions & 873 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ go.work
2626

2727
# End of https://www.toptal.com/developers/gitignore/api/go
2828

29-
ressources
29+
ressources
30+
main.go

command/command.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module michelprogram/photon-parser
22

33
go 1.25.0
4-
5-
require golang.org/x/exp v0.0.0-20260312153236-7ab1446f8b90 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
golang.org/x/exp v0.0.0-20260312153236-7ab1446f8b90 h1:jiDhWWeC7jfWqR9c/uplMOqJ0sbNlNWv0UkzE0vX1MA=
2-
golang.org/x/exp v0.0.0-20260312153236-7ab1446f8b90/go.mod h1:xE1HEv6b+1SCZ5/uscMRjUBKtIxworgEcEi+/n9NQDQ=
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package acknowledge
2+
3+
import "michelprogram/photon-parser/internal/reader"
4+
5+
type Acknowledge struct{}
6+
7+
var _ reader.Parseable = (*Acknowledge)(nil)
8+
9+
func (a Acknowledge) Parse(r *reader.Reader) error {
10+
return nil
11+
}

internal/command/command.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"michelprogram/photon-parser/internal/command/acknowledge"
6+
"michelprogram/photon-parser/internal/command/ping"
7+
"michelprogram/photon-parser/internal/command/sendReliable"
8+
"michelprogram/photon-parser/internal/reader"
9+
)
10+
11+
var _ reader.Parseable = (*Command)(nil)
12+
13+
// ParseFromReader parses a Photon command from a parser.Reader.
14+
// It first reads the 12-byte command header, validates the length field,
15+
// then reads the remaining payload data.
16+
//
17+
// Returns an error if:
18+
// - The header cannot be read
19+
// - The length field is smaller than the header size (invalid)
20+
// - The payload data cannot be fully read
21+
//
22+
// The returned Command struct contains all header fields and the raw payload
23+
// in the Data field. For SendReliable commands, the Data can be further parsed
24+
// using the reliable package.
25+
func (c *Command) Parse(r *reader.Reader) error {
26+
header, err := c.parseHeader(r)
27+
if err != nil {
28+
return err
29+
}
30+
31+
if header.Length < HEADER_SIZE {
32+
return fmt.Errorf("command length %d smaller than header size %d", header.Length, HEADER_SIZE)
33+
}
34+
35+
c.Header = header
36+
parsed, err := c.parsePayload(header.Type, r)
37+
if err != nil {
38+
rest, _ := r.ReadBytes(int(header.Length - HEADER_SIZE))
39+
// don't fatal — just store raw for encrypted packets
40+
c.Payload = UnknownPayload{Raw: rest, Kind: header.Type}
41+
}else{
42+
43+
}
44+
c.Payload = parsed
45+
46+
return nil
47+
}
48+
49+
func (c Command) parsePayload(t Type, r *reader.Reader) (reader.Payload, error) {
50+
switch t {
51+
case SendReliable:
52+
sd := sendReliable.Reliable{}
53+
err := sd.Parse(r)
54+
if err != nil {
55+
return nil, err
56+
}
57+
return sd, nil
58+
case Ping:
59+
ping := &ping.Ping{}
60+
ping.Parse(r)
61+
return ping, nil
62+
case Acknowledge:
63+
acknowledge := &acknowledge.Acknowledge{}
64+
acknowledge.Parse(r)
65+
return acknowledge, nil
66+
default:
67+
return nil, fmt.Errorf("unknown")
68+
}
69+
}
70+
71+
func (s *Command) parseHeader(r *reader.Reader) (Header, error) {
72+
var err error
73+
var header Header
74+
75+
b, err := r.ReadUInt8()
76+
if err != nil {
77+
return Header{}, err
78+
}
79+
80+
header.Type = Type(b)
81+
82+
header.ChannelID, err = r.ReadUInt8()
83+
if err != nil {
84+
return Header{}, err
85+
}
86+
87+
header.Flags, err = r.ReadUInt8()
88+
if err != nil {
89+
return Header{}, err
90+
}
91+
92+
header.ReservedByte, err = r.ReadUInt8()
93+
if err != nil {
94+
return Header{}, err
95+
}
96+
97+
header.Length, err = r.ReadUInt32()
98+
if err != nil {
99+
return Header{}, err
100+
}
101+
102+
header.ReliableSequenceNumber, err = r.ReadUInt32()
103+
if err != nil {
104+
return Header{}, err
105+
}
106+
107+
return header, nil
108+
}
109+
110+
func (c Command) String() string {
111+
return fmt.Sprintf("Type: %d, ChannelID: %d, Flags: %d, ReservedByte: %d, Length: %d, ReliableSequenceNumber: %d", c.Type, c.ChannelID, c.Flags, c.ReservedByte, c.Length, c.ReliableSequenceNumber)
112+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package command_test
22

33
import (
4-
"michelprogram/photon-parser/command"
5-
"michelprogram/photon-parser/parser"
4+
"michelprogram/photon-parser/internal/command"
5+
"michelprogram/photon-parser/internal/reader"
66
"testing"
77
)
88

99
func TestParseSession(t *testing.T) {
1010

1111
payload := []byte{0x6, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x5e, 0x0, 0x0, 0x0, 0xe, 0xf3, 0x4, 0x1, 0x0, 0x29, 0x0, 0x69, 0x0, 0x4, 0x6c, 0xcb, 0x1, 0x73, 0x0, 0xd, 0x4a, 0x4f, 0x53, 0x45, 0x47, 0x41, 0x4d, 0x45, 0x52, 0x33, 0x31, 0x31, 0x33, 0x2, 0x62, 0x73, 0x3, 0x62, 0x1a, 0x5, 0x78, 0x0, 0x0, 0x0, 0x5, 0x1, 0x0, 0x3, 0x2, 0x5, 0x6, 0x78, 0x0, 0x0, 0x0, 0x5, 0x0, 0x3, 0xa, 0x0, 0x5, 0x7, 0x78, 0x0, 0x0, 0x0, 0x10, 0x12, 0x29, 0xd, 0xf6, 0x67, 0x16, 0x0, 0x43, 0xa8, 0xfa, 0x58, 0x55, 0x10, 0x2c, 0x4c, 0xcf, 0xa, 0x73, 0x0, 0x0, 0xb, 0x69, 0x0, 0x0, 0x0, 0x0, 0xc, 0x69, 0x0, 0x0, 0x0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x62, 0x1, 0x4, 0x69, 0x48, 0xd9, 0x3c, 0x1, 0x5, 0x73, 0x0, 0xd, 0x53, 0x61, 0x62, 0x65, 0x72, 0x6c, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x31, 0x6, 0x62, 0x2, 0x7, 0x69, 0xf, 0xa8, 0xfd, 0x5e, 0x8, 0x79, 0x0, 0x1, 0x6b, 0xf, 0x10, 0x9, 0x78, 0x0, 0x0, 0x0, 0x1, 0xac, 0xa, 0x62, 0x0, 0xfc, 0x6b, 0x0, 0x1e}
1212

13-
reader := parser.NewReader(payload)
14-
res, err := command.ParseFromReader(reader)
13+
reader := reader.NewReader(payload)
14+
cmd := &command.Command{}
15+
err := cmd.Parse(reader)
1516

1617
if err != nil {
1718
t.Fatalf("LoadFromWiresharkExport() failed: %v", err)
1819
}
1920

20-
if res.Length != 350 {
21+
if cmd.Length != 350 {
2122
t.Fatalf("LoadFromWiresharkExport() failed: %v", err)
2223
}
2324
}

internal/command/ping/ping.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ping
2+
3+
import "michelprogram/photon-parser/internal/reader"
4+
5+
type Ping struct{}
6+
7+
var _ reader.Parseable = (*Ping)(nil)
8+
9+
func (p Ping) Parse(r *reader.Reader) error {
10+
return nil
11+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package sendReliable
2+
3+
import (
4+
"fmt"
5+
"michelprogram/photon-parser/internal/reader"
6+
"michelprogram/photon-parser/internal/parameters"
7+
)
8+
9+
// HEADER_SIZE is the size in bytes of a reliable message header (5 bytes).
10+
const HEADER_SIZE = 5
11+
12+
// Type represents a Photon reliable message type.
13+
type Type uint8
14+
15+
// Photon Protocol reliable message types.
16+
// These define the different kinds of reliable messages that can be exchanged.
17+
const (
18+
OperationRequest Type = 0x02 // Client requests an operation
19+
OperationResponse Type = 0x07 // Server responds to an operation
20+
OtherOperationResponse Type = 0x03 // Alternative response format
21+
EventDataType Type = 0x04 // Server sends an event to client
22+
ExchangeKeys Type = 0x06 // Key exchange for encryption
23+
)
24+
25+
// Header represents the reliable message header.
26+
// This appears at the start of the payload in SendReliable commands.
27+
type Header struct {
28+
Signature uint8 `json:"signature"` // Message signature (typically 0xF3)
29+
Type Type `json:"type"` // Message type (operation, event, etc.)
30+
EventCode uint8 `json:"event_code"` // Operation/event code (application-specific)
31+
ParameterCount uint16 `json:"parameter_count"` // Number of parameters following this header
32+
}
33+
34+
// Reliable represents a complete reliable message with header and parameters.
35+
// Parameters contain the actual game data as key-value pairs where each
36+
// parameter has an ID, type, and value.
37+
type Reliable struct {
38+
Header
39+
Parameters []parameters.Parameters // Slice of decoded parameters
40+
}
41+
42+
var _ reader.Parseable = (*Reliable)(nil)
43+
44+
// ParseFromReader parses a Photon reliable message from a parser.Reader.
45+
// It reads the 5-byte header, then iterates through and parses each parameter
46+
// as specified by the ParameterCount field.
47+
//
48+
// The message format consists of:
49+
// - Header (5 bytes: signature, type, event code, parameter count)
50+
// - Parameters (ParameterCount entries, each with ID, type, and value)
51+
//
52+
// Returns a Reliable struct with all fields populated including the Parameters slice,
53+
// or an error if any part of parsing fails.
54+
func (r *Reliable) Parse(reader *reader.Reader) error {
55+
var param parameters.Parameters
56+
header, err := r.parseHeader(reader)
57+
if err != nil {
58+
return err
59+
}
60+
61+
if header.Signature != 0xF3 {
62+
return fmt.Errorf("encrypted or unknown packet, signature: 0x%02x", header.Signature)
63+
}
64+
65+
r.Header = header
66+
67+
r.Parameters = make([]parameters.Parameters, header.ParameterCount)
68+
69+
for i := uint16(0); i < r.ParameterCount; i++ {
70+
param = parameters.Parameters{}
71+
err := param.Parse(reader)
72+
if err != nil {
73+
return err
74+
}
75+
r.Parameters[i] = param
76+
}
77+
78+
return nil
79+
}
80+
81+
func (r *Reliable) parseHeader(reader *reader.Reader) (Header, error) {
82+
var err error
83+
var header Header
84+
85+
header.Signature, err = reader.ReadUInt8()
86+
if err != nil {
87+
return Header{}, err
88+
}
89+
90+
b, err := reader.ReadUInt8()
91+
if err != nil {
92+
return Header{}, err
93+
}
94+
95+
header.Type = Type(b)
96+
97+
header.EventCode, err = reader.ReadUInt8()
98+
if err != nil {
99+
return Header{}, err
100+
}
101+
102+
header.ParameterCount, err = reader.ReadUInt16()
103+
if err != nil {
104+
return Header{}, err
105+
}
106+
107+
return header, nil
108+
}

reliable/reliable_test.go renamed to internal/command/sendReliable/reliable_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
package reliable_test
1+
package sendReliable_test
22

33
import (
44
"log"
5-
"michelprogram/photon-parser/parser"
6-
"michelprogram/photon-parser/reliable"
5+
"michelprogram/photon-parser/internal/reader"
6+
"michelprogram/photon-parser/internal/command/sendReliable"
77
"testing"
88
)
99

1010
func TestReliableParsing(t *testing.T) {
1111

1212
payload := []byte{0xf3, 0x4, 0x1, 0x0, 0x29, 0x0, 0x69, 0x0, 0x4, 0x6c, 0xcb, 0x1, 0x73, 0x0, 0xd, 0x4a, 0x4f, 0x53, 0x45, 0x47, 0x41, 0x4d, 0x45, 0x52, 0x33, 0x31, 0x31, 0x33, 0x2, 0x62, 0x73, 0x3, 0x62, 0x1a, 0x5, 0x78, 0x0, 0x0, 0x0, 0x5, 0x1, 0x0, 0x3, 0x2, 0x5, 0x6, 0x78, 0x0, 0x0, 0x0, 0x5, 0x0, 0x3, 0xa, 0x0, 0x5, 0x7, 0x78, 0x0, 0x0, 0x0, 0x10, 0x12, 0x29, 0xd, 0xf6, 0x67, 0x16, 0x0, 0x43, 0xa8, 0xfa, 0x58, 0x55, 0x10, 0x2c, 0x4c, 0xcf, 0xa, 0x73, 0x0, 0x0, 0xb, 0x69, 0x0, 0x0, 0x0, 0x0, 0xc, 0x69, 0x0, 0x0, 0x0, 0x0, 0xd, 0x73, 0x0, 0x0, 0x10, 0x78, 0x0, 0x0, 0x0, 0x8, 0xc2, 0x46, 0xfb, 0xd4, 0xdd, 0xd8, 0xbd, 0xe8, 0x11, 0x78, 0x0, 0x0, 0x0, 0x8, 0xc2, 0x46, 0xfb, 0xd4, 0xdd, 0xd8, 0xbd, 0xe8, 0x12, 0x69, 0x1, 0x96, 0xb9, 0x9, 0x13, 0x66, 0x40, 0x12, 0x99, 0x68, 0x14, 0x66, 0x40, 0x9e, 0x66, 0x66, 0x16, 0x66, 0x44, 0xc1, 0xc0, 0x0, 0x17, 0x66, 0x44, 0xc1, 0xc0, 0x0, 0x19, 0x66, 0x41, 0x78, 0x2, 0xde, 0x1a, 0x69, 0x1, 0x96, 0xbd, 0x52, 0x1b, 0x66, 0x43, 0x20, 0x0, 0x0, 0x1c, 0x66, 0x43, 0x20, 0x0, 0x0, 0x1e, 0x66, 0x3f, 0xff, 0x95, 0x84, 0x1f, 0x69, 0x1, 0x96, 0xbc, 0xf3, 0x20, 0x66, 0x44, 0xbb, 0xe0, 0x0, 0x21, 0x66, 0x44, 0xbb, 0xe0, 0x0, 0x23, 0x66, 0x41, 0xb6, 0x3d, 0x71, 0x24, 0x69, 0x1, 0x96, 0xbd, 0x53, 0x25, 0x66, 0x47, 0x43, 0x19, 0xf2, 0x26, 0x6c, 0x8, 0xd8, 0xa9, 0xfd, 0x9a, 0xb5, 0x35, 0xc1, 0x27, 0x62, 0x0, 0x28, 0x79, 0x0, 0xa, 0x6b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe, 0x1f, 0xb, 0x26, 0x15, 0x97, 0xb, 0xab, 0x0, 0x0, 0x0, 0x0, 0x2b, 0x79, 0x0, 0xe, 0x6b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf, 0x5e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x33, 0x73, 0x0, 0x0, 0x35, 0x62, 0x0, 0x36, 0x62, 0x0, 0x37, 0x79, 0x0, 0x7, 0x6b, 0x0, 0xc, 0xff, 0xff, 0x0, 0x33, 0x0, 0xaf, 0x0, 0xf7, 0xff, 0xff, 0x1, 0x43, 0x38, 0x62, 0x4, 0x39, 0x62, 0xc, 0x3f, 0x69, 0x0, 0x0, 0x0, 0x0, 0xfc, 0x6b, 0x0, 0x1d}
1313

14-
reader := parser.NewReader(payload)
15-
res, err := reliable.ParseFromReader(reader)
14+
reader := reader.NewReader(payload)
15+
sd := sendReliable.Reliable{}
16+
err := sd.Parse(reader)
1617

1718
if err != nil {
1819
t.Fatalf("LoadFromWiresharkExport() failed: %v", err)
1920
}
2021

21-
log.Println(res)
22+
log.Println(sd)
2223

2324
}

0 commit comments

Comments
 (0)