Skip to content

Commit 5600c9d

Browse files
committed
Added getBytes and getContent (fuck you GC)
1 parent 4a59019 commit 5600c9d

File tree

1 file changed

+97
-4
lines changed
  • source/engine/mobile/backend/android

1 file changed

+97
-4
lines changed

source/engine/mobile/backend/android/Assets.hx

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,80 @@ void Assets_obj::native_destroy(::Dynamic jni_env)
103103
}
104104
}
105105

106-
bool Assets_obj::native_exists(const char* file)
106+
bool Assets_obj::native_exists(::String file)
107107
{
108-
AAsset* asset = AAssetManager_open(asset_manager, file, AASSET_MODE_UNKNOWN);
108+
AAsset* asset = AAssetManager_open(asset_manager, file.__s, AASSET_MODE_UNKNOWN);
109109
bool ret = asset != NULL;
110110

111111
if (ret)
112112
AAsset_close(asset);
113113

114114
return ret;
115115
}
116+
117+
::String Assets_obj::native_getContent(::String file) {
118+
std::vector<char> buffer;
119+
120+
hx::EnterGCFreeZone();
121+
AAsset* asset = AAssetManager_open(asset_manager, file.__s, AASSET_MODE_BUFFER);
122+
123+
if (!asset) {
124+
hx::ExitGCFreeZone();
125+
return ::String(null());
126+
}
127+
128+
int len = AAsset_getLength(asset);
129+
if (len <= 0) {
130+
AAsset_close(asset);
131+
hx::ExitGCFreeZone();
132+
return ::String::emptyString;
133+
}
134+
135+
const char* src = (const char*)AAsset_getBuffer(asset);
136+
137+
buffer.resize(len);
138+
memcpy(&buffer[0], src, len);
139+
140+
AAsset_close(asset);
141+
hx::ExitGCFreeZone();
142+
143+
return ::String::create(&buffer[0], buffer.size());
144+
}
145+
146+
Array<unsigned char> Assets_obj::native_getBytes(::String file) {
147+
hx::EnterGCFreeZone();
148+
AAsset* asset = AAssetManager_open(asset_manager, file.__s, AASSET_MODE_BUFFER);
149+
150+
if (!asset) {
151+
hx::ExitGCFreeZone();
152+
return null();
153+
}
154+
155+
int len = AAsset_getLength(asset);
156+
const unsigned char* src = (const unsigned char*)AAsset_getBuffer(asset);
157+
hx::ExitGCFreeZone();
158+
159+
Array<unsigned char> buffer = Array_obj<unsigned char>::__new(len, len);
160+
if (len > 0) {
161+
hx::EnterGCFreeZone();
162+
memcpy(buffer->getBase(), src, len);
163+
AAsset_close(asset);
164+
hx::ExitGCFreeZone();
165+
} else {
166+
hx::EnterGCFreeZone();
167+
AAsset_close(asset);
168+
hx::ExitGCFreeZone();
169+
}
170+
171+
return buffer;
172+
}
116173
')
117174
@:headerClassCode('
118-
static bool native_exists(const char* file);
119175
static void native_init(::Dynamic jni_env);
120176
static void native_destroy(::Dynamic jni_env);
177+
static bool native_exists(::String file);
178+
static ::String native_getContent(::String file);
179+
static Array<unsigned char> native_getBytes(::String file);
121180
')
122181
class Assets
123182
{
@@ -131,8 +190,28 @@ class Assets
131190
_destroy(lime.system.JNI.getEnv());
132191
}
133192

193+
public static function getContent(file:String):String
194+
{
195+
var content:String = _getContent(file);
196+
197+
if (content == null)
198+
throw 'file_contents, $file';
199+
200+
return content;
201+
}
202+
203+
public static function getBytes(file:String):haxe.io.Bytes
204+
{
205+
var data:Array<cpp.UInt8> = _getBytes(file);
206+
207+
if (data == null || data.length <= 0)
208+
throw 'file_contents, $file';
209+
210+
return haxe.io.Bytes.ofData(data);
211+
}
212+
134213
@:native('mobile::backend::android::Assets_obj::native_exists')
135-
public static function exists(file:cpp.ConstCharStar):Bool
214+
public static function exists(file:String):Bool
136215
{
137216
return false;
138217
}
@@ -150,5 +229,19 @@ class Assets
150229
{
151230
return;
152231
}
232+
233+
@:noCompletion
234+
@:native('mobile::backend::android::Assets_obj::native_getContent')
235+
public static function _getContent(file:String):String
236+
{
237+
return null;
238+
}
239+
240+
@:noCompletion
241+
@:native('mobile::backend::android::Assets_obj::native_getBytes')
242+
private static function _getBytes(file:String):Array<cpp.UInt8>
243+
{
244+
return null;
245+
}
153246
}
154247
#end

0 commit comments

Comments
 (0)