-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathresources.go
364 lines (271 loc) · 8.64 KB
/
resources.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"bufio"
"image/gif"
"log"
"math"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/cavaliergopher/grab/v3"
"github.com/faiface/beep"
"github.com/faiface/beep/flac"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/vorbis"
"github.com/faiface/beep/wav"
"github.com/gabriel-vasile/mimetype"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/sdl"
)
type ResourceBank map[string]*Resource
func NewResourceBank() ResourceBank {
return ResourceBank{}
}
func (resourceBank ResourceBank) Get(resourcePath string) *Resource {
resource, exists := resourceBank[resourcePath]
if !exists {
res, err := NewResource(resourcePath)
if err != nil {
return nil
}
resource = res
resourceBank[resourcePath] = res
}
return resource
}
func (resourceBank ResourceBank) Destroy() {
for resourceName, resource := range resourceBank {
if resource.Destructible {
resource.Destroy()
delete(resourceBank, resourceName)
}
}
}
type Resource struct {
Name string // The ID / name identifying the Resource; for offline files, this is the same as LocalFilepath
LocalFilepath string // The actual path to the file on-disk
Extension string
Data interface{} // The data the resource represents; this might be an image, a sound stream, etc.
MimeType string
Response *grab.Response
TempFile bool // Whether the file is temporary (and so should be deleted after MasterPlan closes) - downloaded images, for example, are temporary
SaveFile bool // Whether the file should be saved along with the project - pasted screenshots, as an example, are saved
Parsed bool
Destructible bool // System resources aren't able to be deleted
}
func NewResource(resourcePath string) (*Resource, error) {
resource := &Resource{
Name: resourcePath,
Destructible: true,
}
if _, err := os.ReadFile(resourcePath); err == nil {
resource.LocalFilepath = resourcePath
resource.Extension = filepath.Ext(resourcePath)
resource.Parse()
} else {
log.Println("possible online resource")
project := globals.Project
if globals.NextProject != nil {
project = globals.NextProject
}
destDir := project.Properties.Get(ProjectCacheDirectory).AsString()
if destDir == "" || !FolderExists(destDir) {
destDir = filepath.Join(os.TempDir(), "masterplan")
resource.TempFile = true
// It's online, so we don't need to save it
}
if req, err := grab.NewRequest("", resourcePath); err != nil {
return nil, err
} else {
unescapedPath, _ := url.QueryUnescape(req.URL().Path)
req.Filename = filepath.Join(destDir, filepath.FromSlash(req.URL().Hostname()+"/"+unescapedPath))
// It's already been downloaded
if FileExists(req.Filename) {
resource.LocalFilepath = req.Filename
resource.Extension = filepath.Ext(resource.LocalFilepath)
resource.Parse()
return resource, nil
}
resource.LocalFilepath = req.Filename
resource.Extension = filepath.Ext(resourcePath)
resource.Response = globals.GrabClient.Do(req) // This can take time, up to and including seconds to execute
if resource.Response.IsComplete() && resource.Response.Err() != nil {
return nil, resource.Response.Err()
} else if site, err := http.Get(resourcePath); err == nil {
// We're going to get what the Mime data is by downloading the first 5kb of a link, and then passing
// it into mimetype.Detect().
r := bufio.NewScanner(site.Body)
r.Buffer([]byte{}, 5000)
r.Scan()
resource.MimeType = mimetype.Detect(r.Bytes()).String()
}
}
}
return resource, nil
}
func (resource *Resource) Parse() {
// If the resource has already been parsed, then we can just skip it
if resource.Parsed {
return
}
mime, _ := mimetype.DetectFile(resource.LocalFilepath)
resource.MimeType = mime.String()
// if data, err := os.ReadFile(resource.LocalFilepath); err == nil {
// // We use mimetype because http.DetectContentType doesn't detect mp3 as being an audio file somehow
// resource.MimeType = mimetype.Detect(data).String()
// }
isTGA := resource.Extension == ".tga"
if isTGA || strings.Contains(resource.MimeType, "image") {
if strings.Contains(resource.MimeType, "gif") {
data, err := os.Open(resource.LocalFilepath)
if err != nil {
panic(err)
}
gifAnim, err := gif.DecodeAll(data)
if err != nil {
panic(err)
}
resource.Data = NewGifAnimation(gifAnim)
} else {
surface, err := img.Load(resource.LocalFilepath)
internalSizeSetting := globals.Settings.Get(SettingsMaxInternalImageSize).AsString()
internalSizeMax := int32(256)
switch internalSizeSetting {
case ImageBufferSize512:
internalSizeMax = 512
case ImageBufferSize1024:
internalSizeMax = 1024
case ImageBufferSize2048:
internalSizeMax = 2048
case ImageBufferSize4096:
internalSizeMax = 4096
case ImageBufferSize8192:
internalSizeMax = 8192
case ImageBufferSizeMax:
internalSizeMax = math.MaxInt32
}
if maxSize := SmallestRendererMaxTextureSize(); internalSizeMax > maxSize {
internalSizeMax = maxSize
}
w := surface.W
h := surface.H
// Image is too big to be displayed on our graphics card, we have to resize it
if w > internalSizeMax || h > internalSizeMax {
asr := float64(h) / float64(w)
if w > h {
w = internalSizeMax
h = int32(math.Ceil(float64(w) * asr))
} else if h > w {
h = internalSizeMax
w = int32(math.Ceil(float64(h) / asr))
} else {
w = internalSizeMax
h = internalSizeMax
}
newSurf, _ := sdl.CreateRGBSurfaceWithFormat(0, w, h, int32(surface.Format.BitsPerPixel), surface.Format.Format)
surface.BlitScaled(nil, newSurf, &sdl.Rect{0, 0, w, h})
ogSurf := surface
surface = newSurf
ogSurf.Free()
}
if err != nil {
panic(err)
}
// defer surface.Free()
texture, err := globals.Renderer.CreateTextureFromSurface(surface)
if err != nil {
panic(err)
}
texture.SetBlendMode(sdl.BLENDMODE_BLEND)
resource.Data = Image{
Size: Point{float32(surface.W), float32(surface.H)},
Texture: texture,
// Surface: surface,
}
}
} else if strings.Contains(resource.MimeType, "audio") {
// Sounds aren't shared, actually, so Resource.Data is nil for audio files.
} else {
globals.EventLog.Log("Warning: could not parse resource: %s", true, resource.Name)
}
resource.Parsed = true
}
// DownloadPercentage returns 0-1 as the Resource downloads, until it's finished downloading. Sometimes the download percentage is -1
// for some things (gifer does this, for example).
func (resource *Resource) DownloadPercentage() float64 {
if resource.Response == nil {
return 1
} else if resource.Response.Size() > 0 {
return resource.Response.Progress()
} else if resource.Response.IsComplete() {
return 1
}
return -1
}
func (resource *Resource) FinishedDownloading() bool {
return resource.Response == nil || resource.DownloadPercentage() >= 1
}
func (resource *Resource) IsTexture() bool {
if resource.FinishedDownloading() {
resource.Parse()
return resource.Extension == ".tga" || resource.MimeType != "image/gif" && strings.Contains(resource.MimeType, "image")
}
return false
}
func (resource *Resource) AsImage() Image {
resource.Parse()
return resource.Data.(Image)
}
func (resource *Resource) IsGIF() bool {
if resource.FinishedDownloading() {
resource.Parse()
return strings.Contains(resource.MimeType, "gif")
}
return false
}
func (resource *Resource) AsGIF() *GifAnimation {
resource.Parse()
return resource.Data.(*GifAnimation)
}
func (resource *Resource) IsSound() bool {
if resource.FinishedDownloading() {
resource.Parse()
return strings.Contains(resource.MimeType, "audio")
}
return false
}
func (resource *Resource) AsNewSound() (*Sound, error) {
originalFile, err := os.Open(resource.LocalFilepath)
if err != nil {
return nil, err
}
var originalStream beep.StreamSeekCloser
var format beep.Format
if resource.MimeType == "audio/mpeg" {
originalStream, format, err = mp3.Decode(originalFile)
} else if resource.MimeType == "audio/wav" {
originalStream, format, err = wav.Decode(originalFile)
} else if resource.MimeType == "audio/flac" {
originalStream, format, err = flac.Decode(originalFile)
} else if strings.Contains(resource.MimeType, "ogg") {
originalStream, format, err = vorbis.Decode(originalFile)
}
if err != nil {
return nil, err
}
return NewSound(originalStream, format), nil
}
func (resource *Resource) Destroy() {
if resource.TempFile {
os.Remove(resource.LocalFilepath)
}
if resource.IsTexture() {
resource.AsImage().Texture.Destroy()
}
if resource.IsGIF() {
resource.AsGIF().Destroy()
}
resource.Data = nil
}