-
Notifications
You must be signed in to change notification settings - Fork 7
/
textures.go
78 lines (66 loc) · 2.44 KB
/
textures.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
package minecraft
import (
"bytes"
"encoding/base64"
"encoding/json"
// If we work with PNGs we need this
_ "image/png"
"github.com/pkg/errors"
)
type SessionProfileTextureProperty struct {
TimestampMs uint64 `json:"timestamp"`
ProfileUUID string `json:"profileId"`
ProfileName string `json:"profileName"`
IsPublic bool `json:"isPublic"`
Textures struct {
Skin struct {
Metadata struct {
Model string `json:"model"`
} `json:"metadata"`
URL string `json:"url"`
} `json:"SKIN"`
Cape struct {
URL string `json:"url"`
} `json:"CAPE"`
} `json:"textures"`
}
// DecodeTextureProperty takes a SessionProfileResponse and breaks it down into the Skin/Cape URLs for downloading them
func DecodeTextureProperty(sessionProfile SessionProfileResponse) (SessionProfileTextureProperty, error) {
var texturesProperty *SessionProfileProperty
for _, v := range sessionProfile.Properties {
if v.Name == "textures" {
texturesProperty = &v
break
}
}
if texturesProperty == nil {
return SessionProfileTextureProperty{}, errors.New("unable to DecodeTextureProperty: no textures property")
}
profileTextureProperty := SessionProfileTextureProperty{}
// Base64 decode the texturesProperty and further decode the JSON from it into profileTextureProperty
err := json.NewDecoder(base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(texturesProperty.Value))).Decode(&profileTextureProperty)
if err != nil {
return SessionProfileTextureProperty{}, errors.Wrap(err, "unable to DecodeTextureProperty")
}
return profileTextureProperty, nil
}
func (mc *Minecraft) FetchTexturesWithSessionProfile(sessionProfile SessionProfileResponse) (User, Skin, Cape, error) {
// We have a sessionProfile!
user := &User{UUID: sessionProfile.UUID, Username: sessionProfile.Username}
skin := &Skin{Texture{Mc: mc}}
cape := &Cape{Texture{Mc: mc}}
profileTextureProperty, err := DecodeTextureProperty(sessionProfile)
if err != nil {
return *user, *skin, *cape, errors.Wrap(err, "failed to decode sessionProfile")
}
// We got oursleves a profileTextureProperty - now we can get a Skin/Cape
err = skin.FetchWithTextureProperty(profileTextureProperty, "Skin")
if err != nil {
return *user, *skin, *cape, errors.Wrap(err, "not able to retrieve skin")
}
err = cape.FetchWithTextureProperty(profileTextureProperty, "Cape")
if err != nil {
return *user, *skin, *cape, errors.Wrap(err, "not able to retrieve cape")
}
return *user, *skin, *cape, nil
}