2020-08-19 08:10:42 +03:00
|
|
|
module os
|
|
|
|
|
2021-03-05 20:20:11 +03:00
|
|
|
struct C.AAsset {
|
2020-12-16 15:15:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct C.AAssetManager {
|
2020-08-19 08:10:42 +03:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:15:11 +03:00
|
|
|
struct C.ANativeActivity {
|
|
|
|
assetManager voidptr
|
|
|
|
}
|
2020-08-19 08:10:42 +03:00
|
|
|
|
2021-04-05 20:47:32 +03:00
|
|
|
fn C.AAssetManager_open(&C.AAssetManager, &char, int) &C.AAsset
|
2020-12-16 15:15:11 +03:00
|
|
|
|
2021-03-05 20:20:11 +03:00
|
|
|
fn C.AAsset_getLength(&C.AAsset) int
|
2020-12-16 15:15:11 +03:00
|
|
|
|
2021-03-05 20:20:11 +03:00
|
|
|
fn C.AAsset_read(&C.AAsset, voidptr, int) int
|
2020-12-16 15:15:11 +03:00
|
|
|
|
2020-08-19 08:10:42 +03:00
|
|
|
fn C.AAsset_close(&C.AAsset)
|
|
|
|
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn read_apk_asset(file string) ?[]u8 {
|
2022-01-07 17:57:05 +03:00
|
|
|
$if apk {
|
|
|
|
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.AAssetManager(act.assetManager), file.str, C.AASSET_MODE_STREAMING)
|
|
|
|
if isnil(asset) {
|
|
|
|
return error('File `$file` not found')
|
|
|
|
}
|
|
|
|
len := C.AAsset_getLength(asset)
|
2022-04-15 15:35:35 +03:00
|
|
|
buf := []u8{len: len}
|
2022-01-07 17:57:05 +03:00
|
|
|
for {
|
|
|
|
if C.AAsset_read(asset, buf.data, len) > 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-12-16 15:15:11 +03:00
|
|
|
}
|
2022-01-07 17:57:05 +03:00
|
|
|
C.AAsset_close(asset)
|
|
|
|
return buf
|
|
|
|
} $else {
|
|
|
|
return error(@FN + ' can only be used with APK/AAB packaged Android apps')
|
2020-08-19 08:10:42 +03:00
|
|
|
}
|
|
|
|
}
|