From 3da18166e749b796d9b091990a275c2ea74f8495 Mon Sep 17 00:00:00 2001 From: ThainaYu Date: Thu, 13 Jun 2024 18:09:23 +0700 Subject: [PATCH] fix serverAuthCode in ios --- .gitignore | 3 +- .vscode/settings.json | 55 +++++++++++++++++++++++++ Editor.meta | 8 ++++ Editor/iOS.meta | 8 ++++ Editor/iOS/PListProcessor.cs | 67 +++++++++++++++++++++++++++++++ Editor/iOS/PListProcessor.cs.meta | 11 +++++ Editor/iOS/PlistImporter.cs | 21 ++++++++++ Editor/iOS/PlistImporter.cs.meta | 11 +++++ GoogleSignIn/Impl/NativeFuture.cs | 5 ++- Plugins/iOS/GoogleSignIn.mm | 8 ++-- 10 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 Editor.meta create mode 100644 Editor/iOS.meta create mode 100644 Editor/iOS/PListProcessor.cs create mode 100644 Editor/iOS/PListProcessor.cs.meta create mode 100644 Editor/iOS/PlistImporter.cs create mode 100644 Editor/iOS/PlistImporter.cs.meta diff --git a/.gitignore b/.gitignore index 20168e73..2235a2e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ +**/.DS_Store + .gradle/ .idea/ -.vscode/ bin/ build/ *.iml diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..9c2f3c6f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,55 @@ +{ + "files.exclude": { + "**/.DS_Store": true, + "**/.git": true, + "**/.gitmodules": true, + "**/*.booproj": true, + "**/*.pidb": true, + "**/*.suo": true, + "**/*.user": true, + "**/*.userprefs": true, + "**/*.unityproj": true, + "**/*.dll": true, + "**/*.exe": true, + "**/*.pdf": true, + "**/*.mid": true, + "**/*.midi": true, + "**/*.wav": true, + "**/*.gif": true, + "**/*.ico": true, + "**/*.jpg": true, + "**/*.jpeg": true, + "**/*.png": true, + "**/*.psd": true, + "**/*.tga": true, + "**/*.tif": true, + "**/*.tiff": true, + "**/*.3ds": true, + "**/*.3DS": true, + "**/*.fbx": true, + "**/*.FBX": true, + "**/*.lxo": true, + "**/*.LXO": true, + "**/*.ma": true, + "**/*.MA": true, + "**/*.obj": true, + "**/*.OBJ": true, + "**/*.asset": true, + "**/*.cubemap": true, + "**/*.flare": true, + "**/*.mat": true, + "**/*.meta": true, + "**/*.prefab": true, + "**/*.unity": true, + "build/": true, + "Build/": true, + "Library/": true, + "library/": true, + "obj/": true, + "Obj/": true, + "ProjectSettings/": true, + "temp/": true, + "Temp/": true + }, + "dotnet.defaultSolution": "TestGoogleSignIn.sln" +} \ No newline at end of file diff --git a/Editor.meta b/Editor.meta new file mode 100644 index 00000000..1a11891a --- /dev/null +++ b/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b15db503f08f84da789165e1557fdd7b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/iOS.meta b/Editor/iOS.meta new file mode 100644 index 00000000..22b933e3 --- /dev/null +++ b/Editor/iOS.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 71aabb773a37445bb8dcac856c8c672e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/iOS/PListProcessor.cs b/Editor/iOS/PListProcessor.cs new file mode 100644 index 00000000..67dd6346 --- /dev/null +++ b/Editor/iOS/PListProcessor.cs @@ -0,0 +1,67 @@ +#if UNITY_EDITOR +using UnityEngine; + +using UnityEditor; +using UnityEditor.iOS.Xcode; + +using UnityEditor.Build; +using UnityEditor.Build.Reporting; + +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +public class PListProcessor : IPostprocessBuildWithReport +{ + public int callbackOrder => 99999; + + public void OnPostprocessBuild(BuildReport report) + { +#if UNITY_IOS + var plistFiles = AssetDatabase.FindAssets("t:TextAsset").Select((guid) => AssetDatabase.GUIDToAssetPath(guid)).Where((path) => path.EndsWith(".plist")).Select((path) => { + var asset = AssetDatabase.LoadAssetAtPath(path); + var doc = new PlistDocument(); + doc.ReadFromString(asset.text); + return doc; + }).Where((doc) => { + return doc.root.values.TryGetValue("BUNDLE_ID",out var element) && element.AsString() == PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS); + }).ToArray(); + + if(!(plistFiles?.Length > 0)) + return; + + var google = plistFiles.FirstOrDefault(); + + if(!(google.root.values.TryGetValue("CLIENT_ID",out var CLIENT_ID) && CLIENT_ID?.AsString() is string clientID && clientID.EndsWith("googleusercontent.com"))) + throw new KeyNotFoundException("CLIENT_ID"); + if(!(google.root.values.TryGetValue("REVERSED_CLIENT_ID",out var REVERSED_CLIENT_ID) && REVERSED_CLIENT_ID?.AsString() is string reversedClientID && reversedClientID.StartsWith("com.googleusercontent"))) + throw new KeyNotFoundException("REVERSED_CLIENT_ID"); + + string plistPath = Path.Combine( report.summary.outputPath, "Info.plist" ); + + var info = new PlistDocument(); + info.ReadFromFile(plistPath); + + info.root.SetString("GIDClientID",clientID); + var CFBundleURLTypes = (info.root.values.TryGetValue("CFBundleURLTypes",out var element) ? element.AsArray() : null) ?? info.root.CreateArray("CFBundleURLTypes"); + if(!(CFBundleURLTypes?.values?.Count > 0 && CFBundleURLTypes.values.OfType().Select((dict) => dict.values.TryGetValue("CFBundleURLSchemes",out var value) ? value?.AsArray() : null).OfType().SelectMany((array) => array.values).Any((item) => item?.AsString() == reversedClientID))) + { + var dict = CFBundleURLTypes.AddDict(); + dict.SetString("CFBundleTypeRole","Editor"); + dict.CreateArray("CFBundleURLSchemes").AddString(reversedClientID); + } + + if(google.root.values.TryGetValue("WEB_CLIENT_ID",out var WEB_CLIENT_ID) && WEB_CLIENT_ID?.AsString() is string webClientID && !string.IsNullOrWhiteSpace(webClientID)) + { + if(webClientID.EndsWith("googleusercontent.com")) + info.root.SetString("GIDServerClientID",webClientID); + else throw new ArgumentException("WebClientID"); + } + + info.WriteToFile(plistPath); +#endif + } +} + +#endif \ No newline at end of file diff --git a/Editor/iOS/PListProcessor.cs.meta b/Editor/iOS/PListProcessor.cs.meta new file mode 100644 index 00000000..e450597e --- /dev/null +++ b/Editor/iOS/PListProcessor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 97598a1b92cb146879db81f44bfeb14d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/iOS/PlistImporter.cs b/Editor/iOS/PlistImporter.cs new file mode 100644 index 00000000..569dbb44 --- /dev/null +++ b/Editor/iOS/PlistImporter.cs @@ -0,0 +1,21 @@ +#if UNITY_EDITOR +using System.IO; + +using UnityEngine; + +using UnityEditor.AssetImporters; + +[ScriptedImporter(1, "plist")] +public class PListImporter : ScriptedImporter +{ + public override void OnImportAsset(AssetImportContext ctx) + { + if(ctx.mainObject is TextAsset) + return; + + var subAsset = new TextAsset(File.ReadAllText(ctx.assetPath)); + ctx.AddObjectToAsset("text", subAsset); + ctx.SetMainObject(subAsset); + } +} +#endif \ No newline at end of file diff --git a/Editor/iOS/PlistImporter.cs.meta b/Editor/iOS/PlistImporter.cs.meta new file mode 100644 index 00000000..78dcada8 --- /dev/null +++ b/Editor/iOS/PlistImporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f27a6caddffae4800a364c9c126d4995 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/GoogleSignIn/Impl/NativeFuture.cs b/GoogleSignIn/Impl/NativeFuture.cs index 0cb905ab..3bbd2833 100644 --- a/GoogleSignIn/Impl/NativeFuture.cs +++ b/GoogleSignIn/Impl/NativeFuture.cs @@ -34,7 +34,8 @@ internal NativeFuture(IntPtr ptr) : base(ptr) { public GoogleSignInUser Result { get { - IntPtr ptr = GoogleSignInImpl.GoogleSignIn_Result(SelfPtr()); + HandleRef self = SelfPtr(); + IntPtr ptr = GoogleSignInImpl.GoogleSignIn_Result(self); if (ptr == IntPtr.Zero) { return null; } @@ -54,7 +55,7 @@ public GoogleSignInUser Result { user.IdToken = GoogleSignInImpl.GoogleSignIn_GetIdToken(userPtr); - user.AuthCode = GoogleSignInImpl.GoogleSignIn_GetServerAuthCode(userPtr); + user.AuthCode = GoogleSignInImpl.GoogleSignIn_GetServerAuthCode(self); string url = GoogleSignInImpl.GoogleSignIn_GetImageUrl(userPtr); if (url?.Length > 0) { diff --git a/Plugins/iOS/GoogleSignIn.mm b/Plugins/iOS/GoogleSignIn.mm index b1b91616..827972ca 100644 --- a/Plugins/iOS/GoogleSignIn.mm +++ b/Plugins/iOS/GoogleSignIn.mm @@ -55,6 +55,7 @@ void UnpauseUnityPlayer() { struct SignInResult { int result_code; bool finished; + NSString* serverAuthCode; }; std::unique_ptr currentResult_; @@ -243,6 +244,7 @@ bool GoogleSignIn_Configure(void *unused, bool useGameSignIn, hint:[GoogleSignInHandler sharedInstance]->loginHint completion:^(GIDSignInResult *result, NSError *error) { GIDGoogleUser *user = result.user; + currentResult_.get()->serverAuthCode = result.serverAuthCode; [[GoogleSignInHandler sharedInstance] signIn:[GIDSignIn sharedInstance] didSignInForUser:user withError:error]; }]; result = currentResult_.get(); @@ -322,10 +324,10 @@ static size_t CopyNSString(NSString *src, char *dest, size_t len) { return src ? src.length + 1 : 0; } -size_t GoogleSignIn_GetServerAuthCode(GIDGoogleUser *guser, char *buf, +size_t GoogleSignIn_GetServerAuthCode(SignInResult *result, char *buf, size_t len) { - NSString *val = [guser.configuration serverClientID]; -return CopyNSString(val, buf, len); + NSString *val = result->serverAuthCode; + return CopyNSString(val, buf, len); } size_t GoogleSignIn_GetDisplayName(GIDGoogleUser *guser, char *buf,