mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 22:39:22 +00:00
[AvatarScaleMod] Test 2: Height Request
Now requests initial height on instance join instead of syncing every 10s. Reworked inbould & outbound message queue. Fixed potential race condition where scalefactor could become 0. A lot of this is still incredibly jank. Just trying to get things working before cleanup.
This commit is contained in:
parent
d599f57973
commit
c7eb5715ba
7 changed files with 440 additions and 146 deletions
|
@ -1,4 +1,7 @@
|
|||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.IO;
|
||||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.Player.AvatarTracking;
|
||||
using ABI_RC.Systems.GameEventSystem;
|
||||
using NAK.AvatarScaleMod.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
|
@ -8,9 +11,11 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
{
|
||||
public static AvatarScaleManager Instance;
|
||||
|
||||
public bool Setting_PersistantHeight = false;
|
||||
|
||||
private Dictionary<string, UniversalAvatarScaler> _networkedScalers;
|
||||
private UniversalAvatarScaler _localAvatarScaler;
|
||||
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
private void Awake()
|
||||
|
@ -25,6 +30,27 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
_networkedScalers = new Dictionary<string, UniversalAvatarScaler>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
CVRGameEventSystem.Instance.OnConnected.AddListener(OnInstanceConnected);
|
||||
//SchedulerSystem.AddJob(new SchedulerSystem.Job(ForceHeightUpdate), 0f, 10f, -1);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
CVRGameEventSystem.Instance.OnConnected.RemoveListener(OnInstanceConnected);
|
||||
//SchedulerSystem.RemoveJob(new SchedulerSystem.Job(ForceHeightUpdate));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Game Events
|
||||
|
||||
public void OnInstanceConnected(string instanceId)
|
||||
{
|
||||
SchedulerSystem.AddJob(ModNetwork.RequestHeightSync, 2f, 1f, 1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Local Methods
|
||||
|
@ -33,28 +59,37 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
{
|
||||
if (playerSetup._avatar == null)
|
||||
return;
|
||||
|
||||
if (_localAvatarScaler != null)
|
||||
Destroy(_localAvatarScaler);
|
||||
|
||||
_localAvatarScaler = playerSetup._avatar.AddComponent<UniversalAvatarScaler>();
|
||||
_localAvatarScaler.Initialize(playerSetup._initialAvatarHeight, playerSetup.initialScale);
|
||||
if (_localAvatarScaler == null)
|
||||
{
|
||||
_localAvatarScaler = playerSetup.gameObject.AddComponent<UniversalAvatarScaler>();
|
||||
_localAvatarScaler.Initialize();
|
||||
}
|
||||
|
||||
_localAvatarScaler.OnAvatarInstantiated(playerSetup._avatar, playerSetup._initialAvatarHeight,
|
||||
playerSetup.initialScale);
|
||||
|
||||
if (Setting_PersistantHeight && _localAvatarScaler.IsValid())
|
||||
SchedulerSystem.AddJob(() => { ModNetwork.SendNetworkHeight(_localAvatarScaler.GetHeight()); }, 0.5f, 0f, 1);
|
||||
}
|
||||
|
||||
public void OnAvatarDestroyed()
|
||||
public void OnAvatarDestroyed(PlayerSetup playerSetup)
|
||||
{
|
||||
if (_localAvatarScaler != null)
|
||||
Destroy(_localAvatarScaler);
|
||||
_localAvatarScaler.OnAvatarDestroyed(Setting_PersistantHeight);
|
||||
|
||||
if (Setting_PersistantHeight && _localAvatarScaler.IsValid())
|
||||
SchedulerSystem.AddJob(() => { ModNetwork.SendNetworkHeight(_localAvatarScaler.GetHeight()); }, 0.5f, 0f, 1);
|
||||
}
|
||||
|
||||
public void SetHeight(float targetHeight)
|
||||
{
|
||||
if (_localAvatarScaler == null)
|
||||
if (_localAvatarScaler == null)
|
||||
return;
|
||||
|
||||
_localAvatarScaler.SetHeight(targetHeight);
|
||||
|
||||
_localAvatarScaler.SetTargetHeight(targetHeight);
|
||||
ModNetwork.SendNetworkHeight(targetHeight);
|
||||
|
||||
|
||||
// immediately update play space scale
|
||||
PlayerSetup.Instance.CheckUpdateAvatarScaleToPlaySpaceRelation();
|
||||
}
|
||||
|
@ -64,49 +99,98 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
if (_localAvatarScaler != null)
|
||||
_localAvatarScaler.ResetHeight();
|
||||
}
|
||||
|
||||
|
||||
public float GetHeight()
|
||||
{
|
||||
return (_localAvatarScaler != null) ? _localAvatarScaler.GetHeight() : -1f;
|
||||
return _localAvatarScaler != null ? _localAvatarScaler.GetHeight() : -1f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Network Methods
|
||||
|
||||
public void OnNetworkAvatarInstantiated(PuppetMaster puppetMaster)
|
||||
{
|
||||
if (puppetMaster.avatarObject == null)
|
||||
return;
|
||||
|
||||
string playerId = puppetMaster._playerDescriptor.ownerId;
|
||||
|
||||
if (_networkedScalers.ContainsKey(playerId))
|
||||
_networkedScalers.Remove(playerId);
|
||||
|
||||
UniversalAvatarScaler scaler = puppetMaster.avatarObject.AddComponent<UniversalAvatarScaler>();
|
||||
scaler.Initialize(puppetMaster._initialAvatarHeight, puppetMaster.initialAvatarScale);
|
||||
_networkedScalers[playerId] = scaler;
|
||||
}
|
||||
|
||||
public void OnNetworkAvatarDestroyed(string playerId)
|
||||
{
|
||||
if (_networkedScalers.ContainsKey(playerId))
|
||||
_networkedScalers.Remove(playerId);
|
||||
}
|
||||
|
||||
public void OnNetworkHeightUpdateReceived(string playerId, float targetHeight)
|
||||
{
|
||||
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler))
|
||||
scaler.SetHeight(targetHeight);
|
||||
}
|
||||
|
||||
public float GetNetworkHeight(string playerId)
|
||||
{
|
||||
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler))
|
||||
return scaler.GetHeight();
|
||||
return -1f;
|
||||
}
|
||||
|
||||
|
||||
// we will create a Universal Scaler for only users that send a height update
|
||||
// this is sent at a rate of 10s locally!
|
||||
|
||||
internal void OnNetworkHeightUpdateReceived(string playerId, float targetHeight)
|
||||
{
|
||||
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler))
|
||||
scaler.SetTargetHeight(targetHeight);
|
||||
else
|
||||
SetupHeightScalerForNetwork(playerId, targetHeight);
|
||||
}
|
||||
|
||||
internal void OnNetworkAvatarInstantiated(PuppetMaster puppetMaster)
|
||||
{
|
||||
var playerId = puppetMaster._playerDescriptor.ownerId;
|
||||
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler))
|
||||
scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster._initialAvatarHeight,
|
||||
puppetMaster.initialAvatarScale);
|
||||
}
|
||||
|
||||
internal void OnNetworkAvatarDestroyed(PuppetMaster puppetMaster)
|
||||
{
|
||||
// on disconnect
|
||||
if (puppetMaster == null || puppetMaster._playerDescriptor == null)
|
||||
return;
|
||||
|
||||
var playerId = puppetMaster._playerDescriptor.ownerId;
|
||||
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler))
|
||||
scaler.OnAvatarDestroyed();
|
||||
}
|
||||
|
||||
internal void RemoveNetworkHeightScaler(string playerId)
|
||||
{
|
||||
if (_networkedScalers.ContainsKey(playerId))
|
||||
{
|
||||
AvatarScaleMod.Logger.Msg(
|
||||
$"Removed user height scaler! This is hopefully due to a disconnect or block. : {playerId}");
|
||||
|
||||
_networkedScalers.Remove(playerId);
|
||||
return;
|
||||
}
|
||||
|
||||
AvatarScaleMod.Logger.Msg(
|
||||
$"Failed to remove a user height scaler! This shouldn't happen. : {playerId}");
|
||||
}
|
||||
|
||||
private void SetupHeightScalerForNetwork(string playerId, float targetHeight)
|
||||
{
|
||||
CVRPlayerEntity playerEntity =
|
||||
CVRPlayerManager.Instance.NetworkPlayers.Find(players => players.Uuid == playerId);
|
||||
|
||||
PuppetMaster puppetMaster = playerEntity?.PuppetMaster;
|
||||
|
||||
if (playerEntity == null || puppetMaster == null)
|
||||
{
|
||||
AvatarScaleMod.Logger.Error(
|
||||
$"Attempted to set up height scaler for user which does not exist! : {playerId}");
|
||||
return;
|
||||
}
|
||||
|
||||
AvatarScaleMod.Logger.Msg(
|
||||
$"Setting up new height scaler for user which has sent a height update! : {playerId}");
|
||||
|
||||
if (_networkedScalers.ContainsKey(playerId))
|
||||
_networkedScalers.Remove(playerId); // ??
|
||||
|
||||
UniversalAvatarScaler scaler = puppetMaster.gameObject.AddComponent<UniversalAvatarScaler>();
|
||||
scaler.Initialize(playerId);
|
||||
|
||||
scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster._initialAvatarHeight,
|
||||
puppetMaster.initialAvatarScale);
|
||||
|
||||
_networkedScalers[playerId] = scaler;
|
||||
|
||||
scaler.SetTargetHeight(targetHeight); // set initial height
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
|
@ -122,7 +122,7 @@ public class ScaledScaleConstraint
|
|||
public void Scale(float scaleFactor)
|
||||
{
|
||||
Component.scaleAtRest = InitialScaleAtRest * scaleFactor;
|
||||
Component.scaleOffset = InitialScaleOffset * scaleFactor;
|
||||
// Component.scaleOffset = InitialScaleOffset * scaleFactor;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using ABI_RC.Core;
|
||||
using ABI_RC.Core.Player;
|
||||
using ABI.CCK.Components;
|
||||
using NAK.AvatarScaleMod.ScaledComponents;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
|
@ -22,6 +23,12 @@ public class UniversalAvatarScaler : MonoBehaviour
|
|||
|
||||
#region Variables
|
||||
|
||||
internal bool requestedInitial;
|
||||
|
||||
[NonSerialized]
|
||||
internal string ownerId;
|
||||
|
||||
private Transform _avatarTransform;
|
||||
private CVRAnimatorManager _animatorManager;
|
||||
|
||||
private float _initialHeight;
|
||||
|
@ -33,52 +40,106 @@ public class UniversalAvatarScaler : MonoBehaviour
|
|||
private bool _isLocalAvatar;
|
||||
private bool _heightWasUpdated;
|
||||
|
||||
private bool _isAvatarInstantiated;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
private async void Start()
|
||||
{
|
||||
await FindComponentsOfTypeAsync(scalableComponentTypes);
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
ScaleAvatarRoot(); // override animation-based scaling
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
ClearComponentLists();
|
||||
if (!_isLocalAvatar) AvatarScaleManager.Instance.RemoveNetworkHeightScaler(ownerId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void Initialize(float initialHeight, Vector3 initialScale)
|
||||
public void Initialize(string playerId = null)
|
||||
{
|
||||
_initialHeight = _targetHeight = initialHeight;
|
||||
_initialScale = initialScale;
|
||||
_scaleFactor = 1f;
|
||||
|
||||
ownerId = playerId;
|
||||
|
||||
_isLocalAvatar = gameObject.layer == 8;
|
||||
|
||||
_animatorManager = _isLocalAvatar
|
||||
? GetComponentInParent<PlayerSetup>().animatorManager
|
||||
: GetComponentInParent<PuppetMaster>()._animatorManager;
|
||||
|
||||
|
||||
_heightWasUpdated = false;
|
||||
_isAvatarInstantiated = false;
|
||||
}
|
||||
|
||||
public void SetHeight(float height)
|
||||
public async void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
{
|
||||
_targetHeight = Mathf.Clamp(height, MinHeight, MaxHeight);
|
||||
if (avatarObject == null)
|
||||
{
|
||||
AvatarScaleMod.Logger.Error("Avatar was somehow null?????");
|
||||
return;
|
||||
}
|
||||
AvatarScaleMod.Logger.Msg($"Avatar Object : {_avatarTransform} : {_avatarTransform == null}");
|
||||
|
||||
if (_isAvatarInstantiated) return;
|
||||
_isAvatarInstantiated = true;
|
||||
|
||||
// if we don't have a queued height update, apply initial scaling
|
||||
if (!_heightWasUpdated)
|
||||
_targetHeight = initialHeight;
|
||||
|
||||
_initialHeight = initialHeight;
|
||||
_initialScale = initialScale;
|
||||
_scaleFactor = _targetHeight / _initialHeight;
|
||||
|
||||
_avatarTransform = avatarObject.transform;
|
||||
await FindComponentsOfTypeAsync(scalableComponentTypes);
|
||||
|
||||
ApplyScaling(); // apply queued scaling if avatar was loading
|
||||
}
|
||||
|
||||
public void OnAvatarDestroyed(bool shouldPersist = false)
|
||||
{
|
||||
if (!_isAvatarInstantiated) return;
|
||||
_isAvatarInstantiated = false;
|
||||
|
||||
AvatarScaleMod.Logger.Msg($"Destroying Avatar Object : {_avatarTransform} : {_avatarTransform == null}");
|
||||
|
||||
_avatarTransform = null;
|
||||
_heightWasUpdated = shouldPersist;
|
||||
ClearComponentLists();
|
||||
}
|
||||
|
||||
public void SetTargetHeight(float height)
|
||||
{
|
||||
if (Math.Abs(height - _targetHeight) < float.Epsilon)
|
||||
return;
|
||||
|
||||
_targetHeight = Mathf.Clamp(height, MinHeight, MaxHeight);
|
||||
|
||||
_heightWasUpdated = true;
|
||||
if (!_isAvatarInstantiated)
|
||||
return;
|
||||
|
||||
_scaleFactor = _targetHeight / _initialHeight;
|
||||
ApplyScaling();
|
||||
}
|
||||
|
||||
public void ResetHeight()
|
||||
{
|
||||
if (Math.Abs(_initialHeight - _targetHeight) < float.Epsilon)
|
||||
return;
|
||||
|
||||
_targetHeight = _initialHeight;
|
||||
_scaleFactor = 1f;
|
||||
|
||||
_heightWasUpdated = true;
|
||||
if (!_isAvatarInstantiated)
|
||||
return;
|
||||
|
||||
_scaleFactor = 1f;
|
||||
ApplyScaling();
|
||||
}
|
||||
|
||||
|
@ -87,13 +148,21 @@ public class UniversalAvatarScaler : MonoBehaviour
|
|||
return _targetHeight;
|
||||
}
|
||||
|
||||
public bool IsValid()
|
||||
{
|
||||
return _isAvatarInstantiated;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void ScaleAvatarRoot()
|
||||
{
|
||||
transform.localScale = _initialScale * _scaleFactor;
|
||||
if (_avatarTransform == null)
|
||||
return;
|
||||
|
||||
_avatarTransform.localScale = _initialScale * _scaleFactor;
|
||||
}
|
||||
|
||||
private void UpdateAnimatorParameter()
|
||||
|
@ -109,8 +178,9 @@ public class UniversalAvatarScaler : MonoBehaviour
|
|||
|
||||
private void ApplyScaling()
|
||||
{
|
||||
if (!_heightWasUpdated)
|
||||
if (_avatarTransform == null)
|
||||
return;
|
||||
|
||||
_heightWasUpdated = false;
|
||||
|
||||
ScaleAvatarRoot();
|
||||
|
@ -138,10 +208,19 @@ public class UniversalAvatarScaler : MonoBehaviour
|
|||
private readonly List<ScaledPositionConstraint> _scaledPositionConstraints = new List<ScaledPositionConstraint>();
|
||||
private readonly List<ScaledScaleConstraint> _scaledScaleConstraints = new List<ScaledScaleConstraint>();
|
||||
|
||||
private void ClearComponentLists()
|
||||
{
|
||||
_scaledLights.Clear();
|
||||
_scaledAudioSources.Clear();
|
||||
_scaledParentConstraints.Clear();
|
||||
_scaledPositionConstraints.Clear();
|
||||
_scaledScaleConstraints.Clear();
|
||||
}
|
||||
|
||||
private async Task FindComponentsOfTypeAsync(Type[] types)
|
||||
{
|
||||
var tasks = new List<Task>();
|
||||
var components = GetComponentsInChildren<Component>(true);
|
||||
var components = _avatarTransform.gameObject.GetComponentsInChildren<Component>(true);
|
||||
|
||||
foreach (Component component in components)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue