Skip to content

Commit 8eb6949

Browse files
committed
CHG: Also download https paksets from Android
git-svn-id: svn://tron.homeunix.org/simutrans/simutrans/trunk@11631 8aca7d54-2c30-db11-9de9-000461428c89
1 parent fcd90db commit 8eb6949

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

src/android/android.cc

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,84 @@
1-
#include "android.h"
21
#include "../simutrans/sys/simsys.h"
2+
#include "../simutrans/simdebug.h"
3+
4+
#include <jni.h>
5+
#include <android/log.h>
6+
#include <string>
7+
8+
#include "android.h"
9+
10+
// Function to find the class and method ID (Must call this once on init because otherwise not found) => cache the results!
11+
// for some reason (maybe because main SDL runs in other thread) this works only for static functions
12+
static struct MethodIDs {
13+
jmethodID downloadfile;
14+
jclass myClass;
15+
} methodIDs;
16+
17+
bool getMethodIDs(JNIEnv *env, jobject thisClass) {
18+
// Find the Java class
19+
jclass cls = env->FindClass("com/simutrans/Simutrans"); // Use fully qualified name
20+
if (cls == nullptr) {
21+
dbg->fatal("getMethodIDs()","Class not found");
22+
return false; // Class not found
23+
}
24+
25+
methodIDs.myClass = static_cast<jclass>(env->NewGlobalRef(cls)); // Prevent GC
26+
27+
// Find the instance method
28+
methodIDs.downloadfile = env->GetStaticMethodID(methodIDs.myClass, "downloadFile", "(Ljava/lang/String;Ljava/lang/String;)V");
29+
if (methodIDs.downloadfile == nullptr) {
30+
env->ExceptionClear(); // Clear the exception if the method is not found
31+
__android_log_print(ANDROID_LOG_ERROR, "Simutrans", "methodIDs.downloadfile not found!");
32+
return false; // Method not found
33+
}
34+
__android_log_print(ANDROID_LOG_INFO, "Simutrans", "methodIDs.downloadfile %p", methodIDs.downloadfile);
35+
36+
return true;
37+
}
338

439
extern "C" {
5-
JNIEXPORT jstring JNICALL Java_com_simutrans_Simutrans_getVersion(JNIEnv* env, jobject thisObject) {
6-
return env->NewStringUTF(get_version());
40+
41+
// get the version but
42+
JNIEXPORT jstring JNICALL Java_com_simutrans_Simutrans_getVersion(JNIEnv* env, jobject thisClass)
43+
{
44+
// now we have a valid class reference => use this to get the static memberIDs ...
45+
getMethodIDs(env,thisClass);
46+
return env->NewStringUTF(get_version());
47+
}
748
}
849

50+
51+
#include <SDL_system.h>
52+
53+
const char *download_file(const char *url, const char *filename)
54+
{
55+
const char * error_msg = NULL;
56+
JNIEnv *env = (JNIEnv *)SDL_AndroidGetJNIEnv();
57+
58+
jstring jurl = env->NewStringUTF(url);
59+
jstring jfilename = env->NewStringUTF(filename);
60+
61+
62+
// Call the Java method
63+
env->CallStaticVoidMethod(methodIDs.myClass, methodIDs.downloadfile, jurl, jfilename);
64+
65+
// Check for exceptions
66+
if (env->ExceptionCheck()) {
67+
jthrowable exception = env->ExceptionOccurred();
68+
env->ExceptionDescribe();
69+
env->ExceptionClear();
70+
71+
jclass exceptionCls = env->GetObjectClass(exception);
72+
jmethodID getMessage = env->GetMethodID(exceptionCls, "getMessage", "()Ljava/lang/String;");
73+
jstring message = (jstring)env->CallObjectMethod(exception, getMessage);
74+
error_msg = env->GetStringUTFChars(message, nullptr);
75+
__android_log_print(ANDROID_LOG_INFO, "Simutrans", "downloadfile from %s failed with %s", url, error_msg);
76+
}
77+
78+
env->DeleteLocalRef(jurl);
79+
env->DeleteLocalRef(jfilename);
80+
81+
return error_msg;
982
}
83+
84+

src/simutrans/dataobj/pakset_downloader.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#ifdef __ANDROID__
2525
// since the script does only work on few devices
2626
#define USE_OWN_PAKINSTALL
27+
28+
extern const char* download_file(const char* url, const char* filename);
2729
#endif
2830

2931

@@ -70,13 +72,7 @@ bool pak_can_download(const paksetinfo_t *pi)
7072
return false;
7173
#endif
7274
#else
73-
#ifdef __ANDROID__
74-
// no https for Android for now
75-
if (strncmp(pi->url, "https://",8)==0) {
76-
return false;
77-
}
78-
#endif
79-
// own routines, require http (for Android) and zip
75+
// own routines require zip
8076
if (strstr(pi->url, ".zip")) {
8177
return true;
8278
}
@@ -224,6 +220,13 @@ bool pak_download(vector_tpl<paksetinfo_t*>paks)
224220
sprintf(outfilename, "curl --progress-bar -L '%s' > 'temp.zip'", pi->url);
225221
system(outfilename);
226222
strcpy(outfilename, "temp.zip");
223+
#else
224+
// using system to download
225+
strcpy(outfilename, "temp.zip");
226+
if (const char* err = download_file(pi->url, "temp.zip")) {
227+
outfilename[0] = 0;
228+
dbg->warning("pak_download()","Failed to download %s",pi->url);
229+
}
227230
#endif
228231
#endif
229232
}

0 commit comments

Comments
 (0)