-
Notifications
You must be signed in to change notification settings - Fork 1
/
assets.go
146 lines (131 loc) · 3.12 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
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
package asset
import "io"
type Assets struct {
FormatMap map[Type]Format
Formats []Format
Sources []Source
DefaultSource Source
Assets map[string]*Asset
NamedAssets map[string]*Asset
}
func NewAssets() Assets {
return Assets{
FormatMap: make(map[Type]Format),
Formats: make([]Format, 0, 16),
Sources: make([]Source, 0, 16),
Assets: make(map[string]*Asset, 128),
NamedAssets: make(map[string]*Asset),
}
}
func (assets *Assets) AddFormat(format Format) {
assets.Formats = append(assets.Formats, format)
types := format.Types()
if len(types) > 0 {
for _, t := range types {
assets.FormatMap[t] = format
}
}
}
func (assets *Assets) AddSource(source Source) {
assets.Sources = append(assets.Sources, source)
}
func (assets *Assets) Add(ref Ref) *Asset {
if ref.Name != "" && ref.URI == "" {
return assets.NamedAssets[ref.Name]
}
if loaded, ok := assets.Assets[ref.URI]; ok {
return loaded
}
asset := &Asset{
Ref: ref,
}
for _, source := range assets.Sources {
if source.Handles(ref) {
asset.Source = source
break
}
}
if asset.Source == nil {
asset.Source = assets.DefaultSource
}
for _, format := range assets.Formats {
if format.Handles(ref) {
asset.Format = format
break
}
}
if asset.Format == nil && ref.Type != TypeUnknown {
if typeLoader, ok := assets.FormatMap[ref.Type]; ok {
asset.Format = typeLoader
}
}
assets.Assets[ref.URI] = asset
if ref.Name != "" && assets.NamedAssets[ref.Name] == nil {
assets.NamedAssets[ref.Name] = asset
}
return asset
}
func (assets *Assets) AddMany(refs []Ref) []*Asset {
many := make([]*Asset, len(refs))
if len(refs) > 0 {
for i, ref := range refs {
many[i] = assets.Add(ref)
}
}
return many
}
func (assets *Assets) AddManyMap(refs []Ref) map[string]*Asset {
many := make(map[string]*Asset)
if len(refs) > 0 {
for _, ref := range refs {
asset := assets.Add(ref)
many[asset.Ref.URI] = asset
}
}
return many
}
func (assets *Assets) AddDefaults() {
assets.DefaultSource = &LocalAssetSource{}
assets.AddSource(LocalAssetSource{})
assets.AddSource(WebAssetSource{})
assets.AddSource(EmbedAssetSource{})
assets.AddFormat(&XmlGenericAssetFormat{})
assets.AddFormat(&JsonGenericAssetFormat{})
}
func (assets *Assets) Destroy() {
for _, asset := range assets.Assets {
if asset.ActivateStatus.IsSuccess() {
asset.Format.Deactivate(asset) // ignore error for now
}
if asset.LoadStatus.IsSuccess() {
asset.Format.Unload(asset) // ignore error for now
}
if asset.SourceReader != nil {
if closer, ok := asset.SourceReader.(io.Closer); ok {
closer.Close()
}
}
}
}
func (assets *Assets) Get(uri string) *Asset {
return assets.Assets[uri]
}
func (assets *Assets) GetNamed(name string) *Asset {
return assets.NamedAssets[name]
}
func (assets *Assets) GetEither(nameOrURI string) *Asset {
asset := assets.Assets[nameOrURI]
if asset != nil {
return asset
}
return assets.NamedAssets[nameOrURI]
}
func (assets *Assets) GetRef(ref Ref) *Asset {
if ref.URI != "" {
return assets.Assets[ref.URI]
}
if ref.Name != "" {
return assets.NamedAssets[ref.Name]
}
return nil
}