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 24, 2023
1 parent 12e7f1c commit d743af0
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Support for [NDMF](https://ndmf.nadena.dev) integration [`#375`](https://github.com/anatawa12/AvatarOptimizer/pull/375)

### Changed

Expand Down
3 changes: 2 additions & 1 deletion Editor/OptimizerProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

#if !NADEMOFU
using System;
using System.Linq;
using Anatawa12.ApplyOnPlay;
Expand Down Expand Up @@ -175,3 +175,4 @@ private static void DoProcessObject(OptimizerSession session)
}
}
}
#endif
11 changes: 11 additions & 0 deletions Editor/OptimizerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.Processors;
#if NADEMOFU
using Anatawa12.AvatarOptimizer.ndmf;
using nadena.dev.ndmf;
#endif
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
Expand All @@ -17,6 +21,13 @@ internal class OptimizerSession
public ObjectMappingBuilder MappingBuilder { get; }
public MeshInfo2Holder MeshInfo2Holder { get; private set; } = new MeshInfo2Holder();

#if NADEMOFU
public static implicit operator OptimizerSession(BuildContext context)
{
return context.Extension<OptimizerContext>().session;
}
#endif

public OptimizerSession(GameObject rootObject, bool addToAsset, bool isTest) :
this(rootObject, addToAsset ? Utils.CreateAssetFile() : null, isTest)
{
Expand Down
89 changes: 89 additions & 0 deletions Editor/Processors/OptimizerPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#if NADEMOFU
using System;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using Anatawa12.AvatarOptimizer.ndmf;
using Anatawa12.AvatarOptimizer.Processors;
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, false, 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 =>
{
ctx.GetState<TraceAndOptimizeProcessor>().Process(ctx);
})
.Then.Run("ClearEndpointPosition",
ctx => new Processors.ClearEndpointPositionProcessor().Process(ctx)
)
.Then.Run("MergePhysBone",
ctx => new Processors.MergePhysBoneProcessor().Process(ctx)
)
.Then.Run("EditSkinnedMeshComponent",
ctx => new Processors.EditSkinnedMeshComponentProcessor().Process(ctx)
)
.Then.Run("MakeChildrenProcessor",
ctx => new Processors.MakeChildrenProcessor(early: false).Process(ctx)
)
.Then.Run("TraceAndOptimize:ProcessLater",
ctx => ctx.GetState<TraceAndOptimizeProcessor>().ProcessLater(ctx))
.Then.Run("MergeBoneProcessor", ctx => new Processors.MergeBoneProcessor().Process(ctx))
.Then.Run("ApplyObjectMapping",
ctx => new Processors.ApplyObjectMapping().Apply(ctx)
)
.Then.Run("SaveMeshInfo2", ctx => ((OptimizerSession) ctx).SaveMeshInfo2());
});
}

protected override void OnUnhandledException(Exception e)
{
BuildReport.ReportInternalError(e);
}
}
}
#endif
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.

10 changes: 7 additions & 3 deletions Editor/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
#if !NADEMOFU
using Anatawa12.ApplyOnPlay;
#endif
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -329,17 +331,18 @@ public static GameObject NewGameObject(string name, Transform parent)
rootObject.transform.localScale = Vector3.one;
return rootObject;
}

private const string TemporalDirPath = "Assets/9999-OptimizerGeneratedTemporalAssets";
private const string OutputDirPath = "Assets/AvatarOptimizerOutput";

public static void DeleteTemporalDirectory()
{
AssetDatabase.SaveAssets();
AssetDatabase.DeleteAsset(TemporalDirPath);
FileUtil.DeleteFileOrDirectory(TemporalDirPath);
}

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

public static DummyObject CreateAssetFile()
{
Expand Down Expand Up @@ -391,7 +395,7 @@ private static string GetUniqueFileName(string name, string extension)
if (PathIfNotExists($"{name} ({number}).{extension}") is string otherTry) return otherTry;
}
}

public static IEnumerable<(T, T)> ZipWithNext<T>(this IEnumerable<T> enumerable)
{
using (var enumerator = enumerable.GetEnumerator())
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 @@ -10,7 +10,8 @@
"GUID:2633ab9fa94544a69517fc9a1bc143c9",
"GUID:b9880ca0b6584453a2627bd3c038759f",
"GUID:8542dbf824204440a818dbc2377cb4d6",
"GUID:2665a8d13d1b3f18800f46e256720795"
"GUID:2665a8d13d1b3f18800f46e256720795",
"GUID:62ced99b048af7f4d8dfe4bed8373d76"
],
"includePlatforms": [
"Editor"
Expand All @@ -31,6 +32,12 @@
],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"versionDefines": [
{
"name": "nadena.dev.ndmf",
"expression": "0.4.0",
"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.4.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 d743af0

Please sign in to comment.