Skip to content

Commit 7d8ceb0

Browse files
feat(jni): client handles methods
Forwards the method execution request to jni client, along with a unique id. Handles the native function which provides the result, and based on the id resolves proper stored promise.
1 parent 17b9e17 commit 7d8ceb0

File tree

1 file changed

+92
-2
lines changed

1 file changed

+92
-2
lines changed

templates/module/Source/modulejni/Private/Generated/jni/jniclient.cpp.tpl

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ limitations under the License.
8989
{{$javaClassConverter}}::fill{{Camel .Type }}(Env, {{$javaPropName}}, {{$local_value}});
9090
{{- end }}
9191
{{- end }}
92+
9293
#include "{{$ModuleName}}/Generated/Jni/{{$Iface}}JniClient.h"
9394
#include "{{$ModuleName}}/Generated/Jni/{{$ModuleName}}FromJavaConverter.h"
9495
{{ if or (len .Module.Enums) (len .Module.Structs) -}}
@@ -171,6 +172,7 @@ void {{$Class}}::Initialize(FSubsystemCollectionBase& Collection)
171172
_SubscriptionStatusChanged.Broadcast(value);
172173
_SubscriptionStatusChangedBP.Broadcast(value);
173174
};
175+
174176
{{- range .Interface.Properties }}
175177
on{{Camel .Name}}Changed = [this]({{ueParam "In" . }})
176178
{
@@ -203,6 +205,7 @@ void {{$Class}}::Deinitialize()
203205
{{- range .Interface.Properties}}
204206
on{{Camel .Name}}Changed = on{{Camel .Name}}ChangedEmpty;
205207
{{- end}}
208+
206209
g{{$Class}}Handle = nullptr;
207210
Super::Deinitialize();
208211
}
@@ -265,8 +268,58 @@ void {{$Class}}::Set{{Camel .Name}}({{ueParam "In" .}})
265268
{{- range .Interface.Operations}}
266269
{{ueReturn "" .Return }} {{$Class}}::{{Camel .Name}}({{ueParams "In" .Params}})
267270
{
271+
if (!b_isReady)
272+
{
273+
UE_LOG(Log{{$Iface}}Client_JNI, Error, TEXT("No valid connection to service. Check that android service is set up correctly"));
274+
return {{ueDefault "" .Return }};
275+
}
268276
TPromise<{{ueReturn "" .Return}}> Promise;
269-
// TODO call on java side
277+
278+
#if PLATFORM_ANDROID && USE_ANDROID_JNI
279+
UE_LOG(Log{{$Iface}}Client_JNI, Warning, TEXT("{{$javaClassPath}}/{{$javaClassName}}:{{.Name}} "));
280+
if (m_javaJniClientClass == nullptr)
281+
{
282+
{{- $signatureParams:= jniJavaSignatureParams .Params}}
283+
UE_LOG(Log{{$Iface}}Client_JNI, Warning, TEXT("{{$javaClassPath}}/{{$javaClassName}}:{{camel .Name}}Async:(Ljava/lang/String;{{$signatureParams}})V CLASS not found"));
284+
return {{ueDefault "" .Return }};
285+
}
286+
JNIEnv* Env = FAndroidApplication::GetJavaEnv();
287+
static jmethodID MethodID = Env->GetMethodID(m_javaJniClientClass, "{{camel .Name}}Async", "(Ljava/lang/String;{{$signatureParams}})V");
288+
if (MethodID != nullptr)
289+
{
290+
auto id = g{{$Class}}methodHelper.StorePromise(Promise);
291+
auto idString = FJavaHelper::ToJavaString(Env, id.ToString(EGuidFormats::Digits));
292+
293+
{{- range .Params -}}
294+
{{template "convert_to_java_type_in_param" .}}
295+
{{- end }};
296+
297+
FJavaWrapper::CallVoidMethod(Env, m_javaJniClientInstance, MethodID, *idString, {{- range $idx, $p := .Params -}} {{- if $idx}}, {{ end -}}
298+
{{- $javaPropName := Camel .Name}}
299+
{{- $cppropName := ueVar "In" .}}
300+
{{- $localName := printf "jlocal_%s" $javaPropName }}
301+
{{- if .IsArray }} {{$localName}}
302+
{{- else if or ( or (eq .KindType "enum") (eq .KindType "string") ) (not .IsPrimitive ) }} {{$localName}}
303+
{{- else }} {{$cppropName}}
304+
{{- end -}}
305+
{{- end -}});
306+
307+
{{- range $idx, $p := .Params -}}
308+
{{- $javaPropName := Camel .Name}}
309+
{{- $localName := printf "jlocal_%s" $javaPropName }}
310+
{{- if or ( or .IsArray (eq .KindType "enum" ) ) (eq .KindType "string")}}
311+
Env->DeleteLocalRef({{$localName}});
312+
{{- else if not ( or (eq .KindType "extern") (ueIsStdSimpleType .) ) }}
313+
Env->DeleteLocalRef({{$localName}});
314+
{{- end }}
315+
{{- end }}
316+
}
317+
else
318+
{
319+
UE_LOG(Log{{$Iface}}Client_JNI, Warning, TEXT("{{$javaClassPath}}/{{$javaClassName}}:{{camel .Name}}Async (Ljava/lang/String;{{$signatureParams}})V not found"));
320+
}
321+
#endif
322+
//TODO probalby #elsif set some default on promise as a result.
270323
return Promise.GetFuture().Get();
271324

272325
}
@@ -313,6 +366,7 @@ bool {{$Class}}::_bindToService(FString servicePackage, FString connectionId){
313366
#endif
314367
return false;
315368
}
369+
316370
void {{$Class}}::_unbind()
317371
{
318372
@@ -408,7 +462,43 @@ JNI_METHOD void {{$jniFullFuncPrefix}}_nativeOn{{Camel .Name}}(JNIEnv* Env, jcla
408462

409463
JNI_METHOD void {{$jniFullFuncPrefix}}_nativeOn{{Camel .Name}}Result(JNIEnv* Env, jclass Clazz, {{jniToReturnType .Return }} result, jstring callId)
410464
{
411-
// TODO resolve proper promise
465+
UE_LOG(Log{{$Iface}}Client_JNI, Warning, TEXT("{{$jniFullFuncPrefix}}_nativeOn{{Camel .Name}}Result"));
466+
FString callIdString = FJavaHelper::FStringFromParam(Env, callId);
467+
FGuid guid;
468+
469+
470+
{{- $javaClassConverter := printf "%sJavaConverter" ( Camel .Return.Schema.Import ) }}
471+
{{- $hasLocalVar := 1 }}
472+
{{- if (eq $javaClassConverter "JavaConverter" )}}{{- $javaClassConverter = printf "%sJavaConverter" $ModuleName}}{{ end }}
473+
{{- if .Return.IsArray }}
474+
{{ueReturn "" .}} cpp_result = {{ ueDefault "" .Return }};
475+
{{- if (eq .Return.KindType "string")}}
476+
cpp_result = FJavaHelper::ObjectArrayToFStringTArray(Env, result);
477+
{{- else if .Return.IsPrimitive }}
478+
jobjectArray localArray = (jobjectArray)result;
479+
jsize len = Env->GetArrayLength(localArray);
480+
cpp_result.Reserve(len);
481+
Env->Get{{jniToEnvNameType .Return}}ArrayRegion(result, 0, len, cpp_result.GetData());
482+
Env->DeleteLocalRef(localArray);
483+
{{- else if not (eq .Return.KindType "extern")}}
484+
{{$javaClassConverter}}::fill{{Camel .Return.Type }}Array(Env, result, cpp_result);
485+
{{- end }}
486+
{{- else if eq .Return.KindType "enum" }}
487+
{{ueReturn "" .Return}} cpp_result = {{$javaClassConverter}}::get{{Camel .Return.Type }}Value(Env, result);
488+
{{- else if (eq .Return.KindType "string")}}
489+
FString cpp_result = FJavaHelper::FStringFromParam(Env, result);
490+
{{- else if not (ueIsStdSimpleType .Return )}}
491+
{{ueReturn "" .Return}} cpp_result = {{ ueDefault "" .Return }};
492+
{{$javaClassConverter}}::fill{{Camel .Return.Type }}(Env, result,cpp_result);
493+
{{- else }}
494+
{{- $hasLocalVar = 0 }}
495+
{{- end }}
496+
497+
FGuid::Parse(callIdString, guid);
498+
AsyncTask(ENamedThreads::GameThread, [guid, {{if $hasLocalVar}}cpp_{{end}}result]()
499+
{
500+
g{{$Class}}methodHelper.FulfillPromise(guid, {{if $hasLocalVar}}cpp_{{end}}result);
501+
});
412502
}
413503

414504
{{- end }}

0 commit comments

Comments
 (0)