From 325ef6ca7e5a2a23a434a441880e23c7993abaa5 Mon Sep 17 00:00:00 2001 From: Justin Poliachik Date: Thu, 11 Jul 2024 10:14:39 -0400 Subject: [PATCH 1/3] use app.plugin.js instead of typescript --- app.plugin.js | 107 ++++++++++++++++++++++++++++++++++++++++++- package.json | 1 + plugin/src/index.ts | 77 ------------------------------- plugin/tsconfig.json | 9 ---- 4 files changed, 107 insertions(+), 87 deletions(-) delete mode 100644 plugin/src/index.ts delete mode 100644 plugin/tsconfig.json diff --git a/app.plugin.js b/app.plugin.js index c380863..682f255 100644 --- a/app.plugin.js +++ b/app.plugin.js @@ -1 +1,106 @@ -module.exports = require('./plugin/build'); \ No newline at end of file +const { + AndroidConfig, + withProjectBuildGradle, + withDangerousMod, + withGradleProperties, + withSettingsGradle, + withStringsXml, +} = require('@expo/config-plugins'); +const fs = require('fs'); +const path = require('path'); + +const withUnity = (config, { name = 'react-native-unity' } = {}) => { + config.name = name; + config = withProjectBuildGradleMod(config); + config = withSettingsGradleMod(config); + config = withGradlePropertiesMod(config); + config = withStringsXMLMod(config); + config = withPodfileDangerousMod(config); + return config; +}; + +const REPOSITORIES_END_LINE = `maven { url 'https://www.jitpack.io' }`; + +const withProjectBuildGradleMod = (config) => + withProjectBuildGradle(config, (modConfig) => { + if (modConfig.modResults.contents.includes(REPOSITORIES_END_LINE)) { + // use the last known line in expo's build.gradle file to append the newline after + modConfig.modResults.contents = modConfig.modResults.contents.replace( + REPOSITORIES_END_LINE, + REPOSITORIES_END_LINE + + '\nflatDir { dirs "${project(\':unityLibrary\').projectDir}/libs" }\n' + ); + } else { + throw new Error( + 'Failed to find the end of repositories in the android/build.gradle file`' + ); + } + return modConfig; + }); + +const withSettingsGradleMod = (config) => + withSettingsGradle(config, (modConfig) => { + modConfig.modResults.contents += ` + include ':unityLibrary' + project(':unityLibrary').projectDir=new File('../unity/builds/android/unityLibrary') + `; + return modConfig; + }); + +const withGradlePropertiesMod = (config) => + withGradleProperties(config, (modConfig) => { + modConfig.modResults.push({ + type: 'property', + key: 'unityStreamingAssets', + value: '.unity3d', + }); + return modConfig; + }); + +// add string +const withStringsXMLMod = (config) => + withStringsXml(config, (config) => { + config.modResults = AndroidConfig.Strings.setStringItem( + [ + { + _: 'Game View', + $: { + name: 'game_view_content_description', + }, + }, + ], + config.modResults + ); + return config; + }); + +/* + Adjust the Podfile to exclude arm64 architecture for simulator builds + for all the pods in the project + This is also necessary in order to get the Unity project to build for simulator + */ +const withPodfileDangerousMod = (config) => + withDangerousMod(config, [ + 'ios', + (config) => { + /* + We need to do a 'dangerous' mod to the Podfile + and add lines to the post install hook to exclude arm64 architecture for simulator builds + */ + const file = path.join(config.modRequest.platformProjectRoot, 'Podfile'); + const contents = fs.readFileSync(file).toString(); + + // look for the closing bracket of the `react_native_post_install` block, insert stuff on the following lines + const regex = /react_native_post_install\([^)]+\)\s*/; + const newLine = ` + installer.pods_project.build_configurations.each do |config| + config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64" + end\n\n`; + + const newContents = contents.replace(regex, '$&\n' + newLine); + fs.writeFileSync(file, newContents); + return config; + }, + ]); + +module.exports = withUnity; diff --git a/package.json b/package.json index d1170a7..8b5fa63 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "ios", "cpp", "unity", + "app.plugin.js", "*.podspec", "!ios/build", "!android/build", diff --git a/plugin/src/index.ts b/plugin/src/index.ts deleted file mode 100644 index 9dd56df..0000000 --- a/plugin/src/index.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { - AndroidConfig, - withGradleProperties, - withProjectBuildGradle, - withSettingsGradle, - withStringsXml, -} from '@expo/config-plugins'; -import type { ConfigPlugin } from '@expo/config-plugins'; - -const withUnity: ConfigPlugin<{ name?: string }> = ( - config, - { name = 'react-native-unity' } = {} -) => { - config.name = name; - config = withProjectBuildGradleMod(config); - config = withSettingsGradleMod(config); - config = withGradlePropertiesMod(config); - config = withStringsXMLMod(config); - return config; -}; - -const REPOSITORIES_END_LINE = `maven { url 'https://www.jitpack.io' }`; - -const withProjectBuildGradleMod: ConfigPlugin = (config) => - withProjectBuildGradle(config, (modConfig) => { - if (modConfig.modResults.contents.includes(REPOSITORIES_END_LINE)) { - // use the last known line in expo's build.gradle file to append the newline after - modConfig.modResults.contents = modConfig.modResults.contents.replace( - REPOSITORIES_END_LINE, - REPOSITORIES_END_LINE + - '\nflatDir { dirs "${project(\':unityLibrary\').projectDir}/libs" }\n' - ); - } else { - throw new Error( - 'Failed to find the end of repositories in the android/build.gradle file`' - ); - } - return modConfig; - }); - -const withSettingsGradleMod: ConfigPlugin = (config) => - withSettingsGradle(config, (modConfig) => { - modConfig.modResults.contents += ` -include ':unityLibrary' -project(':unityLibrary').projectDir=new File('../unity/builds/android/unityLibrary') - `; - return modConfig; - }); - -const withGradlePropertiesMod: ConfigPlugin = (config) => - withGradleProperties(config, (modConfig) => { - modConfig.modResults.push({ - type: 'property', - key: 'unityStreamingAssets', - value: '.unity3d', - }); - return modConfig; - }); - -// add string -const withStringsXMLMod: ConfigPlugin = (config) => - withStringsXml(config, (config) => { - config.modResults = AndroidConfig.Strings.setStringItem( - [ - { - _: 'Game View', - $: { - name: 'game_view_content_description', - }, - }, - ], - config.modResults - ); - return config; - }); - -export default withUnity; diff --git a/plugin/tsconfig.json b/plugin/tsconfig.json deleted file mode 100644 index 81cc93b..0000000 --- a/plugin/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "expo-module-scripts/tsconfig.plugin", - "compilerOptions": { - "outDir": "build", - "rootDir": "src" - }, - "include": ["./src"], - "exclude": ["**/__mocks__/*", "**/__tests__/*"] - } \ No newline at end of file From d3c2c972be6a22ed0ccb76b6b2266018572db419 Mon Sep 17 00:00:00 2001 From: Justin Poliachik Date: Wed, 31 Jul 2024 14:34:33 -0400 Subject: [PATCH 2/3] cleanup param names --- app.plugin.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app.plugin.js b/app.plugin.js index 682f255..931f6bf 100644 --- a/app.plugin.js +++ b/app.plugin.js @@ -59,8 +59,8 @@ const withGradlePropertiesMod = (config) => // add string const withStringsXMLMod = (config) => - withStringsXml(config, (config) => { - config.modResults = AndroidConfig.Strings.setStringItem( + withStringsXml(config, (modConfig) => { + modConfig.modResults = AndroidConfig.Strings.setStringItem( [ { _: 'Game View', @@ -69,9 +69,9 @@ const withStringsXMLMod = (config) => }, }, ], - config.modResults + modConfig.modResults ); - return config; + return modConfig; }); /* @@ -82,12 +82,15 @@ const withStringsXMLMod = (config) => const withPodfileDangerousMod = (config) => withDangerousMod(config, [ 'ios', - (config) => { + (modConfig) => { /* We need to do a 'dangerous' mod to the Podfile and add lines to the post install hook to exclude arm64 architecture for simulator builds */ - const file = path.join(config.modRequest.platformProjectRoot, 'Podfile'); + const file = path.join( + modConfig.modRequest.platformProjectRoot, + 'Podfile' + ); const contents = fs.readFileSync(file).toString(); // look for the closing bracket of the `react_native_post_install` block, insert stuff on the following lines @@ -99,7 +102,7 @@ const withPodfileDangerousMod = (config) => const newContents = contents.replace(regex, '$&\n' + newLine); fs.writeFileSync(file, newContents); - return config; + return modConfig; }, ]); From b063ef289140a521e6f3435c8a723b21a4107f85 Mon Sep 17 00:00:00 2001 From: Justin Poliachik Date: Wed, 31 Jul 2024 14:35:39 -0400 Subject: [PATCH 3/3] add style prop to UnityView --- src/UnityView.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/UnityView.tsx b/src/UnityView.tsx index d9f1b9c..1815f19 100644 --- a/src/UnityView.tsx +++ b/src/UnityView.tsx @@ -3,6 +3,7 @@ import React from 'react'; import NativeUnityView, { Commands } from './specs/UnityViewNativeComponent'; import type { DirectEventHandler } from 'react-native/Libraries/Types/CodegenTypes'; import { Platform } from 'react-native'; +import type { StyleProp, ViewStyle } from 'react-native'; type UnityViewContentUpdateEvent = Readonly<{ message: string; @@ -14,6 +15,7 @@ type RNUnityViewProps = { onUnityMessage?: DirectEventHandler; onPlayerUnload?: DirectEventHandler; onPlayerQuit?: DirectEventHandler; + style?: StyleProp; }; type ComponentRef = InstanceType;