Skip to content

Commit 193985d

Browse files
authored
Merge pull request #336 from OneSignal/fix/onesignal_config_rename_on_upgrade
Rename OneSignalConfig -> OneSignalConfig.plugin via an editor script - Fix up to PR #335
2 parents f6b96a4 + dec7289 commit 193985d

12 files changed

+193
-27
lines changed

OneSignalExample/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ Assets/Root
6161

6262
/ProjectSettings/GoogleDependencyOneSignal.xml
6363

64-
/Assets/Plugins/Android/OneSignalConfig/AndroidManifest.xml*
64+
**/OneSignalConfig.plugin/AndroidManifest.xml*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
internal class OSUnityEditorUtils {
8+
9+
// Searches for a list of file names accross the whole Unity project and provides
10+
// the the GUID of the first found result.
11+
internal static string FindFirstFileGUIDByName(String[] filenames) {
12+
foreach (var filename in filenames) {
13+
var pathGUIDs = AssetDatabase.FindAssets(filename);
14+
if (pathGUIDs.Length > 0)
15+
return pathGUIDs[0];
16+
}
17+
return null;
18+
}
19+
20+
internal static void AppendFileExtensionIfMissing(string pathedFile, string extension) {
21+
if (pathedFile == null) {
22+
Debug.LogError($"AppendFileExtensionIfMissing: pathedFile can not be null");
23+
return;
24+
}
25+
26+
var pathParts = pathedFile.Split(Path.DirectorySeparatorChar);
27+
var fileName = pathParts[pathParts.Length - 1];
28+
if (!fileName.Contains("." + extension)) {
29+
var result = AssetDatabase.MoveAsset(pathedFile, $"{pathedFile}.{extension}");
30+
if (!String.IsNullOrEmpty(result))
31+
Debug.LogError($"Could not add '.{extension}' to '{pathedFile}' due to error: {result}");
32+
}
33+
}
34+
}

OneSignalExample/Assets/OneSignal/Editor/OSUnityEditorUtils.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OneSignalExample/Assets/OneSignal/Editor/OneSignalEditorCheckUpdateScript.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,14 @@
3232
using OneSignalPush.MiniJSON;
3333
using System.Collections;
3434

35-
#if !UNITY_CLOUD_BUILD && UNITY_EDITOR && UNITY_2017_1_OR_NEWER
36-
37-
[InitializeOnLoad]
38-
public class OneSignalEditorCheckUpdateScript : AssetPostprocessor
35+
internal class OneSignalEditorCheckUpdateScript
3936
{
4037
//this key is used for the current unity session only
4138
public static string sessionState = "onesignal_checked_update";
4239

4340
static OneSignalUpdateRequest request;
4441

45-
static OneSignalEditorCheckUpdateScript()
46-
{
47-
Request();
48-
}
49-
50-
static void Request()
42+
internal static void Request()
5143
{
5244
//if the SDK already checked for an update during this session, no need to do so again
5345
if (SessionState.GetBool(sessionState, false)) {
@@ -127,6 +119,4 @@ IEnumerator FetchCurrentVersion(string url)
127119

128120
yield break;
129121
}
130-
}
131-
132-
#endif
122+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#if UNITY_EDITOR
2+
using UnityEditor;
3+
4+
[InitializeOnLoad]
5+
public class OneSignalEditorScriptInit : AssetPostprocessor {
6+
static OneSignalEditorScriptInit() {
7+
OneSignalFileStructureUpgrade.DoUpgrade();
8+
#if UNITY_ANDROID
9+
OneSignalEditorScriptAndroid.createOneSignalAndroidManifest();
10+
#endif
11+
#if !UNITY_CLOUD_BUILD && UNITY_2017_1_OR_NEWER
12+
OneSignalEditorCheckUpdateScript.Request();
13+
#endif
14+
}
15+
}
16+
#endif

OneSignalExample/Assets/OneSignal/Editor/OneSignalEditorScriptInit.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OneSignalExample/Assets/OneSignal/Editor/OneSignalEditorScript_Android.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@
3333
#if UNITY_ANDROID && UNITY_EDITOR
3434
using System.Collections.Generic;
3535

36-
[InitializeOnLoad]
37-
public class OneSignalEditorScriptAndroid : AssetPostprocessor {
36+
internal class OneSignalEditorScriptAndroid {
3837

39-
static OneSignalEditorScriptAndroid() {
40-
createOneSignalAndroidManifest();
41-
}
42-
4338
// Copies `AndroidManifestTemplate.xml` to `AndroidManifest.xml`
4439
// then replace `${manifestApplicationId}` with current packagename in the Unity settings.
45-
private static void createOneSignalAndroidManifest() {
46-
string oneSignalConfigPath = "Assets/Plugins/Android/OneSignalConfig.plugin/";
40+
internal static void createOneSignalAndroidManifest() {
41+
var oneSignalConfigPath = OneSignalFileLocator.GetOneSignalConfigFolderNameWithPath() + "/";
4742
string manifestFullPath = oneSignalConfigPath + "AndroidManifest.xml";
4843

4944
File.Copy(oneSignalConfigPath + "AndroidManifestTemplate.xml", manifestFullPath, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
internal class OneSignalFileLocator {
7+
8+
// Returns the full path of the OneSignalConfig folder.
9+
// The folder contains a AndroidManifest.xml file which is required
10+
// for Android push notifications to be received by the app.
11+
internal static string GetOneSignalConfigFolderNameWithPath() {
12+
var guid = GetOneSignalConfigGUID();
13+
if (guid != null)
14+
return AssetDatabase.GUIDToAssetPath(guid);
15+
16+
Debug.LogError($"OneSignal could not find required OneSignalConfig folder, push notifications will NOT display on Android!");
17+
return null;
18+
}
19+
20+
private static string GetOneSignalConfigGUID() {
21+
// 1. Attempt to get GUID for folder
22+
// - This will be successful unless the Unity Dev deletes the matching .meta file or
23+
// moves the folder outside of Unity.
24+
var guid = GetOneSignalConfigGUIDIfAvailable();
25+
if (guid != null)
26+
return guid;
27+
28+
Debug.Log($"Didn't find '{FOLDER_NAME_V1_ONESIGNAL_CONFIG_NAME}' by GUID '{FOLDER_GUID_ONESIGNAL_CONFIG}', seaching by name.");
29+
30+
// 2. Attempt by name if GUID look up fails
31+
return GetOneSignalConfigGUIDByName();
32+
}
33+
34+
// This is a static GUID defined in the OneSignalConfig.plugin.meta file in this repo.
35+
private static readonly string FOLDER_GUID_ONESIGNAL_CONFIG = "4913d3bd89f197541850fb5382f2456d";
36+
// Only provides GUID if it is found in project
37+
private static string GetOneSignalConfigGUIDIfAvailable() {
38+
var path = AssetDatabase.GUIDToAssetPath(FOLDER_GUID_ONESIGNAL_CONFIG);
39+
if (!String.IsNullOrEmpty(path))
40+
return FOLDER_GUID_ONESIGNAL_CONFIG;
41+
return null;
42+
}
43+
44+
private static readonly string FOLDER_NAME_V1_ONESIGNAL_CONFIG_NAME = "OneSignalConfig";
45+
private static readonly string FOLDER_NAME_V2_ONESIGNAL_CONFIG_NAME = "OneSignalConfig.plugin";
46+
private static readonly string[] FOLDER_NAME_ONESIGNAL_CONFIG_NAMES = new String[] {
47+
FOLDER_NAME_V2_ONESIGNAL_CONFIG_NAME,
48+
FOLDER_NAME_V1_ONESIGNAL_CONFIG_NAME
49+
};
50+
// Returns GUID of first found folder by name, null if not found.
51+
private static string GetOneSignalConfigGUIDByName() {
52+
var guid = OSUnityEditorUtils.FindFirstFileGUIDByName(FOLDER_NAME_ONESIGNAL_CONFIG_NAMES);
53+
if (guid != null)
54+
return guid;
55+
56+
var folderNames = String.Join(",", FOLDER_NAME_ONESIGNAL_CONFIG_NAMES);
57+
Debug.LogError($"OneSignal - Could not find required Android folder from list of names; '{folderNames}'");
58+
return null;
59+
}
60+
}

OneSignalExample/Assets/OneSignal/Editor/OneSignalFileLocator.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if UNITY_EDITOR
2+
3+
// Add any files or folders that need to be rename, moved, or deleted in
4+
// as part of the process when updating this SDK.
5+
internal class OneSignalFileStructureUpgrade {
6+
7+
internal static void DoUpgrade() {
8+
UpgradeToSDKVersion2_13_3();
9+
}
10+
11+
private static void UpgradeToSDKVersion2_13_3() {
12+
#if UNITY_ANDROID
13+
RenameAndroidOneSignalConfig();
14+
#endif
15+
}
16+
17+
// This renames the folder "OneSignalConfig" to "OneSignal.plugin".
18+
// ".plugin" is Unity documented folder post-fix required for some plugins features.
19+
// - This is required to fix Unity 2020+ capability.
20+
// This is done via a script since .unitypackage files do not support renaming
21+
// and so this is required for those upgrading from an older version of the SDK.
22+
private static void RenameAndroidOneSignalConfig() {
23+
var path = OneSignalFileLocator.GetOneSignalConfigFolderNameWithPath();
24+
OSUnityEditorUtils.AppendFileExtensionIfMissing(path, "plugin");
25+
}
26+
}
27+
#endif

OneSignalExample/Assets/OneSignal/Editor/OneSignalFileStructureUpgrade.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

buildpackage.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ temp_location=$(pwd)/onesignal_temp
1717
icons_location=$project_path/Assets/AppIcons
1818
icons_temp_location=$(pwd)/onesignal_temp/tempAppIcons
1919
android_location=$project_path/Assets/Plugins/Android
20-
config_location=$android_location/OneSignalConfig
20+
config_location=$android_location/OneSignalConfig.plugin
2121
package_manifest=$project_path/Packages/manifest.json
2222
temp_package_manifest=$temp_location/Packages/manifest.json
2323

@@ -40,16 +40,16 @@ mv $icons_location.meta $icons_temp_location.meta
4040
# This removes any .aar files we don't want to bundle in our package
4141

4242
# temporarily move some necessary files
43-
mv $config_location $temp_location/OneSignalConfig
44-
mv $config_location.meta $temp_location/OneSignalConfig.meta
43+
mv $config_location $temp_location/OneSignalConfig.plugin
44+
mv $config_location.meta $temp_location/OneSignalConfig.plugin.meta
4545

4646
# get rid of a bunch of unnecessary files
4747
rm -r $android_location
4848
mkdir $android_location
4949

5050
# put the config files back
51-
mv $temp_location/OneSignalConfig $config_location
52-
mv $temp_location/OneSignalConfig.meta $config_location.meta
51+
mv $temp_location/OneSignalConfig.plugin $config_location
52+
mv $temp_location/OneSignalConfig.plugin.meta $config_location.meta
5353

5454
## END - Clean Android files
5555

0 commit comments

Comments
 (0)