mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
parent
18732ca879
commit
6d113e4b0d
@ -18,11 +18,8 @@ package ebitenutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"io/ioutil"
|
||||||
"fmt"
|
"net/http"
|
||||||
"syscall/js"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type file struct {
|
type file struct {
|
||||||
@ -34,37 +31,14 @@ func (f *file) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func OpenFile(path string) (ReadSeekCloser, error) {
|
func OpenFile(path string) (ReadSeekCloser, error) {
|
||||||
var err error
|
res, err := http.Get(path)
|
||||||
var content js.Value
|
|
||||||
ch := make(chan struct{})
|
|
||||||
req := js.Global().Get("XMLHttpRequest").New()
|
|
||||||
req.Call("open", "GET", path, true)
|
|
||||||
req.Set("responseType", "arraybuffer")
|
|
||||||
loadf := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
||||||
defer close(ch)
|
|
||||||
status := req.Get("status").Int()
|
|
||||||
if 200 <= status && status < 400 {
|
|
||||||
content = req.Get("response")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
err = errors.New(fmt.Sprintf("http error: %d", status))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
defer loadf.Release()
|
|
||||||
req.Call("addEventListener", "load", loadf)
|
|
||||||
errorf := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
||||||
defer close(ch)
|
|
||||||
err = errors.New(fmt.Sprintf("XMLHttpRequest error: %s", req.Get("statusText").String()))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
req.Call("addEventListener", "error", errorf)
|
|
||||||
defer errorf.Release()
|
|
||||||
req.Call("send")
|
|
||||||
<-ch
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
f := &file{bytes.NewReader(jsutil.ArrayBufferToSlice(content))}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
f := &file{bytes.NewReader(body)}
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user