mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 14:29:25 +00:00
another pointless refactor
This commit is contained in:
parent
27a05545ed
commit
870a29ca9f
2 changed files with 99 additions and 82 deletions
|
@ -1,104 +1,122 @@
|
||||||
using ABI_RC.Core.Player;
|
using ABI_RC.Core.Player;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace NAK.Melons.AASBufferFix;
|
namespace NAK.Melons.AASBufferFix
|
||||||
|
{
|
||||||
public class AASBufferHelper : MonoBehaviour
|
public class AASBufferHelper : MonoBehaviour
|
||||||
{
|
{
|
||||||
public bool isAcceptingAAS = false;
|
//public stuff
|
||||||
|
public bool GameHandlesAAS { get; private set; }
|
||||||
|
|
||||||
internal PuppetMaster puppetMaster;
|
//internal references
|
||||||
|
private PuppetMaster _puppetMaster;
|
||||||
|
|
||||||
//outside buffers that dont get nuked on avatar load
|
//outside aas buffers
|
||||||
private float[] aasBufferFloat = new float[0];
|
private float[] _aasBufferFloat = new float[0];
|
||||||
private int[] aasBufferInt = new int[0];
|
private int[] _aasBufferInt = new int[0];
|
||||||
private byte[] aasBufferByte = new byte[0];
|
private byte[] _aasBufferByte = new byte[0];
|
||||||
|
|
||||||
//footprint is each parameter bit type count multiplied together
|
//calculated footprints
|
||||||
private int aasFootprint = -1;
|
private int _aasFootprint = -1;
|
||||||
private int avatarFootprint = 0;
|
private int _avatarFootprint = 0;
|
||||||
|
|
||||||
public void Start()
|
private void Start() => _puppetMaster = GetComponent<PuppetMaster>();
|
||||||
{
|
|
||||||
puppetMaster = GetComponent<PuppetMaster>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnAvatarInstantiated(Animator animator)
|
public void OnAvatarInstantiated(Animator animator)
|
||||||
{
|
{
|
||||||
//create the loaded avatar footprint
|
|
||||||
avatarFootprint = Utils.GenerateAnimatorAASFootprint(animator);
|
|
||||||
|
|
||||||
//previous "bad data" now matches, apply buffered data
|
///MelonLoader.MelonLogger.Msg("[OnInit] Remote avatar initialized. Generating avatar animator footprint.");
|
||||||
|
|
||||||
|
_avatarFootprint = Utils.GenerateAnimatorAASFootprint(animator);
|
||||||
|
|
||||||
|
// avatar does not contain proper AAS
|
||||||
|
// this is likely because the avatar was made before AAS, or is blocked/hidden
|
||||||
|
if (_avatarFootprint == 1)
|
||||||
|
{
|
||||||
|
///MelonLoader.MelonLogger.Msg("[OnInit] Avatar does not contain valid AAS.");
|
||||||
|
// we will let the game handle this by setting IsAcceptingAAS to true
|
||||||
|
GameHandlesAAS = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
///MelonLoader.MelonLogger.Msg($"[OnInit] Avatar footprint is : {_avatarFootprint}");
|
||||||
|
|
||||||
|
//check if we received expected AAS while we loaded the avatar, and if so, apply it now
|
||||||
if (SyncDataMatchesExpected())
|
if (SyncDataMatchesExpected())
|
||||||
{
|
{
|
||||||
|
///MelonLoader.MelonLogger.Msg("[OnInit] Valid buffered AAS found. Applying AAS immediatly.");
|
||||||
ApplyExternalAASBuffer();
|
ApplyExternalAASBuffer();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//we loaded avatar faster than wearer
|
||||||
|
///MelonLoader.MelonLogger.Msg("[OnInit] Remote avatar initialized faster than wearer. Waiting on valid AAS before applying.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnAvatarDestroyed()
|
public void OnAvatarDestroyed()
|
||||||
{
|
{
|
||||||
aasFootprint = -1;
|
GameHandlesAAS = false;
|
||||||
avatarFootprint = 0;
|
_aasFootprint = -1;
|
||||||
isAcceptingAAS = false;
|
_avatarFootprint = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnApplyAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
|
public void OnApplyAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
|
||||||
{
|
{
|
||||||
//create the synced data footprint
|
//avatar is still loading on our side, we must assume AAS data is correct and store it until we load
|
||||||
aasFootprint = (settingsFloat.Length + 1) * (settingsInt.Length + 1) * (settingsByte.Length + 1);
|
//there is also a chance it errored
|
||||||
|
if (_avatarFootprint == 0)
|
||||||
if (!SyncDataMatchesExpected())
|
|
||||||
{
|
{
|
||||||
if (avatarFootprint == 0)
|
///MelonLoader.MelonLogger.Msg("[OnSync] Avatar is still loading on our end.");
|
||||||
{
|
// Calculate AAS footprint to compare against later.
|
||||||
//we are receiving synced data, but the avatar has not loaded on our end
|
_aasFootprint = (settingsFloat.Length + 1) * (settingsInt.Length + 1) * (settingsByte.Length + 1);
|
||||||
//we can only assume the data is correct, and store it for later
|
|
||||||
StoreExternalAASBuffer(settingsFloat, settingsInt, settingsByte);
|
StoreExternalAASBuffer(settingsFloat, settingsInt, settingsByte);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//avatar is loaded on our screen, but wearer is syncing bad data
|
//avatar is loaded on our end, and is not blocked by filter
|
||||||
//we will need to wait until it has loaded on their end
|
//this does run if it is manually hidden or distance hidden
|
||||||
|
|
||||||
//there is also a chance the avatar is hidden, so the avatar footprint returned 1 on initialization
|
///MelonLoader.MelonLogger.Msg("[OnSync] Avatar is loaded on our side and is not blocked. Comparing for expected values.");
|
||||||
//(this was only one encounter during testing, someone being hidden by safety on world load) (x, 1)
|
///MelonLoader.MelonLogger.Msg($"[OnSync] Avatar Footprint is : {_avatarFootprint}");
|
||||||
//these avatars do attempt to sync AAS, but the avatar footprint will never match
|
|
||||||
|
|
||||||
//there is also a chance the avatar is an old avatar before AAS, so they do not sync any data
|
// Calculate AAS footprint to compare against.
|
||||||
//and have an avatar footprint of 1 (-1, 1)
|
_aasFootprint = (settingsFloat.Length + 1) * (settingsInt.Length + 1) * (settingsByte.Length + 1);
|
||||||
//these avatars do not seem to attempt AAS syncing, so it isnt much of a problem
|
|
||||||
}
|
//if it matches, apply the settings and let game take over
|
||||||
else
|
if (SyncDataMatchesExpected())
|
||||||
{
|
{
|
||||||
//synced data matches what we expect
|
///MelonLoader.MelonLogger.Msg("[OnSync] Avatar values matched and have been applied.");
|
||||||
ApplyExternalAAS(settingsFloat, settingsInt, settingsByte);
|
ApplyExternalAAS(settingsFloat, settingsInt, settingsByte);
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ApplyExternalAASBuffer()
|
//if it did not match, that means the avatar we see on our side is different than what the remote user is wearing and syncing
|
||||||
{
|
///MelonLoader.MelonLogger.Msg("[OnSync] Avatar loaded is different than wearer. The wearer is likely still loading the avatar!");
|
||||||
isAcceptingAAS = true;
|
StoreExternalAASBuffer(settingsFloat, settingsInt, settingsByte);
|
||||||
puppetMaster?.ApplyAdvancedAvatarSettings(aasBufferFloat, aasBufferInt, aasBufferByte);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ApplyExternalAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
|
private void ApplyExternalAASBuffer()
|
||||||
{
|
{
|
||||||
isAcceptingAAS = true;
|
GameHandlesAAS = true;
|
||||||
puppetMaster?.ApplyAdvancedAvatarSettings(settingsFloat, settingsInt, settingsByte);
|
_puppetMaster?.ApplyAdvancedAvatarSettings(_aasBufferFloat, _aasBufferInt, _aasBufferByte);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StoreExternalAASBuffer(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
|
private void ApplyExternalAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
|
||||||
{
|
{
|
||||||
Array.Resize(ref aasBufferFloat, settingsFloat.Length);
|
GameHandlesAAS = true;
|
||||||
Array.Resize(ref aasBufferInt, settingsInt.Length);
|
_puppetMaster?.ApplyAdvancedAvatarSettings(settingsFloat, settingsInt, settingsByte);
|
||||||
Array.Resize(ref aasBufferByte, settingsByte.Length);
|
|
||||||
Array.Copy(settingsFloat, aasBufferFloat, settingsFloat.Length);
|
|
||||||
Array.Copy(settingsInt, aasBufferInt, settingsInt.Length);
|
|
||||||
Array.Copy(settingsByte, aasBufferByte, settingsByte.Length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SyncDataMatchesExpected()
|
private void StoreExternalAASBuffer(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
|
||||||
{
|
{
|
||||||
return aasFootprint == avatarFootprint;
|
Array.Resize(ref _aasBufferFloat, settingsFloat.Length);
|
||||||
|
Array.Resize(ref _aasBufferInt, settingsInt.Length);
|
||||||
|
Array.Resize(ref _aasBufferByte, settingsByte.Length);
|
||||||
|
Array.Copy(settingsFloat, _aasBufferFloat, settingsFloat.Length);
|
||||||
|
Array.Copy(settingsInt, _aasBufferInt, settingsInt.Length);
|
||||||
|
Array.Copy(settingsByte, _aasBufferByte, settingsByte.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool SyncDataMatchesExpected() => _aasFootprint == _avatarFootprint;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -14,7 +14,6 @@ internal class HarmonyPatches
|
||||||
private static void Postfix_PuppetMaster_Start(ref PuppetMaster __instance)
|
private static void Postfix_PuppetMaster_Start(ref PuppetMaster __instance)
|
||||||
{
|
{
|
||||||
AASBufferHelper externalBuffer = __instance.AddComponentIfMissing<AASBufferHelper>();
|
AASBufferHelper externalBuffer = __instance.AddComponentIfMissing<AASBufferHelper>();
|
||||||
externalBuffer.puppetMaster = __instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
|
@ -38,7 +37,7 @@ internal class HarmonyPatches
|
||||||
private static bool Prefix_PuppetMaster_ApplyAdvancedAvatarSettings(float[] settingsFloat, int[] settingsInt, byte[] settingsByte, ref PuppetMaster __instance)
|
private static bool Prefix_PuppetMaster_ApplyAdvancedAvatarSettings(float[] settingsFloat, int[] settingsInt, byte[] settingsByte, ref PuppetMaster __instance)
|
||||||
{
|
{
|
||||||
AASBufferHelper externalBuffer = __instance.GetComponent<AASBufferHelper>();
|
AASBufferHelper externalBuffer = __instance.GetComponent<AASBufferHelper>();
|
||||||
if (externalBuffer != null && !externalBuffer.isAcceptingAAS)
|
if (externalBuffer != null && !externalBuffer.GameHandlesAAS)
|
||||||
{
|
{
|
||||||
externalBuffer.OnApplyAAS(settingsFloat, settingsInt, settingsByte);
|
externalBuffer.OnApplyAAS(settingsFloat, settingsInt, settingsByte);
|
||||||
return false;
|
return false;
|
||||||
|
@ -51,7 +50,7 @@ internal class HarmonyPatches
|
||||||
private static bool Prefix_PuppetMaster_ApplyAdvancedAvatarSettingsFromBuffer(ref Animator ____animator)
|
private static bool Prefix_PuppetMaster_ApplyAdvancedAvatarSettingsFromBuffer(ref Animator ____animator)
|
||||||
{
|
{
|
||||||
AASBufferHelper externalBuffer = ____animator.GetComponentInParent<AASBufferHelper>();
|
AASBufferHelper externalBuffer = ____animator.GetComponentInParent<AASBufferHelper>();
|
||||||
if (externalBuffer != null && !externalBuffer.isAcceptingAAS)
|
if (externalBuffer != null && !externalBuffer.GameHandlesAAS)
|
||||||
{
|
{
|
||||||
//dont apply if stable buffer no exist
|
//dont apply if stable buffer no exist
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue