1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/os/os_android.c.v

40 lines
811 B
V
Raw Normal View History

2020-08-19 08:10:42 +03:00
module os
struct C.AAset {
}
struct C.AAssetManager {
2020-08-19 08:10:42 +03:00
}
struct C.ANativeActivity {
assetManager voidptr
}
2020-08-19 08:10:42 +03:00
fn C.AAssetManager_open(&C.AAsetManager, charptr, int) &C.AAset
2020-08-19 08:10:42 +03:00
fn C.AAsset_getLength(&C.AAset) int
2020-08-19 08:10:42 +03:00
fn C.AAsset_read(&C.AAset, voidptr, int) int
2020-08-19 08:10:42 +03:00
fn C.AAsset_close(&C.AAsset)
pub fn read_apk_asset(file string) ?[]byte {
act := &C.ANativeActivity(C.sapp_android_get_native_activity())
if isnil(act) {
return error('Could not get reference to Android activity')
}
asset := C.AAssetManager_open(&C.AAsetManager(act.assetManager), file.str, C.AASSET_MODE_STREAMING)
2020-08-19 08:10:42 +03:00
if isnil(asset) {
return error('File `$file` not found')
}
len := C.AAsset_getLength(asset)
buf := []byte{len: len}
2020-08-19 08:10:42 +03:00
for {
if C.AAsset_read(asset, buf.data, len) > 0 {
break
}
2020-08-19 08:10:42 +03:00
}
C.AAsset_close(asset)
return buf
}