Skip to content

Commit

Permalink
feat(vrc): perform max synced param cost check (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
poi-vrc authored Mar 9, 2024
1 parent 9e43496 commit c45a91a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Editor/Detail/DK/Passes/VRChat/ApplyVRCExParamsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Chocopoi.DressingFramework.Animations.VRChat;
using Chocopoi.DressingFramework.Extensibility.Sequencing;
using Chocopoi.DressingFramework.Localization;
using Chocopoi.DressingFramework.Menu.VRChat;
using UnityEditor;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
Expand All @@ -34,6 +35,7 @@ public static class MessageCode
public const string UnsupportedUnityParamTypeForVRC = "detail.dk.passes.vrc.applyVrcExParams.msgCode.warn.unsupportedUnityParamTypeForVRC";
// Error
public const string MismatchParameterTypesBetweenAnimators = "detail.dk.passes.vrc.applyVrcExParams.msgCode.error.mismatchParameterTypesBetweenAnimators";
public const string MaxParameterCostExceeded = "detail.dk.passes.vrc.applyVrcExParams.msgCode.error.maxParameterCostExceeded";
}

public override BuildConstraint Constraint => InvokeAtStage(BuildStage.Transpose).Build();
Expand Down Expand Up @@ -122,6 +124,13 @@ public override bool Invoke(Context ctx)
vrcParam.valueType = vrcParamType.Value;
}

var totalCost = VRCMenuUtils.CalculateParametersCost(vrcParamList);
if (totalCost > VRCExpressionParameters.MAX_PARAMETER_COST)
{
ctx.Report.LogWarnLocalized(I18nManager.Instance.FrameworkTranslator, LogLabel, MessageCode.MaxParameterCostExceeded, totalCost, VRCExpressionParameters.MAX_PARAMETER_COST);
return false;
}

// write into vrc params
vrcParams.parameters = vrcParamList.ToArray();
EditorUtility.SetDirty(vrcParams);
Expand Down
16 changes: 16 additions & 0 deletions Editor/Menu/VRChat/VRCMenuUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#if DK_VRCSDK3A
using System;
using System.Collections.Generic;
using UnityEditor;
using VRC.SDK3.Avatars.Components;
using VRC.SDK3.Avatars.ScriptableObjects;
Expand Down Expand Up @@ -262,6 +263,21 @@ public static void WriteLabel(MenuItem.Label from, VRCMenuItemLabelWrapper to)
to.Name = from.Name;
to.Icon = from.Icon;
}

public static int CalculateParametersCost(IEnumerable<VRCExpressionParameters.Parameter> parameters)
{
var cost = 0;

foreach (var p in parameters)
{
if (p.networkSynced)
{
cost += VRCExpressionParameters.TypeCost(p.valueType);
}
}

return cost;
}
}
}
#endif
47 changes: 47 additions & 0 deletions Tests~/Editor/Menu/VRChat/VRCMenuUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#if DK_VRCSDK3A
using System;
using System.Collections.Generic;
using Chocopoi.DressingFramework.Menu;
using Chocopoi.DressingFramework.Menu.VRChat;
using NUnit.Framework;
Expand Down Expand Up @@ -348,6 +349,52 @@ public void MenuItemToControlTest()
Assert.AreEqual("1", ctrl.subParameters[0].name);
});
}

[Test]
public void CalculateParametersCostTest()
{
var parameters = new List<VRCExpressionParameters.Parameter>
{
new VRCExpressionParameters.Parameter()
{
valueType = VRCExpressionParameters.ValueType.Int,
networkSynced = true,
},
new VRCExpressionParameters.Parameter()
{
valueType = VRCExpressionParameters.ValueType.Int,
networkSynced = false,
},
new VRCExpressionParameters.Parameter()
{
valueType = VRCExpressionParameters.ValueType.Float,
networkSynced = true,
},
new VRCExpressionParameters.Parameter()
{
valueType = VRCExpressionParameters.ValueType.Float,
networkSynced = false,
},
new VRCExpressionParameters.Parameter()
{
valueType = VRCExpressionParameters.ValueType.Bool,
networkSynced = true,
},
new VRCExpressionParameters.Parameter()
{
valueType = VRCExpressionParameters.ValueType.Bool,
networkSynced = false,
}
};

// network synced cost does not count
var expectedCost =
VRCExpressionParameters.TypeCost(VRCExpressionParameters.ValueType.Int) +
VRCExpressionParameters.TypeCost(VRCExpressionParameters.ValueType.Float) +
VRCExpressionParameters.TypeCost(VRCExpressionParameters.ValueType.Bool);

Assert.AreEqual(expectedCost, VRCMenuUtils.CalculateParametersCost(parameters));
}
}
}
#endif
1 change: 1 addition & 0 deletions Translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"detail.dk.passes.vrc.cloneVrcAnimLayers.msgCode.error.unableToObtainDefaultAnimLayerAsset": "Unable to obtain default VRC animation layer asset",
"detail.dk.passes.vrc.cloneVrcExMenuAndParams.msgCode.error.unableToObtainDefaultExMenuAsset": "Unable to obtain default VRC expressions menu asset",
"detail.dk.passes.vrc.cloneVrcExMenuAndParams.msgCode.error.unableToObtainDefaultExParamsAsset": "Unable to obtain default VRC expressions parameters asset",
"detail.dk.passes.vrc.applyVrcExParams.msgCode.error.maxParameterCostExceeded": "Maximum synced parameter cost exceeded",
"triggers.vrc.dialog.msg.errorPreprocessingReferReportWindow": "Error preprocessing avatar, please refer to the report window.",
"common.dialog.btn.ok": "OK",
"report.editor.helpbox.resultError": "Result: Errors occurred",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.chocopoi.vrc.dressingframework",
"displayName": "DressingFramework",
"version": "2.0.0-beta.5",
"version": "2.0.0-beta.6",
"unity": "2019.4",
"description": "A framework that assembles DressingTools and provides interfaces for third-party developers.",
"author": {
Expand Down

0 comments on commit c45a91a

Please sign in to comment.