summaryrefslogtreecommitdiff
path: root/src/app.c
blob: dbcdfe9c23ca454f91849dbcbea1fe0546aa495e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <dlfcn.h>
#include <err.h>
#include <assert.h>
#include "jvm/jvm.h"

extern void *apkenv_android_dlopen(const char*, int);
extern void *apkenv_android_dlclose(void*);
extern void *apkenv_android_dlsym(void*, const char*);
extern void apkenv_parse_library_path(const char *path, char *delim);

int
main(int argc, const char *argv[])
{
   if (argc < 2)
      errx(EXIT_FAILURE"usage: so-file");

   printf("loading runtime\n");
   dlopen("libpthread.so", RTLD_NOW | RTLD_GLOBAL);

   printf("loading module: %s\n", argv[1]);

   {
      char path[4096];
      snprintf(path, sizeof(path), "%s", argv[1]);
      apkenv_parse_library_path(dirname(path), ";");
   }

   {
      void *handle;
      if (!(handle = apkenv_android_dlopen(argv[1], RTLD_NOW | RTLD_LOCAL)))
         errx(EXIT_FAILURE"dlopen failed: %s", dlerror());

      printf("trying JNI_OnLoad from: %s\n", argv[1]);

      struct jvm jvm;
      jvm_init(&jvm);

      void* (*JNI_OnLoad)(void*, void*) = apkenv_android_dlsym(handle, "JNI_OnLoad");
      assert(JNI_OnLoad);

      JNI_OnLoad(&jvm.vm, NULL);

      static const char *unity_player_class = "com/unity3d/player/UnityPlayer";
      void (*native_init)(void*, void*, intint) = jvm_get_native_method(&jvm, unity_player_class, "initJni");
      native_init(&jvm.env, (jclass)11024768);

      printf("unloading module: %s\n", argv[1]);
      apkenv_android_dlclose(handle);
      jvm_release(&jvm);
   }

   printf("exiting\n");
   return EXIT_SUCCESS;
}