Skip to content
Permalink
Browse files

fix not compiling on arm64 linux

  • Loading branch information...
UnknownShadow200 committed Jul 11, 2019
1 parent 0371157 commit ee3f89b078d06518d41af1dffed586b750076e8e
Showing with 358 additions and 134 deletions.
  1. +1 −3 src/Formats.c
  2. +31 −54 src/Http.c
  3. +0 −5 src/Launcher.c
  4. +3 −2 src/Logger.c
  5. +63 −7 src/Platform.c
  6. +25 −6 src/Platform.h
  7. +2 −2 src/Program.c
  8. +1 −3 src/Stream.c
  9. +9 −0 src/String.c
  10. +3 −0 src/String.h
  11. +220 −52 src/Window.c
@@ -948,13 +948,11 @@ ReturnCode Dat_Load(struct Stream* stream) {
#define CW_META_RGB NBT_I16,0,1,'R',0,0, NBT_I16,0,1,'G',0,0, NBT_I16,0,1,'B',0,0,

static int Cw_WriteEndString(uint8_t* data, const String* text) {
Codepoint cp;
uint8_t* cur = data + 2;
int i, wrote, len = 0;

for (i = 0; i < text->length; i++) {
cp = Convert_CP437ToUnicode(text->buffer[i]);
wrote = Convert_UnicodeToUtf8(cp, cur);
wrote = Convert_CP437ToUtf8(text->buffer[i], cur);
len += wrote; cur += wrote;
}

@@ -722,66 +722,46 @@ static void Http_SysFree(void) {
}
#elif defined CC_BUILD_ANDROID
#include <android_native_app_glue.h>
#include <jni.h>
struct HttpRequest* java_req;

static jobject MakeJavaString(JNIEnv* env, const String* str) {
String tmp; char tmpBuffer[1024];
String_InitArray_NT(tmp, tmpBuffer);

String_Copy(&tmp, str);
tmp.buffer[tmp.length] = '\0';
return (*env)->NewStringUTF(env, tmp.buffer);
}

static jbyteArray MakeJavaBytes(JNIEnv* env, const uint8_t* src, uint32_t len) {
if (!len) return NULL;
jbyteArray arr = (*env)->NewByteArray(env, len);
(*env)->SetByteArrayRegion(env, arr, 0, len, src);
return arr;
}

static void CallJavaVoid(JNIEnv* env, const char* name, const char* sig, jvalue* args) {
jclass clazz = (*env)->FindClass(env, "com/classicube/Wrappers");
jmethodID method = (*env)->GetStaticMethodID(env, clazz, name, sig);
(*env)->CallStaticVoidMethodA(env, clazz, method, args);
}

static int CallJavaInt(JNIEnv* env, const char* name, const char* sig, jvalue* args) {
jclass clazz = (*env)->FindClass(env, "com/classicube/Wrappers");
jmethodID method = (*env)->GetStaticMethodID(env, clazz, name, sig);
return (*env)->CallStaticIntMethodA(env, clazz, method, args);
}

bool Http_DescribeError(ReturnCode res, String* dst) {
/* TODO: Retrieve from jni */
jni
String err;
JNIEnv* env;
jvalue args[1];
jobject obj;

JavaGetCurrentEnv(env);
args[0].i = res;
obj = JavaCallObject(env, "httpDescribeError", "(I)Ljava/lang/String;", args);
if (!obj) return false;

err = JavaGetString(env, obj);
String_AppendString(dst, &err);
(*env)->ReleaseStringUTFChars(env, obj, err.buffer);
(*env)->DeleteLocalRef(env, obj);
return true;
}

static void Http_SysInit(void) { }

static void Http_AddHeader(const char* key, const String* value) {
JavaVM* vm = (JavaVM*)VM_Handle; JNIEnv* env;
JNIEnv* env;
jvalue args[2];

(*vm)->AttachCurrentThread(vm, &env, NULL);
args[0].l = (*env)->NewStringUTF(env, key);
args[1].l = MakeJavaString(env, value);
JavaGetCurrentEnv(env);
args[0].l = JavaMakeConst(env, key);
args[1].l = JavaMakeString(env, value);

CallJavaVoid(env, "httpSetHeader", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", args);
JavaCallVoid(env, "httpSetHeader", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", args);
(*env)->DeleteLocalRef(env, args[0].l);
(*env)->DeleteLocalRef(env, args[1].l);
}

/* Processes a HTTP header downloaded from the server */
static JNIEXPORT void JNICALL Java_com_classicube_Wrappers_httpParseHeader(JNIEnv* env, jclass c, jstring header) {
String line;
const char* src = (*env)->GetStringUTFChars(env, header, NULL);
jsize length = (*env)->GetStringLength(env, header);

line = String_Init(src, length, length);
String line = JavaGetString(env, header);
Http_ParseHeader(java_req, &line);
(*env)->ReleaseStringUTFChars(env, header, src);
(*env)->ReleaseStringUTFChars(env, header, line.buffer);
}

/* Processes a chunk of data downloaded from the web server */
@@ -800,20 +780,19 @@ static JNIEXPORT void JNICALL Java_com_classicube_Wrappers_httpAppendData(JNIEnv
static ReturnCode Http_SysDo(struct HttpRequest* req) {
static const String userAgent = String_FromConst(GAME_APP_NAME);
static const char* verbs[3] = { "GET", "HEAD", "POST" };

JavaVM* vm = (JavaVM*)VM_Handle; JNIEnv* env;
JNIEnv* env;
jvalue args[3];
String url;
jint res;

(*vm)->AttachCurrentThread(vm, &env, NULL);
JavaGetCurrentEnv(env);
url = String_FromRawArray(req->URL);

args[0].l = MakeJavaString(env, &url);
args[1].l = (*env)->NewStringUTF(env, verbs[req->RequestType]);
args[2].l = MakeJavaBytes(env, req->Data, req->Size);
args[0].l = JavaMakeString(env, &url);
args[1].l = JavaMakeConst(env, verbs[req->RequestType]);
args[2].l = JavaMakeBytes(env, req->Data, req->Size);

res = CallJavaInt(env, "httpInit", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[B)I", args);
res = JavaCallInt(env, "httpInit", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[B)I", args);
(*env)->DeleteLocalRef(env, args[0].l);
(*env)->DeleteLocalRef(env, args[1].l);
(*env)->DeleteLocalRef(env, args[2].l);
@@ -824,7 +803,7 @@ static ReturnCode Http_SysDo(struct HttpRequest* req) {

bufferSize = 0;
http_curProgress = ASYNC_PROGRESS_FETCHING_DATA;
res = CallJavaInt(env, "httpPerform", "()I", args);
res = JavaCallInt(env, "httpPerform", "()I", args);
http_curProgress = 100;
return res;
}
@@ -854,7 +833,7 @@ static void Http_WorkerLoop(void) {
Mutex_Unlock(pendingMutex);

if (stop) return;
/* Block until another thread submits a req to do */
/* Block until another thread submits a request to do */
if (!hasRequest) {
Platform_LogConst("Going back to sleep...");
Waitable_Wait(workerWaitable);
@@ -965,12 +944,10 @@ void Http_UrlEncode(String* dst, const uint8_t* data, int len) {

void Http_UrlEncodeUtf8(String* dst, const String* src) {
uint8_t data[4];
Codepoint cp;
int i, len;

for (i = 0; i < src->length; i++) {
cp = Convert_CP437ToUnicode(src->buffer[i]);
len = Convert_UnicodeToUtf8(cp, data);
len = Convert_CP437ToUtf8(src->buffer[i], data);
Http_UrlEncode(dst, data, len);
}
}
@@ -535,11 +535,6 @@ bool Launcher_StartGame(const String* user, const String* mppass, const String*
if (mppass->length) String_Format3(&args, " %s %s %s", mppass, ip, port);

res = Process_Start(&path, &args);
#ifdef CC_BUILD_WINDOWS
/* TODO: Check this*/
/* HRESULT when user clicks 'cancel' to 'are you sure you want to run ClassiCube.exe' */
if (res == 0x80004005) return;
#endif
if (res) { Logger_Warn(res, "starting game"); return false; }

Launcher_ShouldExit = Options_GetBool(OPT_AUTO_CLOSE_LAUNCHER, false);
@@ -425,7 +425,7 @@ String_Format4(str, "r16=%x r17=%x r18=%x r19=%x" _NL, REG_GNUM(16), REG_GNUM(17
String_Format4(str, "r20=%x r21=%x r22=%x r23=%x" _NL, REG_GNUM(20), REG_GNUM(21), REG_GNUM(22), REG_GNUM(23)); \
String_Format4(str, "r24=%x r25=%x r26=%x r27=%x" _NL, REG_GNUM(24), REG_GNUM(25), REG_GNUM(26), REG_GNUM(27)); \
String_Format3(str, "r28=%x r29=%x r30=%x" _NL, REG_GNUM(28), REG_GNUM(29), REG_GNUM(30)); \
String_Format2(str, "sp =%x pc =%x" _NL, REG_GET(sp,SP), REG_GET(pc,PC));
String_Format2(str, "sp =%x pc =%x" _NL, REG_GET_SP(), REG_GET_PC());

#define Logger_Dump_SPARC() \
String_Format4(str, "o0=%x o1=%x o2=%x o3=%x" _NL, REG_GET(o0,O0), REG_GET(o1,O1), REG_GET(o2,O2), REG_GET(o3,O3)); \
@@ -508,7 +508,8 @@ static void Logger_PrintRegisters(String* str, void* ctx) {
Logger_Dump_X64()
#elif defined __aarch64__
#define REG_GNUM(num) &r.regs[num]
#define REG_GET(reg, ign) &r.##reg
#define REG_GET_SP() &r.sp
#define REG_GET_PC() &r.pc
Logger_Dump_ARM64()
#elif defined __arm__
#define REG_GNUM(num) &r.arm_r##num
@@ -105,8 +105,8 @@ static void SetCurrentToExeDirectory(void) {
for (i = path.length - 1; i >= 0; i--, path.length--) {
if (path.buffer[i] == '/' || path.buffer[i] == '\\') break;
}
res = Directory_SetCurrent(&path);
if (res) { Logger_Warn(res, "setting current directory"); return; }
res = Directory_SetCurrent(&path);
if (res) { Logger_Warn(res, "setting current directory"); return; }
}


@@ -1938,15 +1938,12 @@ bool Platform_DescribeError(ReturnCode res, String* dst) {
int Platform_ConvertString(void* data, const String* src) {
uint8_t* dst = (uint8_t*)data;
uint8_t* cur;

Codepoint cp;
int i, len = 0;
if (src->length > FILENAME_SIZE) Logger_Abort("String too long to expand");

for (i = 0; i < src->length; i++) {
cur = dst + len;
cp = Convert_CP437ToUnicode(src->buffer[i]);
len += Convert_UnicodeToUtf8(cp, cur);
len += Convert_CP437ToUtf8(src->buffer[i], cur);
}
dst[len] = '\0';
return len;
@@ -2058,7 +2055,66 @@ void Platform_Init(void) {

void Platform_SetDefaultCurrentDirectory(void) {
static const String path = String_FromConst("/classicube");
ReturnCode res = Directory_SetCurrent(&path);
ReturnCode res = Directory_SetCurrent(&path);
if (res) Logger_Warn(res, "setting current directory");
}
#endif
/* JNI helpers */
#ifdef CC_BUILD_ANDROID
void* App_Ptr;
JavaVM* VM_Ptr;

UniString JavaGetUniString(JNIEnv* env, jstring str) {
UniString dst;
dst.buffer = (*env)->GetStringChars(env, str, NULL);
dst.length = (*env)->GetStringLength(env, str);
dst.capacity = dst.length;
return dst;
}

String JavaGetString(JNIEnv* env, jstring str) {
String dst;
dst.buffer = (*env)->GetStringUTFChars(env, str, NULL);
dst.length = (*env)->GetStringUTFLength(env, str);
dst.capacity = dst.length;
return dst;
}

jobject JavaMakeString(JNIEnv* env, const String* str) {
uint8_t tmp[2048 + 4];
uint8_t* cur;
int i, len = 0;

for (i = 0; i < str->length && len < 2048; i++) {
cur = tmp + len;
len += Convert_CP437ToUtf8(str->buffer[i], cur);
}
tmp[len] = '\0';
return (*env)->NewStringUTF(env, (const char*)tmp);
}

jbyteArray JavaMakeBytes(JNIEnv* env, const uint8_t* src, uint32_t len) {
if (!len) return NULL;
jbyteArray arr = (*env)->NewByteArray(env, len);
(*env)->SetByteArrayRegion(env, arr, 0, len, src);
return arr;
}

void JavaCallVoid(JNIEnv* env, const char* name, const char* sig, jvalue* args) {
jclass clazz = (*env)->FindClass(env, "com/classicube/Wrappers");
jmethodID method = (*env)->GetStaticMethodID(env, clazz, name, sig);
(*env)->CallStaticVoidMethodA(env, clazz, method, args);
}

jint JavaCallInt(JNIEnv* env, const char* name, const char* sig, jvalue* args) {
jclass clazz = (*env)->FindClass(env, "com/classicube/Wrappers");
jmethodID method = (*env)->GetStaticMethodID(env, clazz, name, sig);
return (*env)->CallStaticIntMethodA(env, clazz, method, args);
}

jobject JavaCallObject(JNIEnv* env, const char* name, const char* sig, jvalue* args) {
jclass clazz = (*env)->FindClass(env, "com/classicube/Wrappers");
jmethodID method = (*env)->GetStaticMethodID(env, clazz, name, sig);
return (*env)->CallStaticObjectMethodA(env, clazz, method, args);
}
#endif
@@ -22,12 +22,6 @@ typedef int FileHandle;
#define UPDATE_FILENAME "update.sh"
#endif

/* android app and VM handle */
#ifdef CC_BUILD_ANDROID
void* App_Handle;
void* VM_Handle;
#endif

/* Origin points for when seeking in a file. */
enum File_SeekFrom { FILE_SEEKFROM_BEGIN, FILE_SEEKFROM_CURRENT, FILE_SEEKFROM_END };
/* Number of milliseconds since 01/01/0001 to start of unix time. */
@@ -240,4 +234,29 @@ CC_API ReturnCode Socket_Close(SocketHandle socket);
/* NOTE: A closed socket is still considered readable. */
/* NOTE: A socket is considered writable once it has finished connecting. */
CC_API ReturnCode Socket_Poll(SocketHandle socket, int mode, bool* success);

#ifdef CC_BUILD_ANDROID
#include <jni.h>
extern void* App_Ptr;
extern JavaVM* VM_Ptr;
#define JavaGetCurrentEnv(env) (*VM_Ptr)->AttachCurrentThread(VM_Ptr, &env, NULL);
#define JavaMakeConst(env, str) (*env)->NewStringUTF(env, str);

/* Allocates a unicode string from the given java string. */
/* NOTE: Don't forget to call env->ReleaseStringChars. */
UniString JavaGetUniString(JNIEnv* env, jstring str);
/* Allocates a string from the given java string. */
/* NOTE: Don't forget to call env->ReleaseStringUTFChars. Only works with ASCII strings. */
String JavaGetString(JNIEnv* env, jstring str);
/* Allocates a java string from the given string. */
jobject JavaMakeString(JNIEnv* env, const String* str);
/* Allocates a java byte array from the given block of memory. */
jbyteArray JavaMakeBytes(JNIEnv* env, const uint8_t* src, uint32_t len);
/* Calls a method in the Wrappers class that returns nothing. */
void JavaCallVoid(JNIEnv* env, const char* name, const char* sig, jvalue* args);
/* Calls a method in the Wrappers class that returns a jint. */
jint JavaCallInt(JNIEnv* env, const char* name, const char* sig, jvalue* args);
/* Calls a method in the Wrappers class that returns a jobject. */
jobject JavaCallObject(JNIEnv* env, const char* name, const char* sig, jvalue* args);
#endif
#endif
@@ -177,8 +177,8 @@ int main(int argc, char** argv) {
#ifdef CC_BUILD_ANDROID
#include <android_native_app_glue.h>
void android_main(struct android_app* app) {
App_Handle = app;
VM_Handle = app->activity->vm;
App_Ptr = app;
VM_Ptr = app->activity->vm;
main(0, NULL);
}
#endif
@@ -468,7 +468,6 @@ ReturnCode Stream_WriteLine(struct Stream* s, String* text) {
uint8_t buffer[2048 + 10]; /* some space for newline */
const char* nl;
uint8_t* cur;
Codepoint cp;
ReturnCode res;
int i, len = 0;

@@ -480,8 +479,7 @@ ReturnCode Stream_WriteLine(struct Stream* s, String* text) {
}

cur = buffer + len;
cp = Convert_CP437ToUnicode(text->buffer[i]);
len += Convert_UnicodeToUtf8(cp, cur);
len += Convert_CP437ToUtf8(text->buffer[i], cur);
}

nl = _NL;
@@ -577,6 +577,15 @@ int Convert_UnicodeToUtf8(Codepoint cp, uint8_t* data) {
}
}

int Convert_CP437ToUtf8(char c, uint8_t* data) {
/* Common ASCII case */
if (c >= 0x20 && c < 0x7F) {
data[0] = (uint8_t)c;
return 1;
}
return Convert_UnicodeToUtf8(Convert_CP437ToUnicode(c), data);
}

void String_AppendUtf16(String* value, const Codepoint* chars, int numBytes) {
int i; char c;

@@ -182,6 +182,9 @@ int Convert_Utf8ToUnicode(Codepoint* cp, const uint8_t* data, uint32_t len);
/* Encodes a unicode character in UTF8, returning number of bytes written. */
/* The number of bytes written is always either 1,2 or 3. */
int Convert_UnicodeToUtf8(Codepoint cp, uint8_t* data);
/* Encodes a code page 437 character in UTF8, returning number of bytes written. */
/* The number of bytes written is always either 1,2 or 3. */
int Convert_CP437ToUtf8(char c, uint8_t* data);

/* Attempts to append all characters from UTF16 encoded data to the given string. */
/* Characters not in code page 437 are omitted. */

0 comments on commit ee3f89b

Please sign in to comment.
You can’t perform that action at this time.