Skip to content

Commit

Permalink
Always rebuild armatures on skeleton updates instead of trying to aug…
Browse files Browse the repository at this point in the history
…ment them
  • Loading branch information
RisaDev committed Oct 12, 2024
1 parent 9cb105b commit 09a093e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 75 deletions.
50 changes: 4 additions & 46 deletions CustomizePlus/Armatures/Data/Armature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,21 @@ public override string ToString()
: $"Armature (#{_localId}) on {ActorIdentifier.IncognitoDebug()} ({Profile}) with no skeleton reference";
}

public bool NewBonesAvailable(CharacterBase* cBase)
public bool IsSkeletonUpdated(CharacterBase* cBase)
{
if (cBase == null)
{
return false;
}
else if (cBase->Skeleton->PartialSkeletonCount > _partialSkeletons.Length)
{
else if (cBase->Skeleton->PartialSkeletonCount != _partialSkeletons.Length)
return true;
}
else
{
for (var i = 0; i < cBase->Skeleton->PartialSkeletonCount; ++i)
{
var newPose = cBase->Skeleton->PartialSkeletons[i].GetHavokPose(Constants.TruePoseIndex);

if (newPose != null
&& newPose->Skeleton->Bones.Length > _partialSkeletons[i].Length)
{
&& newPose->Skeleton->Bones.Length != _partialSkeletons[i].Length)
return true;
}
}
}

Expand All @@ -205,43 +200,6 @@ public void RebuildSkeleton(CharacterBase* cBase)
Plugin.Logger.Debug($"Rebuilt {this}");
}

public void AugmentSkeleton(CharacterBase* cBase)
{
if (cBase == null)
return;

var oldPartials = _partialSkeletons.Select(x => x.ToList()).ToList();
var newPartials = ParseBonesFromObject(this, cBase);

//for each of the new partial skeletons discovered...
for (var i = 0; i < newPartials.Count; ++i)
{
//if the old skeleton doesn't contain the new partial at all, add the whole thing
if (i > oldPartials.Count)
{
oldPartials.Add(newPartials[i]);
}
//otherwise, add every model bone the new partial has that the old one doesn't
else
{
//Case: get carbuncle, enable profile for it, turn carbuncle into human via glamourer
if (oldPartials.Count <= i)
oldPartials.Add(new List<ModelBone>());

for (var j = oldPartials[i].Count; j < newPartials[i].Count; ++j)
{
oldPartials[i].Add(newPartials[i][j]);
}
}
}

_partialSkeletons = oldPartials.Select(x => x.ToArray()).ToArray();

RebuildBoneTemplateBinding(); //todo: intentionally not calling ArmatureChanged.Type.Updated because this is pending rewrite

Plugin.Logger.Debug($"Augmented {this} with new bones");
}

public BoneTransform? GetAppliedBoneTransform(string boneName)
{
if (BoneTemplateBinding.TryGetValue(boneName, out var template)
Expand Down
36 changes: 10 additions & 26 deletions CustomizePlus/Armatures/Services/ArmatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void RefreshArmatures()
}

//Needed because skeleton sometimes appears to be not ready when armature is created
//and also because we want to augment armature with new bones if they are available
//and also because we want to keep armature up to date with any character skeleton changes
TryLinkSkeleton(armature);
}
}
Expand All @@ -207,38 +207,22 @@ private unsafe void ApplyArmatureTransforms()
/// Returns whether or not a link can be established between the armature and an in-game object.
/// If unbuilt, the armature will be rebuilded.
/// </summary>
private bool TryLinkSkeleton(Armature armature, bool forceRebuild = false)
private bool TryLinkSkeleton(Armature armature)
{
_objectManager.Update();

try
{
if (!_objectManager.Identifiers.ContainsKey(armature.ActorIdentifier))
return false;
if (!_objectManager.Identifiers.ContainsKey(armature.ActorIdentifier))
return false;

var actor = _objectManager[armature.ActorIdentifier].Objects[0];
var actor = _objectManager[armature.ActorIdentifier].Objects[0];

if (!armature.IsBuilt || forceRebuild)
{
armature.RebuildSkeleton(actor.Model.AsCharacterBase);
}
else if (armature.NewBonesAvailable(actor.Model.AsCharacterBase))
{
armature.AugmentSkeleton(actor.Model.AsCharacterBase);
}

return true;
}
catch (Exception ex)
if (!armature.IsBuilt || armature.IsSkeletonUpdated(actor.Model.AsCharacterBase))
{
// This is on wait until isse #191 on Github responds. Keeping it in code, delete it if I forget and this is longer then a month ago.

// Disabling this if its any Default Profile due to Log spam. A bit crazy but hey, if its for me id Remove Default profiles all together so this is as much as ill do for now! :)
//if(!(Profile.CharacterName.Equals(Constants.DefaultProfileCharacterName) || Profile.CharacterName.Equals("DefaultCutscene"))) {
_logger.Error($"Error occured while attempting to link skeleton: {armature}");
throw;
//}
_logger.Debug($"Skeleton for actor #{actor.AsObject->ObjectIndex} tied to \"{armature}\" has changed");
armature.RebuildSkeleton(actor.Model.AsCharacterBase);
}

return true;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,27 @@ private void DrawSingleArmature(string prefix, Armature armature)
//ImGui.Text("Profile:");
//DrawSingleProfile($"armature-{armature.GetHashCode()}", armature.Profile);

ImGui.Text($"Bone template bindings:");
foreach (var kvPair in armature.BoneTemplateBinding)
var bindingsShow = ImGui.CollapsingHeader($"Bone template bindings ({armature.BoneTemplateBinding.Count})###{prefix}-armature-{armature.GetHashCode()}-bindings");

if (bindingsShow)
{
foreach (var kvPair in armature.BoneTemplateBinding)
{
ImGui.Text($"{BoneData.GetBoneDisplayName(kvPair.Key)} ({kvPair.Key}) -> {kvPair.Value.Name.Text.Incognify()} ({kvPair.Value.UniqueId})");
}
}

var bonesShow = ImGui.CollapsingHeader($"Armature bones###{prefix}-armature-{armature.GetHashCode()}-bones");

if (!bonesShow)
return;

var bones = armature.GetAllBones().ToList();
ImGui.Text($"{bones.Count} bones");

foreach (var bone in bones)
{
ImGui.Text($"{BoneData.GetBoneDisplayName(kvPair.Key)} ({kvPair.Key}) -> {kvPair.Value.Name.Text.Incognify()} ({kvPair.Value.UniqueId})");
ImGui.Text($"{(bone.IsActive ? "[A] " : "")}{BoneData.GetBoneDisplayName(bone.BoneName)} [{bone.BoneName}] ({bone.PartialSkeletonIndex}-{bone.BoneIndex})");
}
}
}

0 comments on commit 09a093e

Please sign in to comment.