-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtypes.go
131 lines (102 loc) · 2.71 KB
/
types.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
package launch
import (
"context"
"os"
"path/filepath"
"time"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/cmd/operate"
"github.com/itchio/butler/filtering"
"github.com/itchio/ox"
"github.com/itchio/pelican"
"github.com/itchio/wharf/tlc"
"github.com/pkg/errors"
"github.com/itchio/dash"
)
type LaunchStrategy string
const (
LaunchStrategyUnknown LaunchStrategy = ""
LaunchStrategyNative LaunchStrategy = "native"
LaunchStrategyHTML LaunchStrategy = "html"
LaunchStrategyURL LaunchStrategy = "url"
LaunchStrategyShell LaunchStrategy = "shell"
)
type LauncherParams struct {
RequestContext *butlerd.RequestContext
Ctx context.Context
// If relative, it's relative to the WorkingDirectory
FullTargetPath string
// May be nil
PeInfo *pelican.PeInfo
// May be nil
Candidate *dash.Candidate
// Lazily computed
installContainer *tlc.Container
// May be nil
AppManifest *butlerd.Manifest
// May be nil
Action *butlerd.Action
// If true, enable sandbox
Sandbox bool
// Additional command-line arguments
Args []string
// Additional environment variables
Env map[string]string
PrereqsDir string
ForcePrereqs bool
Access *operate.GameAccess
InstallFolder string
Runtime *ox.Runtime
RecordPlayTime RecordPlayTimeFunc
}
// cf. https://github.com/itchio/itch/issues/1751
var ignoredInstallContainerPatterns = []string{
"node_modules",
}
func (lp *LauncherParams) GetInstallContainer() (*tlc.Container, error) {
if lp.installContainer == nil {
var err error
lp.installContainer, err = tlc.WalkDir(lp.InstallFolder, &tlc.WalkOpts{
Filter: func(fileInfo os.FileInfo) bool {
if !filtering.FilterPaths(fileInfo) {
return false
}
for _, pattern := range ignoredInstallContainerPatterns {
match, _ := filepath.Match(pattern, fileInfo.Name())
if match {
return false
}
}
return true
},
})
if err != nil {
return nil, errors.WithStack(err)
}
}
return lp.installContainer, nil
}
func (lp *LauncherParams) SniffFile(fileEntry *tlc.File) (*dash.Candidate, error) {
f, err := os.Open(filepath.Join(lp.InstallFolder, fileEntry.Path))
if err != nil {
return nil, errors.WithStack(err)
}
defer f.Close()
stats, err := f.Stat()
if err != nil {
return nil, errors.WithStack(err)
}
candidate, err := dash.Sniff(f, fileEntry.Path, stats.Size())
if err != nil {
return nil, errors.WithStack(err)
}
return candidate, nil
}
type RecordPlayTimeFunc func(playTime time.Duration) error
type Launcher interface {
Do(params LauncherParams) error
}
var launchers = make(map[LaunchStrategy]Launcher)
func RegisterLauncher(strategy LaunchStrategy, launcher Launcher) {
launchers[strategy] = launcher
}