-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile.go
170 lines (154 loc) · 3.22 KB
/
profile.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
package liter
import (
"encoding/base64"
"encoding/json"
"errors"
"image"
"image/color"
"image/png"
"io"
"net/http"
"sync"
)
type Property struct {
Name string `json:"name"`
Value string `json:"value"`
Sign string `json:"signature,omitempty"`
}
func (p *Property) UnmarshalJSONValue(ptr any) (err error) {
buf, err := base64.StdEncoding.DecodeString(p.Value)
if err != nil {
return
}
return json.Unmarshal(buf, ptr)
}
func (p *Property) Encode(b *PacketBuilder) {
b.String(p.Name)
b.String(p.Value)
hasSign := len(p.Sign) > 0
b.Bool(hasSign)
if hasSign {
b.String(p.Sign)
}
}
func (p *Property) DecodeFrom(r *PacketReader) (err error) {
var ok bool
if p.Name, ok = r.String(); !ok {
return io.EOF
}
if p.Value, ok = r.String(); !ok {
return io.EOF
}
var hasSign bool
if hasSign, ok = r.Bool(); !ok {
return io.EOF
}
if hasSign {
if p.Sign, ok = r.String(); !ok {
return io.EOF
}
} else {
p.Sign = ""
}
return
}
type PlayerProfile struct {
Id UUID `json:"id"`
Name string `json:"name"`
Properties []*Property `json:"properties`
ProfileActions []string `json:"profileActions"`
}
func (p *PlayerProfile) GetProp(name string) (t *Property) {
for _, t = range p.Properties {
if t.Name == name {
return
}
}
return nil
}
const (
skinSizeX = 64
skinSizeY = 64
)
// https://minecraft.wiki/w/Skin
type Skin struct {
// HTTP cache
Etag string
img image.Image
meta map[string]any
headFront image.Image
initOnce sync.Once
}
func (s *Skin) init() {
s.initOnce.Do(s._init)
}
func (s *Skin) _init() {
const headSize = 8
s.headFront = s.subImage(8, 8, headSize, headSize)
}
func (s *Skin) subImage(x, y, width, height int) image.Image {
img := image.NewNRGBA(image.Rect(0, 0, width, height))
for dx := 0; dx < width; dx++ {
for dy := 0; dy < height; dy++ {
x0, y0 := x+dx, y+dy
c := color.NRGBAModel.Convert(s.img.At(x0, y0)).(color.NRGBA)
if c.A == 0 {
c.R, c.G, c.B = 0, 0, 0
}
c.A = 0xff
img.Set(dx, dy, c)
}
}
return img
}
func (s *Skin) Head() image.Image {
s.init()
return s.headFront
}
func (p *PlayerProfile) getDefaultSkin() (skin *Skin, err error) {
return nil, errors.New("Not implemented for default skin yet")
}
func (p *PlayerProfile) GetSkin(cli *http.Client) (skin *Skin, err error) {
t := p.GetProp("textures")
if t == nil {
return p.getDefaultSkin()
}
type Textures struct {
Skin *struct {
Url string `json:"url"`
Meta map[string]any `json:"metadata"`
} `json:"SKIN,omitempty"`
Cape *struct {
Url string `json:"url"`
} `json:"CAPE,omitempty"`
}
var data struct {
Timestamp int64 `json:"timestamp"`
ProfileId string `json:"profileId"`
ProfileName string `json:"profileName"`
Textures Textures `json:"textures"`
}
if err = t.UnmarshalJSONValue(&data); err != nil {
return
}
if data.Textures.Skin == nil {
return p.getDefaultSkin()
}
if cli == nil {
cli = http.DefaultClient
}
res, err := cli.Get(data.Textures.Skin.Url)
if err != nil {
return
}
defer res.Body.Close()
skin = &Skin{
Etag: res.Header.Get("Etag"),
}
if skin.img, err = png.Decode(res.Body); err != nil {
skin = nil
return
}
skin.meta = data.Textures.Skin.Meta
return
}