This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_string.go
175 lines (138 loc) · 4.47 KB
/
parse_string.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package steamid
import (
"errors"
"strconv"
"strings"
)
// ErrInvalidSteamID is returned when an attempt is made to parse an invalid
// SteamID.
var ErrInvalidSteamID = errors.New("invalid SteamID")
var invalidSteamID = FromValues(UniverseUnspecified, 0, AccountTypeInvalid, 0)
// ParseCommunityID creates a SteamID from a community ID and modifier.
//
// The account type is a non-negative number. The only valid account types for
// this function are AccountTypeIndividual and AccountTypeClan.
//
// If an invalid account type is given, this function will return an invalid
// SteamID.
func ParseCommunityID(id uint64, accountType AccountType) SteamID {
modifier := accountType.Modifier()
if modifier == 0 {
return invalidSteamID
}
authServer := uint8(id % 2)
accountID := uint32((id - modifier - uint64(authServer)) / 2)
return FromValues(0, 1, accountType, (accountID<<1)|uint32(authServer))
}
// Parse attempts to parse a SteamID from a string via automatic detection of
// the string's format. If it fails, it will return an invalid SteamID and
// ErrInvalidSteamID.
func Parse(steamID string) (SteamID, error) {
if steamIDUpper := strings.ToUpper(steamID); steamIDUpper == "UNKNOWN" || strings.HasPrefix(steamIDUpper, "STEAM_") {
return ParseV2(steamIDUpper)
}
if strings.HasPrefix(steamID, "[") && strings.HasSuffix(steamID, "]") {
return ParseV3(steamID)
}
return invalidSteamID, ErrInvalidSteamID
}
// ParseV2 attempts to parse a SteamID from the version 2 textual
// representation. For example, STEAM_0:0:1.
func ParseV2(steamID string) (SteamID, error) {
steamID = strings.ToUpper(steamID)
if steamID == "STEAM_ID_PENDING" {
return FromValues(UniverseUnspecified, 0, AccountTypePending, 0), nil
} else if steamID == "UNKNOWN" {
return FromValues(UniverseUnspecified, 0, AccountTypeInvalid, 0), nil
}
if !strings.HasPrefix(steamID, "STEAM_") {
return invalidSteamID, ErrInvalidSteamID
}
steamID = steamID[6:]
split := strings.Split(steamID, ":")
if len(split) != 3 {
return invalidSteamID, ErrInvalidSteamID
}
universe, err := strconv.ParseInt(split[0], 10, 8)
if err != nil {
return invalidSteamID, err
}
authServer, err := strconv.ParseUint(split[1], 10, 1)
if err != nil {
return invalidSteamID, err
}
accountID, err := strconv.ParseUint(split[2], 10, 31)
if err != nil {
return invalidSteamID, err
}
return FromValues(uint8(universe), 1, 1, (uint32(accountID)<<1)|uint32(authServer)), nil
}
// ParseV3 attempts to parse a SteamID from the version 3 textual
// representation. For example, [U:0:2].
func ParseV3(steamID string) (SteamID, error) {
steamID = strings.TrimPrefix(steamID, "[")
steamID = strings.TrimSuffix(steamID, "]")
split := strings.Split(steamID, ":")
if len(split) < 3 || len(split) > 4 {
return invalidSteamID, ErrInvalidSteamID
}
idType := split[0]
if idType != "U" && len(split) != 3 {
return invalidSteamID, ErrInvalidSteamID
}
universe, err := strconv.ParseInt(split[1], 10, 8)
if err != nil {
return invalidSteamID, err
}
accountID, err := strconv.ParseInt(split[2], 10, 32)
if err != nil {
return invalidSteamID, err
}
var accountType AccountType = 255
switch idType {
case "I":
accountType = AccountTypeInvalid
case "U":
if len(split) == 4 {
accountInstance, err := strconv.ParseInt(split[3], 10, 20)
if err != nil {
return invalidSteamID, err
}
return FromValues(uint8(universe), uint32(accountInstance), AccountTypeIndividual, uint32(accountID)), nil
}
return FromValues(uint8(universe), 1, AccountTypeIndividual, uint32(accountID)), nil
case "M":
accountType = AccountTypeMultiseat
case "G":
accountType = AccountTypeGameServer
case "A":
accountType = AccountTypeAnonGameServer
case "P":
accountType = AccountTypePending
case "C":
accountType = AccountTypeContentServer
case "g":
accountType = AccountTypeClan
case "c", "L", "T":
var accountInstance uint32
const (
instanceMask = 0x000FFFFF
clanFlag = (instanceMask + 1) >> 1
lobbyFlag = (instanceMask + 1) >> 2
matchmakingLobbyFlag = (instanceMask + 1) >> 3
)
switch idType {
case "c":
accountInstance = clanFlag
case "L":
accountInstance = lobbyFlag
case "T":
accountInstance = matchmakingLobbyFlag
}
return FromValues(uint8(universe), accountInstance, AccountTypeChat, uint32(accountID)), nil
}
if accountType == 255 {
return invalidSteamID, ErrInvalidSteamID
}
return FromValues(uint8(universe), 0, accountType, uint32(accountID)), nil
}