Skip to content

Commit

Permalink
Merge pull request #805 from anatawa12/ndmf-error-report
Browse files Browse the repository at this point in the history
Migrate to NDMF ErrorReprting API
  • Loading branch information
anatawa12 authored Dec 28, 2023
2 parents 39beba8 + 513382d commit 632e54a
Show file tree
Hide file tree
Showing 76 changed files with 366 additions and 1,756 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- AvatarOptimizer now uses ErrorReporting API of NDMF instead of our own API `#805`

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- AvatarOptimizer now uses ErrorReporting API of NDMF instead of our own API `#805`

### Deprecated

Expand Down
37 changes: 17 additions & 20 deletions Editor/APIInternal/ComponentInfos.VRCSDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.API;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using JetBrains.Annotations;
using UnityEngine;
using VRC.SDK3;
Expand Down Expand Up @@ -62,8 +61,7 @@ protected override void CollectMutations(T component, ComponentMutationsCollecto
collector.ModifyProperties(component.VisemeSkinnedMesh,
$"blendShape.{component.MouthOpenBlendShapeName}");
} else {
BuildReport.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoVisemeSkinnedMesh")
?.WithContext(component);
BuildLog.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoVisemeSkinnedMesh", component);
}
break;
}
Expand All @@ -74,18 +72,17 @@ protected override void CollectMutations(T component, ComponentMutationsCollecto
collector.ModifyProperties(component.VisemeSkinnedMesh,
component.VisemeBlendShapes.Select(blendShape => $"blendShape.{blendShape}"));
} else {
BuildReport.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoVisemeSkinnedMesh")
?.WithContext(component);
BuildLog.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoVisemeSkinnedMesh", component);
}
break;
}
case VRC_AvatarDescriptor.LipSyncStyle.VisemeParameterOnly:
// NOP
break;
default:
BuildReport.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:UnknownLipSyncStyle",
component.lipSync.ToString())
?.WithContext(component);
BuildLog.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:UnknownLipSyncStyle",
component.lipSync.ToString(),
component);
break;
}
}
Expand Down Expand Up @@ -130,7 +127,7 @@ protected override void ApplySpecialMapping(T component, MappingSource mappingSo
removed = true;
}
if (removed)
BuildReport.LogFatal("ApplyObjectMapping:VRCAvatarDescriptor:viseme BlendShape Removed");
BuildLog.LogError("ApplyObjectMapping:VRCAvatarDescriptor:viseme BlendShape Removed");
break;
}
case VRC_AvatarDescriptor.LipSyncStyle.VisemeParameterOnly:
Expand Down Expand Up @@ -182,9 +179,9 @@ void AddCollider(VRCAvatarDescriptor.ColliderConfig collider, string where)
case VRCAvatarDescriptor.ColliderConfig.State.Disabled:
break;
default:
BuildReport.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:UnknownColliderState",
collider.ToString(), where)
?.WithContext(component);
BuildLog.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:UnknownColliderState",
collider.ToString(), where,
component);
break;
}
}
Expand Down Expand Up @@ -234,21 +231,21 @@ from index in component.customEyeLookSettings.eyelidsBlendshapes
}
else
{
BuildReport.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoMeshInEyelidsSkinnedMesh")
?.WithContext(component);
BuildLog.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoMeshInEyelidsSkinnedMesh",
component);
}
}
else
{
BuildReport.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoEyelidsSkinnedMesh")
?.WithContext(component);
BuildLog.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:NoEyelidsSkinnedMesh",
component);
}
}
break;
default:
BuildReport.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:UnknownEyelidType",
component.customEyeLookSettings.eyelidType.ToString())
?.WithContext(component);
BuildLog.LogWarning("ComponentInfos:VRCAvatarDescriptor:warning:UnknownEyelidType",
component.customEyeLookSettings.eyelidType.ToString(),
component);
break;
}
}
Expand Down Expand Up @@ -282,7 +279,7 @@ protected override void ApplySpecialMapping(VRCAvatarDescriptor component, Mappi
}

if (removed)
BuildReport.LogFatal("ApplyObjectMapping:VRCAvatarDescriptor:eyelids BlendShape Removed");
BuildLog.LogError("ApplyObjectMapping:VRCAvatarDescriptor:eyelids BlendShape Removed");
}
break;
default:
Expand Down
3 changes: 1 addition & 2 deletions Editor/APIInternal/ComponentInfos.VRM0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Linq;
using Anatawa12.AvatarOptimizer.API;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using UnityEngine;
using VRM;

Expand Down Expand Up @@ -152,7 +151,7 @@ protected override void ApplySpecialMapping(VRMFirstPerson component, MappingSou
else
{
mergedFirstPersonFlag = firstPersonFlags.Contains(FirstPersonFlag.Both) ? FirstPersonFlag.Both : FirstPersonFlag.Auto;
BuildReport.LogWarning("MergeSkinnedMesh:warning:VRM:FirstPersonFlagsMismatch", mergedFirstPersonFlag.ToString());
BuildLog.LogWarning("MergeSkinnedMesh:warning:VRM:FirstPersonFlagsMismatch", mergedFirstPersonFlag.ToString());
}

return new VRMFirstPerson.RendererFirstPersonFlags
Expand Down
11 changes: 7 additions & 4 deletions Editor/AnimatorParsers/AnimationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using nadena.dev.ndmf;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using static Anatawa12.AvatarOptimizer.ErrorReporting.BuildReport;

namespace Anatawa12.AvatarOptimizer.AnimatorParsers
{
class AnimationParser
{
internal IModificationsContainer ParseMotion(GameObject root, Motion motion,
IReadOnlyDictionary<AnimationClip, AnimationClip> mapping) =>
ReportingObject(motion, () => ParseMotionInner(root, motion, mapping));
IReadOnlyDictionary<AnimationClip, AnimationClip> mapping)
{
using (ErrorReport.WithContextObject(motion))
return ParseMotionInner(root, motion, mapping);
}

private IModificationsContainer ParseMotionInner(GameObject root, Motion motion,
IReadOnlyDictionary<AnimationClip, AnimationClip> mapping)
Expand All @@ -27,7 +30,7 @@ private IModificationsContainer ParseMotionInner(GameObject root, Motion motion,
case BlendTree blendTree:
return ParseBlendTree(root, blendTree, mapping);
default:
LogFatal("Unknown Motion Type: {0} in motion {1}", motion.GetType().Name, motion.name);
BuildLog.LogError("Unknown Motion Type: {0} in motion {1}", motion.GetType().Name, motion.name);
return ImmutableModificationsContainer.Empty;
}
}
Expand Down
39 changes: 19 additions & 20 deletions Editor/AnimatorParsers/AnimatorParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
using System.Linq;
using Anatawa12.AvatarOptimizer.API;
using Anatawa12.AvatarOptimizer.APIInternal;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using static Anatawa12.AvatarOptimizer.ErrorReporting.BuildReport;
using JetBrains.Annotations;
using nadena.dev.ndmf;
using UnityEditor.Animations;
using UnityEngine;
using UnityEngine.Animations;
using Object = UnityEngine.Object;

#if AAO_VRCSDK3_AVATARS
using VRC.Dynamics;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase;
#endif
Expand Down Expand Up @@ -150,12 +145,16 @@ public override void ModifyProperties(Component component, IEnumerable<string> p
private static void OtherMutateComponents(ModificationsContainer mod, BuildContext context)
{
var collector = new Collector(mod);
ReportingObjects(context.GetComponents<Component>(), component =>
foreach (var component in context.GetComponents<Component>())
{
collector.Modifier = new ComponentAnimationSource(component);
if (ComponentInfoRegistry.TryGetInformation(component.GetType(), out var info))
info.CollectMutationsInternal(component, collector);
});
using (ErrorReport.WithContextObject(component))
{
collector.Modifier = new ComponentAnimationSource(component);
if (ComponentInfoRegistry.TryGetInformation(component.GetType(), out var info))
info.CollectMutationsInternal(component, collector);

}
}
}

#endregion
Expand Down Expand Up @@ -273,7 +272,7 @@ private void CollectWeightChangesInController(RuntimeAnimatorController runtimeC
AnimatorLayerMap<AnimatorWeightState> playableWeightChanged,
AnimatorLayerMap<AnimatorLayerWeightMap<int>> animatorLayerWeightChanged)
{
ReportingObject(runtimeController, () =>
using (ErrorReport.WithContextObject(runtimeController))
{
var (controller, _) = GetControllerAndOverrides(runtimeController);

Expand All @@ -287,7 +286,7 @@ private void CollectWeightChangesInController(RuntimeAnimatorController runtimeC
.stateMachine))
CollectWeightChangesInBehaviors(layer.GetOverrideBehaviours(state));
}
});
}

return;

Expand Down Expand Up @@ -315,9 +314,9 @@ void CollectWeightChangesInBehaviors(StateMachineBehaviour[] stateBehaviours)
layer = VRCAvatarDescriptor.AnimLayerType.Additive;
break;
default:
LogWarning("AnimatorParser:PlayableLayerControl:UnknownBlendablePlayableLayer",
$"{playableLayerControl.layer}")
?.WithContext(stateMachineBehaviour);
BuildLog.LogWarning("AnimatorParser:PlayableLayerControl:UnknownBlendablePlayableLayer",
$"{playableLayerControl.layer}",
stateMachineBehaviour);
continue;
}

Expand All @@ -344,9 +343,9 @@ void CollectWeightChangesInBehaviors(StateMachineBehaviour[] stateBehaviours)
layer = VRCAvatarDescriptor.AnimLayerType.Additive;
break;
default:
LogWarning("AnimatorParser:AnimatorLayerControl:UnknownBlendablePlayableLayer",
$"{animatorLayerControl.layer}")
?.WithContext(stateMachineBehaviour);
BuildLog.LogWarning("AnimatorParser:AnimatorLayerControl:UnknownBlendablePlayableLayer",
$"{animatorLayerControl.layer}",
stateMachineBehaviour);
continue;
}

Expand Down Expand Up @@ -439,12 +438,12 @@ private IModificationsContainer AddHumanoidModifications(IModificationsContainer
public IModificationsContainer ParseAnimatorController(GameObject root, RuntimeAnimatorController controller,
[CanBeNull] AnimatorLayerWeightMap<int> externallyWeightChanged = null)
{
return ReportingObject(controller, () =>
using (ErrorReport.WithContextObject(controller))
{
var (animatorController, mapping) = GetControllerAndOverrides(controller);
return AdvancedParseAnimatorController(root, animatorController, mapping,
externallyWeightChanged);
});
}
}

internal IModificationsContainer AdvancedParseAnimatorController(GameObject root, AnimatorController controller,
Expand Down
1 change: 0 additions & 1 deletion Editor/AnimatorParsers/ModificationSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Anatawa12.AvatarOptimizer.ErrorReporting;
using UnityEditor;
using UnityEngine;

Expand Down
1 change: 1 addition & 0 deletions Editor/AnimatorParsers/ModificationsContainers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public bool IsConst

private readonly IModificationSource[] _sources;
public ReadOnlySpan<IModificationSource> Sources => _sources ?? Array.Empty<IModificationSource>();
public IEnumerable<IModificationSource> SourcesEnum => _sources ?? Array.Empty<IModificationSource>();

private AnimationFloatProperty(PropertyState state, float constValue, params IModificationSource[] modifiers) =>
(State, _constValue, _sources) = (state, constValue, modifiers);
Expand Down
17 changes: 1 addition & 16 deletions Editor/AvatarGlobalComponentEditorBase.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
using Anatawa12.AvatarOptimizer.ErrorReporting;
using CustomLocalization4EditorExtension;
using UnityEditor;
using UnityEngine;

#if AAO_VRCSDK3_AVATARS
using VRC.Core;
using VRC.SDK3.Avatars.Components;
#endif

namespace Anatawa12.AvatarOptimizer
{
[InitializeOnLoad]
abstract class AvatarGlobalComponentEditorBase : AvatarTagComponentEditorBase
{
static AvatarGlobalComponentEditorBase()
{
ComponentValidation.RegisterValidator<AvatarGlobalComponent>(component =>
{
#if AAO_VRCSDK3_AVATARS
if (!component.GetComponent<VRCAvatarDescriptor>())
return new[] { ErrorLog.Validation("AvatarGlobalComponent:NotOnAvatarDescriptor") };
#else
if (!nadena.dev.ndmf.runtime.RuntimeUtil.IsAvatarRoot(component.transform))
return new[] { ErrorLog.Validation("AvatarGlobalComponent:NotOnAvatarRoot") };
#endif
return null;
});
}
protected override void OnInspectorGUIInner()
{
#if AAO_VRCSDK3_AVATARS
Expand Down
80 changes: 80 additions & 0 deletions Editor/BuildLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using CustomLocalization4EditorExtension;
using nadena.dev.ndmf;
using nadena.dev.ndmf.localization;
using Debug = System.Diagnostics.Debug;
using Object = UnityEngine.Object;

namespace Anatawa12.AvatarOptimizer
{
internal static class BuildLog
{
public static void LogInfo(string code, params object[] args) =>
ErrorReport.ReportError(new InlineError(ErrorSeverity.Information, code, args));

public static void LogWarning(string code, params object[] args) =>
ErrorReport.ReportError(new InlineError(ErrorSeverity.NonFatal, code, args));

public static void LogError(string code, params object[] args) =>
ErrorReport.ReportError(new InlineError(ErrorSeverity.Error, code, args));
}

internal class InlineError : SimpleError, IError
{
private static Localizer _localizer = new Localizer("en-US", () =>
{
var localization = CL4EE.GetLocalization();
Debug.Assert(localization != null, nameof(localization) + " != null");
return localization.LocalizationByIsoCode.Values
.Select(locale => (locale.LocaleIsoCode, (Func<string, string>)locale.TryGetLocalizedString))
.ToList();
});

private readonly string _key;

public InlineError(ErrorSeverity errorSeverity, string key, params object[] args)
{
Localizer = _localizer;
Severity = errorSeverity;
// https://github.com/bdunderscore/ndmf/issues/99
// https://github.com/bdunderscore/ndmf/issues/98
_key = key;

DetailsSubst = Array.ConvertAll(args, o => o?.ToString());
Flatten(args, _references);
}

protected override string DetailsKey => _key;

private static void Flatten(object arg, List<ObjectReference> list)
{
// https://github.com/bdunderscore/ndmf/issues/95
// https://github.com/bdunderscore/ndmf/issues/96
if (arg is ObjectReference or)
list.Add(or);
else if (arg is Object uo)
list.Add(ObjectRegistry.GetReference(uo));
else if (arg is IContextProvider provider)
Flatten(provider.ProvideContext(), list);
else if (arg is IEnumerable enumerable)
foreach (var value in enumerable)
Flatten(value, list);
}

protected override Localizer Localizer { get; }
public override ErrorSeverity Severity { get; }
protected override string TitleKey { get; }

protected override string[] DetailsSubst { get; }

protected override string[] HintSubst => DetailsSubst;
}

internal interface IContextProvider
{
object ProvideContext();
}
}
File renamed without changes.
Loading

0 comments on commit 632e54a

Please sign in to comment.