1+ #if UNITY_EDITOR
2+ using UnityEngine ;
3+
4+ using UnityEditor ;
5+ using UnityEditor . iOS . Xcode ;
6+
7+ using UnityEditor . Build ;
8+ using UnityEditor . Build . Reporting ;
9+
10+ using System ;
11+ using System . IO ;
12+ using System . Linq ;
13+ using System . Collections . Generic ;
14+
15+ public class PListProcessor : IPostprocessBuildWithReport
16+ {
17+ public int callbackOrder => 99999 ;
18+
19+ public void OnPostprocessBuild ( BuildReport report )
20+ {
21+ #if UNITY_IOS
22+ string projectBundleId = PlayerSettings . GetApplicationIdentifier ( BuildTargetGroup . iOS ) ;
23+ var plistFiles = AssetDatabase . FindAssets ( "glob:\" **/*.plist\" " ) . Select ( ( guid ) => {
24+ var doc = new PlistDocument ( ) ;
25+ doc . ReadFromFile ( AssetDatabase . GUIDToAssetPath ( guid ) ) ;
26+ return doc ;
27+ } ) . Where ( ( doc ) => {
28+ return doc . root . values . TryGetValue ( "BUNDLE_ID" , out var element ) && element . AsString ( ) == projectBundleId ;
29+ } ) . ToArray ( ) ;
30+
31+ if ( ! ( plistFiles ? . Length > 0 ) )
32+ return ;
33+
34+ var google = plistFiles . FirstOrDefault ( ) ;
35+
36+ if ( ! ( google . root . values . TryGetValue ( "CLIENT_ID" , out var CLIENT_ID ) && CLIENT_ID ? . AsString ( ) is string clientID && clientID . EndsWith ( "googleusercontent.com" ) ) )
37+ throw new KeyNotFoundException ( "CLIENT_ID" ) ;
38+ if ( ! ( google . root . values . TryGetValue ( "REVERSED_CLIENT_ID" , out var REVERSED_CLIENT_ID ) && REVERSED_CLIENT_ID ? . AsString ( ) is string reversedClientID && reversedClientID . StartsWith ( "com.googleusercontent" ) ) )
39+ throw new KeyNotFoundException ( "REVERSED_CLIENT_ID" ) ;
40+
41+ string plistPath = Path . Combine ( report . summary . outputPath , "Info.plist" ) ;
42+
43+ var info = new PlistDocument ( ) ;
44+ info . ReadFromFile ( plistPath ) ;
45+
46+ info . root . SetString ( "GIDClientID" , clientID ) ;
47+ var CFBundleURLTypes = ( info . root . values . TryGetValue ( "CFBundleURLTypes" , out var element ) ? element . AsArray ( ) : null ) ?? info . root . CreateArray ( "CFBundleURLTypes" ) ;
48+ if ( ! ( CFBundleURLTypes ? . values ? . Count > 0 && CFBundleURLTypes . values . OfType < PlistElementDict > ( ) . Select ( ( dict ) => dict . values . TryGetValue ( "CFBundleURLSchemes" , out var value ) ? value ? . AsArray ( ) : null ) . OfType < PlistElementArray > ( ) . SelectMany ( ( array ) => array . values ) . Any ( ( item ) => item ? . AsString ( ) == reversedClientID ) ) )
49+ {
50+ var dict = CFBundleURLTypes . AddDict ( ) ;
51+ dict . SetString ( "CFBundleTypeRole" , "Editor" ) ;
52+ dict . CreateArray ( "CFBundleURLSchemes" ) . AddString ( reversedClientID ) ;
53+ }
54+
55+ if ( google . root . values . TryGetValue ( "WEB_CLIENT_ID" , out var WEB_CLIENT_ID ) && WEB_CLIENT_ID ? . AsString ( ) is string webClientID && ! string . IsNullOrWhiteSpace ( webClientID ) )
56+ {
57+ if ( webClientID . EndsWith ( "googleusercontent.com" ) )
58+ info . root . SetString ( "GIDServerClientID" , webClientID ) ;
59+ else throw new ArgumentException ( "WebClientID" ) ;
60+ }
61+
62+ info . WriteToFile ( plistPath ) ;
63+ #endif
64+ }
65+ }
66+
67+ #endif
0 commit comments