[AvatarScaleMod] Further improvments. Still needs a lot...

This commit is contained in:
NotAKidoS 2023-09-24 06:15:31 -05:00
parent 553da7edb3
commit ff06c20c01
7 changed files with 153 additions and 393 deletions

View file

@ -1,2 +1,36 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" /> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn />
</PropertyGroup>
<ItemGroup>
<None Remove="resources\menu.js" />
<None Remove="resources\nak_menu.css" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="resources\menu.js" />
<EmbeddedResource Include="resources\nak_menu.css" />
</ItemGroup>
<ItemGroup>
<Reference Include="BTKUILib">
<HintPath>$(MsBuildThisFileDirectory)\..\.ManagedLibs\BTKUILib.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
</Project>

View file

@ -1,7 +1,7 @@
using ABI_RC.Core.IO; using ABI_RC.Core.IO;
using ABI_RC.Core.Player; using ABI_RC.Core.Player;
using ABI_RC.Core.Player.AvatarTracking;
using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.GameEventSystem;
using NAK.AvatarScaleMod.Components;
using NAK.AvatarScaleMod.Networking; using NAK.AvatarScaleMod.Networking;
using UnityEngine; using UnityEngine;
@ -9,12 +9,19 @@ namespace NAK.AvatarScaleMod.AvatarScaling;
public class AvatarScaleManager : MonoBehaviour public class AvatarScaleManager : MonoBehaviour
{ {
// Universal Scaling Limits
public const float MinHeight = 0.1f;
public const float MaxHeight = 10f;
public static AvatarScaleManager Instance; public static AvatarScaleManager Instance;
public bool Setting_PersistantHeight = false; private LocalScaler _localAvatarScaler;
private Dictionary<string, NetworkScaler> _networkedScalers;
private Dictionary<string, UniversalAvatarScaler> _networkedScalers;
private UniversalAvatarScaler _localAvatarScaler; public bool Setting_UniversalScaling = true;
public bool Setting_PersistantHeight;
private float _lastTargetHeight = -1;
#region Unity Methods #region Unity Methods
@ -27,7 +34,7 @@ public class AvatarScaleManager : MonoBehaviour
} }
Instance = this; Instance = this;
_networkedScalers = new Dictionary<string, UniversalAvatarScaler>(); _networkedScalers = new Dictionary<string, NetworkScaler>();
} }
private void Start() private void Start()
@ -44,13 +51,19 @@ public class AvatarScaleManager : MonoBehaviour
#endregion #endregion
#region Game Events #region Events
public void OnInstanceConnected(string instanceId) public void OnInstanceConnected(string instanceId)
{ {
SchedulerSystem.AddJob(ModNetwork.RequestHeightSync, 2f, 1f, 1); SchedulerSystem.AddJob(ModNetwork.RequestHeightSync, 2f, 1f, 1);
} }
public void OnSettingsChanged()
{
Setting_UniversalScaling = ModSettings.EntryUniversalScaling.Value;
SetHeight(Setting_UniversalScaling ? _lastTargetHeight : -1);
}
#endregion #endregion
#region Local Methods #region Local Methods
@ -62,24 +75,23 @@ public class AvatarScaleManager : MonoBehaviour
if (_localAvatarScaler == null) if (_localAvatarScaler == null)
{ {
_localAvatarScaler = playerSetup.gameObject.AddComponent<UniversalAvatarScaler>(); _localAvatarScaler = playerSetup.gameObject.AddComponent<LocalScaler>();
_localAvatarScaler.Initialize(); _localAvatarScaler.Initialize();
} }
_localAvatarScaler.OnAvatarInstantiated(playerSetup._avatar, playerSetup._initialAvatarHeight, _localAvatarScaler.OnAvatarInstantiated(playerSetup._avatar, playerSetup._initialAvatarHeight,
playerSetup.initialScale); playerSetup.initialScale);
if (Setting_PersistantHeight && _localAvatarScaler.IsValid()) if (Setting_PersistantHeight)
SchedulerSystem.AddJob(() => { ModNetwork.SendNetworkHeight(_localAvatarScaler.GetHeight()); }, 0.5f, 0f, 1); SetHeight(_lastTargetHeight);
else if (_lastTargetHeight > 0)
SetHeight(playerSetup._initialAvatarHeight);
} }
public void OnAvatarDestroyed(PlayerSetup playerSetup) public void OnAvatarDestroyed(PlayerSetup playerSetup)
{ {
if (_localAvatarScaler != null) if (_localAvatarScaler != null)
_localAvatarScaler.OnAvatarDestroyed(Setting_PersistantHeight); _localAvatarScaler.OnAvatarDestroyed();
if (Setting_PersistantHeight && _localAvatarScaler.IsValid())
SchedulerSystem.AddJob(() => { ModNetwork.SendNetworkHeight(_localAvatarScaler.GetHeight()); }, 0.5f, 0f, 1);
} }
public void SetHeight(float targetHeight) public void SetHeight(float targetHeight)
@ -87,6 +99,8 @@ public class AvatarScaleManager : MonoBehaviour
if (_localAvatarScaler == null) if (_localAvatarScaler == null)
return; return;
_lastTargetHeight = targetHeight;
_localAvatarScaler.SetTargetHeight(targetHeight); _localAvatarScaler.SetTargetHeight(targetHeight);
ModNetwork.SendNetworkHeight(targetHeight); ModNetwork.SendNetworkHeight(targetHeight);
@ -98,11 +112,42 @@ public class AvatarScaleManager : MonoBehaviour
{ {
if (_localAvatarScaler != null) if (_localAvatarScaler != null)
_localAvatarScaler.ResetHeight(); _localAvatarScaler.ResetHeight();
ModNetwork.SendNetworkHeight(-1f);
} }
public float GetHeight() public float GetHeight()
{ {
return _localAvatarScaler != null ? _localAvatarScaler.GetHeight() : -1f; if (_localAvatarScaler == null)
return -1f;
return _localAvatarScaler.GetHeight();
}
public float GetHeightForNetwork()
{
if (_localAvatarScaler == null)
return -1f;
if (!_localAvatarScaler.IsHeightAdjustedFromInitial())
return -1f;
return _localAvatarScaler.GetHeight();
}
public float GetInitialHeight()
{
if (_localAvatarScaler == null)
return -1f;
return _localAvatarScaler.GetInitialHeight();
}
public bool IsHeightAdjustedFromInitial()
{
if (_localAvatarScaler == null)
return false;
return _localAvatarScaler.IsHeightAdjustedFromInitial();
} }
#endregion #endregion
@ -111,8 +156,10 @@ public class AvatarScaleManager : MonoBehaviour
public float GetNetworkHeight(string playerId) public float GetNetworkHeight(string playerId)
{ {
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler)) if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
return scaler.GetHeight(); return scaler.GetHeight();
//doesn't have mod, get from player avatar directly
return -1f; return -1f;
} }
@ -121,7 +168,7 @@ public class AvatarScaleManager : MonoBehaviour
internal void OnNetworkHeightUpdateReceived(string playerId, float targetHeight) internal void OnNetworkHeightUpdateReceived(string playerId, float targetHeight)
{ {
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler)) if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
scaler.SetTargetHeight(targetHeight); scaler.SetTargetHeight(targetHeight);
else else
SetupHeightScalerForNetwork(playerId, targetHeight); SetupHeightScalerForNetwork(playerId, targetHeight);
@ -130,7 +177,7 @@ public class AvatarScaleManager : MonoBehaviour
internal void OnNetworkAvatarInstantiated(PuppetMaster puppetMaster) internal void OnNetworkAvatarInstantiated(PuppetMaster puppetMaster)
{ {
var playerId = puppetMaster._playerDescriptor.ownerId; var playerId = puppetMaster._playerDescriptor.ownerId;
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler)) if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster._initialAvatarHeight, scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster._initialAvatarHeight,
puppetMaster.initialAvatarScale); puppetMaster.initialAvatarScale);
} }
@ -142,7 +189,7 @@ public class AvatarScaleManager : MonoBehaviour
return; return;
var playerId = puppetMaster._playerDescriptor.ownerId; var playerId = puppetMaster._playerDescriptor.ownerId;
if (_networkedScalers.TryGetValue(playerId, out UniversalAvatarScaler scaler)) if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
scaler.OnAvatarDestroyed(); scaler.OnAvatarDestroyed();
} }
@ -181,7 +228,7 @@ public class AvatarScaleManager : MonoBehaviour
if (_networkedScalers.ContainsKey(playerId)) if (_networkedScalers.ContainsKey(playerId))
_networkedScalers.Remove(playerId); // ?? _networkedScalers.Remove(playerId); // ??
UniversalAvatarScaler scaler = puppetMaster.gameObject.AddComponent<UniversalAvatarScaler>(); NetworkScaler scaler = puppetMaster.gameObject.AddComponent<NetworkScaler>();
scaler.Initialize(playerId); scaler.Initialize(playerId);
scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster._initialAvatarHeight, scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster._initialAvatarHeight,

View file

@ -1,310 +0,0 @@
using ABI_RC.Core;
using ABI_RC.Core.Player;
using ABI.CCK.Components;
using NAK.AvatarScaleMod.ScaledComponents;
using UnityEngine;
using UnityEngine.Animations;
namespace NAK.AvatarScaleMod.AvatarScaling;
[DefaultExecutionOrder(-99999)] // before playersetup/puppetmaster but after animator
public class UniversalAvatarScaler : MonoBehaviour
{
#region Constants
// Universal Scaling Limits
private const float MinHeight = 0.1f;
private const float MaxHeight = 10f;
private const string ScaleFactorParameterName = "ScaleFactor";
private const string ScaleFactorParameterNameLocal = "#ScaleFactor";
#endregion
#region Variables
internal bool requestedInitial;
[NonSerialized]
internal string ownerId;
private Transform _avatarTransform;
private CVRAnimatorManager _animatorManager;
private float _initialHeight;
private Vector3 _initialScale;
private float _targetHeight;
private float _scaleFactor = 1f;
private bool _isLocalAvatar;
private bool _heightWasUpdated;
private bool _isAvatarInstantiated;
#endregion
#region Unity Methods
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(string playerId = null)
{
ownerId = playerId;
_isLocalAvatar = gameObject.layer == 8;
_animatorManager = _isLocalAvatar
? GetComponentInParent<PlayerSetup>().animatorManager
: GetComponentInParent<PuppetMaster>()._animatorManager;
_heightWasUpdated = false;
_isAvatarInstantiated = false;
}
public async void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
{
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;
_heightWasUpdated = true;
if (!_isAvatarInstantiated)
return;
_scaleFactor = 1f;
ApplyScaling();
}
public float GetHeight()
{
return _targetHeight;
}
public bool IsValid()
{
return _isAvatarInstantiated;
}
#endregion
#region Private Methods
private void ScaleAvatarRoot()
{
if (_avatarTransform == null)
return;
_avatarTransform.localScale = _initialScale * _scaleFactor;
}
private void UpdateAnimatorParameter()
{
if (_animatorManager == null)
return;
// synced parameter
if (_isLocalAvatar) _animatorManager.SetAnimatorParameter(ScaleFactorParameterName, _scaleFactor);
// local parameter
_animatorManager.SetAnimatorParameter(ScaleFactorParameterNameLocal, _scaleFactor);
}
private void ApplyScaling()
{
if (_avatarTransform == null)
return;
_heightWasUpdated = false;
ScaleAvatarRoot();
UpdateAnimatorParameter();
ApplyComponentScaling();
}
#endregion
#region Component Scaling
private static readonly Type[] scalableComponentTypes =
{
typeof(Light),
typeof(AudioSource),
typeof(ParticleSystem),
typeof(ParentConstraint),
typeof(PositionConstraint),
typeof(ScaleConstraint)
};
private readonly List<ScaledLight> _scaledLights = new List<ScaledLight>();
private readonly List<ScaledAudioSource> _scaledAudioSources = new List<ScaledAudioSource>();
private readonly List<ScaledParentConstraint> _scaledParentConstraints = new List<ScaledParentConstraint>();
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 = _avatarTransform.gameObject.GetComponentsInChildren<Component>(true);
foreach (Component component in components)
{
if (this == null) break;
if (component == null) continue;
tasks.Add(Task.Run(() =>
{
Type componentType = component.GetType();
if (types.Contains(componentType))
{
AddScaledComponent(componentType, component);
}
}));
}
await Task.WhenAll(tasks);
}
private void AddScaledComponent(Type type, Component component)
{
switch (type)
{
case not null when type == typeof(AudioSource):
_scaledAudioSources.Add(new ScaledAudioSource((AudioSource)component));
break;
case not null when type == typeof(Light):
_scaledLights.Add(new ScaledLight((Light)component));
break;
case not null when type == typeof(ParentConstraint):
_scaledParentConstraints.Add(new ScaledParentConstraint((ParentConstraint)component));
break;
case not null when type == typeof(PositionConstraint):
_scaledPositionConstraints.Add(new ScaledPositionConstraint((PositionConstraint)component));
break;
case not null when type == typeof(ScaleConstraint):
_scaledScaleConstraints.Add(new ScaledScaleConstraint((ScaleConstraint)component));
break;
}
}
private void ApplyComponentScaling()
{
// UpdateLightScales(); // might break dps
UpdateAudioSourceScales();
UpdateParentConstraintScales();
UpdatePositionConstraintScales();
UpdateScaleConstraintScales();
}
private void UpdateLightScales()
{
// Update range of each light component
foreach (ScaledLight light in _scaledLights)
light.Scale(_scaleFactor);
}
private void UpdateAudioSourceScales()
{
// Update min and max distance of each audio source component
foreach (ScaledAudioSource audioSource in _scaledAudioSources)
audioSource.Scale(_scaleFactor);
}
private void UpdateParentConstraintScales()
{
// Update translationAtRest and translationOffsets of each parent constraint component
foreach (ScaledParentConstraint parentConstraint in _scaledParentConstraints)
parentConstraint.Scale(_scaleFactor);
}
private void UpdatePositionConstraintScales()
{
// Update translationAtRest and translationOffset of each position constraint component
foreach (ScaledPositionConstraint positionConstraint in _scaledPositionConstraints)
positionConstraint.Scale(_scaleFactor);
}
private void UpdateScaleConstraintScales()
{
// Update scaleAtRest and scaleOffset of each scale constraint component
foreach (ScaledScaleConstraint scaleConstraint in _scaledScaleConstraints)
scaleConstraint.Scale(_scaleFactor);
}
#endregion
}

View file

@ -1,4 +1,5 @@
using MelonLoader; using MelonLoader;
using NAK.AvatarScaleMod.InputHandling;
using NAK.AvatarScaleMod.Networking; using NAK.AvatarScaleMod.Networking;
namespace NAK.AvatarScaleMod; namespace NAK.AvatarScaleMod;
@ -15,6 +16,8 @@ public class AvatarScaleMod : MelonMod
ApplyPatches(typeof(HarmonyPatches.PuppetMasterPatches)); ApplyPatches(typeof(HarmonyPatches.PuppetMasterPatches));
ApplyPatches(typeof(HarmonyPatches.GesturePlaneTestPatches)); ApplyPatches(typeof(HarmonyPatches.GesturePlaneTestPatches));
InitializeIntegration("BTKUILib", Integrations.BTKUIAddon.Initialize);
ModNetwork.Subscribe(); ModNetwork.Subscribe();
ModSettings.InitializeModSettings(); ModSettings.InitializeModSettings();
} }
@ -22,7 +25,16 @@ public class AvatarScaleMod : MelonMod
public override void OnUpdate() public override void OnUpdate()
{ {
ModNetwork.Update(); ModNetwork.Update();
ModNetworkDebugger.DoDebugInput(); DebugKeybinds.DoDebugInput();
}
private static void InitializeIntegration(string modName, Action integrationAction)
{
if (RegisteredMelons.All(it => it.Info.Name != modName))
return;
Logger.Msg($"Initializing {modName} integration.");
integrationAction.Invoke();
} }
private void ApplyPatches(Type type) private void ApplyPatches(Type type)

View file

@ -1,5 +1,6 @@
using MelonLoader; using MelonLoader;
using NAK.AvatarScaleMod.AvatarScaling; using NAK.AvatarScaleMod.AvatarScaling;
using NAK.AvatarScaleMod.Networking;
namespace NAK.AvatarScaleMod; namespace NAK.AvatarScaleMod;
@ -7,11 +8,16 @@ namespace NAK.AvatarScaleMod;
internal static class ModSettings internal static class ModSettings
{ {
internal const string SettingsCategory = nameof(AvatarScaleMod);
public static readonly MelonPreferences_Category Category = public static readonly MelonPreferences_Category Category =
MelonPreferences.CreateCategory(nameof(AvatarScaleMod)); MelonPreferences.CreateCategory(nameof(AvatarScaleMod));
public static MelonPreferences_Entry<bool> PersistantHeight; public static MelonPreferences_Entry<bool> PersistantHeight;
public static MelonPreferences_Entry<bool> Debug_NetworkInbound;
public static MelonPreferences_Entry<bool> Debug_NetworkOutbound;
// AvatarScaleTool supported scaling settings // AvatarScaleTool supported scaling settings
public static readonly MelonPreferences_Entry<bool> EntryEnabled = public static readonly MelonPreferences_Entry<bool> EntryEnabled =
Category.CreateEntry("AvatarScaleTool Scaling", true, description: "Should there be persistant avatar scaling? This only works properly across supported avatars."); Category.CreateEntry("AvatarScaleTool Scaling", true, description: "Should there be persistant avatar scaling? This only works properly across supported avatars.");
@ -69,6 +75,11 @@ internal static class ModSettings
PersistantHeight = Category.CreateEntry("Persistant Height", false, description: "Should the avatar height persist between avatar switches?"); PersistantHeight = Category.CreateEntry("Persistant Height", false, description: "Should the avatar height persist between avatar switches?");
PersistantHeight.OnEntryValueChanged.Subscribe(OnPersistantHeightChanged); PersistantHeight.OnEntryValueChanged.Subscribe(OnPersistantHeightChanged);
Debug_NetworkInbound = Category.CreateEntry("Debug Inbound", false, description: "Log inbound Mod Network height updates.");
Debug_NetworkInbound.OnEntryValueChanged.Subscribe(OnDebugNetworkChanged);
Debug_NetworkOutbound = Category.CreateEntry("Debug Outbound", false, description: "Log outbound Mod Network height updates.");
Debug_NetworkOutbound.OnEntryValueChanged.Subscribe(OnDebugNetworkChanged);
//AvatarScaleManager.UseUniversalScaling = EntryEnabled.Value; //AvatarScaleManager.UseUniversalScaling = EntryEnabled.Value;
//AvatarScaleGesture.GestureEnabled = EntryUseScaleGesture.Value; //AvatarScaleGesture.GestureEnabled = EntryUseScaleGesture.Value;
} }
@ -77,4 +88,10 @@ internal static class ModSettings
{ {
AvatarScaleManager.Instance.Setting_PersistantHeight = newValue; AvatarScaleManager.Instance.Setting_PersistantHeight = newValue;
} }
private static void OnDebugNetworkChanged(bool oldValue, bool newValue)
{
ModNetwork.Debug_NetworkInbound = Debug_NetworkInbound.Value;
ModNetwork.Debug_NetworkOutbound = Debug_NetworkOutbound.Value;
}
} }

View file

@ -12,6 +12,9 @@ namespace NAK.AvatarScaleMod.Networking;
public static class ModNetwork public static class ModNetwork
{ {
public static bool Debug_NetworkInbound = false;
public static bool Debug_NetworkOutbound = false;
#region Constants #region Constants
private const string ModId = "MelonMod.NAK.AvatarScaleMod"; private const string ModId = "MelonMod.NAK.AvatarScaleMod";
@ -87,9 +90,6 @@ public static class ModNetwork
modMsg.Write(height); modMsg.Write(height);
modMsg.Send(); modMsg.Send();
} }
var typeDesc = messageType == MessageType.SyncHeight ? "height" : "height request";
MelonLogger.Msg($"Sending {typeDesc}: {height}");
} }
private static void OnMessageReceived(ModNetworkMessage msg) private static void OnMessageReceived(ModNetworkMessage msg)
@ -115,7 +115,6 @@ public static class ModNetwork
}; };
InboundQueue[msg.Sender] = inboundMessage; InboundQueue[msg.Sender] = inboundMessage;
MelonLogger.Msg($"Received message from {msg.Sender}: {receivedHeight}");
} }
#endregion #endregion
@ -129,7 +128,7 @@ public static class ModNetwork
public static void RequestHeightSync() public static void RequestHeightSync()
{ {
var myCurrentHeight = AvatarScaleManager.Instance.GetHeight(); var myCurrentHeight = AvatarScaleManager.Instance.GetHeightForNetwork();
if (myCurrentHeight > 0) if (myCurrentHeight > 0)
OutboundQueue["global"] = new QueuedMessage { Type = MessageType.RequestHeight, Height = myCurrentHeight }; OutboundQueue["global"] = new QueuedMessage { Type = MessageType.RequestHeight, Height = myCurrentHeight };
} }
@ -144,7 +143,13 @@ public static class ModNetwork
return; return;
foreach (QueuedMessage message in OutboundQueue.Values) foreach (QueuedMessage message in OutboundQueue.Values)
{
SendMessage(message.Type, message.Height, message.TargetPlayer); SendMessage(message.Type, message.Height, message.TargetPlayer);
if (Debug_NetworkOutbound)
AvatarScaleMod.Logger.Msg(
$"Sending message {message.Type.ToString()} to {(string.IsNullOrEmpty(message.TargetPlayer) ? "ALL" : message.TargetPlayer)}: {message.Height}");
}
OutboundQueue.Clear(); OutboundQueue.Clear();
LastSentTime = Time.time; LastSentTime = Time.time;
@ -168,7 +173,7 @@ public static class ModNetwork
if (warnings >= MaxWarnings) if (warnings >= MaxWarnings)
{ {
UserTimeouts[userId] = Time.time + TimeoutDuration; UserTimeouts[userId] = Time.time + TimeoutDuration;
MelonLogger.Msg($"User is sending height updates too fast! Applying 10s timeout... : {userId}"); AvatarScaleMod.Logger.Warning($"User is sending height updates too fast! Applying 10s timeout... : {userId}");
return true; return true;
} }
} }
@ -182,23 +187,23 @@ public static class ModNetwork
LastReceivedTimes[userId] = Time.time; LastReceivedTimes[userId] = Time.time;
UserWarnings.Remove(userId); // Reset warnings UserWarnings.Remove(userId); // Reset warnings
// MelonLogger.Msg($"Clearing timeout from user : {userId}");
return false; return false;
} }
private static void ProcessInboundQueue() private static void ProcessInboundQueue()
{ {
foreach (QueuedMessage message in InboundQueue.Values) foreach (QueuedMessage message in InboundQueue.Values)
{
switch (message.Type) switch (message.Type)
{ {
case MessageType.RequestHeight: case MessageType.RequestHeight:
{ {
var myCurrentHeight = AvatarScaleManager.Instance.GetHeight(); var myNetworkHeight = AvatarScaleManager.Instance.GetHeightForNetwork();
if (myCurrentHeight > 0) if (myNetworkHeight > 0)
OutboundQueue[message.Sender] = new QueuedMessage OutboundQueue[message.Sender] = new QueuedMessage
{ {
Type = MessageType.SyncHeight, Type = MessageType.SyncHeight,
Height = myCurrentHeight, Height = myNetworkHeight,
TargetPlayer = message.Sender TargetPlayer = message.Sender
}; };
@ -212,6 +217,10 @@ public static class ModNetwork
AvatarScaleMod.Logger.Error($"Invalid message type received from: {message.Sender}"); AvatarScaleMod.Logger.Error($"Invalid message type received from: {message.Sender}");
break; break;
} }
if (Debug_NetworkInbound)
AvatarScaleMod.Logger.Msg($"Received message {message.Type.ToString()} from {message.Sender}: {message.Height}");
}
InboundQueue.Clear(); InboundQueue.Clear();
} }

View file

@ -1,49 +0,0 @@
using NAK.AvatarScaleMod.AvatarScaling;
using UnityEngine;
namespace NAK.AvatarScaleMod.Networking;
public static class ModNetworkDebugger
{
public static void DoDebugInput()
{
// if (NetworkManager.Instance == null || NetworkManager.Instance.GameNetwork.ConnectionState != ConnectionState.Connected)
// {
// MelonLogger.Warning("Attempted to send a game network message without being connected to an online instance...");
// return;
// }
if (AvatarScaleManager.Instance == null)
return;
float currentHeight;
const float step = 0.1f;
if (Input.GetKeyDown(KeyCode.Equals) || Input.GetKeyDown(KeyCode.KeypadPlus))
{
currentHeight = AvatarScaleManager.Instance.GetHeight();
AvatarScaleManager.Instance.SetHeight(currentHeight + step);
currentHeight = AvatarScaleManager.Instance.GetHeight();
ModNetwork.SendNetworkHeight(currentHeight);
AvatarScaleMod.Logger.Msg($"Networking height: {currentHeight}");
}
else if (Input.GetKeyDown(KeyCode.Minus) || Input.GetKeyDown(KeyCode.KeypadMinus))
{
currentHeight = AvatarScaleManager.Instance.GetHeight();
AvatarScaleManager.Instance.SetHeight(currentHeight - step);
currentHeight = AvatarScaleManager.Instance.GetHeight();
ModNetwork.SendNetworkHeight(currentHeight);
AvatarScaleMod.Logger.Msg($"Networking height: {currentHeight}");
}
else if (Input.GetKeyDown(KeyCode.Backspace))
{
AvatarScaleManager.Instance.ResetHeight();
currentHeight = AvatarScaleManager.Instance.GetHeight();
AvatarScaleMod.Logger.Msg($"Networking height: {currentHeight}");
ModNetwork.SendNetworkHeight(currentHeight);
}
}
}