add AssetFS hook so assets can load from an embed.FS (js/wasm)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-30 07:23:20 +02:00
co-authored by Claude Fable 5
parent 5ed7ac4ed9
commit 99902e0ab4
3 changed files with 33 additions and 4 deletions
+1 -2
View File
@@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
@@ -174,7 +173,7 @@ type audioStream interface {
}
func decodeAudioStream(path string, sampleRate int) (audioStream, int64, error) {
f, err := os.Open(path)
f, err := openAsset(path)
if err != nil {
return nil, 0, err
}
+31
View File
@@ -0,0 +1,31 @@
package pncdsl
import (
"io/fs"
"os"
)
// AssetFS, when non-nil, is the filesystem asset files (images, audio)
// are read from instead of the OS filesystem. Set it to an embed.FS
// before Run to ship assets inside the binary — required for js/wasm
// builds, where there is no OS filesystem and every asset would fall
// back to a placeholder:
//
// //go:embed assets
// var assetsFS embed.FS
//
// func init() { pncdsl.AssetFS = assetsFS }
//
// Asset.Path values are used as-is, so they must be slash-separated
// and relative (e.g. "assets/bg/bedroom.png") — the same form that
// works with os.Open from the project root.
var AssetFS fs.FS
// openAsset opens an asset file honoring AssetFS. Every asset read in
// the package goes through here.
func openAsset(path string) (fs.File, error) {
if AssetFS != nil {
return AssetFS.Open(path)
}
return os.Open(path)
}
+1 -2
View File
@@ -6,7 +6,6 @@ import (
"image/color"
_ "image/jpeg"
_ "image/png"
"os"
"github.com/hajimehoshi/ebiten/v2"
)
@@ -57,7 +56,7 @@ func (la *loadedAssets) isPlaceholder(name string) bool {
}
func loadImageFile(path string) *ebiten.Image {
f, err := os.Open(path)
f, err := openAsset(path)
if err != nil {
return nil
}