/
model.go
136 lines (120 loc) · 3.89 KB
/
model.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
package shortcut
import (
"fmt"
"strconv"
)
/*
"shortcuts": {
"0": {
"AllowDesktopConfig": 1,
"AllowOverlay": 1,
"AppName": "Insomnia",
"Devkit": 0,
"DevkitGameID": "",
"DevkitOverrideAppID": 0,
"Exe": "\"/usr/bin/flatpak\"",
"FlatpakAppID": "",
"IsHidden": 0,
"LastPlayTime": 0,
"LaunchOptions": "run --branch=stable --arch=x86_64 --command=/app/bin/insomnia --file-forwarding rest.insomnia.Insomnia --no-sandbox @@u @@",
"OpenVR": 0,
"ShortcutPath": "/var/lib/flatpak/exports/share/applications/rest.insomnia.Insomnia.desktop",
"StartDir": "\"/usr/bin/\"",
"appid": 3417544970,
"icon": "",
"tags": {}
}
}
}
*/
func NewShortcuts() *Shortcuts {
return &Shortcuts{Shortcuts: map[string]Shortcut{}}
}
type Shortcuts struct {
Shortcuts map[string]Shortcut `json:"shortcuts"`
}
// Add will add the given shortcut
func (s *Shortcuts) Add(shortcut *Shortcut) error {
nextKey, err := s.getNextKey()
if err != nil {
return err
}
s.Shortcuts[nextKey] = *shortcut
return nil
}
// LookupByName will return a shortcut by name
func (s *Shortcuts) LookupByName(name string) (*Shortcut, error) {
for _, sc := range s.Shortcuts {
if sc.AppName == name {
return &sc, nil
}
}
return nil, fmt.Errorf("no shortcut found with name: %v", name)
}
// LookupByID will return a shortcut by name
func (s *Shortcuts) LookupByID(appId int64) (*Shortcut, error) {
for _, sc := range s.Shortcuts {
if sc.Appid == appId {
return &sc, nil
}
}
return nil, fmt.Errorf("no shortcut found with id: %v", appId)
}
// Get the next shortcut id
func (s *Shortcuts) getNextKey() (string, error) {
highestKey := -1
for key := range s.Shortcuts {
keyNum, err := strconv.Atoi(key)
if err != nil {
return "", fmt.Errorf("Non-number shortcut key: %v", err)
}
if keyNum > highestKey {
highestKey = keyNum
}
}
return fmt.Sprintf("%v", highestKey+1), nil
}
// ShortcutSetting is a function that mutates a Shortcut
type ShortcutSetting func(s *Shortcut)
// DefaultShortcut sets the default settings of a shortcut
func DefaultShortcut(s *Shortcut) {
s.AllowDesktopConfig = 1
s.AllowOverlay = 1
}
// NewShortcut will return a new Steam Shortcut
func NewShortcut(name, exe string, settings ...ShortcutSetting) *Shortcut {
shortcut := &Shortcut{AppName: name, Exe: exe}
for _, setting := range settings {
setting(shortcut)
}
return shortcut
}
// Shortcut defines a single shortcut entry in the VDF file
type Shortcut struct {
AllowDesktopConfig int `json:"AllowDesktopConfig"`
AllowOverlay int `json:"AllowOverlay"`
AppName string `json:"AppName"`
Devkit int `json:"Devkit"`
DevkitGameID string `json:"DevkitGameID"`
DevkitOverrideAppID int `json:"DevkitOverrideAppID"`
Exe string `json:"Exe"`
FlatpakAppID string `json:"FlatpakAppID"`
IsHidden int `json:"IsHidden"`
LastPlayTime int `json:"LastPlayTime"`
LaunchOptions string `json:"LaunchOptions"`
OpenVR int `json:"OpenVR"`
ShortcutPath string `json:"ShortcutPath"`
StartDir string `json:"StartDir"`
Appid int64 `json:"appid"`
Icon string `json:"icon"`
Tags map[string]interface{} `json:"tags"`
Images *Images `json:"images,omitempty"`
}
// Images is a structure that holds the paths to grid images for a shortcut.
type Images struct {
Portrait string `json:"portrait"`
Landscape string `json:"landscape"`
Hero string `json:"hero"`
Logo string `json:"logo"`
Icon string `json:"icon"`
}