-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusBytes.go
134 lines (123 loc) · 2.21 KB
/
StatusBytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package server
import (
"encoding/json"
"strings"
)
type StatusBit int
type StatusByte byte
const (
Protected StatusBit = 1 << iota
Dedicated
AllowOldClients
Started
Dynamix
WON
Unknown2
Unknown3
)
var statusBitString = []string{
"Protected",
"Dedicated",
"AllowOldClients",
"Started",
"Dynamix",
"WON",
"Unknown2",
"Unknown3",
}
type StatusByteStruct struct {
Protected bool
Dedicated bool
AllowOldClients bool
Started bool
Dynamix bool
WON bool
Unknown2 bool
Unknown3 bool
}
func (s StatusByteStruct) MarshalBinary() byte {
output := byte(0)
if s.Protected {
output |= byte(Protected)
}
if s.Dedicated {
output |= byte(Dedicated)
}
if s.AllowOldClients {
output |= byte(AllowOldClients)
}
if s.Started {
output |= byte(Started)
}
if s.Dynamix {
output |= byte(Dynamix)
}
if s.WON {
output |= byte(WON)
}
if s.Unknown2 {
output |= byte(Unknown2)
}
if s.Unknown3 {
output |= byte(Unknown3)
}
return output
}
func (s StatusByte) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Struct())
}
func (s StatusByte) Struct() StatusByteStruct {
output := StatusByteStruct{}
statusArr := make(map[StatusBit]bool)
for i := 0; i < 8; i++ {
bit := StatusBit(1 << i)
if int(s)&int(bit) != 0 {
statusArr[bit] = true
} else {
statusArr[bit] = false
}
}
for k, v := range statusArr {
switch k {
case Protected:
output.Protected = v
case Dedicated:
output.Dedicated = v
case AllowOldClients:
output.AllowOldClients = v
case Started:
output.Started = v
case Dynamix:
output.Dynamix = v
case WON:
output.WON = v
case Unknown2:
output.Unknown2 = v
case Unknown3:
output.Unknown3 = v
}
}
return output
}
func (s StatusByte) StringSlice() []string {
statusArr := make([]string, 0)
for i := 0; i < 8; i++ {
bit := StatusBit(1 << i)
if int(s)&int(bit) != 0 {
statusArr = append(statusArr, statusBitString[i])
}
}
return statusArr
}
func (s StatusByte) String() string {
statusArr := s.StringSlice()
var out strings.Builder
for k, v := range statusArr {
if k == len(statusArr)-1 {
out.WriteString(v)
break
}
out.WriteString(v + " | ")
}
return out.String()
}