Browse Source

fixes in handling of DirAccess for resource path on Android, fixes #1447

Juan Linietsky 10 years ago
parent
commit
c6dce44dd8

+ 22 - 31
platform/android/dir_access_jandroid.cpp

@@ -31,11 +31,15 @@
 #include "dir_access_jandroid.h"
 #include "dir_access_jandroid.h"
 #include "file_access_jandroid.h"
 #include "file_access_jandroid.h"
 #include "thread_jandroid.h"
 #include "thread_jandroid.h"
+#include "print_string.h"
+
+
 jobject DirAccessJAndroid::io=NULL;
 jobject DirAccessJAndroid::io=NULL;
 jclass DirAccessJAndroid::cls=NULL;
 jclass DirAccessJAndroid::cls=NULL;
 jmethodID DirAccessJAndroid::_dir_open=NULL;
 jmethodID DirAccessJAndroid::_dir_open=NULL;
 jmethodID DirAccessJAndroid::_dir_next=NULL;
 jmethodID DirAccessJAndroid::_dir_next=NULL;
 jmethodID DirAccessJAndroid::_dir_close=NULL;
 jmethodID DirAccessJAndroid::_dir_close=NULL;
+jmethodID DirAccessJAndroid::_dir_is_dir=NULL;
 
 
 
 
 DirAccess *DirAccessJAndroid::create_fs() {
 DirAccess *DirAccessJAndroid::create_fs() {
@@ -67,43 +71,18 @@ String DirAccessJAndroid::get_next(){
 	if (!str)
 	if (!str)
 		return "";
 		return "";
 
 
-	int sl = env->GetStringLength(str);
-	if (sl==0) {
-		env->DeleteLocalRef((jobject)str);
-		return "";
-	}
-
-	CharString cs;
-	cs.resize(sl+1);
-	env->GetStringRegion(str,0,sl,(jchar*)&cs[0]);
-	cs[sl]=0;
-
-	String ret;
-	ret.parse_utf8(&cs[0]);
+	String ret = String::utf8(env->GetStringUTFChars( (jstring)str, NULL ));
 	env->DeleteLocalRef((jobject)str);
 	env->DeleteLocalRef((jobject)str);
-
 	return ret;
 	return ret;
 
 
 }
 }
 bool DirAccessJAndroid::current_is_dir() const{
 bool DirAccessJAndroid::current_is_dir() const{
 
 
-	JNIEnv *env = ThreadAndroid::get_env();
-	String sd;
-	if (current_dir=="")
-		sd=current;
-	else
-		sd=current_dir+"/"+current;
-
-	jstring js = env->NewStringUTF(sd.utf8().get_data());
 
 
-	int res = env->CallIntMethod(io,_dir_open,js);
-	if (res<=0)
-		return false;
-
-	env->CallObjectMethod(io,_dir_close,res);
+	JNIEnv *env = ThreadAndroid::get_env();
 
 
+	return env->CallBooleanMethod(io,_dir_is_dir,id);
 
 
-	return true;
 }
 }
 
 
 bool DirAccessJAndroid::current_is_hidden() const {
 bool DirAccessJAndroid::current_is_hidden() const {
@@ -142,24 +121,31 @@ Error DirAccessJAndroid::change_dir(String p_dir){
 
 
 	String new_dir;
 	String new_dir;
 
 
+	if (p_dir!="res://" && p_dir.length()>1 && p_dir.ends_with("/"))
+		p_dir=p_dir.substr(0,p_dir.length()-1);
+
 	if (p_dir.begins_with("/"))
 	if (p_dir.begins_with("/"))
 		new_dir=p_dir.substr(1,p_dir.length());
 		new_dir=p_dir.substr(1,p_dir.length());
 	else if (p_dir.begins_with("res://"))
 	else if (p_dir.begins_with("res://"))
 		new_dir=p_dir.substr(6,p_dir.length());
 		new_dir=p_dir.substr(6,p_dir.length());
-	else  //relative
-		new_dir=new_dir+"/"+p_dir;
+	else if (current_dir=="")
+		new_dir=p_dir;
+	else
+		new_dir=current_dir.plus_file(p_dir);
 
 
+	//print_line("new dir is: "+new_dir);
 //test if newdir exists
 //test if newdir exists
 	new_dir=new_dir.simplify_path();
 	new_dir=new_dir.simplify_path();
 
 
 	jstring js = env->NewStringUTF(new_dir.utf8().get_data());
 	jstring js = env->NewStringUTF(new_dir.utf8().get_data());
 	int res = env->CallIntMethod(io,_dir_open,js);
 	int res = env->CallIntMethod(io,_dir_open,js);
+	env->DeleteLocalRef(js);
 	if (res<=0)
 	if (res<=0)
 		return ERR_INVALID_PARAMETER;
 		return ERR_INVALID_PARAMETER;
 
 
 	env->CallObjectMethod(io,_dir_close,res);
 	env->CallObjectMethod(io,_dir_close,res);
 
 
-
+	current_dir=new_dir;
 
 
 	return OK;
 	return OK;
 }
 }
@@ -210,6 +196,7 @@ bool DirAccessJAndroid::dir_exists(String p_dir) {
 
 
 	jstring js = env->NewStringUTF(path.utf8().get_data());
 	jstring js = env->NewStringUTF(path.utf8().get_data());
 	int res = env->CallIntMethod(io,_dir_open,js);
 	int res = env->CallIntMethod(io,_dir_open,js);
+	env->DeleteLocalRef(js);
 	if (res<=0)
 	if (res<=0)
 		return false;
 		return false;
 
 
@@ -263,6 +250,10 @@ void DirAccessJAndroid::setup( jobject p_io) {
 	if(_dir_close != 0) {
 	if(_dir_close != 0) {
 		__android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _dir_close ok!!");
 		__android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _dir_close ok!!");
 	}
 	}
+	_dir_is_dir = env->GetMethodID(cls, "dir_is_dir", "(I)Z");
+	if(_dir_is_dir != 0) {
+		__android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _dir_is_dir ok!!");
+	}
 
 
 //	(*env)->CallVoidMethod(env,obj,aMethodID, myvar);
 //	(*env)->CallVoidMethod(env,obj,aMethodID, myvar);
 }
 }

+ 1 - 0
platform/android/dir_access_jandroid.h

@@ -47,6 +47,7 @@ class DirAccessJAndroid  : public DirAccess {
 	static jmethodID _dir_open;
 	static jmethodID _dir_open;
 	static jmethodID _dir_next;
 	static jmethodID _dir_next;
 	static jmethodID _dir_close;
 	static jmethodID _dir_close;
+	static jmethodID _dir_is_dir;
 
 
 	int id;
 	int id;
 
 

+ 0 - 1
platform/android/file_access_jandroid.cpp

@@ -67,7 +67,6 @@ Error FileAccessJAndroid::_open(const String& p_path, int p_mode_flags) {
 
 
 	jstring js = env->NewStringUTF(path.utf8().get_data());
 	jstring js = env->NewStringUTF(path.utf8().get_data());
 	int res = env->CallIntMethod(io,_file_open,js,p_mode_flags&WRITE?true:false);
 	int res = env->CallIntMethod(io,_file_open,js,p_mode_flags&WRITE?true:false);
-
 	env->DeleteLocalRef(js);
 	env->DeleteLocalRef(js);
 
 
 	if (res<=0)
 	if (res<=0)

+ 34 - 1
platform/android/java/src/com/android/godot/GodotIO.java

@@ -271,6 +271,7 @@ public class GodotIO {
 
 
 		public String[] files;
 		public String[] files;
 		public int current;
 		public int current;
+		public String path;
 	}
 	}
 
 
 	public int last_dir_id=1;
 	public int last_dir_id=1;
@@ -281,6 +282,7 @@ public class GodotIO {
 
 
 		AssetDir ad = new AssetDir();
 		AssetDir ad = new AssetDir();
 		ad.current=0;
 		ad.current=0;
+		ad.path=path;
 
 
 		try {
 		try {
 			ad.files = am.list(path);
 			ad.files = am.list(path);
@@ -290,6 +292,7 @@ public class GodotIO {
 			return -1;
 			return -1;
 		}
 		}
 
 
+		//System.out.printf("Opened dir: %s\n",path);
 		++last_dir_id;
 		++last_dir_id;
 		dirs.put(last_dir_id,ad);
 		dirs.put(last_dir_id,ad);
 
 
@@ -297,6 +300,32 @@ public class GodotIO {
 
 
 	}
 	}
 
 
+	public boolean dir_is_dir(int id) {
+		if (!dirs.containsKey(id)) {
+			System.out.printf("dir_next: invalid dir id: %d\n",id);
+			return false;
+		}
+		AssetDir ad = dirs.get(id);
+		//System.out.printf("go next: %d,%d\n",ad.current,ad.files.length);
+		int idx = ad.current;
+		if (idx>0)
+			idx--;
+
+		if (idx>=ad.files.length)
+			return false;
+		String fname = ad.files[idx];
+
+		try {
+			if (ad.path.equals(""))
+				am.open(fname);
+			else
+				am.open(ad.path+"/"+fname);
+			return false;
+		} catch (Exception e) {
+			return true;
+		}
+	}
+
 	public String dir_next(int id) {
 	public String dir_next(int id) {
 
 
 		if (!dirs.containsKey(id)) {
 		if (!dirs.containsKey(id)) {
@@ -305,8 +334,12 @@ public class GodotIO {
 		}
 		}
 
 
 		AssetDir ad = dirs.get(id);
 		AssetDir ad = dirs.get(id);
-		if (ad.current>=ad.files.length)
+		//System.out.printf("go next: %d,%d\n",ad.current,ad.files.length);
+
+		if (ad.current>=ad.files.length) {
+			ad.current++;
 			return "";
 			return "";
+		}
 		String r = ad.files[ad.current];
 		String r = ad.files[ad.current];
 		ad.current++;
 		ad.current++;
 		return r;
 		return r;