forked from SolarLune/masterplan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.go
115 lines (88 loc) · 3.01 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
package main
import (
"image/gif"
"log"
"os"
"strings"
"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"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Resource struct {
// Path facing the object requesting the resouce (e.g. "~/home/pictures/test.png" or "https://solarlune.com/media/bartender.png")
ResourcePath string
// Where the resource is located on disk (e.g. "~/home/pictures/test.png" or "/tmp/masterplan_resource076755900.png", for downloaded resources)
LocalFilepath string
// Pointer to the data the resource stands for (e.g. a rl.Texture2D for an image)
Data interface{}
// Whether or not the resource is located in the temporary directory
// (e.g. was downloaded by MasterPlan, and so should be deleted after usage).
Temporary bool
// MIME data for the Resource.
MimeData *mimetype.MIME
}
func (project *Project) RegisterResource(resourcePath, localFilepath string, data interface{}) *Resource {
mime, _ := mimetype.DetectFile(localFilepath)
res := &Resource{
ResourcePath: resourcePath,
LocalFilepath: localFilepath,
Data: data,
MimeData: mime,
}
project.Resources[resourcePath] = res
return res
}
func (res *Resource) IsTexture() bool {
_, isTexture := res.Data.(rl.Texture2D)
return isTexture
}
func (res *Resource) Texture() rl.Texture2D {
return res.Data.(rl.Texture2D)
}
func (res *Resource) IsGIF() bool {
_, isGIF := res.Data.(*gif.GIF)
return isGIF
}
func (res *Resource) GIF() *gif.GIF {
return res.Data.(*gif.GIF)
}
func (res *Resource) IsAudio() bool {
return strings.Contains(res.MimeData.String(), "audio")
}
// Audio is special in that there is no resource to be shared between Tasks like with Images, as each Task
// should have its own stream it manages to play back audio. So instead, the resource's Audio() function returns
// a brand new stream pointing to the audio file. The Task (or whatever uses the Stream) has to handle closing
// the Stream when it's deleted (which it does in the ReceiveMessage() function when it is informed that it is
// going to be deleted).
func (res *Resource) Audio() (beep.StreamSeekCloser, beep.Format, error) {
var stream beep.StreamSeekCloser
var format beep.Format
var err error
if res.IsAudio() {
file, err := os.Open(res.LocalFilepath)
if err != nil {
log.Println("Could not open audio file: ", err.Error())
currentProject.Log("Could not open audio file: %s", err.Error())
} else {
switch ext := res.MimeData.Extension(); ext {
case ".wav":
stream, format, err = wav.Decode(file)
case ".flac":
stream, format, err = flac.Decode(file)
case ".ogg":
stream, format, err = vorbis.Decode(file)
case ".mp3":
stream, format, err = mp3.Decode(file)
}
if err != nil {
log.Println("Error decoding audio file: ", err.Error())
currentProject.Log("Error decoding audio file: %s", err.Error())
}
}
}
return stream, format, err
}