From a0ea8ceabe17ff8e1ec31678ed95b62ca3b09d59 Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Sun, 10 Dec 2017 19:30:42 +0100 Subject: [PATCH] Add asset for js --- raylib/platform_android.go | 5 +-- raylib/platform_web.go | 90 +++++++++++++++++++++++++++++++++++--- raylib/raylib.go | 7 +++ raylib/rres.go | 6 +-- raylib/utils.go | 9 +--- raylib/utils_android.go | 7 --- 6 files changed, 95 insertions(+), 29 deletions(-) diff --git a/raylib/platform_android.go b/raylib/platform_android.go index 51f30d7..01f418d 100644 --- a/raylib/platform_android.go +++ b/raylib/platform_android.go @@ -16,7 +16,6 @@ const char* internal_storage_path; import "C" import ( - "errors" "fmt" "io" "unsafe" @@ -100,7 +99,7 @@ func OpenAsset(name string) (Asset, error) { a := &asset{C.AAssetManager_open(C.asset_manager, cname, C.AASSET_MODE_UNKNOWN)} if a.ptr == nil { - return nil, errors.New("asset file could not be opened") + return nil, fmt.Errorf("asset file could not be opened") } return a, nil @@ -121,7 +120,7 @@ func (a *asset) Read(p []byte) (n int, err error) { func (a *asset) Seek(offset int64, whence int) (int64, error) { off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence)) if off == -1 { - return 0, errors.New(fmt.Sprintf("bad result for offset=%d, whence=%d", offset, whence)) + return 0, fmt.Errorf("bad result for offset=%d, whence=%d", offset, whence) } return int64(off), nil } diff --git a/raylib/platform_web.go b/raylib/platform_web.go index 2175b52..ad0b2eb 100644 --- a/raylib/platform_web.go +++ b/raylib/platform_web.go @@ -3,7 +3,6 @@ package raylib import ( - "os" "unsafe" "github.com/gopherjs/gopherjs/js" @@ -59,10 +58,87 @@ func ClearDroppedFiles() { } // OpenAsset - Open asset -func OpenAsset(name string) (Asset, error) { - f, err := os.Open(name) - if err != nil { - return nil, err - } - return f, nil +func OpenAsset(name string) (a Asset, err error) { + defer func() { + e := recover() + + if e == nil { + return + } + + if e, ok := e.(*js.Error); ok { + err = e + } else { + panic(e) + } + }() + + ptr := js.Global.Get("FS").Call("open", name, "r") + a = &asset{ptr, 0} + + return +} + +type asset struct { + ptr *js.Object + offset int64 +} + +func (a *asset) Read(p []byte) (n int, err error) { + defer func() { + e := recover() + + if e == nil { + return + } + + if e, ok := e.(*js.Error); ok { + err = e + } else { + panic(e) + } + }() + + js.Global.Get("FS").Call("read", a.ptr, p, 0, cap(p), a.offset) + n = len(p) + return +} + +func (a *asset) Seek(offset int64, whence int) (off int64, err error) { + defer func() { + e := recover() + + if e == nil { + return + } + + if e, ok := e.(*js.Error); ok { + err = e + } else { + panic(e) + } + }() + + off = js.Global.Get("FS").Call("llseek", a.ptr, int(offset), int(whence)).Int64() + a.offset = off + return +} + +func (a *asset) Close() (err error) { + defer func() { + e := recover() + + if e == nil { + return + } + + if e, ok := e.(*js.Error); ok { + err = e + } else { + panic(e) + } + }() + + js.Global.Get("FS").Call("close", a.ptr) + return nil } diff --git a/raylib/raylib.go b/raylib/raylib.go index 7641a6e..d44abf9 100644 --- a/raylib/raylib.go +++ b/raylib/raylib.go @@ -1053,3 +1053,10 @@ const ( LogError LogDebug ) + +var traceDebugMsgs = false + +// SetDebug - Set debug messages +func SetDebug(enabled bool) { + traceDebugMsgs = enabled +} diff --git a/raylib/rres.go b/raylib/rres.go index 8b89c1e..3043788 100644 --- a/raylib/rres.go +++ b/raylib/rres.go @@ -2,7 +2,6 @@ package raylib import ( "encoding/binary" - "fmt" "io" "os" "unsafe" @@ -27,8 +26,7 @@ func LoadResource(reader io.ReadSeeker, rresID int, key []byte) (data rres.Data) } // Verify "rRES" identifier - id := fmt.Sprintf("%c", fileHeader.ID) - if id != "[r R E S]" { + if string(fileHeader.ID[:]) != "rRES" { TraceLog(LogWarning, "not a valid raylib resource file") return } @@ -78,7 +76,7 @@ func LoadResource(reader io.ReadSeeker, rresID int, key []byte) (data rres.Data) } if data.Data == nil { - TraceLog(LogInfo, "[ID %d] Requested resource could not be found", rresID) + TraceLog(LogWarning, "[ID %d] Requested resource could not be found", rresID) } return diff --git a/raylib/utils.go b/raylib/utils.go index 2ca8842..7d4fc95 100644 --- a/raylib/utils.go +++ b/raylib/utils.go @@ -1,4 +1,4 @@ -// +build !android,!windows +// +build !android,!windows,!js package raylib @@ -7,13 +7,6 @@ import ( "os" ) -var traceDebugMsgs = false - -// SetDebug - Set debug messages -func SetDebug(enabled bool) { - traceDebugMsgs = enabled -} - // TraceLog - Show trace log messages (INFO, WARNING, ERROR, DEBUG) func TraceLog(msgType int, text string, v ...interface{}) { switch msgType { diff --git a/raylib/utils_android.go b/raylib/utils_android.go index 7f797c9..1bdb600 100644 --- a/raylib/utils_android.go +++ b/raylib/utils_android.go @@ -20,13 +20,6 @@ import ( "unsafe" ) -var traceDebugMsgs = false - -// SetDebug - Set debug messages -func SetDebug(enabled bool) { - traceDebugMsgs = enabled -} - // TraceLog - Trace log messages showing (INFO, WARNING, ERROR, DEBUG) func TraceLog(msgType int, text string, v ...interface{}) { switch msgType {