Skip to content

Commit

Permalink
fix: Fix to ensure upload doesn't interrupt if TrackingControlDisable…
Browse files Browse the repository at this point in the history
…r execution fails (#25)
  • Loading branch information
suzuryg committed Aug 24, 2023
1 parent 5b35f87 commit 7c0feb1
Showing 1 changed file with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Suzuryg.FaceEmo.Domain;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase;
Expand All @@ -15,7 +17,8 @@ namespace Suzuryg.FaceEmo.Components.MA
public class TrackingControlDisabler : RunBeforeModularAvatar
{
// TODO: Is asset creation needed?
private static readonly string DestinationPath = $"Assets/Suzuryg/{DomainConstants.SystemName}/Generated/TrackingControlDisabled.controller";
private static readonly string DestinationDir = $"Assets/Suzuryg/{DomainConstants.SystemName}/Generated";
private static readonly string DestinationPath = DestinationDir + "/TrackingControlDisabled.controller";

public override void OnPreProcessAvatar()
{
Expand Down Expand Up @@ -45,11 +48,12 @@ public override void OnPreProcessAvatar()
// TODO: Is asset creation needed?
// VRC_AnimatorTrackingControl is not references to the AnimatorController but values held in the AnimatorController,
// so if the AnimatorController is copied, changing the destination asset will not affect the original AnimatorController.
if (!AssetDatabase.IsValidFolder(DestinationDir))
{ CreateFolder(); }

var originalPath = AssetDatabase.GetAssetPath(originalFxController);
if (!AssetDatabase.CopyAsset(originalPath, DestinationPath)) // TODO: Suppress warnings (missing script)
{
throw new FaceEmoException("Faild to clone Fx controller.");
}
{ ShowErrorMessage(originalPath); }
var clonedFxController = AssetDatabase.LoadAssetAtPath<AnimatorController>(DestinationPath);

// Disable tracking controls
Expand All @@ -63,6 +67,39 @@ public override void OnPreProcessAvatar()
}

#if UNITY_EDITOR
private static void CreateFolder()
{
CreateFolderRecursively(DestinationDir);
if (AssetDatabase.IsValidFolder(DestinationDir))
{
Debug.Log("Folder created: " + DestinationDir);
}
else
{
Debug.LogError("Failed to create folder: " + DestinationDir);
}
}

private static void ShowErrorMessage(string originalPath)
{
string errorMessage = string.Empty;
var currentCulture = CultureInfo.CurrentCulture;
if (currentCulture.Name == "ja-JP")
{
errorMessage += "VRC Animator Tracing Controlの無効化に失敗しました。\n" +
"表情が正しく動かない場合、元々のアバターのFXレイヤーから表情操作のレイヤーを削除してください。\n" +
"レイヤーを削除する際は、作業の前に必ずバックアップを作成してください。";
}
else
{
errorMessage += "Failed to disable VRC Animator Tracing Control.\n" +
"If the facial expressions do not work correctly, please delete the facial expression control layers from the original avatar's FX layer.\n" +
"Be sure to make a backup before deleting the layer.";
}
errorMessage += $"\n\n{originalPath} -> {DestinationPath}";
EditorUtility.DisplayDialog(DomainConstants.SystemName, errorMessage, "OK");
}

private static void DisableTrackingControlRecursively(AnimatorStateMachine animatorStateMachine)
{
foreach (var childStateMachine in animatorStateMachine.stateMachines)
Expand All @@ -88,6 +125,31 @@ private static void DisableTrackingControlRecursively(AnimatorStateMachine anima
}
}
}

// https://hacchi-man.hatenablog.com/entry/2020/08/23/220000
private static void CreateFolderRecursively(string path)
{
// If it doesn't start with Assets, it can't be processed.
if (!path.StartsWith("Assets/"))
{
return;
}

// AssetDatabase, so the delimiter is /.
var dirs = path.Split('/');
var combinePath = dirs[0];

// Skip the Assets part.
foreach (var dir in dirs.Skip(1))
{
// Check existence of directory
if (!AssetDatabase.IsValidFolder(combinePath + '/' + dir))
{
AssetDatabase.CreateFolder(combinePath, dir);
}
combinePath += '/' + dir;
}
}
#endif
}
}

0 comments on commit 7c0feb1

Please sign in to comment.