1+ /*
2+ * Copyright (c) 2022 PlayEveryWare
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5+ * of this software and associated documentation files (the "Software"), to deal
6+ * in the Software without restriction, including without limitation the rights
7+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ * copies of the Software, and to permit persons to whom the Software is
9+ * furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in all
12+ * copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ * SOFTWARE.
21+ */
22+
23+ using UnityEditor ;
24+ using UnityEditor . Build ;
25+ using UnityEditor . Build . Reporting ;
26+ using UnityEngine ;
27+ using System . IO ;
28+ using System . Linq ;
29+ using PlayEveryWare . EpicOnlineServices ;
30+ using System . Collections . Generic ;
31+
32+ public class EOSOnPreprocessBuild_android : IPreprocessBuildWithReport
33+ {
34+ public int callbackOrder { get { return 3 ; } }
35+
36+ //-------------------------------------------------------------------------
37+ private static string GetAndroidEOSValuesConfigPath ( )
38+ {
39+ string assetsPathname = Path . Combine ( Application . dataPath , "Plugins/Android/EOS/" ) ;
40+ return Path . Combine ( assetsPathname , "eos_dependencies.androidlib/res/values/eos_values.xml" ) ;
41+ }
42+
43+ //-------------------------------------------------------------------------
44+ private static string GetPackageName ( )
45+ {
46+ return "com.playeveryware.eos" ;
47+ }
48+
49+
50+ //-------------------------------------------------------------------------
51+ public void OnPreprocessBuild ( BuildReport report )
52+ {
53+ if ( report . summary . platform == BuildTarget . Android )
54+ {
55+ InstallEOSDependentLibrary ( ) ;
56+ ConfigureEOSDependentLibrary ( ) ;
57+ }
58+ }
59+
60+ //-------------------------------------------------------------------------
61+ static private void OverwriteCopy ( string fileToInstallPathName , string destPathname )
62+ {
63+ if ( File . Exists ( destPathname ) )
64+ {
65+ File . SetAttributes ( destPathname , File . GetAttributes ( destPathname ) & ~ FileAttributes . ReadOnly ) ;
66+ }
67+
68+ File . Copy ( fileToInstallPathName , destPathname , true ) ;
69+ }
70+
71+ //-------------------------------------------------------------------------
72+ static private void InstallFiles ( string [ ] filenames , string pathToInstallFrom , string pathToInstallTo )
73+ {
74+
75+ if ( ! EmptyPredicates . IsEmptyOrNull ( pathToInstallFrom ) )
76+ {
77+ foreach ( var fileToInstall in filenames )
78+ {
79+ string fileToInstallPathName = Path . Combine ( pathToInstallFrom , fileToInstall ) ;
80+
81+ if ( File . Exists ( fileToInstallPathName ) )
82+ {
83+ string fileToInstallParentDirectory = Path . GetDirectoryName ( Path . Combine ( pathToInstallTo , fileToInstall ) ) ;
84+
85+ if ( ! Directory . Exists ( fileToInstallParentDirectory ) )
86+ {
87+ Directory . CreateDirectory ( fileToInstallParentDirectory ) ;
88+ }
89+ string destPathname = Path . Combine ( fileToInstallParentDirectory , Path . GetFileName ( fileToInstallPathName ) ) ;
90+
91+ OverwriteCopy ( fileToInstallPathName , destPathname ) ;
92+ }
93+ else
94+ {
95+ Debug . LogError ( "Missing platform specific file: " + fileToInstall ) ;
96+ }
97+ }
98+ }
99+ }
100+
101+ //-------------------------------------------------------------------------
102+ private string GetPlatformSpecificAssetsPath ( string subpath )
103+ {
104+ string packagePathname = Path . GetFullPath ( "Packages/" + GetPackageName ( ) + "/PlatformSpecificAssets~/" + subpath ) ;
105+ string streamingAssetsSamplesPathname = Path . Combine ( Application . dataPath , "../PlatformSpecificAssets/" + subpath ) ;
106+ string pathToInstallFrom = "" ;
107+
108+ if ( Directory . Exists ( packagePathname ) )
109+ {
110+ // Install from package path
111+ pathToInstallFrom = packagePathname ;
112+ }
113+ else if ( Directory . Exists ( streamingAssetsSamplesPathname ) )
114+ {
115+ pathToInstallFrom = streamingAssetsSamplesPathname ;
116+ }
117+ return pathToInstallFrom ;
118+ }
119+
120+ //-------------------------------------------------------------------------
121+ bool DoesGradlePropertiesContainSetting ( string gradleTemplatePathname , string setting )
122+ {
123+ // check if it contains the android.useAndroidX=true
124+ foreach ( string line in File . ReadAllLines ( gradleTemplatePathname ) )
125+ {
126+ if ( line . Contains ( setting ) && ! line . StartsWith ( "#" ) )
127+ {
128+ return true ;
129+ }
130+ }
131+ return false ;
132+ }
133+
134+ //-------------------------------------------------------------------------
135+ void ReplaceOrSetGradleProperty ( string gradleTemplatePathname , string setting , string value )
136+ {
137+ var gradleTemplateToWrite = new List < string > ( ) ;
138+ bool wasAdded = false ;
139+
140+ foreach ( string line in File . ReadAllLines ( gradleTemplatePathname ) )
141+ {
142+ if ( line . Contains ( setting ) && ! line . StartsWith ( "#" ) )
143+ {
144+ gradleTemplateToWrite . Add ( $ "{ setting } ={ value } ") ;
145+ wasAdded = true ;
146+ }
147+ else
148+ {
149+ gradleTemplateToWrite . Add ( line ) ;
150+ }
151+ }
152+
153+ if ( ! wasAdded )
154+ {
155+ gradleTemplateToWrite . Add ( $ "{ setting } ={ value } ") ;
156+ }
157+ File . WriteAllLines ( gradleTemplatePathname , gradleTemplateToWrite . ToArray ( ) ) ;
158+ }
159+
160+ //-------------------------------------------------------------------------
161+ public void InstallEOSDependentLibrary ( )
162+ {
163+ string packagedPathname = GetPlatformSpecificAssetsPath ( "EOS/Android/" ) ;
164+
165+ if ( Directory . Exists ( packagedPathname ) )
166+ {
167+ string assetsPathname = Path . Combine ( Application . dataPath , "Plugins/Android/EOS/" ) ;
168+ string [ ] filenames =
169+ {
170+ "eos_dependencies.androidlib/AndroidManifest.xml" ,
171+ "eos_dependencies.androidlib/build.gradle" ,
172+ "eos_dependencies.androidlib/project.properties" ,
173+ "eos_dependencies.androidlib/res/values/eos_values.xml" ,
174+ "eos_dependencies.androidlib/res/values/styles.xml"
175+
176+ } ;
177+ InstallFiles ( filenames , packagedPathname , assetsPathname ) ;
178+
179+ // Unity has a fixed location for the gradleTemplate.properties file. (as of 2021)
180+ string gradleTemplatePathname = Path . Combine ( Application . dataPath , "Plugins/Android/gradleTemplate.properties" ) ;
181+ if ( File . Exists ( gradleTemplatePathname ) )
182+ {
183+ if ( ! DoesGradlePropertiesContainSetting ( gradleTemplatePathname , "android.useAndroidX=true" ) )
184+ {
185+ ReplaceOrSetGradleProperty ( gradleTemplatePathname , "android.useAndroidX" , "true" ) ;
186+ }
187+ }
188+ else
189+ {
190+ // Use one we have bundled
191+ string bundledGradleTemplatePathname = GetPlatformSpecificAssetsPath ( "EOS/Android/gradleTemplate.properties" ) ;
192+ File . Copy ( bundledGradleTemplatePathname , gradleTemplatePathname ) ;
193+ }
194+ }
195+ }
196+
197+ //-------------------------------------------------------------------------
198+ public void ConfigureEOSDependentLibrary ( )
199+ {
200+ string configFilePath = Path . Combine ( Application . streamingAssetsPath , "EOS" , EOSManager . ConfigFileName ) ;
201+ var eosConfigFile = new EOSConfigFile < EOSConfig > ( configFilePath ) ;
202+ eosConfigFile . LoadConfigFromDisk ( ) ;
203+ string clientIDAsLower = eosConfigFile . currentEOSConfig . clientID . ToLower ( ) ;
204+
205+ var pathToEOSValuesConfig = GetAndroidEOSValuesConfigPath ( ) ;
206+ var currentEOSValuesConfigAsXML = new System . Xml . XmlDocument ( ) ;
207+ currentEOSValuesConfigAsXML . Load ( pathToEOSValuesConfig ) ;
208+
209+ var node = currentEOSValuesConfigAsXML . DocumentElement . SelectSingleNode ( "/resources" ) ;
210+
211+ if ( node != null )
212+ {
213+ string eosProtocolScheme = node . InnerText ;
214+ string storedClientID = eosProtocolScheme . Split ( "." ) . Last ( ) ;
215+
216+ if ( storedClientID != clientIDAsLower )
217+ {
218+ node . InnerText = $ "eos.{ clientIDAsLower } ";
219+ currentEOSValuesConfigAsXML . Save ( pathToEOSValuesConfig ) ;
220+ }
221+ }
222+ }
223+
224+ }
0 commit comments