Skip to content

Commit

Permalink
refactor: change to CallOrder and call every provider even module is …
Browse files Browse the repository at this point in the history
…not exist
  • Loading branch information
poi-vrc committed Aug 24, 2023
1 parent ede4dcb commit c3c7ae1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void GroupDynamics(GameObject wearableGameObject, List<IDynamicsProxy> w
}
}

private bool ApplyWearable(ApplyWearableContext wearCtx)
private bool ApplyWearable(ApplyWearableContext wearCtx, List<ModuleProviderBase> sortedProviders)
{
GameObject wearableObj;
if (DTEditorUtils.IsGrandParent(_cabCtx.cabinet.avatarGameObject.transform, wearCtx.wearableGameObject.transform))
Expand Down Expand Up @@ -230,20 +230,25 @@ private bool ApplyWearable(ApplyWearableContext wearCtx)
return 1;
}
return m1Provider.ApplyOrder.CompareTo(m2Provider.ApplyOrder);
return m1Provider.CallOrder.CompareTo(m2Provider.CallOrder);
});

// do module apply
// check if unknown modules
foreach (var module in modules)
{
// locate the module provider
var provider = ModuleProviderLocator.Instance.GetProvider(module.moduleName);

if (provider == null)
{
DTReportUtils.LogErrorLocalized(_cabCtx.report, LogLabel, MessageCode.ModuleHasNoProviderAvailable, module.moduleName);
return false;
}
}

// do provider hooks
foreach (var provider in sortedProviders)
{
// could be null if config does not have such module, provider should handle this
var module = DTEditorUtils.FindWearableModule(wearCtx.config, provider.ModuleIdentifier);

if (!provider.OnApplyWearable(_cabCtx, wearCtx, module))
{
Expand All @@ -263,12 +268,10 @@ private bool ApplyWearable(ApplyWearableContext wearCtx)
return true;
}

private static bool DoBeforeApplyCabinetProviderHooks(ApplyCabinetContext ctx)
private static bool DoBeforeApplyCabinetProviderHooks(ApplyCabinetContext ctx, List<ModuleProviderBase> sortedProviders)
{
// do provider hooks
var providers = new List<ModuleProviderBase>(ModuleProviderLocator.Instance.GetAllProviders());
providers.Sort((p1, p2) => p1.ApplyOrder.CompareTo(p2.ApplyOrder));
foreach (var provider in providers)
foreach (var provider in sortedProviders)
{
if (!provider.OnBeforeApplyCabinet(ctx))
{
Expand All @@ -279,12 +282,10 @@ private static bool DoBeforeApplyCabinetProviderHooks(ApplyCabinetContext ctx)
return true;
}

private static bool DoAfterApplyCabinetProviderHooks(ApplyCabinetContext ctx)
private static bool DoAfterApplyCabinetProviderHooks(ApplyCabinetContext ctx, List<ModuleProviderBase> sortedProviders)
{
// do provider hooks
var providers = new List<ModuleProviderBase>(ModuleProviderLocator.Instance.GetAllProviders());
providers.Sort((p1, p2) => p1.ApplyOrder.CompareTo(p2.ApplyOrder));
foreach (var provider in providers)
foreach (var provider in sortedProviders)
{
if (!provider.OnAfterApplyCabinet(ctx))
{
Expand All @@ -304,7 +305,10 @@ public void Execute()
// scan for avatar dynamics
_cabCtx.avatarDynamics = DTEditorUtils.ScanDynamics(_cabCtx.cabinet.avatarGameObject, true);

if (!DoBeforeApplyCabinetProviderHooks(_cabCtx))
var sortedProviders = new List<ModuleProviderBase>(ModuleProviderLocator.Instance.GetAllProviders());
sortedProviders.Sort((p1, p2) => p1.CallOrder.CompareTo(p2.CallOrder));

if (!DoBeforeApplyCabinetProviderHooks(_cabCtx, sortedProviders))
{
return;
}
Expand Down Expand Up @@ -340,14 +344,14 @@ public void Execute()
wearableDynamics = DTEditorUtils.ScanDynamics(wearable.wearableGameObject, false)
};

if (!ApplyWearable(wearCtx))
if (!ApplyWearable(wearCtx, sortedProviders))
{
DTReportUtils.LogErrorLocalized(_cabCtx.report, LogLabel, MessageCode.ApplyingWearableHasErrors, config.Info.name);
return;
}
}

if (!DoAfterApplyCabinetProviderHooks(_cabCtx))
if (!DoAfterApplyCabinetProviderHooks(_cabCtx, sortedProviders))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class VRChatIntegrationModuleProvider : ModuleProviderBase

[ExcludeFromCodeCoverage] public override string ModuleIdentifier => Identifier;
[ExcludeFromCodeCoverage] public override string FriendlyName => "Integration: VRChat";
[ExcludeFromCodeCoverage] public override int ApplyOrder => int.MaxValue;
[ExcludeFromCodeCoverage] public override int CallOrder => int.MaxValue;
[ExcludeFromCodeCoverage] public override bool AllowMultiple => false;

static VRChatIntegrationModuleProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal class AnimationGenerationModuleProvider : ModuleProviderBase

[ExcludeFromCodeCoverage] public override string ModuleIdentifier => Identifier;
[ExcludeFromCodeCoverage] public override string FriendlyName => "Animation Generation";
[ExcludeFromCodeCoverage] public override int ApplyOrder => 4;
[ExcludeFromCodeCoverage] public override int CallOrder => 4;
[ExcludeFromCodeCoverage] public override bool AllowMultiple => false;

static AnimationGenerationModuleProvider()
Expand Down Expand Up @@ -81,7 +81,6 @@ private static void InvertToggleStates(DTCabinet cabinet, WearableConfig config,
// invert wearable toggles
foreach (var toggle in agm.wearableAnimationOnWear.toggles)
{
Debug.Log("toggle: " + toggle != null);
var wearableToggleObj = wearableGameObject.transform.Find(toggle.path);
if (wearableToggleObj == null)
{
Expand All @@ -94,11 +93,8 @@ private static void InvertToggleStates(DTCabinet cabinet, WearableConfig config,

public override bool OnAddWearableToCabinet(DTCabinet cabinet, WearableConfig config, GameObject wearableGameObject, WearableModule module)
{
if (module == null)
{
// we need the wearable to have our module installed
return true;
}
// we need the wearable to have our module installed
if (module == null) return true;

InvertToggleStates(cabinet, config, wearableGameObject, module);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static class MessageCode

[ExcludeFromCodeCoverage] public override string ModuleIdentifier => Identifier;
[ExcludeFromCodeCoverage] public override string FriendlyName => "Armature Mapping";
[ExcludeFromCodeCoverage] public override int ApplyOrder => 2;
[ExcludeFromCodeCoverage] public override int CallOrder => 2;
[ExcludeFromCodeCoverage] public override bool AllowMultiple => false;

private static bool GenerateMappings(ApplyCabinetContext cabCtx, ApplyWearableContext wearCtx, ArmatureMappingModuleConfig moduleConfig, string wearableName, out List<BoneMapping> resultantBoneMappings)
Expand Down Expand Up @@ -359,6 +359,9 @@ private static string RandomString(int length)

public override bool OnApplyWearable(ApplyCabinetContext cabCtx, ApplyWearableContext wearCtx, WearableModule module)
{
// no module in config
if (module == null) return true;

var armatureMappingConfig = (ArmatureMappingModuleConfig)module.config;

if (!GenerateMappings(cabCtx, wearCtx, armatureMappingConfig, wearCtx.config.Info.name, out var boneMappings))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class BlendshapeSyncModuleProvider : ModuleProviderBase

[ExcludeFromCodeCoverage] public override string ModuleIdentifier => Identifier;
[ExcludeFromCodeCoverage] public override string FriendlyName => "Blendshape Sync";
[ExcludeFromCodeCoverage] public override int ApplyOrder => 6;
[ExcludeFromCodeCoverage] public override int CallOrder => 6;
[ExcludeFromCodeCoverage] public override bool AllowMultiple => false;

static BlendshapeSyncModuleProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class MoveRootModuleProvider : ModuleProviderBase

[ExcludeFromCodeCoverage] public override string ModuleIdentifier => Identifier;
[ExcludeFromCodeCoverage] public override string FriendlyName => "Move Root";
[ExcludeFromCodeCoverage] public override int ApplyOrder => 2;
[ExcludeFromCodeCoverage] public override int CallOrder => 2;
[ExcludeFromCodeCoverage] public override bool AllowMultiple => false;

static MoveRootModuleProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class ModuleProviderBase
{
public abstract string ModuleIdentifier { get; }
public abstract string FriendlyName { get; }
public abstract int ApplyOrder { get; }
public abstract int CallOrder { get; }
public abstract bool AllowMultiple { get; }

public abstract IModuleConfig DeserializeModuleConfig(JObject jObject);
Expand Down

0 comments on commit c3c7ae1

Please sign in to comment.