-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a default filesystem loader for OpenAssetFunc #8
Comments
For your example, I think it can be simplified a bit. Unless you really want to eagerly read the file. Since there is caching, either way would work. l.OpenAssetFunc = func(path string) io.ReadCloser {
// May want to check for an error just in case.
f, _ := os.Open(path)
return f
} For games that have 100% of their assets embedded, it would look like this: //go:embed all:_data
var gameAssets embed.FS
func OpenAsset(path string) io.ReadCloser {
f, err := gameAssets.Open("_data/" + path)
if err != nil {
panic(err)
}
return f
} For games that may want to have external resources it can be a bit more complicated. I would suggest adding some magic prefix to the resource paths, so OpenAsset may understand that: func MakeOpenAssetFunc(dataFolder string) func(path string) io.ReadCloser {
return func(path string) io.ReadCloser {
if strings.HasPrefix(path, "$") {
// An external file.
f, err := os.Open(filepath.Join(dataFolder, path[len("$"):]))
if err != nil {
panic(err)
}
return f
}
// An embedded file.
f, err := gameAssets.Open("_data/" + path)
if err != nil {
panic(err)
}
return f
}
}
OpenAsset = MakeOpenAssetFunc("path/to/data/dir") By using a closure approach, the open asset func can have some "state" like data dir location, etc. Although it might be a global variable too. |
So I tried that:
But it doesnt work with an infinite loop mp3. Length in that function returns -1.
|
It was a bit of a pain trying to figure out how to load an MP3 from the file system. I ended up with something like this
`
type TEST struct {
bytes.Reader
}
func (TEST) Close() error {
return nil
}
`
The text was updated successfully, but these errors were encountered: