Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Dec 16, 2024
1 parent abebecf commit ba3106d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Editor/API/AnimatorServices/ICommitable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;

Expand Down Expand Up @@ -43,6 +44,12 @@ public CommitContext(IPlatformAnimatorBindings platform)
{
_platform = platform;
}

internal IEnumerable<UnityEngine.Object> AllObjects => _commitCache.Values.Select(o =>
{
if (o is UnityEngine.Object unityObj) return unityObj;
return null;
}).Where(o => o != null)!;

[return: NotNullIfNotNull("obj")]
internal R? CommitObject<R>(ICommitable<R>? obj) where R : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
Expand Down Expand Up @@ -97,6 +98,13 @@ public bool IsSpecialMotion(Motion m)

if (!root.TryGetComponent<VRCAvatarDescriptor>(out var vrcAvatarDescriptor)) yield break;

// Initialize the VRChat avatar descriptor. Unfortunately the only way to do this is to run the editor for
// it. Ick.
var editor = Editor.CreateEditor(vrcAvatarDescriptor);
var onEnable = AccessTools.Method(editor.GetType(), "OnEnable");
onEnable?.Invoke(editor, null);
UnityEngine.Object.DestroyImmediate(editor);

// TODO: Fallback layers
foreach (var layer in vrcAvatarDescriptor.baseAnimationLayers)
{
Expand Down Expand Up @@ -135,6 +143,8 @@ IDictionary<object, RuntimeAnimatorController> controllers

GenericPlatformAnimatorBindings.Instance.CommitControllers(root, controllers);

vrcAvatarDescriptor.customizeAnimationLayers = true;

void EditLayers(VRCAvatarDescriptor.CustomAnimLayer[] layers)
{
for (var i = 0; i < layers.Length; i++)
Expand Down
13 changes: 11 additions & 2 deletions Editor/API/AnimatorServices/VirtualControllerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,17 @@ public void OnDeactivate(BuildContext context)
commitContext.ActiveInnateLayerKey = kv.Key;
return (RuntimeAnimatorController)commitContext.CommitObject(kv.Value.VirtualController!);
});

_platformBindings!.CommitControllers(root, controllers);

using (var scope = context.OpenSerializationScope())
{
_platformBindings!.CommitControllers(root, controllers);

// Save all animator objects to prevent references from breaking later
foreach (var obj in commitContext.AllObjects)
{
context.AssetSaver.SaveAsset(obj);
}
}
}

public IEnumerable<VirtualAnimatorController> GetAllControllers()
Expand Down
9 changes: 5 additions & 4 deletions Editor/API/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling;
using VRC.SDK3.Avatars.Components;
using Debug = UnityEngine.Debug;
using UnityObject = UnityEngine.Object;

Expand Down Expand Up @@ -378,16 +379,16 @@ public void DeactivateAllExtensionContexts()
}
}

public T ActivateExtensionContextRecurse<T>() where T : IExtensionContext
public T ActivateExtensionContextRecursive<T>() where T : IExtensionContext
{
return (T) ActivateExtensionContextRecurse(typeof(T));
return (T) ActivateExtensionContextRecursive(typeof(T));
}

public IExtensionContext ActivateExtensionContextRecurse(Type ty)
public IExtensionContext ActivateExtensionContextRecursive(Type ty)
{
foreach (var dependency in ty.ContextDependencies())
{
ActivateExtensionContextRecurse(dependency);
ActivateExtensionContextRecursive(dependency);
}

return ActivateExtensionContext(ty);
Expand Down

0 comments on commit ba3106d

Please sign in to comment.