This commit is contained in:
NotAKidoS 2023-02-27 23:57:18 -06:00
parent 3b4f564ad8
commit 819a00b27b
2 changed files with 116 additions and 118 deletions

View file

@ -2,133 +2,132 @@
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 DebuggingFlag = false;
//public stuff
public bool GameHandlesAAS { get; private set; }
//internal references
private PuppetMaster _puppetMaster;
//outside aas buffers
private float[] _aasBufferFloat = new float[0];
private int[] _aasBufferInt = new int[0];
private byte[] _aasBufferByte = new byte[0];
//calculated footprints
private int _aasFootprint = -1;
private int _avatarFootprint = 0;
private void Start() => _puppetMaster = GetComponent<PuppetMaster>();
public void OnAvatarInstantiated(Animator animator)
{ {
///public bool DebuggingFlag = false; //check if avatar uses Avatar Advanced Settings
///SendDebug("[OnInit] Remote avatar initialized. Checking for AAS...");
//public stuff CVRAvatar avatar = animator.GetComponent<CVRAvatar>();
public bool GameHandlesAAS { get; private set; } if (avatar != null && !avatar.avatarUsesAdvancedSettings)
//internal references
private PuppetMaster _puppetMaster;
//outside aas buffers
private float[] _aasBufferFloat = new float[0];
private int[] _aasBufferInt = new int[0];
private byte[] _aasBufferByte = new byte[0];
//calculated footprints
private int _aasFootprint = -1;
private int _avatarFootprint = 0;
private void Start() => _puppetMaster = GetComponent<PuppetMaster>();
public void OnAvatarInstantiated(Animator animator)
{
//check if avatar uses Avatar Advanced Settings
///SendDebug("[OnInit] Remote avatar initialized. Checking for AAS...");
CVRAvatar avatar = animator.GetComponent<CVRAvatar>();
if (avatar != null && !avatar.avatarUsesAdvancedSettings)
{
GameHandlesAAS = true;
return;
}
//check if AAS footprint is valid
///SendDebug("[OnInit] Avatar uses AAS. Generating AAS footprint...");
_avatarFootprint = Utils.GenerateAnimatorAASFootprint(animator);
if (_avatarFootprint == 1)
{
// we will let the game handle this by setting GameHandlesAAS to true
///SendDebug("[OnInit] Avatar does not contain valid AAS. It is likely hidden or blocked.");
GameHandlesAAS = true;
return;
}
///SendDebug($"[OnInit] Avatar footprint is : {_avatarFootprint}");
//check if we received expected AAS while we loaded the avatar, and if so, apply it now
if (SyncDataMatchesExpected())
{
///SendDebug("[OnInit] Valid buffered AAS found. Applying buffer...");
ApplyExternalAASBuffer();
return;
}
//we loaded avatar faster than wearer
///SendDebug("[OnInit] Remote avatar initialized faster than wearer. Waiting on valid AAS...");
}
public void OnAvatarDestroyed()
{
GameHandlesAAS = false;
_aasFootprint = -1;
_avatarFootprint = 0;
}
public void OnReceiveAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
{
// Calculate AAS footprint to compare against.
_aasFootprint = (settingsFloat.Length + 1) * (settingsInt.Length + 1) * (settingsByte.Length + 1);
//if it matches, apply the settings and let game take over
if (SyncDataMatchesExpected())
{
///SendDebug("[OnSync] Avatar values matched and have been applied.");
ApplyExternalAAS(settingsFloat, settingsInt, settingsByte);
return;
}
//avatar is still loading on our side, we must assume AAS data is correct and store it until we load
//there is also a chance it errored
//if (_avatarFootprint == 0)
//{
// ///SendDebug("[OnSync] Avatar is still loading on our end.");
// StoreExternalAASBuffer(settingsFloat, settingsInt, settingsByte);
// return;
//}
//avatar is loaded on our end, and is not blocked by filter
//this does run if it is manually hidden or distance hidden
///SendDebug("[OnSync] Avatar is loaded on our side and is not blocked. Comparing for expected values.");
///SendDebug($"[OnSync] Avatar Footprint is : {_avatarFootprint}");
//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
///SendDebug("[OnSync] Avatar loaded is different than wearer. The wearer is likely still loading the avatar!");
StoreExternalAASBuffer(settingsFloat, settingsInt, settingsByte);
}
private void ApplyExternalAASBuffer()
{ {
GameHandlesAAS = true; GameHandlesAAS = true;
_puppetMaster?.ApplyAdvancedAvatarSettings(_aasBufferFloat, _aasBufferInt, _aasBufferByte); return;
} }
private void ApplyExternalAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte) //check if AAS footprint is valid
///SendDebug("[OnInit] Avatar uses AAS. Generating AAS footprint...");
_avatarFootprint = Utils.GenerateAnimatorAASFootprint(animator);
if (_avatarFootprint == 1)
{ {
// we will let the game handle this by setting GameHandlesAAS to true
///SendDebug("[OnInit] Avatar does not contain valid AAS. It is likely hidden or blocked.");
GameHandlesAAS = true; GameHandlesAAS = true;
_puppetMaster?.ApplyAdvancedAvatarSettings(settingsFloat, settingsInt, settingsByte); return;
} }
private void StoreExternalAASBuffer(float[] settingsFloat, int[] settingsInt, byte[] settingsByte) ///SendDebug($"[OnInit] Avatar footprint is : {_avatarFootprint}");
//check if we received expected AAS while we loaded the avatar, and if so, apply it now
if (SyncDataMatchesExpected())
{ {
Array.Resize(ref _aasBufferFloat, settingsFloat.Length); ///SendDebug("[OnInit] Valid buffered AAS found. Applying buffer...");
Array.Resize(ref _aasBufferInt, settingsInt.Length); ApplyExternalAASBuffer();
Array.Resize(ref _aasBufferByte, settingsByte.Length); return;
Array.Copy(settingsFloat, _aasBufferFloat, settingsFloat.Length);
Array.Copy(settingsInt, _aasBufferInt, settingsInt.Length);
Array.Copy(settingsByte, _aasBufferByte, settingsByte.Length);
} }
private bool SyncDataMatchesExpected() => _aasFootprint == _avatarFootprint; //we loaded avatar faster than wearer
///SendDebug("[OnInit] Remote avatar initialized faster than wearer. Waiting on valid AAS...");
///private void SendDebug(string message)
///{
/// if (!DebuggingFlag) return;
/// AASBufferFix.Logger.Msg(message);
///}
} }
public void OnAvatarDestroyed()
{
GameHandlesAAS = false;
_aasFootprint = -1;
_avatarFootprint = 0;
}
public void OnReceiveAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
{
// Calculate AAS footprint to compare against.
_aasFootprint = (settingsFloat.Length + 1) * (settingsInt.Length + 1) * (settingsByte.Length + 1);
//if it matches, apply the settings and let game take over
if (SyncDataMatchesExpected())
{
///SendDebug("[OnSync] Avatar values matched and have been applied.");
ApplyExternalAAS(settingsFloat, settingsInt, settingsByte);
return;
}
//avatar is still loading on our side, we must assume AAS data is correct and store it until we load
//there is also a chance it errored
//if (_avatarFootprint == 0)
//{
// ///SendDebug("[OnSync] Avatar is still loading on our end.");
// StoreExternalAASBuffer(settingsFloat, settingsInt, settingsByte);
// return;
//}
//avatar is loaded on our end, and is not blocked by filter
//this does run if it is manually hidden or distance hidden
///SendDebug("[OnSync] Avatar is loaded on our side and is not blocked. Comparing for expected values.");
///SendDebug($"[OnSync] Avatar Footprint is : {_avatarFootprint}");
//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
///SendDebug("[OnSync] Avatar loaded is different than wearer. The wearer is likely still loading the avatar!");
StoreExternalAASBuffer(settingsFloat, settingsInt, settingsByte);
}
private void ApplyExternalAASBuffer()
{
GameHandlesAAS = true;
_puppetMaster?.ApplyAdvancedAvatarSettings(_aasBufferFloat, _aasBufferInt, _aasBufferByte);
}
private void ApplyExternalAAS(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
{
GameHandlesAAS = true;
_puppetMaster?.ApplyAdvancedAvatarSettings(settingsFloat, settingsInt, settingsByte);
}
private void StoreExternalAASBuffer(float[] settingsFloat, int[] settingsInt, byte[] settingsByte)
{
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;
///private void SendDebug(string message)
///{
/// if (!DebuggingFlag) return;
/// AASBufferFix.Logger.Msg(message);
///}
} }

View file

@ -13,7 +13,7 @@ internal class HarmonyPatches
[HarmonyPatch(typeof(PuppetMaster), "Start")] [HarmonyPatch(typeof(PuppetMaster), "Start")]
private static void Postfix_PuppetMaster_Start(ref PuppetMaster __instance) private static void Postfix_PuppetMaster_Start(ref PuppetMaster __instance)
{ {
AASBufferHelper externalBuffer = __instance.AddComponentIfMissing<AASBufferHelper>(); __instance.AddComponentIfMissing<AASBufferHelper>();
} }
[HarmonyPostfix] [HarmonyPostfix]
@ -63,7 +63,6 @@ internal class HarmonyPatches
private static bool Prefix_PlayerSetup_SendAdvancedAvatarSettings(ref PlayerSetup __instance) private static bool Prefix_PlayerSetup_SendAdvancedAvatarSettings(ref PlayerSetup __instance)
{ {
//dont sync wrong settings to remote users //dont sync wrong settings to remote users
if (__instance.avatarIsLoading) return false; return !__instance.avatarIsLoading;
return true;
} }
} }