mirror of
https://github.com/hpd840321/craftlabs-authorization-sdk.git
synced 2026-06-09 18:10:30 +08:00
46 lines
2.2 KiB
C
46 lines
2.2 KiB
C
/* JNI bridge — connects Java NativeBridge to Rust C ABI
|
|
* Compile: gcc -shared -fPIC -I$JAVA_HOME/include -I$JAVA_HOME/include/darwin
|
|
* -I native/include -o libcraftlabs_jni_bridge.so jni_bridge.c -L. -lcraftlabs_auth_core
|
|
*
|
|
* Then: java -Djava.library.path=. ... loads both libraries.
|
|
* Or rename the output to craftlabs_auth_core and load it directly.
|
|
*/
|
|
#include <jni.h>
|
|
#include "craftlabs_auth.h"
|
|
|
|
JNIEXPORT jlong JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeInitialize(
|
|
JNIEnv *env, jclass clazz, jstring configJson) {
|
|
const char *utf = (*env)->GetStringUTFChars(env, configJson, NULL);
|
|
jlong handle = (jlong)(intptr_t)craft_initialize(utf);
|
|
(*env)->ReleaseStringUTFChars(env, configJson, utf);
|
|
return handle;
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeDestroy(
|
|
JNIEnv *env, jclass clazz, jlong handle) {
|
|
(void)env; (void)clazz;
|
|
if (handle) craft_destroy((AuthHandle)(intptr_t)handle);
|
|
}
|
|
|
|
JNIEXPORT jobject JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeActivate(
|
|
JNIEnv *env, jclass clazz, jlong handle, jstring licenseKey) {
|
|
(void)clazz;
|
|
const char *key = (*env)->GetStringUTFChars(env, licenseKey, NULL);
|
|
AuthResult r = craft_activate((AuthHandle)(intptr_t)handle, key);
|
|
(*env)->ReleaseStringUTFChars(env, licenseKey, key);
|
|
jclass cls = (*env)->FindClass(env, "cn/craftlabs/auth/AuthResult");
|
|
jmethodID ctor = (*env)->GetMethodID(env, cls, "<init>", "(ZLjava/lang/String;)V");
|
|
jstring msg = (*env)->NewStringUTF(env, r.message ? r.message : "");
|
|
return (*env)->NewObject(env, cls, ctor, r.success ? JNI_TRUE : JNI_FALSE, msg);
|
|
}
|
|
|
|
JNIEXPORT jobject JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeCheckLicense(
|
|
JNIEnv *env, jclass clazz, jlong handle) {
|
|
(void)clazz;
|
|
AuthResult r = craft_check_license((AuthHandle)(intptr_t)handle);
|
|
jclass cls = (*env)->FindClass(env, "cn/craftlabs/auth/AuthResult");
|
|
jmethodID ctor = (*env)->GetMethodID(env, cls, "<init>", "(ZLjava/lang/String;)V");
|
|
jstring msg = (*env)->NewStringUTF(env, r.message ? r.message : "");
|
|
return (*env)->NewObject(env, cls, ctor, r.success ? JNI_TRUE : JNI_FALSE, msg);
|
|
}
|