mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
Added support for asset loading on Android.
Requires passing the activity's AssetManager object from Java to GNUstep by calling +[NSBundle setJavaAssetManager:withJNIEnv:], which then enables the following features: - NSBundle main bundle resource paths support for Android assets, e.g. for pathForResource:ofType:, URLForResource:ofType: and related methods. - NSBundle main bundle info dictionary support if Info.plist exists in Android assets. - -initWithContentsOfFile: and related methods support for reading Android assets from main bundle in various classes (e.g. NSData, NSDictionary, NSArray, etc.). - NSFileManager fileExistsAtPath:(isDirectory:) and isReadableFileAtPath: return YES for main bundle asset / asset directory paths. - NSFileHandle support for reading Android assets from main bundle. - NSDirectoryEnumerator support for enumerating Android assets from main bundle. Note that recursion into subdirectories is currently not supported by the native Android asset manager API (see https://issuetracker.google.com/issues/37002833). Also adds support for automatic NSProcessInfo initialization on Android with a fake executable path "/data/data/<app identifier>/exe" (as Android apps don't have a real executable path), and tweaks main bundle initialization to allow that path. Main bundle resource paths are prefixed by "/data/data/<app identifier>/Resources".
This commit is contained in:
parent
ecbecbeabd
commit
3b60b1a8be
10 changed files with 440 additions and 46 deletions
|
@ -253,6 +253,35 @@ readContentsOfFile(NSString *path, void **buf, off_t *len, NSZone *zone)
|
|||
NSWarnFLog(@"Open (%@) attempt failed - bad path", path);
|
||||
return NO;
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
// Android: try using asset manager if path is in main bundle resources
|
||||
AAsset *asset = [NSBundle assetForPath:path];
|
||||
if (asset) {
|
||||
fileLength = AAsset_getLength(asset);
|
||||
|
||||
tmp = NSZoneMalloc(zone, fileLength);
|
||||
if (tmp == 0) {
|
||||
NSLog(@"Malloc failed for file (%@) of length %jd - %@", path,
|
||||
(intmax_t)fileLength, [NSError _last]);
|
||||
AAsset_close(asset);
|
||||
goto failure;
|
||||
}
|
||||
|
||||
int result = AAsset_read(asset, tmp, fileLength);
|
||||
AAsset_close(asset);
|
||||
|
||||
if (result < 0) {
|
||||
NSWarnFLog(@"read of file (%@) contents failed - %@", path,
|
||||
[NSError errorWithDomain:NSPOSIXErrorDomain code:result userInfo:nil]);
|
||||
goto failure;
|
||||
}
|
||||
|
||||
*buf = tmp;
|
||||
*len = fileLength;
|
||||
return YES;
|
||||
}
|
||||
#endif /* __ANDROID__ */
|
||||
|
||||
att = [mgr fileAttributesAtPath: path traverseLink: YES];
|
||||
if (nil == att)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue