Skip to content

Commit

Permalink
Merge pull request #124 from anatawa12/error-reporting-system
Browse files Browse the repository at this point in the history
Error Reporting System
  • Loading branch information
anatawa12 authored May 9, 2023
2 parents 28f19bf + 7437dec commit 71facc8
Show file tree
Hide file tree
Showing 63 changed files with 1,686 additions and 92 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Error Reporting System `#124`
- This adds window shows errors on build
- This is based on Modular Avatar's Error Reporting Window. thanks `@bdunderscore`

### Changed

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Error Reporting System `#124`
- This adds window shows errors on build
- This is based on Modular Avatar's Error Reporting Window. thanks `@bdunderscore`

### Changed

Expand Down
13 changes: 13 additions & 0 deletions Editor/AvatarGlobalComponentEditorBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
using Anatawa12.AvatarOptimizer.ErrorReporting;
using CustomLocalization4EditorExtension;
using UnityEditor;
using UnityEngine;
using VRC.Core;
using VRC.SDK3.Avatars.Components;

namespace Anatawa12.AvatarOptimizer
{
[InitializeOnLoad]
abstract class AvatarGlobalComponentEditorBase : AvatarTagComponentEditorBase
{
static AvatarGlobalComponentEditorBase()
{
ComponentValidation.RegisterValidator<AvatarGlobalComponent>(component =>
{
if (!component.GetComponent<VRCAvatarDescriptor>())
return new[] { ErrorLog.Validation("AvatarGlobalComponent:NotOnAvatarDescriptor") };
return null;
});
}

protected override void OnInspectorGUIInner()
{
if (!((Component)serializedObject.targetObject).GetComponent<VRCAvatarDescriptor>())
Expand Down
137 changes: 136 additions & 1 deletion Editor/MergePhysBoneEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using CustomLocalization4EditorExtension;
using JetBrains.Annotations;
using UnityEditor;
Expand Down Expand Up @@ -371,7 +372,7 @@ protected override void TransformSection() {
EditorGUILayout.LabelField("Multi Child Type", "Must be Ignore");
var multiChildType = _sourcePhysBone.FindProperty("multiChildType");
if (multiChildType.enumValueIndex != 0 || multiChildType.hasMultipleDifferentValues)
EditorGUILayout.HelpBox("Some PysBone has multi child type != Ignore", MessageType.Error);
EditorGUILayout.HelpBox(CL4EE.Tr("MergePhysBone:error:multiChildType"), MessageType.Error);
}
protected override void OptionParameter() {
EditorGUILayout.PropertyField(_mergedPhysBone.FindProperty("parameter"));
Expand Down Expand Up @@ -786,4 +787,138 @@ private static int PopupNoIndent(Rect position, int selectedIndex, string[] disp
return result;
}
}

[InitializeOnLoad]
sealed class MergePhysBoneValidator : MergePhysBoneEditorModificationUtils
{
private readonly List<ErrorLog> _errorLogs;
private readonly List<string> _differProps = new List<string>();
private readonly MergePhysBone _mergePhysBone;

static MergePhysBoneValidator()
{
ComponentValidation.RegisterValidator<MergePhysBone>(Validate);
}

private static List<ErrorLog> Validate(MergePhysBone mergePhysBone)
{
var list = new List<ErrorLog>();
if (mergePhysBone.makeParent && mergePhysBone.transform.childCount != 0)
list.Add(ErrorLog.Validation("MergePhysBone:error:makeParentWithChildren", mergePhysBone));

new MergePhysBoneValidator(list, mergePhysBone).DoProcess();

return list;
}

public MergePhysBoneValidator(List<ErrorLog> errorLogs, MergePhysBone mergePhysBone)
: base(new SerializedObject(mergePhysBone))
{
_errorLogs = errorLogs;
_mergePhysBone = mergePhysBone;
}

private static void Void()
{
}

protected override void BeginPbConfig() => Void();
protected override bool BeginSection(string name, string docTag) => true;
protected override void EndSection() => Void();
protected override void EndPbConfig() {
if (_differProps.Count != 0)
{
_errorLogs.Add(ErrorLog.Validation("MergePhysBone:error:differValues",
new[] { string.Join(", ", _differProps) }));
}
}

protected override void NoSource() =>
_errorLogs.Add(ErrorLog.Validation("MergePhysBone:error:noSources"));

protected override void TransformSection()
{
if (!_mergePhysBone.makeParent)
{
var differ = _sourcePhysBone.targetObjects.Cast<Component>()
.Select(x => x.transform.parent)
.ZipWithNext()
.Any(x => x.Item1 != x.Item2);
if (differ)
_errorLogs.Add(ErrorLog.Validation("MergePhysBone:error:parentDiffer"));
}
var multiChildType = _sourcePhysBone.FindProperty(nameof(VRCPhysBoneBase.multiChildType));
if (multiChildType.enumValueIndex != 0 || multiChildType.hasMultipleDifferentValues)
_errorLogs.Add(ErrorLog.Validation("MergePhysBone:error:multiChildType"));
}

protected override void OptionParameter() => Void();
protected override void OptionIsAnimated() => Void();

protected override void UnsupportedPbVersion() =>
_errorLogs.Add(ErrorLog.Validation("MergePhysBone:error:unsupportedPbVersion"));

protected override void PbVersionProp(string label, string pbPropName, SerializedProperty overridePropName,
params SerializedProperty[] overrides)
=> PbProp(label, pbPropName, overridePropName, overrides);

protected override void PbProp(string label, string pbPropName, SerializedProperty overridePropName,
params SerializedProperty[] overrides) =>
PbPropImpl(label, overridePropName, overrides,
() => _sourcePhysBone.FindProperty(pbPropName).hasMultipleDifferentValues);

protected override void PbCurveProp(string label, string pbPropName, string pbCurvePropName,
SerializedProperty overridePropName,
params SerializedProperty[] overrides) =>
PbPropImpl(label, overridePropName, overrides,
() => _sourcePhysBone.FindProperty(pbPropName).hasMultipleDifferentValues
|| _sourcePhysBone.FindProperty(pbCurvePropName).hasMultipleDifferentValues);

protected override void PbPermissionProp(string label, string pbPropName, string pbFilterPropName,
SerializedProperty overridePropName,
params SerializedProperty[] overrides)
{
PbPropImpl(label, overridePropName, overrides, () =>
{
var sourceValueProp = _sourcePhysBone.FindProperty(pbPropName);
var sourceFilterProp = _sourcePhysBone.FindProperty(pbFilterPropName);
return sourceValueProp.hasMultipleDifferentValues
|| sourceValueProp.enumValueIndex == 2 && sourceFilterProp.hasMultipleDifferentValues;
});
}

protected override void Pb3DCurveProp(string label, string pbPropName, string pbXCurveLabel,
string pbXCurvePropName, string pbYCurveLabel,
string pbYCurvePropName, string pbZCurveLabel, string pbZCurvePropName, SerializedProperty overridePropName,
params SerializedProperty[] overrides) =>
PbPropImpl(label, overridePropName, overrides, () =>
_sourcePhysBone.FindProperty(pbPropName).hasMultipleDifferentValues ||
_sourcePhysBone.FindProperty(pbXCurvePropName).hasMultipleDifferentValues ||
_sourcePhysBone.FindProperty(pbYCurvePropName).hasMultipleDifferentValues ||
_sourcePhysBone.FindProperty(pbZCurvePropName).hasMultipleDifferentValues);

private void PbPropImpl(string label,
SerializedProperty overrideProp,
SerializedProperty[] overrides,
Func<bool> copy)
{
if (overrides.Any(x => x.boolValue) || overrideProp.boolValue) return;

// Copy mode
var differ = copy();

if (differ)
_differProps.Add(label);
}

protected override void ColliderProp(string label, string pbProp, SerializedProperty overrideProp)
{
if (_mergePhysBone.colliders == CollidersSettings.Copy)
{
if (_sourcePhysBone.FindProperty(pbProp).hasMultipleDifferentValues)
_errorLogs.Add(ErrorLog.Validation("MergePhysBone:error:differValue", new[] { label }));
}
}
}

}
12 changes: 12 additions & 0 deletions Editor/MergeSkinnedMeshEditor.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using CustomLocalization4EditorExtension;
using UnityEditor;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer
{
[CustomEditor(typeof(MergeSkinnedMesh))]
[InitializeOnLoad]
internal class MergeSkinnedMeshEditor : AvatarTagComponentEditorBase
{
private static class Style
Expand All @@ -24,6 +26,16 @@ private static class Style
};
}

static MergeSkinnedMeshEditor()
{
ComponentValidation.RegisterValidator<MergeSkinnedMesh>(component =>
{
if (component.GetComponent<SkinnedMeshRenderer>().sharedMesh)
return new[] { ErrorLog.Warning("MergeSkinnedMesh:warning:MeshIsNotNone") };
return null;
});
}

SerializedProperty _renderersSetProp;
SerializedProperty _staticRenderersSetProp;
SerializedProperty _removeEmptyRendererObjectProp;
Expand Down
10 changes: 8 additions & 2 deletions Editor/OptimizerProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

using System;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using UnityEditor;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase.Editor.BuildPipeline;

namespace Anatawa12.AvatarOptimizer
Expand Down Expand Up @@ -49,8 +51,11 @@ private static void ProcessObject(OptimizerSession session)

private static void DoProcessObject(OptimizerSession session)
{
new Processors.UnusedBonesByReferencesToolEarlyProcessor().Process(session);
session.MarkDirtyAll();
using (BuildReport.ReportingOnAvatar(session.GetRootComponent<VRCAvatarDescriptor>()))
{
new Processors.UnusedBonesByReferencesToolEarlyProcessor().Process(session);
session.MarkDirtyAll();
}
}
}

Expand Down Expand Up @@ -83,6 +88,7 @@ public static void ProcessObject(OptimizerSession session)
{
if (_processing) return;
using (Utils.StartEditingScope(true))
using (BuildReport.ReportingOnAvatar(session.GetRootComponent<VRCAvatarDescriptor>()))
{
try
{
Expand Down
5 changes: 4 additions & 1 deletion Editor/Processors/ApplyObjectMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
Expand Down Expand Up @@ -34,7 +35,9 @@ public void Apply(OptimizerSession session)
if (mapper == null)
mapper = new AnimatorControllerMapper(mapping, component.transform, session);

var mapped = mapper.MapAnimatorController(controller);
// ReSharper disable once AccessToModifiedClosure
var mapped = BuildReport.ReportingObject(controller,
() => mapper.MapAnimatorController(controller));
if (mapped != null)
p.objectReferenceValue = mapped;
}
Expand Down
3 changes: 2 additions & 1 deletion Editor/Processors/ClearEndpointPositionProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Anatawa12.AvatarOptimizer.ErrorReporting;
using UnityEditor;
using UnityEngine;
using VRC.Dynamics;
Expand All @@ -9,7 +10,7 @@ internal class ClearEndpointPositionProcessor
public void Process(OptimizerSession session)
{
foreach (var component in session.GetComponents<ClearEndpointPosition>())
Process(component.GetComponent<VRCPhysBoneBase>());
BuildReport.ReportingObject(component, () => Process(component.GetComponent<VRCPhysBoneBase>()));
}

public static void Process(VRCPhysBoneBase pb)
Expand Down
5 changes: 3 additions & 2 deletions Editor/Processors/DeleteGameObjectProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Anatawa12.AvatarOptimizer.ErrorReporting;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer.Processors
Expand All @@ -6,7 +7,7 @@ internal class DeleteGameObjectProcessor
{
public void Process(OptimizerSession session)
{
foreach (var mergePhysBone in session.GetComponents<DeleteGameObject>())
BuildReport.ReportingObjects(session.GetComponents<DeleteGameObject>(), mergePhysBone =>
{
void Destroy(Object obj)
{
Expand All @@ -25,7 +26,7 @@ void Destroy(Object obj)
Destroy(component);
return true;
});
}
});
}
}
}
1 change: 1 addition & 0 deletions Editor/Processors/EditSkinnedMeshComponentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void Process(OptimizerSession session)

foreach (var processor in processors.GetSorted())
{
// TODO
processor.Process(session, target, holder);
target.AssertInvariantContract(
$"after {processor.GetType().Name} " +
Expand Down
6 changes: 4 additions & 2 deletions Editor/Processors/MakeChildrenProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using Anatawa12.AvatarOptimizer.ErrorReporting;

namespace Anatawa12.AvatarOptimizer.Processors
{
internal class MakeChildrenProcessor
{
public void Process(OptimizerSession session)
{
foreach (var makeChildren in session.GetComponents<MakeChildren>())
BuildReport.ReportingObjects(session.GetComponents<MakeChildren>(), makeChildren =>
{
foreach (var makeChildrenChild in makeChildren.children.GetAsSet())
if (makeChildrenChild)
makeChildrenChild.parent = makeChildren.transform;
}
});
}
}
}
5 changes: 3 additions & 2 deletions Editor/Processors/MergeBoneProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.ErrorReporting;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Assertions;
Expand All @@ -24,11 +25,11 @@ public void Process(OptimizerSession session)
// normalize map
mergeMapping.FlattenMapping();

foreach (var renderer in session.GetComponents<SkinnedMeshRenderer>())
BuildReport.ReportingObjects(session.GetComponents<SkinnedMeshRenderer>(), renderer =>
{
if (renderer.bones.Where(x => x).Any(mergeMapping.ContainsKey))
DoBoneMap(session, renderer, mergeMapping);
}
});

foreach (var pair in mergeMapping)
{
Expand Down
Loading

0 comments on commit 71facc8

Please sign in to comment.