Adding upstream version 0.0~git20250520.a1d9079+dfsg.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
590ac7ff5f
commit
20149b7f3a
456 changed files with 70406 additions and 0 deletions
24
asset/asset.go
Normal file
24
asset/asset.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build darwin || linux || windows
|
||||
|
||||
package asset
|
||||
|
||||
import "io"
|
||||
|
||||
// Open opens a named asset.
|
||||
//
|
||||
// Errors are of type *os.PathError.
|
||||
//
|
||||
// This must not be called from init when used in android apps.
|
||||
func Open(name string) (File, error) {
|
||||
return openAsset(name)
|
||||
}
|
||||
|
||||
// File is an open asset.
|
||||
type File interface {
|
||||
io.ReadSeeker
|
||||
io.Closer
|
||||
}
|
109
asset/asset_android.go
Normal file
109
asset/asset_android.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package asset
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -landroid
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <jni.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static AAssetManager* asset_manager_init(uintptr_t java_vm, uintptr_t jni_env, jobject ctx) {
|
||||
JavaVM* vm = (JavaVM*)java_vm;
|
||||
JNIEnv* env = (JNIEnv*)jni_env;
|
||||
|
||||
// Equivalent to:
|
||||
// assetManager = ctx.getResources().getAssets();
|
||||
jclass ctx_clazz = (*env)->FindClass(env, "android/content/Context");
|
||||
jmethodID getres_id = (*env)->GetMethodID(env, ctx_clazz, "getResources", "()Landroid/content/res/Resources;");
|
||||
jobject res = (*env)->CallObjectMethod(env, ctx, getres_id);
|
||||
jclass res_clazz = (*env)->FindClass(env, "android/content/res/Resources");
|
||||
jmethodID getam_id = (*env)->GetMethodID(env, res_clazz, "getAssets", "()Landroid/content/res/AssetManager;");
|
||||
jobject am = (*env)->CallObjectMethod(env, res, getam_id);
|
||||
|
||||
// Pin the AssetManager and load an AAssetManager from it.
|
||||
am = (*env)->NewGlobalRef(env, am);
|
||||
return AAssetManager_fromJava(env, am);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/mobile/internal/mobileinit"
|
||||
)
|
||||
|
||||
var assetOnce sync.Once
|
||||
|
||||
// asset_manager is the asset manager of the app.
|
||||
var assetManager *C.AAssetManager
|
||||
|
||||
func assetInit() {
|
||||
err := mobileinit.RunOnJVM(func(vm, env, ctx uintptr) error {
|
||||
assetManager = C.asset_manager_init(C.uintptr_t(vm), C.uintptr_t(env), C.jobject(ctx))
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("asset: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func openAsset(name string) (File, error) {
|
||||
assetOnce.Do(assetInit)
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
a := &asset{
|
||||
ptr: C.AAssetManager_open(assetManager, cname, C.AASSET_MODE_UNKNOWN),
|
||||
name: name,
|
||||
}
|
||||
if a.ptr == nil {
|
||||
return nil, a.errorf("open", "bad asset")
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
type asset struct {
|
||||
ptr *C.AAsset
|
||||
name string
|
||||
}
|
||||
|
||||
func (a *asset) errorf(op string, format string, v ...interface{}) error {
|
||||
return &os.PathError{
|
||||
Op: op,
|
||||
Path: a.name,
|
||||
Err: fmt.Errorf(format, v...),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *asset) Read(p []byte) (n int, err error) {
|
||||
n = int(C.AAsset_read(a.ptr, unsafe.Pointer(&p[0]), C.size_t(len(p))))
|
||||
if n == 0 && len(p) > 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if n < 0 {
|
||||
return 0, a.errorf("read", "negative bytes: %d", n)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (a *asset) Seek(offset int64, whence int) (int64, error) {
|
||||
// TODO(crawshaw): use AAsset_seek64 if it is available.
|
||||
off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence))
|
||||
if off == -1 {
|
||||
return 0, a.errorf("seek", "bad result for offset=%d, whence=%d", offset, whence)
|
||||
}
|
||||
return int64(off), nil
|
||||
}
|
||||
|
||||
func (a *asset) Close() error {
|
||||
C.AAsset_close(a.ptr)
|
||||
return nil
|
||||
}
|
23
asset/asset_desktop.go
Normal file
23
asset/asset_desktop.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2014 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build (linux && !android) || (darwin && !ios) || windows
|
||||
|
||||
package asset
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func openAsset(name string) (File, error) {
|
||||
if !filepath.IsAbs(name) {
|
||||
name = filepath.Join("assets", name)
|
||||
}
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
13
asset/asset_ios.go
Normal file
13
asset/asset_ios.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
// Copyright 2024 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package asset
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func openAsset(name string) (File, error) {
|
||||
return os.Open(name)
|
||||
}
|
17
asset/doc.go
Normal file
17
asset/doc.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package asset provides access to application-bundled assets.
|
||||
//
|
||||
// On Android, assets are accessed via android.content.res.AssetManager.
|
||||
// These files are stored in the assets/ directory of the app. Any raw asset
|
||||
// can be accessed by its direct relative name. For example assets/img.png
|
||||
// can be opened with Open("img.png").
|
||||
//
|
||||
// On iOS an asset is a resource stored in the application bundle.
|
||||
// Resources can be loaded using the same relative paths.
|
||||
//
|
||||
// For consistency when debugging on a desktop, assets are read from a
|
||||
// directory named assets under the current working directory.
|
||||
package asset
|
Loading…
Add table
Add a link
Reference in a new issue