Skip to content

Commit 6af9621

Browse files
allow stateful allocators to implement APK Archive file views
Also bring TAR archive reader up to par with old impl.
1 parent c3cad88 commit 6af9621

16 files changed

+232
-101
lines changed

include/nbl/system/CFileArchive.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "nbl/system/CFileView.h"
1111
#include "nbl/system/IFileViewAllocator.h"
1212

13+
#ifdef _NBL_PLATFORM_ANDROID_
14+
#include "nbl/system/CFileViewAPKAllocator.h"
15+
#endif
1316

1417
namespace nbl::system
1518
{
@@ -90,6 +93,13 @@ class CFileArchive : public IFileArchive
9093
case EAT_VIRTUAL_ALLOC:
9194
return getFile_impl<VirtualMemoryAllocator>(item);
9295
break;
96+
case EAT_APK_ALLOCATOR:
97+
#ifdef _NBL_PLATFORM_ANDROID_
98+
return getFile_impl<CFileViewAPKAllocator>(item);
99+
#else
100+
assert(false);
101+
#endif
102+
break;
93103
default: // directory or something
94104
break;
95105
}
@@ -134,17 +144,23 @@ class CFileArchive : public IFileArchive
134144
m_fileFlags+item->ID,
135145
getDefaultAbsolutePath()/item->pathRelativeToArchive,
136146
IFile::ECF_READ, // TODO: stay like this until we allow write access to archived files
137-
std::get<void*>(fileBuffer),
138-
std::get<size_t>(fileBuffer),
139-
Allocator() // no archive uses stateful allocators yet
147+
fileBuffer.buffer,
148+
fileBuffer.size,
149+
Allocator(fileBuffer.allocatorState) // no archive uses stateful allocators yet
140150
);
141151
}
142152
// don't grab because we've already grabbed
143153
return core::smart_refctd_ptr<CInnerArchiveFile<Allocator>>(file,core::dont_grab);
144154
}
145155

146156
// this function will return a buffer that needs to be deallocated with an allocator matching `item->allocatorType`
147-
virtual std::pair<void*,size_t> getFileBuffer(const IFileArchive::SListEntry* item) = 0;
157+
struct file_buffer_t
158+
{
159+
void* buffer;
160+
size_t size;
161+
void* allocatorState;
162+
};
163+
virtual file_buffer_t getFileBuffer(const IFileArchive::SListEntry* item) = 0;
148164

149165
std::atomic_flag* m_fileFlags = nullptr;
150166
std::byte* m_filesBuffer = nullptr;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _NBL_SYSTEM_C_FILE_VIEW_APK_ALLOCATOR_H_INCLUDED_
2+
#define _NBL_SYSTEM_C_FILE_VIEW_APK_ALLOCATOR_H_INCLUDED_
3+
4+
5+
#include "nbl/system/IFileViewAllocator.h"
6+
7+
8+
namespace nbl::system
9+
{
10+
#ifdef _NBL_PLATFORM_ANDROID_
11+
class CFileViewAPKAllocator : public IFileViewAllocator
12+
{
13+
public:
14+
using IFileViewAllocator::IFileViewAllocator;
15+
16+
// should never be called
17+
void* alloc(size_t size) override;
18+
bool dealloc(void* data, size_t size) override;
19+
};
20+
#endif
21+
}
22+
23+
#endif

include/nbl/system/CFileViewVirtualAllocatorPOSIX.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace nbl::system
77
class CFileViewVirtualAllocatorPOSIX : public IFileViewAllocator
88
{
99
public:
10+
using IFileViewAllocator::IFileViewAllocator;
11+
1012
void* alloc(size_t size) override;
1113
bool dealloc(void* data, size_t size) override;
1214
};

include/nbl/system/CFileViewVirtualAllocatorWin32.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace nbl::system
77
class CFileViewVirtualAllocatorWin32 : public IFileViewAllocator
88
{
99
public:
10+
using IFileViewAllocator::IFileViewAllocator;
11+
1012
void* alloc(size_t size) override;
1113
bool dealloc(void* data, size_t size) override;
1214
};

include/nbl/system/CSystemAndroid.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@
44

55
#include "nbl/system/ISystem.h"
66

7-
8-
namespace nbl::system
9-
{
107
#ifdef _NBL_PLATFORM_ANDROID_
118
#include "nbl/system/ISystemPOSIX.h"
129

1310
struct ANativeActivity;
1411
struct JNIEnv;
1512

13+
namespace nbl::system
14+
{
15+
1616
class CSystemAndroid final : public ISystemPOSIX
1717
{
18-
ANativeActivity* m_nativeActivity;
19-
JNIEnv* m_jniEnv;
2018
public:
21-
CSystemAndroid(ANativeActivity* activity, JNIEnv* jni, const system::path& APKResourcesPath);
19+
CSystemAndroid(ANativeActivity* activity, JNIEnv* jni, const path& APKResourcesPath);
2220

2321
//
2422
SystemInfo getSystemInfo() const override;
23+
24+
protected:
25+
ANativeActivity* m_nativeActivity;
26+
JNIEnv* m_jniEnv;
2527
};
2628

27-
#endif
2829
}
30+
#endif
2931

3032
#endif

include/nbl/system/IFileArchive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class IFileArchive : public core::IReferenceCounted
2727
EAT_NONE = 0,
2828
EAT_NULL, // read directly from archive's underlying mapped file
2929
EAT_VIRTUAL_ALLOC, // decompress to RAM (with sparse paging)
30+
EAT_APK_ALLOCATOR, // specialization to be able to call `AAsset_close`
3031
EAT_MALLOC // decompress to RAM
3132
};
3233
//! An entry in a list of items, can be a folder or a file.

include/nbl/system/IFileViewAllocator.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@ namespace nbl::system
1313
class IFileViewAllocator
1414
{
1515
public:
16+
IFileViewAllocator(void* _state) : m_state(_state) {}
17+
1618
virtual void* alloc(size_t size) = 0;
1719
virtual bool dealloc(void* data, size_t size) = 0;
20+
21+
protected:
22+
void* m_state;
1823
};
1924

2025
// Regular old file in RAM
2126
class CPlainHeapAllocator : public IFileViewAllocator
2227
{
2328
public:
29+
using IFileViewAllocator::IFileViewAllocator;
30+
2431
void* alloc(size_t size) override
2532
{
2633
return malloc(size);
@@ -37,6 +44,8 @@ class CPlainHeapAllocator : public IFileViewAllocator
3744
class CNullAllocator : public IFileViewAllocator
3845
{
3946
public:
47+
using IFileViewAllocator::IFileViewAllocator;
48+
4049
void* alloc(size_t size) override
4150
{
4251
return nullptr;

source/Nabla/CTarReader.cpp

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/nbl/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,13 @@ set(NBL_SYSTEM_SOURCES
200200
${NBL_ROOT_PATH}/src/nbl/system/ILogger.cpp
201201
${NBL_ROOT_PATH}/src/nbl/system/CArchiveLoaderZip.cpp
202202
${NBL_ROOT_PATH}/src/nbl/system/CArchiveLoaderTar.cpp
203+
${NBL_ROOT_PATH}/src/nbl/system/CAPKResourcesArchive.cpp
203204
${NBL_ROOT_PATH}/src/nbl/system/ISystem.cpp
204205
${NBL_ROOT_PATH}/src/nbl/system/IFileArchive.cpp
205206
${NBL_ROOT_PATH}/src/nbl/system/CStdoutLoggerAndroid.cpp
206207
${NBL_ROOT_PATH}/src/nbl/system/CFileViewVirtualAllocatorWin32.cpp
207208
${NBL_ROOT_PATH}/src/nbl/system/CFileViewVirtualAllocatorPOSIX.cpp
209+
${NBL_ROOT_PATH}/src/nbl/system/CFileViewAPKAllocator.cpp
208210
${NBL_ROOT_PATH}/src/nbl/system/CFileWin32.cpp
209211
${NBL_ROOT_PATH}/src/nbl/system/CFilePOSIX.cpp
210212
${NBL_ROOT_PATH}/src/nbl/system/CSystemWin32.cpp
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "nbl/system/CAPKResourcesArchive.h"
2+
3+
using namespace nbl;
4+
using namespace nbl::system;
5+
6+
#ifdef _NBL_PLATFORM_ANDROID_
7+
#include <jni.h>
8+
#include <asset_manager.h>
9+
10+
CAPKResourcesArchive::CAPKResourcesArchive(const path& _path, system::logger_opt_smart_ptr&& logger, ANativeActivity* act, JNIEnv* jni)
11+
: CFileArchive(path(_path),std::move(logger),computeItems(_path.string())), m_mgr(act->assetManager), m_activity(act), m_jniEnv(jni)
12+
{
13+
}
14+
15+
core::vector<IFileArchive::SListEntry> CAPKResourcesArchive::computeItems(const std::string& asset_path)
16+
{
17+
auto context_object = activity->clazz;
18+
auto getAssets_method = env->GetMethodID(env->GetObjectClass(context_object), "getAssets", "()Landroid/content/res/AssetManager;");
19+
auto assetManager_object = env->CallObjectMethod(context_object,getAssets_method);
20+
auto list_method = env->GetMethodID(env->GetObjectClass(assetManager_object), "list", "(Ljava/lang/String;)[Ljava/lang/String;");
21+
22+
jstring path_object = env->NewStringUTF(asset_path.c_str());
23+
24+
auto files_object = (jobjectArray)env->CallObjectMethod(assetManager_object, list_method, path_object);
25+
26+
env->DeleteLocalRef(path_object);
27+
28+
auto length = env->GetArrayLength(files_object);
29+
30+
core::vector<IFileArchive::SListEntry> result;
31+
for (decltype(length) i=0; i<length; i++)
32+
{
33+
jstring jstr = (jstring)env->GetObjectArrayElement(files_object,i);
34+
35+
const char* filename = env->GetStringUTFChars(jstr,nullptr);
36+
if (filename != nullptr)
37+
{
38+
auto& item = result.emplace_back();
39+
item.pathRelativeToArchive = filename;
40+
{
41+
AAsset* asset = AAssetManager_open(m_mgr,filename,AASSET_MODE_STREAMING);
42+
item.size = AAsset_getLength(asset);
43+
AAsset_close(asset);
44+
}
45+
item.offset = 0xdeadbeefu;
46+
item.ID = i;
47+
item.allocatorType = EAT_APK_ALLOCATOR;
48+
49+
env->ReleaseStringUTFChars(jstr,filename);
50+
}
51+
52+
env->DeleteLocalRef(jstr);
53+
}
54+
}
55+
56+
CFileArchive::file_buffer_t CAPKResourcesArchive::getFileBuffer(const IFileArchive::SListEntry* item)
57+
{
58+
AAsset* asset = AAssetManager_open(m_mgr,item->pathRelativeToArchive.string().c_str(),AASSET_MODE_BUFFER);
59+
return {AAsset_getBuffer(asset),AAsset_getLength(asset),asset};
60+
}
61+
62+
#endif

0 commit comments

Comments
 (0)