godot_plugin_jni.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*************************************************************************/
  2. /* godot_plugin_jni.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "godot_plugin_jni.h"
  31. #include <core/engine.h>
  32. #include <core/error_macros.h>
  33. #include <core/project_settings.h>
  34. #include <platform/android/jni_utils.h>
  35. #include <platform/android/string_android.h>
  36. static HashMap<String, JNISingleton *> jni_singletons;
  37. extern "C" {
  38. JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterSingleton(JNIEnv *env, jobject obj, jstring name) {
  39. String singname = jstring_to_string(name, env);
  40. JNISingleton *s = memnew(JNISingleton);
  41. s->set_instance(env->NewGlobalRef(obj));
  42. jni_singletons[singname] = s;
  43. Engine::get_singleton()->add_singleton(Engine::Singleton(singname, s));
  44. ProjectSettings::get_singleton()->set(singname, s);
  45. }
  46. JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterMethod(JNIEnv *env, jobject obj, jstring sname, jstring name, jstring ret, jobjectArray args) {
  47. String singname = jstring_to_string(sname, env);
  48. ERR_FAIL_COND(!jni_singletons.has(singname));
  49. JNISingleton *s = jni_singletons.get(singname);
  50. String mname = jstring_to_string(name, env);
  51. String retval = jstring_to_string(ret, env);
  52. Vector<Variant::Type> types;
  53. String cs = "(";
  54. int stringCount = env->GetArrayLength(args);
  55. for (int i = 0; i < stringCount; i++) {
  56. jstring string = (jstring)env->GetObjectArrayElement(args, i);
  57. const String rawString = jstring_to_string(string, env);
  58. types.push_back(get_jni_type(rawString));
  59. cs += get_jni_sig(rawString);
  60. }
  61. cs += ")";
  62. cs += get_jni_sig(retval);
  63. jclass cls = env->GetObjectClass(s->get_instance());
  64. jmethodID mid = env->GetMethodID(cls, mname.ascii().get_data(), cs.ascii().get_data());
  65. if (!mid) {
  66. print_line("Failed getting method ID " + mname);
  67. }
  68. s->add_method(mname, mid, types, get_jni_type(retval));
  69. }
  70. JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterGDNativeLibraries(JNIEnv *env, jobject obj, jobjectArray gdnlib_paths) {
  71. int gdnlib_count = env->GetArrayLength(gdnlib_paths);
  72. if (gdnlib_count == 0) {
  73. return;
  74. }
  75. // Retrieve the current list of gdnative libraries.
  76. Array singletons = Array();
  77. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  78. singletons = ProjectSettings::get_singleton()->get("gdnative/singletons");
  79. }
  80. // Insert the libraries provided by the plugin
  81. for (int i = 0; i < gdnlib_count; i++) {
  82. jstring relative_path = (jstring)env->GetObjectArrayElement(gdnlib_paths, i);
  83. String path = "res://" + jstring_to_string(relative_path, env);
  84. if (!singletons.has(path)) {
  85. singletons.push_back(path);
  86. }
  87. env->DeleteLocalRef(relative_path);
  88. }
  89. // Insert the updated list back into project settings.
  90. ProjectSettings::get_singleton()->set("gdnative/singletons", singletons);
  91. }
  92. }