/
assets.go
82 lines (64 loc) · 2.05 KB
/
assets.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
package launcher
import (
"path/filepath"
"github.com/brawaru/marct/globstate"
"github.com/brawaru/marct/launcher/download"
"github.com/brawaru/marct/locales"
"github.com/brawaru/marct/utils/terrgroup"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
func (w *Instance) AssetIndexPath(id string) string {
return filepath.Join(w.Path, filepath.FromSlash(assetIndexesPath), id+".json") // FIXME: non-sanitised input
}
func (w *Instance) AssetsVirtualPath(id string) string {
return filepath.Join(w.Path, filepath.FromSlash(assetsVirtualPath), id) // FIXME: non-sanitised input
}
func (w *Instance) AssetsObjectsPath() string {
return filepath.Join(w.Path, filepath.FromSlash(assetsObjectsPath))
}
func (w *Instance) DefaultAssetsObjectResolver() AssetPathResolver {
of := w.AssetsObjectsPath()
return func(asset Asset) string {
return filepath.Join(of, asset.Path())
}
}
func (w *Instance) DownloadAssetIndex(descriptor AssetIndexDescriptor) error {
dest := w.AssetIndexPath(descriptor.ID)
if err := download.FromURL(descriptor.URL, dest, download.WithSHA1(descriptor.SHA1)); err != nil {
return err
}
return nil
}
func (w *Instance) ReadAssetIndex(id string) (i *AssetIndex, err error) {
err = unmarshalJSONFile(w.AssetIndexPath(id), &i)
return
}
func (w *Instance) DownloadAssets(index AssetIndex) error {
op := w.AssetsObjectsPath()
g, _ := terrgroup.New(8)
for n, a := range index.Objects {
// Clone variables, so they don't change in async execution later
name := n
asset := a
g.Go(func() error {
p := filepath.Join(op, filepath.FromSlash(asset.Path()))
if err := download.FromURL(asset.URL(), p, download.WithSHA1(asset.Hash)); err != nil {
return err
}
if globstate.VerboseLogs {
println(locales.TranslateUsing(&i18n.LocalizeConfig{
TemplateData: map[string]string{
"Asset": name,
"ID": asset.Hash,
},
DefaultMessage: &i18n.Message{
ID: "log.verbose.downloaded-asset",
Other: "downloaded asset {{ .ID }} ({{ .Asset }})",
},
}))
}
return nil
})
}
return g.Wait()
}