forked from itchio/butler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
208 lines (182 loc) · 4.03 KB
/
runtime.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package manager
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/itchio/butler/comm"
itchio "github.com/itchio/go-itchio"
)
type ItchPlatform string
const (
ItchPlatformOSX ItchPlatform = "osx"
ItchPlatformWindows = "windows"
ItchPlatformLinux = "linux"
ItchPlatformUnknown = "unknown"
)
// Runtime describes an os-arch combo in a convenient way
type Runtime struct {
Platform ItchPlatform `json:"platform"`
Is64 bool `json:"is64"`
}
func (r *Runtime) String() string {
var arch string
if r.Is64 {
arch = "64-bit"
} else {
arch = "32-bit"
}
return fmt.Sprintf("%s %s", arch, r.Platform)
}
func (r *Runtime) Arch() string {
if r.Is64 {
return "amd64"
}
return "386"
}
var cachedRuntime *Runtime
func CurrentRuntime() *Runtime {
if cachedRuntime == nil {
var is64 = is64Bit()
var platform ItchPlatform
switch runtime.GOOS {
case "linux":
platform = ItchPlatformLinux
case "darwin":
platform = ItchPlatformOSX
case "windows":
platform = ItchPlatformWindows
default:
platform = ItchPlatformUnknown
}
cachedRuntime = &Runtime{
Is64: is64,
Platform: platform,
}
}
return cachedRuntime
}
var win64Arches = map[string]bool{
"AMD64": true,
"IA64": true,
}
var hasDeterminedLinux64 = false
var cachedIsLinux64 bool
func is64Bit() bool {
switch runtime.GOOS {
case "darwin":
// we don't ship for 32-bit mac
return true
case "linux":
if !hasDeterminedLinux64 {
cachedIsLinux64 = determineLinux64()
hasDeterminedLinux64 = true
}
return cachedIsLinux64
case "windows":
// if we're currently running as a 64-bit executable then,
// yeah, we're on 64-bit windows
if runtime.GOARCH == "amd64" {
return true
}
// otherwise, check environment variables
// any value not in the map will return false (the zero value for bool ()
return win64Arches[os.Getenv("PROCESSOR_ARCHITECTURE")] ||
win64Arches[os.Getenv("PROCESSOR_ARCHITEW6432")]
}
// unsupported platform eh :(
return false
}
func determineLinux64() bool {
unameOutput, err := exec.Command("uname", "-m").Output()
if err != nil {
comm.Warnf("Could not determine if linux64 via uname: %s", err.Error())
} else {
return strings.TrimSpace(string(unameOutput)) == "x86_64"
}
archOutput, err := exec.Command("arch").Output()
if err != nil {
comm.Warnf("Could not determine if linux64 via uname: %s", err.Error())
} else {
return strings.TrimSpace(string(archOutput)) == "x86_64"
}
// if we're lacking uname AND arch, honestly, our chances are slim.
// but in doubt, let's just assume the architecture of butler is the
// same as the os
comm.Warnf("Falling back to build architecture for linux64 detection")
return runtime.GOARCH == "amd64"
}
type Platforms struct {
Linux bool
Windows bool
OSX bool
Android bool
}
func PlatformsForGame(game *itchio.Game) *Platforms {
return &Platforms{
Linux: game.Linux,
Windows: game.Windows,
OSX: game.OSX,
Android: game.Android,
}
}
func PlatformsForUpload(upload *itchio.Upload) *Platforms {
return &Platforms{
Linux: upload.Linux,
Windows: upload.Windows,
OSX: upload.OSX,
Android: upload.Android,
}
}
func (p *Platforms) IsCompatible(rt *Runtime) bool {
switch rt.Platform {
case ItchPlatformLinux:
return p.Linux
case ItchPlatformOSX:
return p.OSX
case ItchPlatformWindows:
return p.Windows
}
return false
}
// UploadExclusivityScore returns a higher value the closest an
// upload is to being *only for this platform*
func (p *Platforms) ExclusivityScore(rt *Runtime) int64 {
var score int64 = 400
switch rt.Platform {
case ItchPlatformLinux:
if p.OSX {
score -= 100
}
if p.Windows {
score -= 100
}
if p.Android {
score -= 200
}
case ItchPlatformOSX:
if p.Linux {
score -= 100
}
if p.Windows {
score -= 100
}
if p.Android {
score -= 200
}
case ItchPlatformWindows:
if p.Linux {
score -= 100
}
if p.OSX {
score -= 100
}
if p.Android {
score -= 200
}
default:
score = 0
}
return score
}