-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatars.go
91 lines (73 loc) · 2.29 KB
/
avatars.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
package oscvrc
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"os/user"
"runtime"
)
type AvatarParamConfig struct {
ID string `json:"id"`
Name string `json:"name"`
Parameters []ParameterConfig `json:"parameters"`
}
type ParameterConfig struct {
Name string `json:"name"`
Input InputConfig `json:"input,omitempty"`
Output OutputConfig `json:"output"`
}
type InputConfig struct {
Address string `json:"address"`
Type string `json:"type"`
client *Client
}
type OutputConfig struct {
Address string `json:"address"`
Type string `json:"type"`
}
func (ic *InputConfig) setInputClient(c *Client) {
ic.client = c
}
// ReadAvatarParamConfig reads the avatar parameter configuration from the specified file.
// The returned struct should be used in conjunction with the client.SendMessage function
func (c *Client) ReadAvatarParamConfig(avatarId, userId string) (AvatarParamConfig, error) {
var path string
user, err := user.Current()
if err != nil {
return AvatarParamConfig{}, fmt.Errorf("failed to get current user: %w", err)
}
switch runtime.GOOS {
case "windows":
path = fmt.Sprintf(`%s\AppData\LocalLow\VRChat\VRChat\OSC\%s\Avatars\%s.json`, user.HomeDir, userId, avatarId)
case "linux":
path = fmt.Sprintf(`%s/.local/share/Steam/steamapps/compatdata/438100/pfx/drive_c/users/steamuser/AppData/LocalLow/VRChat/VRChat/OSC/%s/Avatars/%s.json`, user.HomeDir, userId, avatarId)
default:
return AvatarParamConfig{}, errors.New("unsupported operating system")
}
data, err := os.ReadFile(path)
if err != nil {
return AvatarParamConfig{}, fmt.Errorf("failed to read file: %w", err)
}
data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf")) // remove BOM
var avatarParamConfig AvatarParamConfig
err = json.Unmarshal(data, &avatarParamConfig)
if err != nil {
return AvatarParamConfig{}, fmt.Errorf("failed to unmarshal json: %w", err)
}
for i := range avatarParamConfig.Parameters {
avatarParamConfig.Parameters[i].Input.setInputClient(c)
}
return avatarParamConfig, nil
}
func (i *InputConfig) Send(value ...interface{}) error {
if i.client == nil {
return errors.New("client not set")
}
err := i.client.SendMessage(i.Address, value...)
if err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
return nil
}