Skip to content

Commit

Permalink
Merge pull request #1148 from anatawa12/refactor-for-1.8
Browse files Browse the repository at this point in the history
Refactor Avatar Optimizer with new features in 2022
  • Loading branch information
anatawa12 authored Aug 20, 2024
2 parents 0c473a6 + 41db9da commit 629b553
Show file tree
Hide file tree
Showing 142 changed files with 1,006 additions and 1,244 deletions.
5 changes: 5 additions & 0 deletions .csc.rsp.nullsafe
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#"Shared csc.rsp file with null safe. origin is at .csc.rsp.nullsafe and symlinked to each assemblies of AAO"
-langversion:10
#"PUBLISH DELETE MARKER // lines below are only for development to reduce unexpected compile errors"
-warnaserror:nullable
-nullable:enable
4 changes: 4 additions & 0 deletions .csc.rsp.nullunsafe
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#"Shared csc.rsp file with null unsafe. origin is at .csc.rsp.nullunsafe and symlinked to each assemblies of AAO"
-langversion:10
#"PUBLISH DELETE MARKER // lines below are only for development to reduce unexpected compile errors"
-warnaserror:nullable
5 changes: 5 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ jobs:
sed -i'.bak' -e "s|${STABLE_URL}|${STABLE_URL}#${VERSION}|" package.json
fi
rm package.json.bak
- name: update csc.rsp
run: |
sed -n '/PUBLISH DELETE MARKER/q;p' -i .csc.rsp.nullunsafe
sed -n '/PUBLISH DELETE MARKER/q;p' -i .csc.rsp.nullsafe
# region vpm release
- name: Build VPM release
Expand Down
18 changes: 9 additions & 9 deletions API-Editor/ComponentInformation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

using System;
using System.Collections.Generic;
using JetBrains.Annotations;
Expand Down Expand Up @@ -30,7 +32,7 @@ public sealed class ComponentInformationAttribute : APIInternal.ComponentInforma
[PublicAPI]
public ComponentInformationAttribute(Type targetType)
{
TargetType = targetType;
TargetType = targetType ?? throw new ArgumentNullException(nameof(targetType));
}

internal override Type GetTargetType() => TargetType;
Expand Down Expand Up @@ -167,7 +169,7 @@ internal ComponentDependencyCollector()
/// <param name="dependency">The dependency</param>
/// <returns>The object to configure the dependency</returns>
[PublicAPI]
public abstract ComponentDependencyInfo AddDependency(Component dependant, Component dependency);
public abstract ComponentDependencyInfo AddDependency(Component? dependant, Component? dependency);

/// <summary>
/// Adds <see cref="dependency"/> as dependencies of current component.
Expand All @@ -178,7 +180,7 @@ internal ComponentDependencyCollector()
/// <param name="dependency">The dependency</param>
/// <returns>The object to configure the dependency</returns>
[PublicAPI]
public abstract ComponentDependencyInfo AddDependency(Component dependency);
public abstract ComponentDependencyInfo AddDependency(Component? dependency);

/// <summary>
/// Adds relative path from <see cref="root"/> to <see cref="dependency"/> as dependencies of current component.
Expand Down Expand Up @@ -223,7 +225,7 @@ internal ComponentDependencyCollector()
/// <param name="root">The GameObject the relative path starts from</param>
/// <exception cref="ArgumentException">If the <see cref="dependency"/> is not child of <see cref="root"/>.</exception>
[PublicAPI]
public abstract PathDependencyInfo AddPathDependency([NotNull] Transform dependency, [NotNull] Transform root);
public abstract PathDependencyInfo AddPathDependency(Transform dependency, Transform root);

// TODO: rename to better name and make public
// NOTE for external users: this is API Proposal to compute value of animatable bool property
Expand Down Expand Up @@ -309,13 +311,11 @@ internal ComponentMutationsCollector()
/// <param name="component">The component current component will modifies</param>
/// <param name="properties">The list of properties current component will modifies</param>
[PublicAPI]
public abstract void ModifyProperties([NotNull] Component component,
[NotNull] [ItemNotNull] IEnumerable<string> properties);
public abstract void ModifyProperties(Component component, IEnumerable<string> properties);

/// <inheritdoc cref="ModifyProperties(UnityEngine.Component,System.Collections.Generic.IEnumerable{string})"/>
[PublicAPI]
public void ModifyProperties([NotNull] Component component,
[NotNull] [ItemNotNull] params string[] properties) =>
public void ModifyProperties(Component component, params string[] properties) =>
ModifyProperties(component, (IEnumerable<string>)properties);
}

Expand Down Expand Up @@ -370,7 +370,7 @@ internal MappedComponentInfo()
/// to animation property, for example, blendShape related SkinnedMeshRenderer.
/// </summary>
[PublicAPI]
public abstract T MappedComponent { get; }
public abstract T? MappedComponent { get; }

/// <summary>
/// Maps animation property name to component and MappedPropertyInfo.
Expand Down
7 changes: 4 additions & 3 deletions API-Editor/Internal/InternalParts.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#nullable enable

using System;
using Anatawa12.AvatarOptimizer.API;
using JetBrains.Annotations;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer.APIInternal
{
public abstract class ComponentInformationAttributeBase : Attribute
{
internal ComponentInformationAttributeBase() { }
[CanBeNull] internal abstract Type GetTargetType();
internal abstract Type? GetTargetType();
}

/// <summary>
Expand All @@ -33,4 +34,4 @@ internal ComponentInformation()
internal class AllowInheritAttribute : Attribute
{
}
}
}
11 changes: 6 additions & 5 deletions Editor/.MergePhysBoneEditorModificationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const config: Config = {
} satisfies Config;

console.log("// <generated />")
console.log("// generated by .MergePhysBoneEditorModificationUtils.ts")
console.log("#nullable enable")
console.log("")
console.log("#if AAO_VRCSDK3_AVATARS")
console.log("")
console.log("using System.Collections.Generic;")
Expand All @@ -59,14 +60,14 @@ for (let [type, info] of Object.entries(config)) {
console.log(` {`)
for (let [value] of info.values) {
console.log(` public readonly SerializedProperty Override${value};`)
console.log(` public SerializedProperty Source${value} { get; private set; }`)
console.log(` public SerializedProperty? Source${value} { get; private set; }`)
console.log(` public readonly string PhysBone${value}Name;`)
}
console.log(``)
console.log(` public ${type}(`)
console.log(` [NotNull] SerializedProperty rootProperty`)
console.log(` SerializedProperty rootProperty`)
for (let [value] of info.values) {
console.log(` , [NotNull] string physBone${value}Name`)
console.log(` , string physBone${value}Name`)
}
console.log(` ) : base(rootProperty)`)
console.log(` {`)
Expand All @@ -83,7 +84,7 @@ for (let [type, info] of Object.entries(config)) {
}
console.log(` }`)
for (let [value] of info.values) {
console.log(` public SerializedProperty Get${value}Property(bool @override) => @override ? Override${value} : Source${value};`)
console.log(` public SerializedProperty Get${value}Property(bool @override) => @override ? Override${value} : Source${value}!;`)
}
console.log(` }`)
}
Expand Down
7 changes: 3 additions & 4 deletions Editor/APIInternal/ComponentInfoRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace Anatawa12.AvatarOptimizer.APIInternal
{
internal static class ComponentInfoRegistry
{
private static readonly Dictionary<Type, ComponentInformation> InformationByType =
new Dictionary<Type, ComponentInformation>();
private static readonly Dictionary<Type, ComponentInformation> InformationByType = new();

[InitializeOnLoadMethod]
static void FindAllInfoImplements()
Expand Down Expand Up @@ -107,7 +106,7 @@ internal static bool TryGetInformation(Type type, out ComponentInformation infor
return false;
}

private static HashSet<Type> _meaninglessTypes;
private static HashSet<Type>? _meaninglessTypes;

public static void InvalidateCache() => _meaninglessTypes = null;

Expand All @@ -128,7 +127,7 @@ private static bool IsMeaninglessType(Type type)

class MeaninglessComponentInformation : ComponentInformation
{
public static MeaninglessComponentInformation Instance { get; } = new MeaninglessComponentInformation();
public static MeaninglessComponentInformation Instance { get; } = new();

private MeaninglessComponentInformation()
{
Expand Down
6 changes: 3 additions & 3 deletions Editor/APIInternal/ComponentInformationWithGUIDAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public ComponentInformationWithGUIDAttribute(string guid, int fileID)
FileID = fileID;
}

internal override Type GetTargetType()
internal override Type? GetTargetType()
{
if (!GlobalObjectId.TryParse($"GlobalObjectId_V1-{1}-{Guid}-{(uint)FileID}-{0}", out var id)) return null;
var script = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id) as MonoScript;
if (!script) return null;
if (script == null) return null;
return script.GetClass();
}
}
}
}
2 changes: 1 addition & 1 deletion Editor/APIInternal/ComponentInfos.DynamicBone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ protected override void CollectDependency(Component component, ComponentDependen
{
}
}
}
}
11 changes: 0 additions & 11 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 JetBrains.Annotations;
using nadena.dev.ndmf.runtime;
using UnityEngine;
using VRC.SDK3;
Expand All @@ -14,10 +13,8 @@
using VRC.SDK3.Dynamics.PhysBone.Components;
using VRC.SDKBase;

#if AAO_VRCSDK3_AVATARS_CONSTRAINTS
using VRC.Dynamics.ManagedTypes;
using VRC.SDK3.Dynamics.Constraint.Components;
#endif

namespace Anatawa12.AvatarOptimizer.APIInternal.VRCSDK
{
Expand Down Expand Up @@ -162,7 +159,6 @@ protected override void ApplySpecialMapping(T component, MappingSource mappingSo
}
}

[NotNull]
private static string ParseBlendShapeProperty(string prop) =>
prop.StartsWith("blendShape.", StringComparison.Ordinal)
? prop.Substring("blendShape.".Length)
Expand Down Expand Up @@ -472,7 +468,6 @@ protected override void CollectDependency(VRCImpostorSettings component, Compone
}
}

#if AAO_VRCSDK3_AVATARS_IMPOSTER_SETTINGS
// this component has no documentation so this implementation is based on assumption
[ComponentInformation(typeof(VRCImpostorEnvironment))]
internal class VRCImpostorEnvironmentInformation : ComponentInformation<VRCImpostorEnvironment>
Expand All @@ -483,9 +478,7 @@ protected override void CollectDependency(VRCImpostorEnvironment component, Comp
collector.MarkEntrypoint();
}
}
#endif

#if AAO_VRCSDK3_AVATARS_HEAD_CHOP
[ComponentInformation(typeof(VRCHeadChop))]
internal class VRCHeadChopInformation : ComponentInformation<VRCHeadChop>
{
Expand All @@ -501,9 +494,7 @@ protected override void CollectDependency(VRCHeadChop component, ComponentDepend
collector.MarkBehaviour();
}
}
#endif

#if AAO_VRCSDK3_AVATARS_CONSTRAINTS
[ComponentInformation(typeof(VRCConstraintBase))]
[ComponentInformation(typeof(VRCParentConstraintBase))]
[ComponentInformation(typeof(VRCParentConstraint))]
Expand Down Expand Up @@ -548,7 +539,5 @@ protected override void CollectDependency(VRCWorldUpConstraintBase component, Co
collector.AddDependency(component.WorldUpTransform);
}
}

#endif
}
#endif
2 changes: 1 addition & 1 deletion Editor/APIInternal/ComponentInfos.VRM0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ protected override void ApplySpecialMapping(VRMFirstPerson component, MappingSou
}
}

#endif
#endif
8 changes: 4 additions & 4 deletions Editor/APIInternal/ComponentInfos.VRM1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ protected override void CollectMutations(Vrm10Instance component, ComponentMutat
}
}

Transform GetBoneTransformForVrm10(Vrm10Instance component, HumanBodyBones bones)
Transform? GetBoneTransformForVrm10(Vrm10Instance component, HumanBodyBones bones)
{
if (component.GetComponent<Humanoid>() is Humanoid avatarHumanoid)
if (component.GetComponent<Humanoid>() is { } avatarHumanoid)
{
return avatarHumanoid.GetBoneTransform(bones);
}

if (component.GetComponent<Animator>() is Animator avatarAnimator)
if (component.GetComponent<Animator>() is { } avatarAnimator)
{
return avatarAnimator.GetBoneTransform(bones);
}
Expand Down Expand Up @@ -180,4 +180,4 @@ protected override void CollectDependency(Humanoid component, ComponentDependenc

}

#endif
#endif
8 changes: 0 additions & 8 deletions Editor/APIInternal/ComponentInfos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,7 @@ protected override void CollectDependency(ParticleSystem component, ComponentDep
switch (component.collision.type) // not animated
{
case ParticleSystemCollisionType.Planes:
#if UNITY_2020_2_OR_NEWER
for (var i = 0; i < component.collision.planeCount; i++)
#else
for (var i = 0; i < component.collision.maxPlaneCount; i++)
#endif
collector.AddDependency(component.collision.GetPlane(i));
break;
case ParticleSystemCollisionType.World:
Expand All @@ -193,11 +189,7 @@ protected override void CollectDependency(ParticleSystem component, ComponentDep

if (collector.GetAnimatedFlag(component, "TriggerModule.enabled", component.trigger.enabled) != false)
{
#if UNITY_2020_2_OR_NEWER
for (var i = 0; i < component.trigger.colliderCount; i++)
#else
for (var i = 0; i < component.trigger.maxColliderCount; i++)
#endif
{
var collider = component.trigger.GetCollider(i);
if (!collider) continue;
Expand Down
18 changes: 8 additions & 10 deletions Editor/AnimatorParserV2/AnimationParser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using nadena.dev.ndmf;
using UnityEditor;
using UnityEditor.Animations;
Expand All @@ -12,15 +11,15 @@ namespace Anatawa12.AvatarOptimizer.AnimatorParsersV2
{
class AnimationParser
{
internal ImmutableNodeContainer ParseMotion([NotNull] GameObject root, [CanBeNull] Motion motion,
[NotNull] IReadOnlyDictionary<AnimationClip, AnimationClip> mapping)
internal ImmutableNodeContainer ParseMotion(GameObject root, Motion? motion,
IReadOnlyDictionary<AnimationClip, AnimationClip> mapping)
{
using (ErrorReport.WithContextObject(motion))
return ParseMotionInner(root, motion, mapping);
}

private ImmutableNodeContainer ParseMotionInner([NotNull] GameObject root, [CanBeNull] Motion motion,
[NotNull] IReadOnlyDictionary<AnimationClip, AnimationClip> mapping)
private ImmutableNodeContainer ParseMotionInner(GameObject root, Motion? motion,
IReadOnlyDictionary<AnimationClip, AnimationClip> mapping)
{
switch (motion)
{
Expand Down Expand Up @@ -74,7 +73,7 @@ public BlendTreeMergeProperty(BlendTreeType blendType)
_blendType = blendType;
}

public ImmutableNodeContainer CreateContainer() => new ImmutableNodeContainer();
public ImmutableNodeContainer CreateContainer() => new();
public ImmutableNodeContainer GetContainer(ImmutableNodeContainer source) => source;

public BlendTreeElement<float> GetIntermediate(ImmutableNodeContainer source,
Expand All @@ -90,18 +89,17 @@ public ImmutablePropModNode<Object> MergeNode(List<BlendTreeElement<Object>> nod
new BlendTreeNode<Object>(nodes, _blendType, partial: nodes.Count != sourceCount);
}

[NotNull] private readonly Dictionary<(GameObject, AnimationClip), ImmutableNodeContainer> _parsedAnimationCache =
new Dictionary<(GameObject, AnimationClip), ImmutableNodeContainer>();
private readonly Dictionary<(GameObject, AnimationClip), ImmutableNodeContainer> _parsedAnimationCache = new();

internal ImmutableNodeContainer GetParsedAnimation([NotNull] GameObject root, [CanBeNull] AnimationClip clip)
internal ImmutableNodeContainer GetParsedAnimation(GameObject root, AnimationClip? clip)
{
if (clip == null) return new ImmutableNodeContainer();
if (!_parsedAnimationCache.TryGetValue((root, clip), out var parsed))
_parsedAnimationCache.Add((root, clip), parsed = ParseAnimation(root, clip));
return parsed;
}

public static ImmutableNodeContainer ParseAnimation([NotNull] GameObject root, [NotNull] AnimationClip clip)
public static ImmutableNodeContainer ParseAnimation(GameObject root, AnimationClip clip)
{
var nodes = new ImmutableNodeContainer();

Expand Down
Loading

0 comments on commit 629b553

Please sign in to comment.