From f80ca2cceb5b32b341b57b0f3b754bf358a7b1fb Mon Sep 17 00:00:00 2001 From: qiuguohua Date: Sun, 9 Mar 2025 23:34:12 +0800 Subject: [PATCH] Implementation of google play auto login, achievements, PGS Recall API (#18416) --- @types/jsb.d.ts | 82 ++++++ native/CMakeLists.txt | 52 +++- .../bindings/manual/jsb_module_register.cpp | 8 + .../google/billing/GoogleBilling.java | 0 .../google/billing/GoogleBillingHelper.java | 2 +- .../google/play/AchievementsClientHelper.java | 105 ++++++++ .../google/play/GamesSignInClientHelper.java | 53 ++++ .../play/google/play/PlayGamesSdkHelper.java | 42 +++ .../play/google/play/RecallClientHelper.java | 39 +++ .../vendor/play/google/play/TaskManager.java | 212 +++++++++++++++ .../android/libcocos2dx/AndroidManifest.xml | 4 + .../platform/android/libcocos2dx/build.gradle | 10 +- .../src/main/res/values-zh-rCN/strings.xml | 1 + .../src/main/res/values/strings.xml | 1 + .../java/jni/JniGooglePlayHandler.cpp | 50 ++++ .../{ => google/billing}/google_billing.i | 0 .../android/google/play/google_play.i | 43 +++ .../vendor/google/billing/GoogleBilling.cpp | 25 -- native/vendor/google/billing/GoogleBilling.h | 17 +- .../google/billing/GoogleBillingHelper.cpp | 53 +--- native/vendor/google/billing/JniBilling.cpp | 34 +-- native/vendor/google/common/JniUtils.cpp | 112 ++++++++ native/vendor/google/common/JniUtils.h | 48 ++++ native/vendor/google/common/JsUtils.cpp | 51 ++++ native/vendor/google/common/JsUtils.h | 47 ++++ .../vendor/google/common/ScopedListener.cpp | 31 +++ native/vendor/google/common/ScopedListener.h | 48 ++++ .../vendor/google/play/AchievementsClient.cpp | 80 ++++++ .../vendor/google/play/AchievementsClient.h | 47 ++++ .../vendor/google/play/GamesSignInClient.cpp | 52 ++++ native/vendor/google/play/GamesSignInClient.h | 38 +++ native/vendor/google/play/PlayGames.cpp | 38 +++ native/vendor/google/play/PlayGames.h | 59 ++++ native/vendor/google/play/PlayTask.cpp | 235 ++++++++++++++++ native/vendor/google/play/PlayTask.h | 89 ++++++ native/vendor/google/play/PlayTaskManager.cpp | 86 ++++++ native/vendor/google/play/PlayTaskManager.h | 61 +++++ native/vendor/google/play/RecallClient.cpp | 36 +++ native/vendor/google/play/RecallClient.h | 36 +++ native/vendor/google/play/TResult.h | 63 +++++ .../source/platforms/google-play.ts | 3 +- templates/google-play/build/gradle.properties | 2 + vendor/google/billing/billing-enum.ts | 2 +- vendor/google/billing/billing.ts | 159 ----------- vendor/google/index.ts | 158 ++++++++++- vendor/google/play/games.ts | 253 ++++++++++++++++++ vendor/google/play/task.ts | 55 ++++ 47 files changed, 2438 insertions(+), 284 deletions(-) rename native/cocos/platform/android/java/vendor/{ => billing}/google/billing/GoogleBilling.java (100%) rename native/cocos/platform/android/java/vendor/{ => billing}/google/billing/GoogleBillingHelper.java (99%) create mode 100644 native/cocos/platform/android/java/vendor/play/google/play/AchievementsClientHelper.java create mode 100644 native/cocos/platform/android/java/vendor/play/google/play/GamesSignInClientHelper.java create mode 100644 native/cocos/platform/android/java/vendor/play/google/play/PlayGamesSdkHelper.java create mode 100644 native/cocos/platform/android/java/vendor/play/google/play/RecallClientHelper.java create mode 100644 native/cocos/platform/android/java/vendor/play/google/play/TaskManager.java create mode 100644 native/cocos/platform/java/jni/JniGooglePlayHandler.cpp rename native/tools/swig-config/android/{ => google/billing}/google_billing.i (100%) create mode 100644 native/tools/swig-config/android/google/play/google_play.i create mode 100644 native/vendor/google/common/JniUtils.cpp create mode 100644 native/vendor/google/common/JniUtils.h create mode 100644 native/vendor/google/common/JsUtils.cpp create mode 100644 native/vendor/google/common/JsUtils.h create mode 100644 native/vendor/google/common/ScopedListener.cpp create mode 100644 native/vendor/google/common/ScopedListener.h create mode 100644 native/vendor/google/play/AchievementsClient.cpp create mode 100644 native/vendor/google/play/AchievementsClient.h create mode 100644 native/vendor/google/play/GamesSignInClient.cpp create mode 100644 native/vendor/google/play/GamesSignInClient.h create mode 100644 native/vendor/google/play/PlayGames.cpp create mode 100644 native/vendor/google/play/PlayGames.h create mode 100644 native/vendor/google/play/PlayTask.cpp create mode 100644 native/vendor/google/play/PlayTask.h create mode 100644 native/vendor/google/play/PlayTaskManager.cpp create mode 100644 native/vendor/google/play/PlayTaskManager.h create mode 100644 native/vendor/google/play/RecallClient.cpp create mode 100644 native/vendor/google/play/RecallClient.h create mode 100644 native/vendor/google/play/TResult.h delete mode 100644 vendor/google/billing/billing.ts create mode 100644 vendor/google/play/games.ts create mode 100644 vendor/google/play/task.ts diff --git a/@types/jsb.d.ts b/@types/jsb.d.ts index 69251386de..a25e326620 100644 --- a/@types/jsb.d.ts +++ b/@types/jsb.d.ts @@ -1415,6 +1415,88 @@ declare namespace jsb { showExternalOfferInformationDialog: (listener: ExternalOfferInformationDialogListener) => void; showInAppMessages: (params: InAppMessageParams, listener: InAppMessageResponseListener) => void; } + + export class PlayException { + getMessage(): string; + getLocalizedMessage(): string; + printStackTrace(): void; + toString(): string; + } + + export interface OnCanceledListener { + onCanceled(): void; + } + + export interface OnCompleteListener { + onComplete(task: any): void; + } + + export interface OnFailureListener { + onFailure(e: PlayException): void; + } + + export interface OnSuccessListener { + onSuccess(result: any): void; + } + + export interface OnContinueWithListener { + then(result: any): void; + } + + export interface OnContinueWithTaskListener { + then(result: any): void; + } + + export class PlayTask { + public addOnCanceledListener(listener: OnCanceledListener): PlayTask; + public addOnCompleteListener(listener: OnCompleteListener): PlayTask; + public addOnFailureListener(listener: OnFailureListener): PlayTask; + public addOnSuccessListener(listener: OnSuccessListener): PlayTask; + public continueWith(listener: OnContinueWithListener): PlayTask; + public getResult(listener: OnSuccessListener): any; + public isCanceled(): boolean; + public isComplete(): boolean; + public isSuccessful(): boolean; + } + export class AuthenticationResult { + private constructor(); + public isAuthenticated(): boolean; + } + export class RecallAccess { + private constructor(); + public hashCode(): number; + public getSessionId(): string; + public equals(other: RecallAccess): boolean; + } + export class GamesSignInClient { + public isAuthenticated(): PlayTask; + public requestServerSideAccess(serverClientId: string, forceRefreshToken: boolean): PlayTask; + public signIn(): PlayTask; + } + export class AchievementsClient { + public showAchievements(): void; + public incrementImmediate(id: string, numSteps: number): PlayTask; + public load(forceReload: boolean): PlayTask; + public revealImmediate(id: string): PlayTask; + public setStepsImmediate(id: string, numSteps: number): PlayTask; + public unlockImmediate(id: string): PlayTask; + public increment(id: string, numSteps: number): void; + public reveal(id: string): void; + public setSteps(id: string, numSteps: number): void; + public unlock(id: string): void; + } + + export class RecallClient { + public requestRecallAccess(): PlayTask; + } + export class PlayGames { + public static getAchievementsClient(): AchievementsClient; + public static getGamesSignInClient(): GamesSignInClient; + public static getRecallClient(): RecallClient; + } + export class PlayGamesSdk { + public static initialize(): void; + } } declare namespace ns { diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index bf4ee047a8..b18089cdc0 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -53,7 +53,8 @@ cc_set_if_undefined(USE_WEBP ON) cc_set_if_undefined(NET_MODE 0) # 0 is client cc_set_if_undefined(USE_REMOTE_LOG OFF) cc_set_if_undefined(USE_ADPF OFF) -cc_set_if_undefined(USE_GOOGLE_BILLING OFF) +cc_set_if_undefined(USE_GOOGLE_BILLING OFF) +cc_set_if_undefined(USE_GOOGLE_PLAY_GAMES OFF) if(ANDROID AND NOT DEFINED USE_CCACHE) if("$ENV{COCOS_USE_CCACHE}" STREQUAL "1") @@ -763,6 +764,18 @@ elseif(ANDROID OR OHOS) endif() endif() + +if(USE_GOOGLE_BILLING OR USE_GOOGLE_PLAY_GAMES) + cocos_source_files( + vendor/google/common/ScopedListener.cpp + vendor/google/common/ScopedListener.h + vendor/google/common/JniUtils.cpp + vendor/google/common/JniUtils.h + vendor/google/common/JsUtils.cpp + vendor/google/common/JsUtils.h + ) +endif() + if(USE_GOOGLE_BILLING) cocos_source_files( vendor/google/billing/GoogleBilling.cpp @@ -796,6 +809,25 @@ if(USE_GOOGLE_BILLING) ) endif() +if(USE_GOOGLE_PLAY_GAMES) +cocos_source_files( + vendor/google/play/PlayTask.h + vendor/google/play/PlayTask.cpp + vendor/google/play/PlayTaskManager.h + vendor/google/play/PlayTaskManager.cpp + vendor/google/play/TResult.h + + vendor/google/play/PlayGames.h + vendor/google/play/PlayGames.cpp + vendor/google/play/GamesSignInClient.h + vendor/google/play/GamesSignInClient.cpp + vendor/google/play/AchievementsClient.h + vendor/google/play/AchievementsClient.cpp + vendor/google/play/RecallClient.h + vendor/google/play/RecallClient.cpp +) +endif() + if(ANDROID) cocos_source_files( cocos/platform/android/modules/Screen.cpp @@ -1102,9 +1134,12 @@ set(SWIG_OUTPUT ${SWIG_OUTPUT_ROOT}/cocos/bindings/auto) cc_gen_swig_files(${CMAKE_CURRENT_LIST_DIR}/tools/swig-config ${SWIG_OUTPUT}) if(USE_GOOGLE_BILLING) - cc_gen_swig_files(${CMAKE_CURRENT_LIST_DIR}/tools/swig-config/android ${SWIG_OUTPUT}) + cc_gen_swig_files(${CMAKE_CURRENT_LIST_DIR}/tools/swig-config/android/google/billing ${SWIG_OUTPUT}) endif() +if(USE_GOOGLE_PLAY_GAMES) + cc_gen_swig_files(${CMAKE_CURRENT_LIST_DIR}/tools/swig-config/android/google/play ${SWIG_OUTPUT}) +endif() ##### platform cocos_source_files( cocos/platform/Image.cpp @@ -1228,6 +1263,10 @@ if(USE_GOOGLE_BILLING) list(APPEND CC_JNI_SRC_FILES ${CWD}/cocos/platform/java/jni/JniGoogleBillingHandler.cpp) endif() +if(USE_GOOGLE_PLAY_GAMES) + list(APPEND CC_JNI_SRC_FILES ${CWD}/cocos/platform/java/jni/JniGooglePlayHandler.cpp) +endif() + ##### renderer cocos_source_files( cocos/renderer/core/PassUtils.h @@ -2286,6 +2325,13 @@ if(USE_GOOGLE_BILLING) ) endif() +if(USE_GOOGLE_PLAY_GAMES) + cocos_source_files( + NO_WERROR NO_UBUILD ${SWIG_OUTPUT}/jsb_google_play_auto.cpp + ${SWIG_OUTPUT}/jsb_google_play_auto.h + ) +endif() + if(USE_XR_REMOTE_PREVIEW) cocos_source_files( NO_WERROR cocos/xr/XRRemotePreviewManager.h @@ -3386,7 +3432,9 @@ function(cc_apply_definations target) $<$,$>:CC_DEBUG=1> $,CC_USE_ADPF=1,CC_USE_ADPF=0> $,CC_USE_GOOGLE_BILLING=1,CC_USE_GOOGLE_BILLING=0> + $,CC_USE_GOOGLE_PLAY_GAMES=1,CC_USE_GOOGLE_PLAY_GAMES=0> $,CC_USE_BOX2D_JSB=1,CC_USE_BOX2D_JSB=0> + ) endfunction() diff --git a/native/cocos/bindings/manual/jsb_module_register.cpp b/native/cocos/bindings/manual/jsb_module_register.cpp index 95f1a54bb5..95cb26a035 100644 --- a/native/cocos/bindings/manual/jsb_module_register.cpp +++ b/native/cocos/bindings/manual/jsb_module_register.cpp @@ -138,6 +138,10 @@ #include "cocos/bindings/auto/jsb_google_billing_auto.h" #endif +#if CC_USE_GOOGLE_PLAY_GAMES + #include "cocos/bindings/auto/jsb_google_play_auto.h" +#endif + bool jsb_register_all_modules() { se::ScriptEngine *se = se::ScriptEngine::getInstance(); @@ -211,6 +215,10 @@ bool jsb_register_all_modules() { se->addRegisterCallback(register_all_billing); #endif +#if CC_USE_GOOGLE_PLAY_GAMES + se->addRegisterCallback(register_all_play); +#endif + #if CC_USE_MIDDLEWARE se->addRegisterCallback(register_all_editor_support); diff --git a/native/cocos/platform/android/java/vendor/google/billing/GoogleBilling.java b/native/cocos/platform/android/java/vendor/billing/google/billing/GoogleBilling.java similarity index 100% rename from native/cocos/platform/android/java/vendor/google/billing/GoogleBilling.java rename to native/cocos/platform/android/java/vendor/billing/google/billing/GoogleBilling.java diff --git a/native/cocos/platform/android/java/vendor/google/billing/GoogleBillingHelper.java b/native/cocos/platform/android/java/vendor/billing/google/billing/GoogleBillingHelper.java similarity index 99% rename from native/cocos/platform/android/java/vendor/google/billing/GoogleBillingHelper.java rename to native/cocos/platform/android/java/vendor/billing/google/billing/GoogleBillingHelper.java index 2ba775d8c9..8c940a5e04 100644 --- a/native/cocos/platform/android/java/vendor/google/billing/GoogleBillingHelper.java +++ b/native/cocos/platform/android/java/vendor/billing/google/billing/GoogleBillingHelper.java @@ -1,7 +1,7 @@ /**************************************************************************** Copyright (c) 2024 Xiamen Yaji Software Co., Ltd. -http://www.cocos2d-x.org +https://www.cocos.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/native/cocos/platform/android/java/vendor/play/google/play/AchievementsClientHelper.java b/native/cocos/platform/android/java/vendor/play/google/play/AchievementsClientHelper.java new file mode 100644 index 0000000000..913dbde64a --- /dev/null +++ b/native/cocos/platform/android/java/vendor/play/google/play/AchievementsClientHelper.java @@ -0,0 +1,105 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + +https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +package google.play; + +import static androidx.core.app.ActivityCompat.startActivityForResult; + +import android.content.Intent; +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; + +import com.cocos.lib.GlobalObject; +import com.google.android.gms.games.AchievementsClient; +import com.google.android.gms.games.AnnotatedData; +import com.google.android.gms.games.PlayGames; +import com.google.android.gms.games.achievement.AchievementBuffer; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.android.gms.tasks.Task; + +public final class AchievementsClientHelper { + public static void showAchievements() { + GlobalObject.runOnUiThread(new Runnable() { + @Override + public void run() { + PlayGames.getAchievementsClient(GlobalObject.getActivity()) + .getAchievementsIntent() + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Intent intent) { + GlobalObject.getActivity().startActivityForResult(intent, 100); + } + }); + } + }); + } + public static int incrementImmediate(@NonNull String id, @IntRange(from = 0L) int numSteps) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + Task task = achievementsClient.incrementImmediate(id, numSteps); + return TaskManager.putTask(task); + } + + public static int load(boolean forceReload) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + Task> task = achievementsClient.load(forceReload); + return TaskManager.putTask(task); + } + public static int revealImmediate(@NonNull String id) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + Task task = achievementsClient.revealImmediate(id); + return TaskManager.putTask(task); + } + + public static int setStepsImmediate(@NonNull String id, @IntRange(from = 0L) int numSteps) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + Task task = achievementsClient.setStepsImmediate(id, numSteps); + return TaskManager.putTask(task); + } + + public static int unlockImmediate(@NonNull String id) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + Task task = achievementsClient.unlockImmediate(id); + return TaskManager.putTask(task); + } + + public static void increment(@NonNull String id, @IntRange(from = 0L) int numSteps) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + achievementsClient.increment(id, numSteps); + } + + public static void reveal(@NonNull String id) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + achievementsClient.reveal(id); + } + + public static void setSteps(@NonNull String id, @IntRange(from = 0L) int numSteps) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + achievementsClient.setSteps(id, numSteps); + } + + public static void unlock(@NonNull String id) { + AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity()); + achievementsClient.unlock(id); + } +} diff --git a/native/cocos/platform/android/java/vendor/play/google/play/GamesSignInClientHelper.java b/native/cocos/platform/android/java/vendor/play/google/play/GamesSignInClientHelper.java new file mode 100644 index 0000000000..a91ea0837e --- /dev/null +++ b/native/cocos/platform/android/java/vendor/play/google/play/GamesSignInClientHelper.java @@ -0,0 +1,53 @@ +/**************************************************************************** +Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + +https://www.cocos.com/ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ****************************************************************************/ + +package google.play; + +import androidx.annotation.NonNull; + +import com.cocos.lib.GlobalObject; +import com.google.android.gms.games.AuthenticationResult; +import com.google.android.gms.games.GamesSignInClient; +import com.google.android.gms.games.PlayGames; +import com.google.android.gms.tasks.Task; + +public final class GamesSignInClientHelper { + public static int isAuthenticated() { + GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(GlobalObject.getActivity()); + Task task = gamesSignInClient.isAuthenticated(); + return TaskManager.putTask(task); + } + + public static int requestServerSideAccess(@NonNull String serverClientId, boolean forceRefreshToken) { + GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(GlobalObject.getActivity()); + Task task = gamesSignInClient.requestServerSideAccess(serverClientId, forceRefreshToken); + return TaskManager.putTask(task); + } + + public static int signIn() { + GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(GlobalObject.getActivity()); + Task task = gamesSignInClient.signIn(); + return TaskManager.putTask(task); + } +} diff --git a/native/cocos/platform/android/java/vendor/play/google/play/PlayGamesSdkHelper.java b/native/cocos/platform/android/java/vendor/play/google/play/PlayGamesSdkHelper.java new file mode 100644 index 0000000000..63e6f09bdb --- /dev/null +++ b/native/cocos/platform/android/java/vendor/play/google/play/PlayGamesSdkHelper.java @@ -0,0 +1,42 @@ +/**************************************************************************** +Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + +https://www.cocos.com/ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ****************************************************************************/ + +package google.play; +import com.cocos.lib.GlobalObject; +import com.cocos.lib.R; +import com.google.android.gms.games.PlayGamesSdk; +import android.content.Context; +import android.util.Log; + +public final class PlayGamesSdkHelper { + private static final String TAG = TaskManager.class.getSimpleName(); + public static void initialize() { + String gameServicesProjectId = GlobalObject.getActivity().getResources().getString(R.string.game_services_project_id); + if(gameServicesProjectId.isEmpty()) { + Log.e(TAG, "The game server's project ID has not been configured"); + } + PlayGamesSdk.initialize(GlobalObject.getActivity()); + + } +} diff --git a/native/cocos/platform/android/java/vendor/play/google/play/RecallClientHelper.java b/native/cocos/platform/android/java/vendor/play/google/play/RecallClientHelper.java new file mode 100644 index 0000000000..22ba74553a --- /dev/null +++ b/native/cocos/platform/android/java/vendor/play/google/play/RecallClientHelper.java @@ -0,0 +1,39 @@ +/**************************************************************************** +Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + +https://www.cocos.com/ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ****************************************************************************/ + +package google.play; + +import com.cocos.lib.GlobalObject; +import com.google.android.gms.games.PlayGames; +import com.google.android.gms.games.RecallAccess; +import com.google.android.gms.games.RecallClient; +import com.google.android.gms.tasks.Task; + +public final class RecallClientHelper { + public static int requestRecallAccess() { + RecallClient recallClient = PlayGames.getRecallClient(GlobalObject.getActivity()); + Task task = recallClient.requestRecallAccess(); + return TaskManager.putTask(task); + } +} diff --git a/native/cocos/platform/android/java/vendor/play/google/play/TaskManager.java b/native/cocos/platform/android/java/vendor/play/google/play/TaskManager.java new file mode 100644 index 0000000000..ee9c16d19c --- /dev/null +++ b/native/cocos/platform/android/java/vendor/play/google/play/TaskManager.java @@ -0,0 +1,212 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +package google.play; +import android.util.Log; + +import com.cocos.lib.CocosHelper; +import com.google.android.gms.tasks.Continuation; +import com.google.android.gms.tasks.Task; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReference; + +public final class TaskManager { + private static final String TAG = TaskManager.class.getSimpleName(); + private static Map> _taskMap = new HashMap<>(); + private static Map _exceptionsMap = new HashMap<>(); + private static int _nextTaskId = 0; + private static int _nextExceptionId = 0; + + private static native void onTaskCanceledNative(int taskId, int listenerId); + private static native void onTaskCompleteNative(int taskId, int listenerId, int nextTaskId); + private static native void onTaskFailureNative(int taskId, int listenerId, Object obj, int nextExceptionId); + private static native void onTaskSuccessNative(int taskId, int listenerId, Object obj); + private static native Object onContinueWithNative(int taskId, int listenerId, int nextTaskId); + + static int putTask(Task task) { + int taskId = ++_nextTaskId; + _taskMap.put(taskId, task); + return taskId; + } + + static void removeTask(int taskId) { + if(_taskMap.containsKey(taskId)) { + _taskMap.remove(taskId); + } + } + + static void removeTaskException(int exceptionId) { + if(_exceptionsMap.containsKey(exceptionId)) { + _exceptionsMap.remove(exceptionId); + } + } + + static void printExceptStackTrace(int exceptionId) { + if(_exceptionsMap.containsKey(exceptionId)) { + _exceptionsMap.get(exceptionId).printStackTrace(); + } + } + + static Task getTask(int taskId) { + if(_taskMap.containsKey(taskId)) { + return _taskMap.get(taskId); + } + return null; + } + static Object getResult(int taskId) { + if(_taskMap.containsKey(taskId)) { + return _taskMap.get(taskId).getResult(); + } + return null; + } + static boolean isCanceled(int taskId) { + if(_taskMap.containsKey(taskId)) { + return _taskMap.get(taskId).isCanceled(); + } + return false; + } + static boolean isSuccessful(int taskId) { + if(_taskMap.containsKey(taskId)) { + return _taskMap.get(taskId).isSuccessful(); + } + return false; + } + static boolean isComplete(int taskId) { + if(_taskMap.containsKey(taskId)) { + return _taskMap.get(taskId).isComplete(); + } + return false; + } + + static int addOnCanceledListener(int taskId, int listenerId) { + if(!_taskMap.containsKey(taskId)) { + Log.e(TAG, "Called task id does not exist"); + return -1; + } + Task newTask = _taskMap.get(taskId).addOnCanceledListener(()->{ + CocosHelper.runOnGameThread(new Runnable() { + @Override + public void run() { + onTaskCanceledNative(taskId, listenerId); + } + }); + }); + + return putTask(newTask); + } + + static int addOnCompleteListener(int taskId, int listenerId) { + if(!_taskMap.containsKey(taskId)) { + Log.e(TAG, "Called task id does not exist"); + return -1; + } + Task newTask = _taskMap.get(taskId).addOnCompleteListener((task)->{ + CocosHelper.runOnGameThread(new Runnable() { + @Override + public void run() { + int nextTaskId = putTask(task); + onTaskCompleteNative(taskId, listenerId, nextTaskId); + } + }); + }); + + return putTask(newTask); + } + static int addOnFailureListener(int taskId, int listenerId) { + if(!_taskMap.containsKey(taskId)) { + Log.e(TAG, "Called task id does not exist"); + return -1; + } + Task newTask = _taskMap.get(taskId).addOnFailureListener((e)->{ + CocosHelper.runOnGameThread(new Runnable() { + @Override + public void run() { + int nextExceptionId = ++_nextExceptionId; + _exceptionsMap.put(nextExceptionId, e); + onTaskFailureNative(taskId, listenerId, e, nextExceptionId); + } + }); + }); + return putTask(newTask); + } + static int addOnSuccessListener(int taskId, int listenerId) { + if(!_taskMap.containsKey(taskId)) { + Log.e(TAG, "Called task id does not exist"); + return -1; + } + Task newTask = _taskMap.get(taskId).addOnSuccessListener((result)->{ + CocosHelper.runOnGameThread(new Runnable() { + @Override + public void run() { + onTaskSuccessNative(taskId, listenerId, result); + } + }); + }); + return putTask(newTask); + } + + + private static Task continueWithTaskForAny( + Task task, final Continuation continuation) { + + return task.continueWith(new Continuation() { + @Override + public TContinuationResult then(Task task) throws Exception { + return continuation.then(task); + } + }); + } + static int continueWith(int taskId, int listenerId) { + if(!_taskMap.containsKey(taskId)) { + Log.e(TAG, "Called task id does not exist"); + return -1; + } + Task task = (Task)_taskMap.get(taskId); + Task newTask = task.continueWith(new Continuation() { + @Override + public Object then(Task task) throws Exception { + final CountDownLatch latch = new CountDownLatch(1); + final AtomicReference resultHolder = new AtomicReference<>(); + if (task.isSuccessful()) { + CocosHelper.runOnGameThread(new Runnable() { + @Override + public void run() { + int calllbackTaskId = putTask(task); + resultHolder.set(onContinueWithNative(taskId, listenerId, calllbackTaskId)); + latch.countDown(); + } + }); + latch.await(); + } else { + resultHolder.set(null); + } + return resultHolder.get(); + } + }); + return putTask(newTask); + } +} diff --git a/native/cocos/platform/android/libcocos2dx/AndroidManifest.xml b/native/cocos/platform/android/libcocos2dx/AndroidManifest.xml index 5364438c30..a29308bb22 100644 --- a/native/cocos/platform/android/libcocos2dx/AndroidManifest.xml +++ b/native/cocos/platform/android/libcocos2dx/AndroidManifest.xml @@ -1,4 +1,8 @@ + + + diff --git a/native/cocos/platform/android/libcocos2dx/build.gradle b/native/cocos/platform/android/libcocos2dx/build.gradle index 801cff10d3..3f96b4cf9b 100644 --- a/native/cocos/platform/android/libcocos2dx/build.gradle +++ b/native/cocos/platform/android/libcocos2dx/build.gradle @@ -22,8 +22,13 @@ android { } if(project.hasProperty("PROP_ENABLE_GOOGLE_BILLING") && project.PROP_ENABLE_GOOGLE_BILLING.toBoolean()) { - sourceSets.main.java.srcDirs += ["../java/vendor"] + sourceSets.main.java.srcDirs += ["../java/vendor/billing"] } + + if(project.hasProperty("PROP_ENABLE_GOOGLE_PLAY_GAMES") && project.PROP_ENABLE_GOOGLE_PLAY_GAMES.toBoolean()) { + sourceSets.main.java.srcDirs += ["../java/vendor/play"] + } + buildDir = new File(rootProject.buildDir, project.name) buildTypes { @@ -43,4 +48,7 @@ dependencies { if(project.hasProperty("PROP_ENABLE_GOOGLE_BILLING") && project.PROP_ENABLE_GOOGLE_BILLING.toBoolean()) { implementation "com.android.billingclient:billing:7.1.0" } + if(project.hasProperty("PROP_ENABLE_GOOGLE_PLAY_GAMES") && project.PROP_ENABLE_GOOGLE_PLAY_GAMES.toBoolean()) { + implementation "com.google.android.gms:play-services-games-v2:+" + } } diff --git a/native/cocos/platform/android/libcocos2dx/src/main/res/values-zh-rCN/strings.xml b/native/cocos/platform/android/libcocos2dx/src/main/res/values-zh-rCN/strings.xml index 0c0c2641ee..c6b13e1650 100644 --- a/native/cocos/platform/android/libcocos2dx/src/main/res/values-zh-rCN/strings.xml +++ b/native/cocos/platform/android/libcocos2dx/src/main/res/values-zh-rCN/strings.xml @@ -7,4 +7,5 @@ 前往 发送 请到系统设置关闭安全键盘 + diff --git a/native/cocos/platform/android/libcocos2dx/src/main/res/values/strings.xml b/native/cocos/platform/android/libcocos2dx/src/main/res/values/strings.xml index 97d607395f..81ac6d48b9 100644 --- a/native/cocos/platform/android/libcocos2dx/src/main/res/values/strings.xml +++ b/native/cocos/platform/android/libcocos2dx/src/main/res/values/strings.xml @@ -7,4 +7,5 @@ Go Send Please go to the system settings to turn off the security keyboard! + diff --git a/native/cocos/platform/java/jni/JniGooglePlayHandler.cpp b/native/cocos/platform/java/jni/JniGooglePlayHandler.cpp new file mode 100644 index 0000000000..a959b2a850 --- /dev/null +++ b/native/cocos/platform/java/jni/JniGooglePlayHandler.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include +#include "platform/java/jni/JniHelper.h" +#include "vendor/google/play/PlayTaskManager.h" +extern "C" { + +// NOLINTNEXTLINE +JNIEXPORT void JNICALL Java_google_play_TaskManager_onTaskCanceledNative(JNIEnv *env, jclass clazz, jint taskId, jint listenerId) { + cc::PlayTaskManager::getInstance()->onTaskCanceled(taskId, listenerId); +} + +JNIEXPORT void JNICALL Java_google_play_TaskManager_onTaskCompleteNative(JNIEnv *env, jclass clazz, jint taskId, jint listenerId, jint nextTaskId) { + cc::PlayTaskManager::getInstance()->onTaskComplete(taskId, listenerId, nextTaskId); +} + +JNIEXPORT void JNICALL Java_google_play_TaskManager_onTaskFailureNative(JNIEnv *env, jclass clazz, jint taskId, jint listenerId, jobject obj, jint exceptionId) { + cc::PlayTaskManager::getInstance()->onTaskFailure(taskId, listenerId, reinterpret_cast(obj), exceptionId); +} + +JNIEXPORT void JNICALL Java_google_play_TaskManager_onTaskSuccessNative(JNIEnv *env, jclass clazz, jint taskId, jint listenerId, jobject obj) { + cc::PlayTaskManager::getInstance()->onTaskSucess(taskId, listenerId, reinterpret_cast(obj)); +} + +JNIEXPORT jobject JNICALL Java_google_play_TaskManager_onContinueWithNative(JNIEnv *env, jclass clazz, jint taskId, jint listenerId, jint nextTaskId) { + return reinterpret_cast(cc::PlayTaskManager::getInstance()->onContinueWith(taskId, listenerId, nextTaskId)); +} +} diff --git a/native/tools/swig-config/android/google_billing.i b/native/tools/swig-config/android/google/billing/google_billing.i similarity index 100% rename from native/tools/swig-config/android/google_billing.i rename to native/tools/swig-config/android/google/billing/google_billing.i diff --git a/native/tools/swig-config/android/google/play/google_play.i b/native/tools/swig-config/android/google/play/google_play.i new file mode 100644 index 0000000000..b7428d9864 --- /dev/null +++ b/native/tools/swig-config/android/google/play/google_play.i @@ -0,0 +1,43 @@ +// Define module +// target_namespace means the name exported to JS, could be same as which in other modules +// audio at the last means the suffix of binding function name, different modules should use unique name +// Note: doesn't support number prefix +%module(target_namespace="jsb") play + +// Disable some swig warnings, find warning number reference here ( https://www.swig.org/Doc4.1/Warnings.html ) +#pragma SWIG nowarn=503,302,401,317,402 + +// Insert code at the beginning of generated header file (.h) +%insert(header_file) %{ +#pragma once +#include "bindings/jswrapper/SeApi.h" +#include "bindings/manual/jsb_conversions.h" +#include "vendor/google/play/PlayTask.h" +#include "vendor/google/play/TResult.h" +#include "vendor/google/play/PlayGames.h" +%} + +// Insert code at the beginning of generated source file (.cpp) +%{ +#include "bindings/auto/jsb_google_play_auto.h" +#include "vendor/google/play/PlayTask.h" +#include "vendor/google/play/TResult.h" +%} + +// ----- Import Section ------ +// Brief: Import header files which are depended by 'Include Section' +// Note: +// %import "your_header_file.h" will not generate code for that header file +// +%import "base/Macros.h" + +// ----- Include Section ------ +// Brief: Include header files in which classes and methods will be bound +%include "vendor/google/play/TResult.h" +%include "vendor/google/play/PlayTask.h" +%include "vendor/google/play/GamesSignInClient.h" +%include "vendor/google/play/RecallClient.h" +%include "vendor/google/play/AchievementsClient.h" +%include "vendor/google/play/PlayGames.h" + + diff --git a/native/vendor/google/billing/GoogleBilling.cpp b/native/vendor/google/billing/GoogleBilling.cpp index b945f0402a..71b12d5d29 100644 --- a/native/vendor/google/billing/GoogleBilling.cpp +++ b/native/vendor/google/billing/GoogleBilling.cpp @@ -39,31 +39,6 @@ #include "vendor/google/billing/result-values/BillingResult.h" namespace cc { -BillingClient::scopedListener::scopedListener(se::Object* obj) : _obj(obj) { - if (_obj) { - _obj->root(); - _obj->incRef(); - } -} - -BillingClient::scopedListener::~scopedListener() { - if (_obj) { - _obj->unroot(); - _obj->decRef(); - } -} - -void BillingClient::scopedListener::reset(se::Object* obj) { - if (_obj) { - _obj->unroot(); - _obj->decRef(); - } - _obj = obj; - if (_obj) { - _obj->root(); - _obj->incRef(); - } -} BillingClient::Builder& BillingClient::Builder::enableUserChoiceBilling(se::Object* listener) { if (!listener) { diff --git a/native/vendor/google/billing/GoogleBilling.h b/native/vendor/google/billing/GoogleBilling.h index 00613f7c43..9735f9af66 100644 --- a/native/vendor/google/billing/GoogleBilling.h +++ b/native/vendor/google/billing/GoogleBilling.h @@ -28,6 +28,7 @@ #include #include "base/Macros.h" #include "base/RefCounted.h" +#include "vendor/google/common/ScopedListener.h" namespace se { class Object; @@ -46,22 +47,6 @@ class BillingResult; class CC_DLL BillingClient : public cc::RefCounted { private: - class scopedListener { - public: - scopedListener() = default; - scopedListener(se::Object* obj); - ~scopedListener(); - se::Object* get() const { - return _obj; - } - void reset(se::Object* obj); - operator bool() const { - return _obj != nullptr; - } - - private: - se::Object* _obj{nullptr}; - }; public: class Builder { diff --git a/native/vendor/google/billing/GoogleBillingHelper.cpp b/native/vendor/google/billing/GoogleBillingHelper.cpp index 825f7c164a..ec286ff367 100644 --- a/native/vendor/google/billing/GoogleBillingHelper.cpp +++ b/native/vendor/google/billing/GoogleBillingHelper.cpp @@ -40,6 +40,8 @@ #include "vendor/google/billing/build-params/ConsumeParams.h" #include "vendor/google/billing/result-values/ProductDetails.h" #include "vendor/google/billing/result-values/Purchase.h" +#include "vendor/google/common/JsUtils.h" + namespace { #ifndef JCLS_BILLING @@ -48,49 +50,14 @@ namespace { }; // namespace namespace cc { -namespace { -void invokeJsCallback(se::Object* obj, const char* jsFunctionName, const se::ValueArray& args) { - if (!obj) { - return; - } - - se::Value funcVal; - if (!obj->getProperty(jsFunctionName, &funcVal) || !funcVal.isObject()) { - return; - } - se::Value rval; - funcVal.toObject()->call(args, obj, &rval); -} - -template -void callJSfunc(se::Object* obj, const char* jsFunctionName, Args&&... inargs) { // NOLINT(readability-identifier-naming) - if (!se::ScriptEngine::getInstance()->isValid()) { - return; - } - se::AutoHandleScope scope; - se::ValueArray args; - args.resize(sizeof...(Args)); - nativevalue_to_se_args_v(args, inargs...); - invokeJsCallback(obj, jsFunctionName, args); -} - -void callJSfunc(se::Object* obj, const char* jsFunctionName) { // NOLINT(readability-identifier-naming) - if (!se::ScriptEngine::getInstance()->isValid()) { - return; - } - se::AutoHandleScope scope; - invokeJsCallback(obj, jsFunctionName, se::ValueArray()); -} - -template void callJSfunc(se::Object* obj, const char*, BillingResult*&&); -template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, const std::vector&); -template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, const std::vector&); -template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, const std::string&); -template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, BillingConfig*&&); -template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, AlternativeBillingOnlyReportingDetails*&&); -template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, ExternalOfferReportingDetails*&&); -template void callJSfunc(se::Object* obj, const char*, InAppMessageResult*&&); -} // namespace +template se::Value callJSfunc(se::Object* obj, const char*, BillingResult*&&); +template se::Value callJSfunc(se::Object* obj, const char*, BillingResult*&&, const std::vector&); +template se::Value callJSfunc(se::Object* obj, const char*, BillingResult*&&, const std::vector&); +template se::Value callJSfunc(se::Object* obj, const char*, BillingResult*&&, const std::string&); +template se::Value callJSfunc(se::Object* obj, const char*, BillingResult*&&, BillingConfig*&&); +template se::Value callJSfunc(se::Object* obj, const char*, BillingResult*&&, AlternativeBillingOnlyReportingDetails*&&); +template se::Value callJSfunc(se::Object* obj, const char*, BillingResult*&&, ExternalOfferReportingDetails*&&); +template se::Value callJSfunc(se::Object* obj, const char*, InAppMessageResult*&&); int GoogleBillingHelper::createGoogleBilling(BillingClient::Builder* builder) { int tag = JniHelper::callStaticIntMethod(JCLS_BILLING, "newTag"); diff --git a/native/vendor/google/billing/JniBilling.cpp b/native/vendor/google/billing/JniBilling.cpp index f902bcdcf3..0695f94526 100644 --- a/native/vendor/google/billing/JniBilling.cpp +++ b/native/vendor/google/billing/JniBilling.cpp @@ -35,46 +35,14 @@ #include "cocos/bindings/manual/jsb_global_init.h" #include "vendor/google/billing/GoogleBilling.h" #include "vendor/google/billing/build-params/PendingPurchasesParams.h" - +#include "vendor/google/common/JniUtils.h" namespace { - #ifndef JCLS_BILLING #define JCLS_BILLING "google/billing/GoogleBillingHelper" #endif }; // namespace namespace cc { -namespace { -std::string callStringMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { - jmethodID methodId = env->GetMethodID(clazz, methodName, "()Ljava/lang/String;"); - jobject jStringObj = env->CallObjectMethod(obj, methodId); - if (jStringObj != nullptr) { - return cc::StringUtils::getStringUTFCharsJNI(env, static_cast(jStringObj)); - } - return ""; -} -jobject callObjectMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName, const char* returnType) { - std::string returnSign = cc::StringUtils::format("()L%s", returnType); - jmethodID methodId = env->GetMethodID(clazz, methodName, returnSign.c_str()); - return env->CallObjectMethod(obj, methodId); -} - -int callIntMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { - jmethodID methodId = env->GetMethodID(clazz, methodName, "()I"); - return env->CallIntMethod(obj, methodId); -} - -bool callBooleanMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { - jmethodID methodId = env->GetMethodID(clazz, methodName, "()Z"); - return env->CallBooleanMethod(obj, methodId); -} - -long callLongMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { - jmethodID methodId = env->GetMethodID(clazz, methodName, "()J"); - return env->CallLongMethod(obj, methodId); -} - -} // namespace cc::BillingResult* JniBilling::toBillingResult(JNIEnv* env, jobject obj) { jclass clazz = env->GetObjectClass(obj); diff --git a/native/vendor/google/common/JniUtils.cpp b/native/vendor/google/common/JniUtils.cpp new file mode 100644 index 0000000000..076bab26f9 --- /dev/null +++ b/native/vendor/google/common/JniUtils.cpp @@ -0,0 +1,112 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "vendor/google/common/JniUtils.h" +#include "base/UTF8.h" + +namespace cc { +std::string callStringMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { + jmethodID methodId = env->GetMethodID(clazz, methodName, "()Ljava/lang/String;"); + jobject jStringObj = env->CallObjectMethod(obj, methodId); + if (jStringObj != nullptr) { + return cc::StringUtils::getStringUTFCharsJNI(env, static_cast(jStringObj)); + } + return ""; +} +jobject callObjectMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName, const char* returnType) { + std::string returnSign = cc::StringUtils::format("()L%s", returnType); + jmethodID methodId = env->GetMethodID(clazz, methodName, returnSign.c_str()); + return env->CallObjectMethod(obj, methodId); +} + +int callIntMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { + jmethodID methodId = env->GetMethodID(clazz, methodName, "()I"); + return env->CallIntMethod(obj, methodId); +} + +bool callBooleanMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { + jmethodID methodId = env->GetMethodID(clazz, methodName, "()Z"); + return env->CallBooleanMethod(obj, methodId); +} + +long callLongMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName) { + jmethodID methodId = env->GetMethodID(clazz, methodName, "()J"); + return env->CallLongMethod(obj, methodId); +} + +template +jobject convertToJObject(JNIEnv* env, T value, const char* className, const char* methodSig) { + jclass clazz = env->FindClass(className); + if (!clazz) { + return nullptr; + } + + jmethodID valueOfMethod = env->GetStaticMethodID(clazz, "valueOf", methodSig); + if (!valueOfMethod) { + return nullptr; + } + return env->CallStaticObjectMethod(clazz, valueOfMethod, value); +} + +jobject intToJObject(JNIEnv* env, int value) { + return convertToJObject(env, value, "java/lang/Integer", "(I)Ljava/lang/Integer;"); +} + +jobject doubleToJObject(JNIEnv* env, double value) { + return convertToJObject(env, value, "java/lang/Double", "(D)Ljava/lang/Double;"); +} + +jobject boolToJObject(JNIEnv* env, bool value) { + return convertToJObject(env, value, "java/lang/Boolean", "(Z)Ljava/lang/Boolean;"); +} + +jstring stringToJString(JNIEnv* env, const std::string& str) { + return env->NewStringUTF(str.c_str()); +} + +int integerObjectToInt(JNIEnv* env, jclass clazz, jobject obj) { + jmethodID valueMethod = env->GetMethodID(clazz, "intValue", "()I"); + if (!valueMethod) { + return 0; + } + return env->CallIntMethod(obj, valueMethod); +} + +double doubleObjectToDouble(JNIEnv* env, jclass clazz, jobject obj) { + jmethodID valueMethod = env->GetMethodID(clazz, "doubleValue", "()D"); + if (!valueMethod) { + return 0.0f; + } + return env->CallDoubleMethod(obj, valueMethod); +} + +bool BooleanObjectToBool(JNIEnv* env, jclass clazz, jobject obj) { + jmethodID valueMethod = env->GetMethodID(clazz, "booleanValue", "()Z"); + if (!valueMethod) { + return false; + } + return env->CallBooleanMethod(obj, valueMethod); +} + +} // namespace cc diff --git a/native/vendor/google/common/JniUtils.h b/native/vendor/google/common/JniUtils.h new file mode 100644 index 0000000000..c14cacebc8 --- /dev/null +++ b/native/vendor/google/common/JniUtils.h @@ -0,0 +1,48 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "platform/java/jni/JniHelper.h" + +namespace cc { +std::string callStringMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName); +jobject callObjectMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName, const char* returnType); +int callIntMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName); +bool callBooleanMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName); +long callLongMethod(JNIEnv* env, jclass clazz, jobject obj, const char* methodName); + +// int -> Integer +jobject intToJObject(JNIEnv* env, int value); +// double -> Double +jobject doubleToJObject(JNIEnv* env, double value); +// bool -> Boolean +jobject boolToJObject(JNIEnv* env, bool value); +// std::string -> jstring +jstring stringToJString(JNIEnv* env, const std::string& str); + +int integerObjectToInt(JNIEnv* env, jclass clazz, jobject obj); +double doubleObjectToDouble(JNIEnv* env, jclass clazz, jobject obj); +bool BooleanObjectToBool(JNIEnv* env, jclass clazz, jobject obj); +} \ No newline at end of file diff --git a/native/vendor/google/common/JsUtils.cpp b/native/vendor/google/common/JsUtils.cpp new file mode 100644 index 0000000000..788cb2143f --- /dev/null +++ b/native/vendor/google/common/JsUtils.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "vendor/google/common/JsUtils.h" + +namespace cc { +se::Value invokeJsCallback(se::Object* obj, const char* jsFunctionName, const se::ValueArray& args) { + if (!obj) { + return se::Value(); + } + + se::Value funcVal; + if (!obj->getProperty(jsFunctionName, &funcVal) || !funcVal.isObject()) { + return se::Value(); + } + se::Value rval; + funcVal.toObject()->call(args, obj, &rval); + return rval; +} + +template +se::Value callJSfunc(se::Object* obj, const char* jsFunctionName, Args&&... inargs); + +se::Value callJSfunc(se::Object* obj, const char* jsFunctionName) { // NOLINT(readability-identifier-naming) + if (!se::ScriptEngine::getInstance()->isValid()) { + return se::Value(); + } + se::AutoHandleScope scope; + return invokeJsCallback(obj, jsFunctionName, se::ValueArray()); +} +} \ No newline at end of file diff --git a/native/vendor/google/common/JsUtils.h b/native/vendor/google/common/JsUtils.h new file mode 100644 index 0000000000..95d9d78cc4 --- /dev/null +++ b/native/vendor/google/common/JsUtils.h @@ -0,0 +1,47 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "bindings/jswrapper/SeApi.h" + +namespace cc { + +se::Value invokeJsCallback(se::Object* obj, const char* jsFunctionName, const se::ValueArray& args); + +template +se::Value callJSfunc(se::Object* obj, const char* jsFunctionName, Args&&... inargs){ // NOLINT(readability-identifier-naming) + if (!se::ScriptEngine::getInstance()->isValid()) { + return se::Value(); + } + se::AutoHandleScope scope; + se::ValueArray args; + args.resize(sizeof...(Args)); + nativevalue_to_se_args_v(args, inargs...); + return invokeJsCallback(obj, jsFunctionName, args); +} + +se::Value callJSfunc(se::Object* obj, const char* jsFunctionName); + +} \ No newline at end of file diff --git a/native/vendor/google/common/ScopedListener.cpp b/native/vendor/google/common/ScopedListener.cpp new file mode 100644 index 0000000000..a8ac36b341 --- /dev/null +++ b/native/vendor/google/common/ScopedListener.cpp @@ -0,0 +1,31 @@ + +#include "vendor/google/common/ScopedListener.h" +#include "cocos/bindings/jswrapper/SeApi.h" + +namespace cc { +scopedListener::scopedListener(se::Object* obj) : _obj(obj) { + if (_obj) { + _obj->root(); + _obj->incRef(); + } +} + +scopedListener::~scopedListener() { + if (_obj) { + _obj->unroot(); + _obj->decRef(); + } +} + +void scopedListener::reset(se::Object* obj) { + if (_obj) { + _obj->unroot(); + _obj->decRef(); + } + _obj = obj; + if (_obj) { + _obj->root(); + _obj->incRef(); + } +} +} diff --git a/native/vendor/google/common/ScopedListener.h b/native/vendor/google/common/ScopedListener.h new file mode 100644 index 0000000000..ff09a591bb --- /dev/null +++ b/native/vendor/google/common/ScopedListener.h @@ -0,0 +1,48 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +namespace se { +class Object; +} + +namespace cc { +class scopedListener { +public: + scopedListener() = default; + scopedListener(se::Object* obj); + ~scopedListener(); + se::Object* get() const { + return _obj; + } + void reset(se::Object* obj); + operator bool() const { + return _obj != nullptr; + } + +private: + se::Object* _obj{nullptr}; +}; +} \ No newline at end of file diff --git a/native/vendor/google/play/AchievementsClient.cpp b/native/vendor/google/play/AchievementsClient.cpp new file mode 100644 index 0000000000..ae03a42b54 --- /dev/null +++ b/native/vendor/google/play/AchievementsClient.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "cocos/platform/java/jni/JniHelper.h" +#include "vendor/google/play/PlayTaskManager.h" +#include "vendor/google/play/AchievementsClient.h" + +namespace cc { + +namespace { +#ifndef JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT + #define JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT "google/play/AchievementsClientHelper" +#endif +}; // namespace + +void AchievementsClient::showAchievements() { + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "showAchievements"); +} + +PlayTask* AchievementsClient::load(bool forceReload) { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "load", forceReload); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* AchievementsClient::incrementImmediate(const std::string& id, int numSteps) { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "incrementImmediate", id, numSteps); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* AchievementsClient::revealImmediate(const std::string& id) { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "revealImmediate", id); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* AchievementsClient::setStepsImmediate(const std::string& id, int numSteps) { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "setStepsImmediate", id, numSteps); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* AchievementsClient::unlockImmediate(const std::string& id) { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "unlockImmediate", id); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +void AchievementsClient::increment(const std::string& id, int numSteps) { + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "increment", id, numSteps); +} + +void AchievementsClient::reveal(const std::string& id) { + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "reveal", id); +} + +void AchievementsClient::setSteps(const std::string& id, int numSteps) { + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "setSteps", id, numSteps); +} + +void AchievementsClient::unlock(const std::string& id) { + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_ACHIEVEMENTS_CLIENT, "unlock", id); +} +} // namespace cc \ No newline at end of file diff --git a/native/vendor/google/play/AchievementsClient.h b/native/vendor/google/play/AchievementsClient.h new file mode 100644 index 0000000000..a80537ebfb --- /dev/null +++ b/native/vendor/google/play/AchievementsClient.h @@ -0,0 +1,47 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include +#include "base/RefCounted.h" + +namespace cc { +class PlayTask; +class AchievementsClient { +public: + void showAchievements(); + PlayTask* load(bool forceReload); + PlayTask* incrementImmediate(const std::string& id, int numSteps); + PlayTask* revealImmediate(const std::string& id); + PlayTask* setStepsImmediate(const std::string& id, int numSteps); + PlayTask* unlockImmediate(const std::string& id); + + void increment(const std::string& id, int numSteps); + void reveal(const std::string& id); + void setSteps(const std::string& id, int numSteps); + void unlock(const std::string& id); +}; + +} // namespace cc diff --git a/native/vendor/google/play/GamesSignInClient.cpp b/native/vendor/google/play/GamesSignInClient.cpp new file mode 100644 index 0000000000..56c4f84066 --- /dev/null +++ b/native/vendor/google/play/GamesSignInClient.cpp @@ -0,0 +1,52 @@ + +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "cocos/platform/java/jni/JniHelper.h" +#include "vendor/google/play/PlayTaskManager.h" +#include "vendor/google/play/GamesSignInClient.h" + +namespace cc { + +namespace { +#ifndef JCLS_GOOGLE_PLAY_GAMES_SIGNIN_CLIENT + #define JCLS_GOOGLE_PLAY_GAMES_SIGNIN_CLIENT "google/play/GamesSignInClientHelper" +#endif +}; // namespace + +PlayTask* GamesSignInClient::isAuthenticated() { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_GAMES_SIGNIN_CLIENT, "isAuthenticated"); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* GamesSignInClient::requestServerSideAccess(const std::string& serverClientId, bool forceRefreshToken) { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_GAMES_SIGNIN_CLIENT, "requestServerSideAccess", serverClientId, forceRefreshToken); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* GamesSignInClient::signIn() { + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_GAMES_SIGNIN_CLIENT, "signIn"); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +} // namespace cc \ No newline at end of file diff --git a/native/vendor/google/play/GamesSignInClient.h b/native/vendor/google/play/GamesSignInClient.h new file mode 100644 index 0000000000..facdecc429 --- /dev/null +++ b/native/vendor/google/play/GamesSignInClient.h @@ -0,0 +1,38 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include +#include "base/RefCounted.h" + +namespace cc { +class PlayTask; +class GamesSignInClient { +public: + PlayTask* isAuthenticated(); + PlayTask* requestServerSideAccess(const std::string& serverClientId, bool forceRefreshToken); + PlayTask* signIn(); +}; +} // namespace cc diff --git a/native/vendor/google/play/PlayGames.cpp b/native/vendor/google/play/PlayGames.cpp new file mode 100644 index 0000000000..00792c9c2a --- /dev/null +++ b/native/vendor/google/play/PlayGames.cpp @@ -0,0 +1,38 @@ + +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "vendor/google/play/PlayGames.h" +#include "platform/java/jni/JniHelper.h" + +namespace cc { +GamesSignInClient PlayGames::_signinClient; +AchievementsClient PlayGames::_achievementsClient; +RecallClient PlayGames::_recallClient; + + +void PlayGamesSdk::initialize() { + JniHelper::callStaticVoidMethod("google/play/PlayGamesSdkHelper", "initialize"); +} + +} // namespace cc \ No newline at end of file diff --git a/native/vendor/google/play/PlayGames.h b/native/vendor/google/play/PlayGames.h new file mode 100644 index 0000000000..be770a7ae0 --- /dev/null +++ b/native/vendor/google/play/PlayGames.h @@ -0,0 +1,59 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include +#include "base/RefCounted.h" +#include "vendor/google/play/AchievementsClient.h" +#include "vendor/google/play/GamesSignInClient.h" +#include "vendor/google/play/RecallClient.h" + +namespace cc { +class PlayGamesSdk { +public: + static void initialize(); +}; + +class PlayGames { +public: + static GamesSignInClient& getGamesSignInClient() { + return _signinClient; + } + static AchievementsClient& getAchievementsClient() { + return _achievementsClient; + } + static RecallClient& getRecallClient() { + return _recallClient; + } + +private: + static GamesSignInClient _signinClient; + static AchievementsClient _achievementsClient; + static RecallClient _recallClient; +}; + + + +} // namespace cc diff --git a/native/vendor/google/play/PlayTask.cpp b/native/vendor/google/play/PlayTask.cpp new file mode 100644 index 0000000000..b5ffe270c3 --- /dev/null +++ b/native/vendor/google/play/PlayTask.cpp @@ -0,0 +1,235 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "vendor/google/play/PlayTask.h" +#include "base/UTF8.h" +#include "bindings/jswrapper/SeApi.h" +#include "cocos/bindings/jswrapper/SeApi.h" +#include "cocos/bindings/jswrapper/Value.h" +#include "cocos/bindings/manual/jsb_conversions.h" + +#include "vendor/google/play/PlayTaskManager.h" +#include "vendor/google/play/TResult.h" +#include "vendor/google/common/JsUtils.h" +#include "vendor/google/common/JniUtils.h" + +namespace cc { + +template se::Value callJSfunc(se::Object* obj, const char*, cc::AuthenticationResult*&&); +template se::Value callJSfunc(se::Object* obj, const char*, cc::PlayTask*&&); +namespace { +#ifndef JCLS_GOOGLE_PLAY_TASK_MANAGER + #define JCLS_GOOGLE_PLAY_TASK_MANAGER "google/play/TaskManager" +#endif +} // namespace + +TaskException::~TaskException() { + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "removeTaskException", _exceptionId); +} + +void TaskException::printStackTrace() { + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "printExceptStackTrace", _exceptionId); +} + +int PlayTask::_nextListnerId = 0; + +PlayTask::PlayTask(int taskId) : _taskId(taskId) { +} + +PlayTask::~PlayTask() { + PlayTaskManager::getInstance()->removeTask(_taskId); + JniHelper::callStaticVoidMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "removeTask", _taskId); +} + +int PlayTask::getNextListenerId() { + return _nextListnerId++; +} + +PlayTask* PlayTask::addOnCanceledListener(se::Object* listener) { + int listenerId = addListener(listener); + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "addOnCanceledListener", _taskId, listenerId); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* PlayTask::addOnCompleteListener(se::Object* listener) { + int listenerId = addListener(listener); + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "addOnCompleteListener", _taskId, listenerId); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* PlayTask::addOnFailureListener(se::Object* listener) { + int listenerId = addListener(listener); + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "addOnFailureListener", _taskId, listenerId); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* PlayTask::addOnSuccessListener(se::Object* listener) { + int listenerId = addListener(listener); + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "addOnSuccessListener", _taskId, listenerId); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +PlayTask* PlayTask::continueWith(se::Object* listener) { + int listenerId = addListener(listener); + int taskId = JniHelper::callStaticIntMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "continueWith", _taskId, listenerId); + return PlayTaskManager::getInstance()->addTask(taskId); +} + + +void PlayTask::getResult(se::Object* listener) { + auto* env = JniHelper::getEnv(); + cc::JniMethodInfo t; + if (cc::JniHelper::getStaticMethodInfo(t, JCLS_GOOGLE_PLAY_TASK_MANAGER, "getResult", "(I)Ljava/lang/Object;")) { + jobject obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, _taskId); + callJSfuncWithJObject(listener, "onSuccess", reinterpret_cast(obj)); + } + return; +} + +bool PlayTask::isCanceled() { + return JniHelper::callStaticBooleanMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "isCanceled", _taskId); +} + +bool PlayTask::isComplete() { + return JniHelper::callStaticBooleanMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "isComplete", _taskId); +} + +bool PlayTask::isSuccessful() { + return JniHelper::callStaticBooleanMethod(JCLS_GOOGLE_PLAY_TASK_MANAGER, "isSuccessful", _taskId); +} + +int PlayTask::addListener(se::Object* listener) { + int listenerId = getNextListenerId(); + _listeners[listenerId].reset(listener); + return listenerId; +} + +void PlayTask::onTaskCanceled(int listerId) { + auto it = _listeners.find(listerId); + if (it != _listeners.end()) { + cc::callJSfunc(it->second.get(), "onCanceled"); + _listeners.erase(it); + } +} + +void PlayTask::onTaskComplete(int listerId, int nextTaskId) { + auto it = _listeners.find(listerId); + if (it != _listeners.end()) { + PlayTask* newTask = PlayTaskManager::getInstance()->addTask(nextTaskId); + cc::callJSfunc(it->second.get(), "onComplete", newTask); + _listeners.erase(it); + } +} + +void PlayTask::onTaskFailure(int listerId, void* obj, int exceptionId) { + auto it = _listeners.find(listerId); + if (it != _listeners.end()) { + auto* env = JniHelper::getEnv(); + jobject jobj = reinterpret_cast(obj); + jclass objClass = env->GetObjectClass(jobj); + jmethodID getNameMethod = env->GetMethodID(env->FindClass("java/lang/Class"), "getName", "()Ljava/lang/String;"); + jstring className = (jstring)env->CallObjectMethod(objClass, getNameMethod); + std::string name = cc::StringUtils::getStringUTFCharsJNI(env, className); + if(name == "java.lang.Exception") { + auto* taskException = new TaskException; + taskException->_detailMessage = callStringMethod(env, objClass, jobj, "getMessage"); + taskException->_toString = callStringMethod(env, objClass, jobj, "toString"); + cc::callJSfunc(it->second.get(), "onFailure", taskException); + } + _listeners.erase(it); + } +} + +void PlayTask::onTaskSuccess(int listerId, void* obj) { + auto it = _listeners.find(listerId); + if (it != _listeners.end()) { + callJSfuncWithJObject(it->second.get(), "onSuccess", obj); + _listeners.erase(it); + } +} + +void* PlayTask::onTaskContinueWith(int listerId, int nextTaskId) { + void * ptr = nullptr; + auto it = _listeners.find(listerId); + if (it != _listeners.end()) { + se::Value r = callJSfunc(it->second.get(), "then"); + + if(r.isNumber()) { + ptr = reinterpret_cast(intToJObject(JniHelper::getEnv(), r.toInt32())); + } else if(r.isBoolean()) { + ptr = reinterpret_cast(boolToJObject(JniHelper::getEnv(), r.toBoolean())); + } else if(r.isString()) { + ptr = reinterpret_cast(stringToJString(JniHelper::getEnv(), r.toString())); + } else if(r.isObject()) { + // Currently, there is no need to parse objects, and the implemented functional objects (such as AuthenticationResult) do not provide constructors. + // If needed in the future, they can be parsed as follows : + // se::Object* obj = r.toObject(); + // std::string name = obj->_getClass()->getName(); + // if(name == "AuthenticationResult") { + // auto* result = reinterpret_cast(obj->getPrivateData()); + // } + } + _listeners.erase(it); + } + return ptr; +} + +void PlayTask::callJSfuncWithJObject(se::Object* listener, const char* functionName, void* obj) { + jobject jobj = reinterpret_cast(obj); + if(jobj != nullptr) { + auto* env = JniHelper::getEnv(); + jclass objClass = env->GetObjectClass(jobj); + jmethodID getNameMethod = env->GetMethodID(env->FindClass("java/lang/Class"), "getName", "()Ljava/lang/String;"); + jstring className = (jstring)env->CallObjectMethod(objClass, getNameMethod); + std::string name = cc::StringUtils::getStringUTFCharsJNI(env, className); + if (name == "com.google.android.gms.games.AuthenticationResult") { + auto* authenticationResult = new AuthenticationResult; + authenticationResult->_isAuthenticated = callBooleanMethod(env, objClass, jobj, "isAuthenticated"); + cc::callJSfunc(listener, functionName, authenticationResult); + } else if (name == "com.google.android.gms.games.RecallAccess") { + auto* recallAccess = new RecallAccess; + recallAccess->_hashCode = callIntMethod(env, objClass, jobj, "hashCode"); + recallAccess->_sessionId = callStringMethod(env, objClass, jobj, "getSessionId"); + cc::callJSfunc(listener, functionName, recallAccess); + } else if(name == "java.lang.Integer") { + int value = integerObjectToInt(env, objClass, jobj); + cc::callJSfunc(listener, functionName, value); + } else if(name == "java.lang.Double") { + double value = doubleObjectToDouble(env, objClass, jobj); + cc::callJSfunc(listener, functionName, value); + } else if(name == "java.lang.Boolean") { + bool value = BooleanObjectToBool(env, objClass, jobj); + cc::callJSfunc(listener, functionName, value); + } else if(name == "java.lang.String") { + std::string value = cc::StringUtils::getStringUTFCharsJNI(env, static_cast(jobj)); + cc::callJSfunc(listener, functionName, value); + } else { + CC_LOG_WARNING("Unsupported types"); + cc::callJSfunc(listener, functionName); + } + } else { + cc::callJSfunc(listener, functionName); + } +} + +} // namespace cc diff --git a/native/vendor/google/play/PlayTask.h b/native/vendor/google/play/PlayTask.h new file mode 100644 index 0000000000..25b2087f53 --- /dev/null +++ b/native/vendor/google/play/PlayTask.h @@ -0,0 +1,89 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include +#include "base/RefCounted.h" +#include "vendor/google/common/ScopedListener.h" + +namespace se { +class Object; +} +namespace cc { +class TaskException : public RefCounted { +public: + ~TaskException(); + const std::string& getMessage() const { + return _detailMessage; + } + const std::string& toString() const { + return _toString; + } + const std::string& getLocalizedMessage() const { + return getMessage(); + } + void printStackTrace(); +private: + friend class PlayTask; + int _exceptionId{0}; + std::string _detailMessage; + std::string _toString; +}; + +class PlayTask : public RefCounted { +public: + PlayTask(int taskId); + ~PlayTask(); + PlayTask* addOnCanceledListener(se::Object* listener); + PlayTask* addOnCompleteListener(se::Object* listener); + PlayTask* addOnFailureListener(se::Object* listener); + PlayTask* addOnSuccessListener(se::Object* listener); + + PlayTask* continueWith(se::Object* listener); + void getResult(se::Object* listener); + bool isCanceled(); + bool isComplete(); + bool isSuccessful(); + +protected: + friend class PlayTaskManager; + void onTaskCanceled(int listerId); + void onTaskComplete(int listerId, int nextTaskId); + void onTaskFailure(int listerId, void* obj, int exceptionId); + void onTaskSuccess(int listerId, void* obj); + void* onTaskContinueWith(int listerId, int nextTaskId); + void callJSfuncWithJObject(se::Object* listener, const char* functionName, void* obj); + +private: + int getNextListenerId(); + int addListener(se::Object* listener); + +private: + static int _nextListnerId; + int _taskId{0}; + std::unordered_map _listeners; +}; + +} // namespace cc diff --git a/native/vendor/google/play/PlayTaskManager.cpp b/native/vendor/google/play/PlayTaskManager.cpp new file mode 100644 index 0000000000..f5bea9e4bd --- /dev/null +++ b/native/vendor/google/play/PlayTaskManager.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "vendor/google/play/PlayTaskManager.h" +#include "cocos/platform/java/jni/JniHelper.h" +#include "vendor/google/play/PlayTask.h" + +namespace cc { + +void PlayTaskManager::onTaskCanceled(int taskId, int listenerId) { + auto it = _tasks.find(taskId); + if (it != _tasks.end()) { + it->second->onTaskCanceled(listenerId); + } +} + +void PlayTaskManager::onTaskComplete(int taskId, int listenerId, int nextTaskId) { + auto it = _tasks.find(taskId); + if (it != _tasks.end()) { + it->second->onTaskComplete(listenerId, nextTaskId); + } +} + +void PlayTaskManager::onTaskFailure(int taskId, int listenerId, void* obj, int exceptionId) { + auto it = _tasks.find(taskId); + if (it != _tasks.end()) { + it->second->onTaskFailure(listenerId, obj, exceptionId); + } +} + +void PlayTaskManager::onTaskSucess(int taskId, int listenerId, void* obj) { + auto it = _tasks.find(taskId); + if (it != _tasks.end()) { + it->second->onTaskSuccess(listenerId, obj); + } +} + +PlayTask* PlayTaskManager::addTask(int taskId) { + auto it = _tasks.find(taskId); + if (it != _tasks.end()) { + CC_LOG_WARNING("The task already exists."); + return nullptr; + } + auto* task = new PlayTask(taskId); + _tasks.insert(std::make_pair(taskId, task)); + return task; +} + +void PlayTaskManager::removeTask(int taskId) { + auto it = _tasks.find(taskId); + if (it == _tasks.end()) { + CC_LOG_WARNING("The task does not exist."); + return; + } + _tasks.erase(it); +} + +void* PlayTaskManager::onContinueWith(int taskId, int listenerId, int nextTaskId) { + auto it = _tasks.find(taskId); + if (it != _tasks.end()) { + return it->second->onTaskContinueWith(listenerId, nextTaskId); + } + return nullptr; +} + +} // namespace cc diff --git a/native/vendor/google/play/PlayTaskManager.h b/native/vendor/google/play/PlayTaskManager.h new file mode 100644 index 0000000000..a858bc4c22 --- /dev/null +++ b/native/vendor/google/play/PlayTaskManager.h @@ -0,0 +1,61 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include +#include "base/RefCounted.h" + +namespace se { +class Object; +} +namespace cc { +class PlayTask; + +class PlayTaskManager { +public: + static PlayTaskManager* getInstance() { + static PlayTaskManager mgr; + return &mgr; + } + + void onTaskCanceled(int taskId, int listenerId); + void onTaskComplete(int taskId, int listenerId, int nextTaskId); + void onTaskFailure(int taskId, int listenerId, void* obj, int exceptionId); + void onTaskSucess(int taskId, int listenerId, void* obj); + void* onContinueWith(int taskId, int listenerId, int nextTaskId); + PlayTask* addTask(int taskId); + void removeTask(int taskId); +private: + int getNextListenerId(); + int addListener(se::Object* listener); + + PlayTaskManager(/* args */) = default; + ~PlayTaskManager() = default; + +private: + std::unordered_map _tasks; +}; + +} // namespace cc diff --git a/native/vendor/google/play/RecallClient.cpp b/native/vendor/google/play/RecallClient.cpp new file mode 100644 index 0000000000..45d3ea60bc --- /dev/null +++ b/native/vendor/google/play/RecallClient.cpp @@ -0,0 +1,36 @@ + +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "cocos/platform/java/jni/JniHelper.h" +#include "vendor/google/play/PlayTaskManager.h" +#include "vendor/google/play/RecallClient.h" + +namespace cc { + +PlayTask* RecallClient::requestRecallAccess() { + int taskId = JniHelper::callStaticIntMethod("google/play/RecallClientHelper", "requestRecallAccess"); + return PlayTaskManager::getInstance()->addTask(taskId); +} + +} // namespace cc diff --git a/native/vendor/google/play/RecallClient.h b/native/vendor/google/play/RecallClient.h new file mode 100644 index 0000000000..3ebf9421a0 --- /dev/null +++ b/native/vendor/google/play/RecallClient.h @@ -0,0 +1,36 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include +#include "base/RefCounted.h" + +namespace cc { +class PlayTask; +class RecallClient { +public: + PlayTask* requestRecallAccess(); +}; +} // namespace cc diff --git a/native/vendor/google/play/TResult.h b/native/vendor/google/play/TResult.h new file mode 100644 index 0000000000..b1e8d02dc3 --- /dev/null +++ b/native/vendor/google/play/TResult.h @@ -0,0 +1,63 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/RefCounted.h" + +namespace cc { +class AuthenticationResult : public RefCounted { +public: + bool isAuthenticated() const { + return _isAuthenticated; + } +private: + friend class PlayTask; + bool _isAuthenticated {false}; +}; + +class RecallAccess : public RefCounted { +public: + int hashCode() const { + return _hashCode; + } + + const std::string& getSessionId() const { + return _sessionId; + } + + bool equals(const RecallAccess& other) { + if(&other == this) { + return true; + } + return _sessionId == other._sessionId; + } +private: + friend class PlayTask; + int _hashCode{0}; + std::string _sessionId; +}; + + +} diff --git a/scripts/native-pack-tool/source/platforms/google-play.ts b/scripts/native-pack-tool/source/platforms/google-play.ts index 74659e8012..71a72b15c2 100644 --- a/scripts/native-pack-tool/source/platforms/google-play.ts +++ b/scripts/native-pack-tool/source/platforms/google-play.ts @@ -50,6 +50,7 @@ export interface IAndroidParams { appBundle: boolean; resizeableActivity: boolean; googleBilling: boolean; + playGames: boolean; customIconInfo: ICustomIconInfo, } @@ -404,7 +405,7 @@ export class GooglePlayPackTool extends NativePackTool { content = content.replace(/NATIVE_DIR=.*/, `NATIVE_DIR=${cchelper.fixPath(this.paths.platformTemplateDirInPrj)}`); content = content.replace(/PROP_ENABLE_GOOGLE_BILLING=.*/, `PROP_ENABLE_GOOGLE_BILLING=${options.googleBilling ? "true" : "false"}`); - + content = content.replace(/PROP_ENABLE_GOOGLE_PLAY_GAMES=.*/, `PROP_ENABLE_GOOGLE_PLAY_GAMES=${options.playGames ? "true" : "false"}`); if (process.platform === 'win32') { diff --git a/templates/google-play/build/gradle.properties b/templates/google-play/build/gradle.properties index c52b0a5084..8464027251 100644 --- a/templates/google-play/build/gradle.properties +++ b/templates/google-play/build/gradle.properties @@ -54,6 +54,8 @@ COCOS_ENGINE_PATH= # Billing PROP_ENABLE_GOOGLE_BILLING=true +# PLAY +PROP_ENABLE_GOOGLE_PLAY_GAMES=true # Res path RES_PATH= diff --git a/vendor/google/billing/billing-enum.ts b/vendor/google/billing/billing-enum.ts index aa9fd2a0a5..457e85784f 100644 --- a/vendor/google/billing/billing-enum.ts +++ b/vendor/google/billing/billing-enum.ts @@ -26,7 +26,7 @@ import { JSB } from 'internal:constants'; declare const jsb: any; -if (JSB) { +if (JSB && jsb.BillingClient) { jsb.BillingClient.ConnectionState = { DISCONNECTED: 0, CONNECTING: 1, diff --git a/vendor/google/billing/billing.ts b/vendor/google/billing/billing.ts deleted file mode 100644 index 9cc50e9ada..0000000000 --- a/vendor/google/billing/billing.ts +++ /dev/null @@ -1,159 +0,0 @@ -/* eslint-disable @typescript-eslint/no-namespace */ -/**************************************************************************** - Copyright (c) 2024 Xiamen Yaji Software Co., Ltd. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights to - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - of the Software, and to permit persons to whom the Software is furnished to do so, - subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -*****************************************************************************/ - -import { JSB } from 'internal:constants'; -import './billing-enum'; - -const ns = JSB ? jsb : {} as unknown as typeof jsb; -export namespace google { - export namespace billing { - export const BillingClient = ns.BillingClient; - export const BillingResult = ns.BillingResult; - export const InAppMessageResult = ns.InAppMessageResult; - export const ProductDetails = ns.ProductDetails; - export const Purchase = ns.Purchase; - export const PendingPurchasesParams = ns.PendingPurchasesParams; - export const QueryProductDetailsParams = ns.QueryProductDetailsParams; - export const BillingFlowParams = ns.BillingFlowParams; - export const ConsumeParams = ns.ConsumeParams; - export const AcknowledgePurchaseParams = ns.AcknowledgePurchaseParams; - export const QueryPurchasesParams = ns.QueryPurchasesParams; - export const GetBillingConfigParams = ns.GetBillingConfigParams; - export const InAppMessageParams = ns.InAppMessageParams; - } -} -export declare namespace google { - export namespace billing { - export type AcknowledgePurchaseResponseListener = jsb.AcknowledgePurchaseResponseListener; - export type AlternativeBillingOnlyAvailabilityListener = jsb.AlternativeBillingOnlyAvailabilityListener; - export type AlternativeBillingOnlyInformationDialogListener = jsb.AlternativeBillingOnlyInformationDialogListener; - export type AlternativeBillingOnlyReportingDetailsListener = jsb.AlternativeBillingOnlyReportingDetailsListener; - export type BillingConfigResponseListener = jsb.BillingConfigResponseListener; - export type ConsumeResponseListener = jsb.ConsumeResponseListener; - export type ExternalOfferAvailabilityListener = jsb.ExternalOfferAvailabilityListener; - export type ExternalOfferInformationDialogListener = jsb.ExternalOfferInformationDialogListener; - export type ExternalOfferReportingDetailsListener = jsb.ExternalOfferReportingDetailsListener; - export type InAppMessageResponseListener = jsb.InAppMessageResponseListener; - export type ProductDetailsResponseListener = jsb.ProductDetailsResponseListener; - export type PurchasesResponseListener = jsb.PurchasesResponseListener; - export type PurchasesUpdatedListener = jsb.PurchasesUpdatedListener; - export type UserChoiceBillingListener = jsb.UserChoiceBillingListener; - export type BillingClientStateListener = jsb.BillingClientStateListener; - - export type UserChoiceDetails = jsb.UserChoiceDetails; - export type AccountIdentifiers = jsb.AccountIdentifiers; - export type PendingPurchaseUpdate = jsb.PendingPurchaseUpdate; - export type BillingConfig = jsb.BillingConfig; - export type AlternativeBillingOnlyReportingDetails = jsb.AlternativeBillingOnlyReportingDetails; - export type ExternalOfferReportingDetails = jsb.ExternalOfferReportingDetails; - export namespace InAppMessageResult { - export type InAppMessageResponseCode = jsb.InAppMessageResponseCode; - } - export type InAppMessageResult = jsb.InAppMessageResult; - - export namespace PendingPurchasesParams { - export type Builder = jsb.PendingPurchasesParamsBuilder; - } - export type PendingPurchasesParams = jsb.PendingPurchasesParams; - - export namespace ProductDetails { - export type RecurrenceMode = jsb.RecurrenceMode; - export type OneTimePurchaseOfferDetails = jsb.OneTimePurchaseOfferDetails; - export type InstallmentPlanDetails = jsb.InstallmentPlanDetails; - export type PricingPhase = jsb.PricingPhase; - export type PricingPhases = jsb.PricingPhases; - export type SubscriptionOfferDetails = jsb.SubscriptionOfferDetails; - } - export type ProductDetails = jsb.ProductDetails; - - export namespace Purchase { - export type PurchaseState = jsb.PurchaseState; - } - export type Purchase = jsb.Purchase; - - export namespace BillingClient { - export type Builder = jsb.BillingClientBuilder; - export type ConnectionState = jsb.ConnectionState; - export type BillingResponseCode = jsb.BillingResponseCode; - export type FeatureType = jsb.FeatureType; - export type ProductType = jsb.ProductType; - } - export type BillingClient = jsb.BillingClient; - export namespace BillingFlowParams { - export type Builder = jsb.BillingFlowParamsBuilder; - export namespace SubscriptionUpdateParams { - export type Builder = jsb.SubscriptionUpdateParamsBuilder; - export type ReplacementMode = jsb.ReplacementMode; - } - export type SubscriptionUpdateParams = jsb.SubscriptionUpdateParams; - export namespace ProductDetailsParams { - export type Builder = jsb.ProductDetailsParamsBuilder; - } - export type ProductDetailsParams = jsb.ProductDetailsParams; - - } - export type BillingFlowParams = jsb.BillingFlowParams; - - export namespace QueryProductDetailsParams { - export type Builder = jsb.QueryProductDetailsParamsBuilder; - export namespace Product { - export type Builder = jsb.QueryProductDetailsParamsProductBuilder; - } - export type Product = jsb.QueryProductDetailsParamsProduct; - } - export type QueryProductDetailsParams = jsb.QueryProductDetailsParams; - - export namespace BillingResult { - export type Builder = jsb.BillingResultBuilder; - } - export type BillingResult = jsb.BillingResult; - - export namespace ConsumeParams { - export type Builder = jsb.ConsumeParamsBuilder; - } - export type ConsumeParams = jsb.ConsumeParams; - - export namespace AcknowledgePurchaseParams { - export type Builder = jsb.AcknowledgePurchaseParamsBuilder; - } - export type AcknowledgePurchaseParams = jsb.AcknowledgePurchaseParams; - - export namespace QueryPurchasesParams { - export type Builder = jsb.QueryPurchasesParamsBuilder; - } - export type QueryPurchasesParams = jsb.QueryPurchasesParams; - - namespace InAppMessageParams { - export type Builder = jsb.InAppMessageParamsBuilder; - export type InAppMessageCategoryId = jsb.InAppMessageCategoryId; - } - export type InAppMessageParams = jsb.InAppMessageParams; - namespace GetBillingConfigParams { - export type Builder = jsb.GetBillingConfigParams; - } - export type GetBillingConfigParams = jsb.GetBillingConfigParams; - - } -} diff --git a/vendor/google/index.ts b/vendor/google/index.ts index 51359d6f46..13f80aa786 100644 --- a/vendor/google/index.ts +++ b/vendor/google/index.ts @@ -1,5 +1,6 @@ +/* eslint-disable @typescript-eslint/no-namespace */ /* - Copyright (c) 2024 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2024-2025 Xiamen Yaji Software Co., Ltd. https://www.cocos.com/ @@ -22,4 +23,157 @@ THE SOFTWARE. */ -export * from './billing/billing'; +import { JSB } from 'internal:constants'; +import './billing/billing-enum'; +import { PlayGamesHelper, GamesSignInClientHelper } from './play/games'; +import { TaskHelper, ContinuationHelper } from './play/task'; + +//export * from './play/games'; +const ns = JSB ? jsb : {} as unknown as typeof jsb; +export namespace google { + export namespace billing { + export const BillingClient = ns.BillingClient; + export const BillingResult = ns.BillingResult; + export const InAppMessageResult = ns.InAppMessageResult; + export const ProductDetails = ns.ProductDetails; + export const Purchase = ns.Purchase; + export const PendingPurchasesParams = ns.PendingPurchasesParams; + export const QueryProductDetailsParams = ns.QueryProductDetailsParams; + export const BillingFlowParams = ns.BillingFlowParams; + export const ConsumeParams = ns.ConsumeParams; + export const AcknowledgePurchaseParams = ns.AcknowledgePurchaseParams; + export const QueryPurchasesParams = ns.QueryPurchasesParams; + export const GetBillingConfigParams = ns.GetBillingConfigParams; + export const InAppMessageParams = ns.InAppMessageParams; + } + export namespace play { + export const PlayGamesSdk = ns.PlayGamesSdk; + export const PlayGames = PlayGamesHelper; + export const GamesSignInClient = GamesSignInClientHelper; + export const AuthenticationResult = ns.AuthenticationResult; + export const RecallAccess = ns.RecallAccess; + export const PlayException = ns.PlayException; + } +} +export declare namespace google { + export namespace billing { + export type AcknowledgePurchaseResponseListener = jsb.AcknowledgePurchaseResponseListener; + export type AlternativeBillingOnlyAvailabilityListener = jsb.AlternativeBillingOnlyAvailabilityListener; + export type AlternativeBillingOnlyInformationDialogListener = jsb.AlternativeBillingOnlyInformationDialogListener; + export type AlternativeBillingOnlyReportingDetailsListener = jsb.AlternativeBillingOnlyReportingDetailsListener; + export type BillingConfigResponseListener = jsb.BillingConfigResponseListener; + export type ConsumeResponseListener = jsb.ConsumeResponseListener; + export type ExternalOfferAvailabilityListener = jsb.ExternalOfferAvailabilityListener; + export type ExternalOfferInformationDialogListener = jsb.ExternalOfferInformationDialogListener; + export type ExternalOfferReportingDetailsListener = jsb.ExternalOfferReportingDetailsListener; + export type InAppMessageResponseListener = jsb.InAppMessageResponseListener; + export type ProductDetailsResponseListener = jsb.ProductDetailsResponseListener; + export type PurchasesResponseListener = jsb.PurchasesResponseListener; + export type PurchasesUpdatedListener = jsb.PurchasesUpdatedListener; + export type UserChoiceBillingListener = jsb.UserChoiceBillingListener; + export type BillingClientStateListener = jsb.BillingClientStateListener; + + export type UserChoiceDetails = jsb.UserChoiceDetails; + export type AccountIdentifiers = jsb.AccountIdentifiers; + export type PendingPurchaseUpdate = jsb.PendingPurchaseUpdate; + export type BillingConfig = jsb.BillingConfig; + export type AlternativeBillingOnlyReportingDetails = jsb.AlternativeBillingOnlyReportingDetails; + export type ExternalOfferReportingDetails = jsb.ExternalOfferReportingDetails; + export namespace InAppMessageResult { + export type InAppMessageResponseCode = jsb.InAppMessageResponseCode; + } + export type InAppMessageResult = jsb.InAppMessageResult; + + export namespace PendingPurchasesParams { + export type Builder = jsb.PendingPurchasesParamsBuilder; + } + export type PendingPurchasesParams = jsb.PendingPurchasesParams; + + export namespace ProductDetails { + export type RecurrenceMode = jsb.RecurrenceMode; + export type OneTimePurchaseOfferDetails = jsb.OneTimePurchaseOfferDetails; + export type InstallmentPlanDetails = jsb.InstallmentPlanDetails; + export type PricingPhase = jsb.PricingPhase; + export type PricingPhases = jsb.PricingPhases; + export type SubscriptionOfferDetails = jsb.SubscriptionOfferDetails; + } + export type ProductDetails = jsb.ProductDetails; + + export namespace Purchase { + export type PurchaseState = jsb.PurchaseState; + } + export type Purchase = jsb.Purchase; + + export namespace BillingClient { + export type Builder = jsb.BillingClientBuilder; + export type ConnectionState = jsb.ConnectionState; + export type BillingResponseCode = jsb.BillingResponseCode; + export type FeatureType = jsb.FeatureType; + export type ProductType = jsb.ProductType; + } + export type BillingClient = jsb.BillingClient; + export namespace BillingFlowParams { + export type Builder = jsb.BillingFlowParamsBuilder; + export namespace SubscriptionUpdateParams { + export type Builder = jsb.SubscriptionUpdateParamsBuilder; + export type ReplacementMode = jsb.ReplacementMode; + } + export type SubscriptionUpdateParams = jsb.SubscriptionUpdateParams; + export namespace ProductDetailsParams { + export type Builder = jsb.ProductDetailsParamsBuilder; + } + export type ProductDetailsParams = jsb.ProductDetailsParams; + + } + export type BillingFlowParams = jsb.BillingFlowParams; + + export namespace QueryProductDetailsParams { + export type Builder = jsb.QueryProductDetailsParamsBuilder; + export namespace Product { + export type Builder = jsb.QueryProductDetailsParamsProductBuilder; + } + export type Product = jsb.QueryProductDetailsParamsProduct; + } + export type QueryProductDetailsParams = jsb.QueryProductDetailsParams; + + export namespace BillingResult { + export type Builder = jsb.BillingResultBuilder; + } + export type BillingResult = jsb.BillingResult; + + export namespace ConsumeParams { + export type Builder = jsb.ConsumeParamsBuilder; + } + export type ConsumeParams = jsb.ConsumeParams; + + export namespace AcknowledgePurchaseParams { + export type Builder = jsb.AcknowledgePurchaseParamsBuilder; + } + export type AcknowledgePurchaseParams = jsb.AcknowledgePurchaseParams; + + export namespace QueryPurchasesParams { + export type Builder = jsb.QueryPurchasesParamsBuilder; + } + export type QueryPurchasesParams = jsb.QueryPurchasesParams; + + namespace InAppMessageParams { + export type Builder = jsb.InAppMessageParamsBuilder; + export type InAppMessageCategoryId = jsb.InAppMessageCategoryId; + } + export type InAppMessageParams = jsb.InAppMessageParams; + namespace GetBillingConfigParams { + export type Builder = jsb.GetBillingConfigParams; + } + export type GetBillingConfigParams = jsb.GetBillingConfigParams; + + } + + export namespace play { + export type PlayGamesSdk = jsb.PlayGamesSdk; + export type AuthenticationResult = jsb.AuthenticationResult; + export type Continuation = ContinuationHelper; + export type Task = TaskHelper; + export type RecallAccess = jsb.RecallAccess; + export type PlayException = jsb.PlayException; + } +} diff --git a/vendor/google/play/games.ts b/vendor/google/play/games.ts new file mode 100644 index 0000000000..26c0970e3e --- /dev/null +++ b/vendor/google/play/games.ts @@ -0,0 +1,253 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*****************************************************************************/ + +import { TaskHelper, ContinuationHelper, OnCompleteListener, OnCanceledListener, OnFailureListener, OnSuccessListener } from './task'; + +/** + * TaskManager is responsible for managing the lifecycle of tasks. + * + * Example usage in TypeScript: + * + * task.addOnSuccessListener({ + * onSuccess: (result: google.play.AuthenticationResult): void => { + * console.log('Authenticated: ', result.isAuthenticated()); + * }, + * }); + * + * Ideally, after invoking this function, the callback should be awaited. + * However, in TypeScript, if the task object is not stored, it may be released + * before the callback is executed. To prevent this, a class is needed to manage + * the tasks being listened to. + */ +class TaskManager { + private tasks: Map> = new Map>(); + private nextTaskId: number = 0; + addTask (task: GooglePlayTask, taskId: number | null = null): void { + if (typeof taskId === 'number') { + this.tasks.set(taskId, task); + } else { + this.tasks.set(this.getNextTaskId(), task); + } + } + + getNextTaskId (): number { + return this.nextTaskId++; + } + + removeTask (taskId: number): void { + this.tasks.delete(taskId); + } +} + +export class GooglePlayTask implements TaskHelper { + private _nativeTask: jsb.PlayTask; + public static taskMgr: TaskManager = new TaskManager(); + constructor (task: jsb.PlayTask) { + this._nativeTask = task; + } + + addOnCanceledListener (listener: OnCanceledListener): TaskHelper { + const taskId = GooglePlayTask.taskMgr.getNextTaskId(); + const newNativeTask = this._nativeTask.addOnCanceledListener({ + onCanceled: (): void => { + listener.onCanceled(); + GooglePlayTask.taskMgr.removeTask(taskId); + }, + }); + const newTask = new GooglePlayTask(newNativeTask); + GooglePlayTask.taskMgr.addTask(newTask, taskId); + return newTask; + } + + addOnCompleteListener (listener: OnCompleteListener): TaskHelper { + const taskId = GooglePlayTask.taskMgr.getNextTaskId(); + const newNativeTask = this._nativeTask.addOnCompleteListener({ + onComplete: (result: jsb.PlayTask): void => { + listener.onComplete(new GooglePlayTask(result)); + GooglePlayTask.taskMgr.removeTask(taskId); + }, + }); + const newTask = new GooglePlayTask(newNativeTask); + GooglePlayTask.taskMgr.addTask(newTask, taskId); + return newTask; + } + + addOnFailureListener (listener: OnFailureListener): TaskHelper { + const taskId = GooglePlayTask.taskMgr.getNextTaskId(); + const newNativeTask = this._nativeTask.addOnFailureListener({ + onFailure: (result: jsb.PlayException): void => { + listener.onFailure(result); + GooglePlayTask.taskMgr.removeTask(taskId); + }, + }); + + const newTask = new GooglePlayTask(newNativeTask); + GooglePlayTask.taskMgr.addTask(newTask, taskId); + return newTask; + } + + addOnSuccessListener (continuation: OnSuccessListener): TaskHelper { + const taskId = GooglePlayTask.taskMgr.getNextTaskId(); + const newNativeTask = this._nativeTask.addOnSuccessListener({ + onSuccess: (result: any): void => { + continuation.onSuccess(result as TResult); + GooglePlayTask.taskMgr.removeTask(taskId); + }, + }); + const newTask = new GooglePlayTask(newNativeTask); + GooglePlayTask.taskMgr.addTask(newTask, taskId); + return newTask; + } + + continueWith (continuation: ContinuationHelper): TaskHelper { + const newNativeTask = this._nativeTask.continueWith({ + then: (result: jsb.PlayTask): TContinuationResult => { + const newTask = new GooglePlayTask(result); + GooglePlayTask.taskMgr.addTask(newTask); + return continuation.then(newTask); + }, + }); + const newTask = new GooglePlayTask(newNativeTask); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + + continueWithTask (continuation: TaskHelper>): TaskHelper { + throw new Error('Method not implemented.'); + } + + getResult (): TResult | null { + // Since getResult can return any type, it is not possible to directly return an arbitrary type to TypeScript in C++. + // Therefore, a solution was implemented: + // a callback function is set up to receive a value of type any. + // In C++, this callback function can be called with any type, and it will handle automatic type conversion. + let anyType: TResult | null = null; + this._nativeTask.getResult({ + onSuccess: (result: any): void => { + anyType = result as TResult; + }, + }); + return anyType; + } + + isCanceled (): boolean { + return this._nativeTask.isCanceled(); + } + isComplete (): boolean { + return this._nativeTask.isComplete(); + } + isSuccessful (): boolean { + return this._nativeTask.isSuccessful(); + } +} + +export class GamesSignInClientHelper { + isAuthenticated (): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getGamesSignInClient().isAuthenticated()); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + + requestServerSideAccess (serverClientId: string, forceRefreshToken: boolean): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getGamesSignInClient().requestServerSideAccess(serverClientId, forceRefreshToken)); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + + signIn (): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getGamesSignInClient().signIn()); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } +} + +export class AchievementsClient { + showAchievements (): void { + jsb.PlayGames.getAchievementsClient().showAchievements(); + } + + incrementImmediate (id: string, numSteps: number): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getAchievementsClient().incrementImmediate(id, numSteps)); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + load (id: boolean): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getAchievementsClient().load(id)); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + revealImmediate (id: string): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getAchievementsClient().revealImmediate(id)); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + setStepsImmediate (id: string, numSteps: number): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getAchievementsClient().setStepsImmediate(id, numSteps)); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + unlockImmediate (id: string): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getAchievementsClient().unlockImmediate(id)); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } + increment (id: string, numSteps: number): void { + jsb.PlayGames.getAchievementsClient().increment(id, numSteps); + } + + reveal (id: string): void { + jsb.PlayGames.getAchievementsClient().reveal(id); + } + + setSteps (id: string, numSteps: number): void { + jsb.PlayGames.getAchievementsClient().setSteps(id, numSteps); + } + + unlock (id: string): void { + jsb.PlayGames.getAchievementsClient().unlock(id); + } +} + +export class RecallClient { + requestRecallAccess (): TaskHelper { + const newTask = new GooglePlayTask(jsb.PlayGames.getRecallClient().requestRecallAccess()); + GooglePlayTask.taskMgr.addTask(newTask); + return newTask; + } +} + +export class PlayGamesHelper { + private static achievementsClient: AchievementsClient = new AchievementsClient(); + private static gamesSignInClient: GamesSignInClientHelper = new GamesSignInClientHelper(); + private static recallClient: RecallClient = new RecallClient(); + public static getAchievementsClient (): AchievementsClient { + return this.achievementsClient; + } + public static getGamesSignInClient (): GamesSignInClientHelper { + return this.gamesSignInClient; + } + public static getRecallClient (): RecallClient { + return this.recallClient; + } +} diff --git a/vendor/google/play/task.ts b/vendor/google/play/task.ts new file mode 100644 index 0000000000..7d9dc98431 --- /dev/null +++ b/vendor/google/play/task.ts @@ -0,0 +1,55 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*****************************************************************************/ +export interface OnCanceledListener { + onCanceled(): void; +} + +export interface OnCompleteListener { + onComplete(result: TaskHelper): void; +} + +export interface OnFailureListener { + onFailure(e: jsb.PlayException): void; +} + +export interface OnSuccessListener { + onSuccess(result: TResult): void; +} + +export interface ContinuationHelper { + then(task: TaskHelper): TContinuationResult; +} + +export interface TaskHelper { + addOnCanceledListener(listener: OnCanceledListener): TaskHelper; + addOnCompleteListener(listener: OnCompleteListener): TaskHelper; + addOnFailureListener(listener: OnFailureListener): TaskHelper; + addOnSuccessListener(listener: OnSuccessListener): TaskHelper; + continueWith(result: ContinuationHelper): TaskHelper; + continueWithTask (result: TaskHelper>): TaskHelper; + getResult(): TResult | null; + isCanceled(): boolean; + isComplete(): boolean; + isSuccessful(): boolean; +}