Skip to content

Commit

Permalink
Merge pull request #8 from DerpyNewbie/add-build-preprocessor
Browse files Browse the repository at this point in the history
Add centralized build preprocessor
  • Loading branch information
DerpyNewbie authored Jun 12, 2023
2 parents b2e0b51 + ca8da07 commit ab60e5d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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)
{
var isBuilding = BuildPipeline.isBuildingPlayer ||
UnityEngine.Object.FindObjectOfType<PipelineSaver>() != null ||
change != PlayModeStateChange.ExitingEditMode;
if (isBuilding)
return;
NewbieInjectProcessor.DoPrePlayInject(change);
}

public int callbackOrder => 2048;

public bool OnBuildRequested(VRCSDKRequestedBuildType requestedBuildType)
{
NewbieInjectProcessor.DoPreBuildInject(requestedBuildType);
return true;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 24 additions & 43 deletions Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@ public static string GetHierarchyName(Component component)

return sb.ToString();
}

public static void DoPrePlayInject(PlayModeStateChange change)
{
if (!NewbieInjectConfig.InjectOnPlay)
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
Expand Down Expand Up @@ -147,47 +171,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<PipelineSaver>() != null ||
change != PlayModeStateChange.ExitingEditMode)
return;

Log("Pre-Play injection started.");
NewbieInjectProcessor.Inject(SceneManager.GetActiveScene());
Log("Pre-Play injection ended.");
}
}
}

0 comments on commit ab60e5d

Please sign in to comment.