summaryrefslogtreecommitdiff
path: root/teleirc/matterbridge/vendor/github.com/Philipp15b/go-steam/steamid/steamid.go
blob: 922bb2446f0852595afc1083c74a0fd1b13c8fe8 (plain)
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
package steamid

import (
	"fmt"
	"strconv"
	"errors"
	"regexp"
	"strings"
)

type ChatInstanceFlag uint64

const (
	Clan     ChatInstanceFlag = 0x100000 >> 1
	Lobby    ChatInstanceFlag = 0x100000 >> 2
	MMSLobby ChatInstanceFlag = 0x100000 >> 3
)

type SteamId uint64

func NewId(id string) (SteamId, error) {
	valid, err := regexp.MatchString(`STEAM_[0-5]:[01]:\d+`, id)
	if err != nil {
		return SteamId(0), err
	}
	if valid {
		id = strings.Replace(id, "STEAM_", "", -1) // remove STEAM_

		splitid := strings.Split(id, ":")          // split 0:1:00000000 into 0 1 00000000

		universe, _ := strconv.ParseInt(splitid[0], 10, 32)
		if universe == 0 { //EUniverse_Invalid

			universe = 1 //EUniverse_Public

		}
		authServer, _ := strconv.ParseUint(splitid[1], 10, 32)
		accId, _ := strconv.ParseUint(splitid[2], 10, 32)
		accountType := int32(1) //EAccountType_Individual

		accountId := (uint32(accId) << 1) | uint32(authServer)
		return NewIdAdv(uint32(accountId), 1, int32(universe), accountType), nil
	} else {
		newid, err := strconv.ParseUint(id, 10, 64)
		if err != nil {
			return SteamId(0), err
		}
		return SteamId(newid), nil
	}
	return SteamId(0), errors.New(fmt.Sprintf("Invalid SteamId: %s\n", id))
}

func NewIdAdv(accountId, instance uint32, universe int32, accountType int32) SteamId {
	s := SteamId(0)
	s = s.SetAccountId(accountId)
	s = s.SetAccountInstance(instance)
	s = s.SetAccountUniverse(universe)
	s = s.SetAccountType(accountType)
	return s
}

func (s SteamId) ToUint64() uint64 {
	return uint64(s)
}

func (s SteamId) ToString() string {
	return strconv.FormatUint(uint64(s), 10)
}

func (s SteamId) String() string {
	switch s.GetAccountType() {
	case 0: // EAccountType_Invalid

		fallthrough
	case 1: // EAccountType_Individual

		if s.GetAccountUniverse() <= 1 { // EUniverse_Public

			return fmt.Sprintf("STEAM_0:%d:%d", s.GetAccountId()&1, s.GetAccountId()>>1)
		} else {
			return fmt.Sprintf("STEAM_%d:%d:%d", s.GetAccountUniverse(), s.GetAccountId()&1, s.GetAccountId()>>1)
		}
	default:
		return strconv.FormatUint(uint64(s), 10)
	}
}

func (s SteamId) get(offset uint, mask uint64) uint64 {
	return (uint64(s) >> offset) & mask
}

func (s SteamId) set(offset uint, mask, value uint64) SteamId {
	return SteamId((uint64(s) & ^(mask << offset)) | (value&mask)<<offset)
}

func (s SteamId) GetAccountId() uint32 {
	return uint32(s.get(0, 0xFFFFFFFF))
}

func (s SteamId) SetAccountId(id uint32) SteamId {
	return s.set(0, 0xFFFFFFFF, uint64(id))
}

func (s SteamId) GetAccountInstance() uint32 {
	return uint32(s.get(32, 0xFFFFF))
}

func (s SteamId) SetAccountInstance(value uint32) SteamId {
	return s.set(32, 0xFFFFF, uint64(value))
}

func (s SteamId) GetAccountType() int32 {
	return int32(s.get(52, 0xF))
}

func (s SteamId) SetAccountType(t int32) SteamId {
	return s.set(52, 0xF, uint64(t))
}

func (s SteamId) GetAccountUniverse() int32 {
	return int32(s.get(56, 0xF))
}

func (s SteamId) SetAccountUniverse(universe int32) SteamId {
	return s.set(56, 0xF, uint64(universe))
}

//used to fix the Clan SteamId to a Chat SteamId

func (s SteamId) ClanToChat() SteamId {
	if s.GetAccountType() == int32(7) { //EAccountType_Clan

		s = s.SetAccountInstance(uint32(Clan))
		s = s.SetAccountType(8) //EAccountType_Chat

	}
	return s
}

//used to fix the Chat SteamId to a Clan SteamId

func (s SteamId) ChatToClan() SteamId {
	if s.GetAccountType() == int32(8) { //EAccountType_Chat

		s = s.SetAccountInstance(0)
		s = s.SetAccountType(int32(7)) //EAccountType_Clan

	}
	return s
}