Skip to content

Commit 510d61d

Browse files
committed
Read me update
1 parent 7f104fa commit 510d61d

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,122 @@ func main() {
7171

7272
fmt.Println("Assets packed and ready for your game!")
7373
}
74+
```
75+
76+
## WASM Example
77+
78+
We can also use WASM to load the game assets.
79+
80+
```go
81+
// Load the game_assets.pak from the web environment
82+
assetBin, err := loadAssetPackFromWasm("./game_assets.pak")
83+
if err != nil {
84+
panic(err)
85+
}
86+
87+
// Initialize the asset reader with the binary data instead of a file path
88+
reader, err := asset_packer.NewAssetReaderFromBytes(assetBin, key)
89+
if err != nil {
90+
panic(err)
91+
}
92+
93+
94+
func loadAssetPackFromWasm(path string) ([]byte, error) {
95+
fmt.Printf("Attempting to load asset from WASM: %s\n", path)
96+
97+
done := make(chan struct{})
98+
var result []byte
99+
var err error
100+
101+
js.Global().Call("fetch", path).Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
102+
response := args[0]
103+
// Check if the response is OK
104+
if !response.Get("ok").Bool() {
105+
err = fmt.Errorf("HTTP error, status %d", response.Get("status").Int())
106+
close(done)
107+
return nil
108+
}
109+
110+
// Convert response to ArrayBuffer
111+
response.Call("arrayBuffer").Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
112+
jsArrayBuffer := args[0]
113+
jsUint8Array := js.Global().Get("Uint8Array").New(jsArrayBuffer)
114+
115+
// Convert to Go slice
116+
result = make([]byte, jsUint8Array.Get("length").Int())
117+
js.CopyBytesToGo(result, jsUint8Array)
118+
close(done)
119+
return nil
120+
})).Call("catch", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
121+
err = fmt.Errorf("failed to convert response to array buffer: %v", args[0].String())
122+
close(done)
123+
return nil
124+
}))
125+
return nil
126+
})).Call("catch", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
127+
err = fmt.Errorf("fetch error: %v", args[0].String())
128+
close(done)
129+
return nil
130+
}))
131+
132+
<-done
133+
134+
if err != nil {
135+
return nil, err
136+
}
137+
138+
fmt.Printf("Fetched data length: %d, first 10 bytes: %v, last 10 bytes: %v\n", len(result), result[:10], result[len(result)-10:])
139+
140+
// Clean binary data if necessary
141+
cleanedData := cleanBinaryData(result)
142+
143+
return cleanedData, nil
144+
}
145+
146+
// Clean up the binary data by removing Unicode Replacement Characters
147+
func cleanBinaryData(data []byte) []byte {
148+
var clean []byte
149+
for i := 0; i < len(data); {
150+
if i+2 < len(data) && data[i] == 239 && data[i+1] == 191 && data[i+2] == 189 {
151+
// Skip the three bytes representing the replacement character
152+
i += 3
153+
} else {
154+
clean = append(clean, data[i])
155+
i++
156+
}
157+
}
158+
return clean
159+
}
160+
```
161+
162+
The HTML script:
163+
164+
```html
165+
<!DOCTYPE html>
166+
<html>
167+
<head>
168+
<meta charset="UTF-8" />
169+
<title>Your Game</title>
170+
</head>
171+
<body>
172+
<script src="wasm_exec.js"></script>
173+
<script>
174+
console.log(
175+
"Go object:",
176+
typeof Go !== "undefined" ? "Go exists" : "Go is not defined"
177+
);
178+
const go = new Go();
179+
WebAssembly.instantiateStreaming(
180+
fetch("'${{ env.EXECUTABLE_NAME }}'.wasm"),
181+
go.importObject
182+
)
183+
.then((result) => {
184+
go.run(result.instance);
185+
})
186+
.catch((err) => {
187+
console.error("Failed to load WASM module:", err);
188+
});
189+
</script>
190+
</body>
191+
</html>
192+
```

0 commit comments

Comments
 (0)