From 262c92592e09eb6359428d41f8e9b981b44fd9eb Mon Sep 17 00:00:00 2001 From: DerpyNewbie Date: Mon, 12 Jun 2023 00:04:01 +0900 Subject: [PATCH 1/2] Add new build preprocessor class to centralize preprocessing --- .../Editor/NewbieCommonsBuildPreprocessor.cs | 27 ++++++++ .../NewbieCommonsBuildPreprocessor.cs.meta | 3 + .../Editor/NewbieInjectProcessor.cs | 69 +++++++------------ 3 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs create mode 100644 Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs.meta diff --git a/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs b/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs new file mode 100644 index 0000000..a4b8bff --- /dev/null +++ b/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs @@ -0,0 +1,27 @@ +using UnityEditor; +using VRC.SDKBase.Editor.BuildPipeline; + +namespace DerpyNewbie.Common.Editor +{ + [InitializeOnLoad] + public class NewbieCommonsBuildPreprocessor : IVRCSDKBuildRequestedCallback + { + static NewbieCommonsBuildPreprocessor() + { + EditorApplication.playModeStateChanged += PlayModeStateChanged; + } + + private static void PlayModeStateChanged(PlayModeStateChange change) + { + NewbieInjectProcessor.DoPrePlayInject(change); + } + + public int callbackOrder => 2048; + + public bool OnBuildRequested(VRCSDKRequestedBuildType requestedBuildType) + { + NewbieInjectProcessor.DoPreBuildInject(requestedBuildType); + return true; + } + } +} \ No newline at end of file diff --git a/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs.meta b/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs.meta new file mode 100644 index 0000000..c8b19dd --- /dev/null +++ b/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bb59121d2df442fdb8311fae2f7deff7 +timeCreated: 1686495008 \ No newline at end of file diff --git a/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs b/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs index cff3215..11f700c 100644 --- a/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs +++ b/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs @@ -113,6 +113,32 @@ public static string GetHierarchyName(Component component) return sb.ToString(); } + + public static void DoPrePlayInject(PlayModeStateChange change) + { + if (!NewbieInjectConfig.InjectOnPlay || BuildPipeline.isBuildingPlayer || + UnityEngine.Object.FindObjectOfType() != null || + change != PlayModeStateChange.ExitingEditMode) + return; + + Log("Pre-Play injection started."); + Inject(SceneManager.GetActiveScene()); + Log("Pre-Play injection ended."); + } + + public static bool DoPreBuildInject(VRCSDKRequestedBuildType requestedBuildType) + { + if (!NewbieInjectConfig.InjectOnBuild) + return true; + + if (requestedBuildType == VRCSDKRequestedBuildType.Avatar) + return true; + + Log("Pre-Build injection started."); + Inject(SceneManager.GetActiveScene()); + Log("Pre-Build injection ended."); + return true; + } } public static class NewbieInjectConfig @@ -147,47 +173,4 @@ private static void SetConfigValue(string name, bool value) EditorUserSettings.SetConfigValue(name, value.ToString()); } } - - public class NewbieCommonsBuildInject : IVRCSDKBuildRequestedCallback - { - public int callbackOrder => 0; - - public bool OnBuildRequested(VRCSDKRequestedBuildType requestedBuildType) - { - if (!NewbieInjectConfig.InjectOnBuild) - return true; - - if (requestedBuildType == VRCSDKRequestedBuildType.Avatar) - { - LogError("Build Injection not supported with Avatar Build."); - return true; - } - - Log("Pre-Build injection started."); - NewbieInjectProcessor.Inject(SceneManager.GetActiveScene()); - Log("Pre-Build injection ended."); - return true; - } - } - - [InitializeOnLoad] - public static class NewbieCommonsPlayInject - { - static NewbieCommonsPlayInject() - { - EditorApplication.playModeStateChanged += PlayModeStateChanged; - } - - private static void PlayModeStateChanged(PlayModeStateChange change) - { - if (!NewbieInjectConfig.InjectOnPlay || BuildPipeline.isBuildingPlayer || - UnityEngine.Object.FindObjectOfType() != null || - change != PlayModeStateChange.ExitingEditMode) - return; - - Log("Pre-Play injection started."); - NewbieInjectProcessor.Inject(SceneManager.GetActiveScene()); - Log("Pre-Play injection ended."); - } - } } \ No newline at end of file From ca8da07160e2c7fd8aab36c90ddf06ae503e7a27 Mon Sep 17 00:00:00 2001 From: DerpyNewbie Date: Mon, 12 Jun 2023 12:48:11 +0900 Subject: [PATCH 2/2] Move build play mode transition checks into preprocessor --- .../Editor/NewbieCommonsBuildPreprocessor.cs | 5 +++++ .../dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs b/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs index a4b8bff..597e4c6 100644 --- a/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs +++ b/Packages/dev.derpynewbie.common/Editor/NewbieCommonsBuildPreprocessor.cs @@ -13,6 +13,11 @@ static NewbieCommonsBuildPreprocessor() private static void PlayModeStateChanged(PlayModeStateChange change) { + var isBuilding = BuildPipeline.isBuildingPlayer || + UnityEngine.Object.FindObjectOfType() != null || + change != PlayModeStateChange.ExitingEditMode; + if (isBuilding) + return; NewbieInjectProcessor.DoPrePlayInject(change); } diff --git a/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs b/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs index 11f700c..7b937e4 100644 --- a/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs +++ b/Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs @@ -116,9 +116,7 @@ public static string GetHierarchyName(Component component) public static void DoPrePlayInject(PlayModeStateChange change) { - if (!NewbieInjectConfig.InjectOnPlay || BuildPipeline.isBuildingPlayer || - UnityEngine.Object.FindObjectOfType() != null || - change != PlayModeStateChange.ExitingEditMode) + if (!NewbieInjectConfig.InjectOnPlay) return; Log("Pre-Play injection started.");