2020-08-19 08:10:42 +03:00
|
|
|
module os
|
|
|
|
|
2022-06-23 11:22:55 +03:00
|
|
|
#include <asset_manager.h>
|
|
|
|
#include <asset_manager_jni.h>
|
|
|
|
|
2022-06-22 21:38:27 +03:00
|
|
|
pub enum AssetMode {
|
|
|
|
buffer = C.AASSET_MODE_BUFFER // Caller plans to ask for a read-only buffer with all data.
|
|
|
|
random = C.AASSET_MODE_RANDOM // Read chunks, and seek forward and backward.
|
|
|
|
streaming = C.AASSET_MODE_STREAMING // Read sequentially, with an occasional forward seek.
|
|
|
|
unknown = C.AASSET_MODE_UNKNOWN // No specific information about how data will be accessed.
|
2020-12-16 15:15:11 +03:00
|
|
|
}
|
|
|
|
|
2022-06-22 21:38:27 +03:00
|
|
|
struct C.ANativeActivity {
|
|
|
|
assetManager &AssetManager // Pointer to the Asset Manager instance for the application.
|
|
|
|
clazz voidptr // (jobject) The NativeActivity object handle.
|
|
|
|
env voidptr // (JNIEnv *) JNI context for the main thread of the app.
|
|
|
|
externalDataPath &char // Path to this application's external (removable/mountable) data directory.
|
|
|
|
instance voidptr // This is the native instance of the application.
|
|
|
|
internalDataPath &char // Path to this application's internal data directory.
|
|
|
|
obbPath &char // Available starting with Honeycomb: path to the directory containing the application's OBB files (if any).
|
|
|
|
sdkVersion int // The platform's SDK version code.
|
|
|
|
vm voidptr // (JavaVM *) The global handle on the process's Java VM.
|
2020-08-19 08:10:42 +03:00
|
|
|
}
|
|
|
|
|
2022-06-22 21:38:27 +03:00
|
|
|
// NativeActivity defines the native side of an android.app.NativeActivity.
|
|
|
|
pub type NativeActivity = C.ANativeActivity
|
|
|
|
|
|
|
|
struct C.AAssetManager {
|
2020-12-16 15:15:11 +03:00
|
|
|
}
|
2020-08-19 08:10:42 +03:00
|
|
|
|
2022-06-22 21:38:27 +03:00
|
|
|
// AssetManager provides access to an application's raw assets by creating Asset objects.
|
|
|
|
pub type AssetManager = C.AAssetManager
|
|
|
|
|
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
|
|
|
|
2022-06-22 21:38:27 +03:00
|
|
|
// open opens an Android `Asset`
|
|
|
|
pub fn (am &AssetManager) open(filename string, mode AssetMode) !&Asset {
|
|
|
|
asset := C.AAssetManager_open(am, filename.str, int(mode))
|
|
|
|
if isnil(asset) {
|
|
|
|
return error('file `$filename` not found')
|
|
|
|
}
|
|
|
|
return asset
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C.AAsset {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Asset = C.AAsset
|
|
|
|
|
|
|
|
fn C.AAsset_getBuffer(&C.AAsset) voidptr
|
|
|
|
|
|
|
|
// get_buffer returns a pointer to a buffer holding the entire contents of the asset.
|
|
|
|
pub fn (a &Asset) get_buffer() voidptr {
|
|
|
|
return C.AAsset_getBuffer(a)
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:20:11 +03:00
|
|
|
fn C.AAsset_getLength(&C.AAsset) int
|
2020-12-16 15:15:11 +03:00
|
|
|
|
2022-06-22 21:38:27 +03:00
|
|
|
// get_length returns the total size of the asset data.
|
|
|
|
pub fn (a &Asset) get_length() int {
|
|
|
|
return C.AAsset_getLength(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn C.AAsset_getLength64(&C.AAsset) i64
|
|
|
|
|
|
|
|
// get_length_64 returns the total size of the asset data.
|
|
|
|
// get_length_64 returns the size using a 64-bit number insted of 32-bit as `get_length`.
|
|
|
|
pub fn (a &Asset) get_length_64() i64 {
|
|
|
|
return C.AAsset_getLength64(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn C.AAsset_read(&C.AAsset, voidptr, usize) int
|
|
|
|
|
|
|
|
// read attempts to read 'count' bytes of data from the current offset.
|
|
|
|
// read returns the number of bytes read, zero on EOF, or < 0 on error.
|
|
|
|
pub fn (a &Asset) read(buffer voidptr, count usize) int {
|
|
|
|
return C.AAsset_read(a, buffer, count)
|
|
|
|
}
|
2020-12-16 15:15:11 +03:00
|
|
|
|
2020-08-19 08:10:42 +03:00
|
|
|
fn C.AAsset_close(&C.AAsset)
|
|
|
|
|
2022-06-22 21:38:27 +03:00
|
|
|
// close closes the asset, freeing all associated resources.
|
|
|
|
pub fn (a &Asset) close() {
|
|
|
|
C.AAsset_close(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read_apk_asset returns all the data located at `path`.
|
|
|
|
// `path` is expected to be relative to the `assets`
|
|
|
|
pub fn read_apk_asset(path string) ![]u8 {
|
2022-01-07 17:57:05 +03:00
|
|
|
$if apk {
|
2022-06-22 21:38:27 +03:00
|
|
|
act := &NativeActivity(C.sapp_android_get_native_activity())
|
2022-01-07 17:57:05 +03:00
|
|
|
if isnil(act) {
|
2022-06-22 21:38:27 +03:00
|
|
|
return error('could not get reference to Android activity')
|
2022-01-07 17:57:05 +03:00
|
|
|
}
|
2022-06-22 21:38:27 +03:00
|
|
|
asset_manager := &AssetManager(act.assetManager)
|
|
|
|
asset := asset_manager.open(path, .streaming)!
|
|
|
|
len := asset.get_length()
|
2022-04-15 15:35:35 +03:00
|
|
|
buf := []u8{len: len}
|
2022-01-07 17:57:05 +03:00
|
|
|
for {
|
2022-06-22 21:38:27 +03:00
|
|
|
if asset.read(buf.data, usize(len)) > 0 {
|
2022-01-07 17:57:05 +03:00
|
|
|
break
|
|
|
|
}
|
2020-12-16 15:15:11 +03:00
|
|
|
}
|
2022-06-22 21:38:27 +03:00
|
|
|
asset.close()
|
2022-01-07 17:57:05 +03:00
|
|
|
return buf
|
|
|
|
} $else {
|
|
|
|
return error(@FN + ' can only be used with APK/AAB packaged Android apps')
|
2020-08-19 08:10:42 +03:00
|
|
|
}
|
|
|
|
}
|