Skip to content

Commit

Permalink
feat: implement NDMF integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Sep 17, 2023
1 parent 340cd30 commit 4cfa199
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog].
## [Unreleased]
### Added
- Support for Multi Frame BlendShapes `#333`
- Support for [NDMF](https://ndmf.nadena.dev) integration `#375`

### Changed
- Auto FreezeBlendShape now freezes meaningless BlendShape `#334`
Expand Down
5 changes: 3 additions & 2 deletions Editor/OptimizerProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

#if !NADEMOFU
using System;
using Anatawa12.ApplyOnPlay;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase.Editor.BuildPipeline;
using Anatawa12.ApplyOnPlay;

namespace Anatawa12.AvatarOptimizer
{
Expand Down Expand Up @@ -150,3 +150,4 @@ private static void DoProcessObject(OptimizerSession session)
}
}
}
#endif
9 changes: 9 additions & 0 deletions Editor/OptimizerSession.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.ndmf;
using nadena.dev.ndmf;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
Expand All @@ -15,6 +17,13 @@ internal class OptimizerSession
public bool IsTest { get; }
public ObjectMappingBuilder MappingBuilder { get; }

#if NADEMOFU

public static implicit operator OptimizerSession(BuildContext context)
=> context.Extension<OptimizerContext>().session;

#endif

public OptimizerSession(GameObject rootObject, bool addToAsset, bool isTest) :
this(rootObject, addToAsset ? Utils.CreateAssetFile() : null, isTest)
{
Expand Down
73 changes: 73 additions & 0 deletions Editor/Processors/OptimizerPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using Anatawa12.AvatarOptimizer.ndmf;
using nadena.dev.ndmf;
using nadena.dev.ndmf.builtin;

[assembly: ExportsPlugin(typeof(OptimizerPlugin))]

namespace Anatawa12.AvatarOptimizer.ndmf
{
internal class OptimizerContext : IExtensionContext
{
private IDisposable buildReportScope;
internal OptimizerSession session;

public void OnActivate(BuildContext context)
{
buildReportScope = BuildReport.ReportingOnAvatar(context.AvatarDescriptor);
session = new OptimizerSession(context.AvatarRootObject, context.AssetContainer,
false);
}

public void OnDeactivate(BuildContext context)
{
session.MarkDirtyAll();
buildReportScope.Dispose();
}
}

internal class OptimizerPlugin : Plugin<OptimizerPlugin>
{
public override string DisplayName => "Anatawa12's Avatar Optimizer";

public override string QualifiedName => "com.anatawa12.avatar-optimizer";

protected override void Configure()
{
// Run early steps before EditorOnly objects are purged
InPhase(BuildPhase.Resolving)
.WithRequiredExtension(typeof(OptimizerContext), seq =>
{
seq.Run("Early: UnusedBonesByReference",
ctx => new Processors.UnusedBonesByReferencesToolEarlyProcessor().Process(ctx)
)
.Then.Run("Early: MakeChildren",
ctx => new Processors.MakeChildrenProcessor(early: true).Process(ctx)
)
.BeforePass(RemoveEditorOnlyPass.Instance);
});

// Run everything else in the optimize phase
InPhase(BuildPhase.Optimizing)
.WithRequiredExtension(typeof(OptimizerContext), seq =>
{
seq.Run("TraceAndOptimize",
ctx => new Processors.TraceAndOptimizeProcessor().Process(ctx)
)
.Then.Run("ClearEndpointPosition",
ctx => new Processors.ClearEndpointPositionProcessor().Process(ctx)
)
.Then.Run("MergePhysBone",
ctx => new Processors.MergePhysBoneProcessor().Process(ctx)
)
.Then.Run("MakeChildrenProcessor",
ctx => new Processors.MakeChildrenProcessor(early: false).Process(ctx)
)
.Then.Run("ApplyObjectMapping",
ctx => new Processors.ApplyObjectMapping().Apply(ctx)
);
});
}
}
}
3 changes: 3 additions & 0 deletions Editor/Processors/OptimizerPlugin.cs.meta

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

6 changes: 5 additions & 1 deletion Editor/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
#if !NADEMOFU
using Anatawa12.ApplyOnPlay;
#endif
using JetBrains.Annotations;
using Unity.Collections;
using UnityEditor;
Expand Down Expand Up @@ -321,7 +323,8 @@ public static void DeleteTemporalDirectory()
AssetDatabase.DeleteAsset(TemporalDirPath);
FileUtil.DeleteFileOrDirectory(TemporalDirPath);
}


#if !NADEMOFU
[CanBeNull]
public static DummyObject CreateOutputAssetFile(GameObject avatarGameObject, ApplyReason reason)
{
Expand All @@ -334,6 +337,7 @@ public static DummyObject CreateOutputAssetFile(GameObject avatarGameObject, App
return CreateOutputAssetFile(avatarGameObject);
}
}
#endif

public static DummyObject CreateAssetFile()
{
Expand Down
11 changes: 9 additions & 2 deletions Editor/com.anatawa12.avatar-optimizer.editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"GUID:f69eeb3e25674f4a9bd20e6d7e69e0e6",
"GUID:2633ab9fa94544a69517fc9a1bc143c9",
"GUID:b9880ca0b6584453a2627bd3c038759f",
"GUID:8542dbf824204440a818dbc2377cb4d6"
"GUID:8542dbf824204440a818dbc2377cb4d6",
"GUID:62ced99b048af7f4d8dfe4bed8373d76"
],
"includePlatforms": [
"Editor"
Expand All @@ -29,6 +30,12 @@
],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"versionDefines": [
{
"name": "nadena.dev.ndmf",
"expression": "[0.3.0,999.999.999]",
"define": "NADEMOFU"
}
],
"noEngineReferences": false
}
4 changes: 3 additions & 1 deletion Internal/ApplyOnPlay/Editor/ApplyOnPlayCallbackRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NADEMOFU
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -101,4 +102,5 @@ private static void LogException(string message, params Exception[] exceptions)
}
}
}
}
}
#endif
4 changes: 3 additions & 1 deletion Internal/ApplyOnPlay/Editor/ApplyOnPlayCaller.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NADEMOFU
using System;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -143,4 +144,5 @@ public static void CallManualBakeFinalizer(IManualBakeFinalizer[] finalizers, Ga
}
}
}
}
}
#endif
4 changes: 3 additions & 1 deletion Internal/ApplyOnPlay/Editor/ApplyOnPlayConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NADEMOFU
using System.Linq;
using CustomLocalization4EditorExtension;
using UnityEditor;
Expand Down Expand Up @@ -64,4 +65,5 @@ private void OnGUI()
GUILayout.EndHorizontal();
}
}
}
}
#endif
2 changes: 2 additions & 0 deletions Internal/ApplyOnPlay/Editor/IApplyOnPlayCallback.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NADEMOFU
using UnityEditor.Build;
using UnityEngine;

Expand Down Expand Up @@ -33,3 +34,4 @@ public enum ApplyReason
ManualBake,
}
}
#endif
2 changes: 2 additions & 0 deletions Internal/ApplyOnPlay/Editor/ManualBake.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NADEMOFU
using UnityEditor;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
Expand Down Expand Up @@ -45,3 +46,4 @@ private static void ExecuteManualBake()
}
}
}
#endif
2 changes: 2 additions & 0 deletions Internal/ApplyOnPlay/Editor/ManualBakeFinallizer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NADEMOFU
using UnityEditor.Build;
using UnityEngine;

Expand Down Expand Up @@ -27,3 +28,4 @@ public interface IManualBakeFinalizer : IOrderedCallback
void FinalizeManualBake(GameObject original, GameObject cloned);
}
}
#endif
4 changes: 3 additions & 1 deletion Internal/ApplyOnPlay/Editor/RemoveEditorOnlyOnPlay.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NADEMOFU
using UnityEngine;
using Object = UnityEngine.Object;

Expand All @@ -17,4 +18,5 @@ public bool ApplyOnPlay(GameObject avatarGameObject, ApplyReason reason)
return true;
}
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
"versionDefines": [
{
"name": "nadena.dev.modular-avatar",
"expression": "[1.0.0,2.0.0)",
"expression": "[1.0.0,1.7.99999)",
"define": "MODULAR_AVATAR"
},
{
"name": "nadena.dev.ndmf",
"expression": "[0.3.0,99999.0.0)",
"define": "NADEMOFU"
}
],
"noEngineReferences": false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
using System.Collections.Generic;
using Anatawa12.ApplyOnPlay;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase.Editor.BuildPipeline;
#if !NADEMOFU
using Anatawa12.ApplyOnPlay;
#endif

namespace Anatawa12.AvatarOptimizer.ErrorReporting
{
/// <summary>
/// Initializes Error Reporting System
/// </summary>
internal class ErrorReportingInitializerProcessor : IVRCSDKPreprocessAvatarCallback, IApplyOnPlayCallback, IManualBakeFinalizer
internal class ErrorReportingInitializerProcessor : IVRCSDKPreprocessAvatarCallback
#if !NADEMOFU
, IApplyOnPlayCallback, IManualBakeFinalizer
#endif
{
public int callbackOrder => int.MinValue;

public string CallbackName => "Error Reporting Initialization";
public string CallbackId => "com.anatawa12.error-reporting";

#if !NADEMOFU
public bool ApplyOnPlay(GameObject avatarGameObject, ApplyReason reason) => OnPreprocessAvatar(avatarGameObject);
#endif

public bool OnPreprocessAvatar(GameObject avatarGameObject)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"versionDefines": [
{
"name": "nadena.dev.ndmf",
"expression": "[0.3.0,99999.0.0)",
"define": "NADEMOFU"
}
],
"noEngineReferences": false
}

0 comments on commit 4cfa199

Please sign in to comment.