quantum-space-buddies/QSB/Animation/QSBNetworkAnimator.cs

374 lines
13 KiB
C#
Raw Normal View History

2020-11-08 16:10:10 +00:00
using QSB.Animation.Events;
2020-12-02 21:21:43 +00:00
using QSB.QuantumUNET;
2020-11-08 14:41:16 +00:00
using UnityEngine;
using UnityEngine.Networking;
namespace QSB.Animation
{
2020-11-08 16:10:10 +00:00
// Cleaned up unity code. UNET is so broken I gave up and fixed it myself.
2020-12-02 09:51:53 +00:00
class QSBNetworkAnimator : QSBNetworkBehaviour
2020-11-08 14:41:16 +00:00
{
2020-11-08 16:10:10 +00:00
private static QSBAnimationMessage AnimationMessage = new QSBAnimationMessage();
private static QSBAnimationParametersMessage ParametersMessage = new QSBAnimationParametersMessage();
private static QSBAnimationTriggerMessage TriggersMessage = new QSBAnimationTriggerMessage();
2020-11-08 14:41:16 +00:00
private Animator m_Animator;
private uint m_ParameterSendBits;
private int m_AnimationHash;
private int m_TransitionHash;
private NetworkWriter m_ParameterWriter;
private float m_SendTimer;
public Animator animator
{
get
{
return m_Animator;
}
set
{
m_Animator = value;
2020-11-08 16:10:10 +00:00
m_ParameterSendBits = 0U;
2020-11-08 14:41:16 +00:00
}
}
public void SetParameterAutoSend(int index, bool value)
{
if (value)
m_ParameterSendBits |= (uint)(1 << index);
else
m_ParameterSendBits &= (uint)~(1 << index);
}
public bool GetParameterAutoSend(int index)
{
return ((int)m_ParameterSendBits & 1 << index) != 0;
}
public override void OnStartAuthority()
{
m_ParameterWriter = new NetworkWriter();
}
private void FixedUpdate()
{
if (m_ParameterWriter == null)
{
return;
}
CheckSendRate();
if (!CheckAnimStateChanged(out var stateHash, out var normalizedTime))
{
return;
}
var animationMessage = new QSBAnimationMessage
{
2020-12-02 09:51:53 +00:00
netId = NetId,
2020-11-08 14:41:16 +00:00
stateHash = stateHash,
normalizedTime = normalizedTime
};
2020-11-08 16:10:10 +00:00
2020-11-08 14:41:16 +00:00
m_ParameterWriter.SeekZero();
WriteParameters(m_ParameterWriter, false);
animationMessage.parameters = m_ParameterWriter.ToArray();
2020-11-08 16:10:10 +00:00
2020-12-02 12:42:26 +00:00
if (HasAuthority || QSBClientScene.readyConnection != null)
2020-11-08 14:41:16 +00:00
{
2020-12-02 12:42:26 +00:00
QSBClientScene.readyConnection.Send(40, animationMessage);
2020-11-08 14:41:16 +00:00
}
else
{
2020-12-02 09:51:53 +00:00
if (!IsServer || LocalPlayerAuthority)
2020-11-08 16:10:10 +00:00
{
2020-11-08 14:41:16 +00:00
return;
2020-11-08 16:10:10 +00:00
}
2020-12-02 18:40:38 +00:00
QSBNetworkServer.SendToReady(gameObject, 40, animationMessage);
2020-11-08 14:41:16 +00:00
}
}
private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime)
{
stateHash = 0;
normalizedTime = 0.0f;
if (m_Animator.IsInTransition(0))
{
var animatorTransitionInfo = m_Animator.GetAnimatorTransitionInfo(0);
if (animatorTransitionInfo.fullPathHash == m_TransitionHash)
{
return false;
}
m_TransitionHash = animatorTransitionInfo.fullPathHash;
m_AnimationHash = 0;
return true;
}
var animatorStateInfo = m_Animator.GetCurrentAnimatorStateInfo(0);
if (animatorStateInfo.fullPathHash == m_AnimationHash)
{
return false;
}
if (m_AnimationHash != 0)
{
stateHash = animatorStateInfo.fullPathHash;
normalizedTime = animatorStateInfo.normalizedTime;
}
m_TransitionHash = 0;
m_AnimationHash = animatorStateInfo.fullPathHash;
return true;
}
private void CheckSendRate()
{
if (GetNetworkSendInterval() == 0.0 || m_SendTimer >= Time.time)
{
return;
}
m_SendTimer = Time.time + GetNetworkSendInterval();
var parametersMessage = new QSBAnimationParametersMessage
{
2020-12-02 09:51:53 +00:00
netId = NetId
2020-11-08 14:41:16 +00:00
};
m_ParameterWriter.SeekZero();
WriteParameters(m_ParameterWriter, true);
parametersMessage.parameters = m_ParameterWriter.ToArray();
2020-12-02 12:42:26 +00:00
if (HasAuthority && QSBClientScene.readyConnection != null)
2020-11-08 14:41:16 +00:00
{
2020-12-02 12:42:26 +00:00
QSBClientScene.readyConnection.Send(41, parametersMessage);
2020-11-08 14:41:16 +00:00
}
else
{
2020-12-02 09:51:53 +00:00
if (!IsServer || LocalPlayerAuthority)
2020-11-08 14:41:16 +00:00
return;
2020-12-02 18:40:38 +00:00
QSBNetworkServer.SendToReady(gameObject, 41, parametersMessage);
2020-11-08 14:41:16 +00:00
}
}
internal void HandleAnimMsg(QSBAnimationMessage msg, NetworkReader reader)
{
2020-12-02 09:51:53 +00:00
if (HasAuthority)
2020-11-08 14:41:16 +00:00
{
return;
}
if (msg.stateHash != 0)
{
m_Animator.Play(msg.stateHash, 0, msg.normalizedTime);
}
ReadParameters(reader, false);
}
internal void HandleAnimParamsMsg(QSBAnimationParametersMessage msg, NetworkReader reader)
{
2020-12-02 09:51:53 +00:00
if (HasAuthority)
2020-11-08 14:41:16 +00:00
{
return;
}
ReadParameters(reader, true);
}
internal void HandleAnimTriggerMsg(int hash)
{
m_Animator.SetTrigger(hash);
}
private void WriteParameters(NetworkWriter writer, bool autoSend)
{
for (int index = 0; index < m_Animator.parameters.Length; ++index)
{
if (!autoSend || GetParameterAutoSend(index))
{
var parameter = m_Animator.parameters[index];
switch (parameter.type)
{
case AnimatorControllerParameterType.Int:
writer.WritePackedUInt32((uint)m_Animator.GetInteger(parameter.nameHash));
break;
case AnimatorControllerParameterType.Float:
writer.Write(m_Animator.GetFloat(parameter.nameHash));
break;
case AnimatorControllerParameterType.Bool:
writer.Write(m_Animator.GetBool(parameter.nameHash));
break;
}
}
}
}
private void ReadParameters(NetworkReader reader, bool autoSend)
{
for (int index = 0; index < m_Animator.parameters.Length; ++index)
{
if (!autoSend || GetParameterAutoSend(index))
{
var parameter = m_Animator.parameters[index];
if (reader.Length == reader.Position)
{
return;
}
switch (parameter.type)
{
case AnimatorControllerParameterType.Int:
var num = (int)reader.ReadPackedUInt32();
m_Animator.SetInteger(parameter.nameHash, num);
break;
case AnimatorControllerParameterType.Float:
var single = reader.ReadSingle();
m_Animator.SetFloat(parameter.nameHash, single);
break;
case AnimatorControllerParameterType.Bool:
var flag = reader.ReadBoolean();
m_Animator.SetBool(parameter.nameHash, flag);
break;
}
}
}
}
public override bool OnSerialize(NetworkWriter writer, bool forceAll)
{
if (!forceAll)
{
return false;
}
if (m_Animator.IsInTransition(0))
{
var animatorStateInfo = m_Animator.GetNextAnimatorStateInfo(0);
writer.Write(animatorStateInfo.fullPathHash);
writer.Write(animatorStateInfo.normalizedTime);
}
else
{
var animatorStateInfo = m_Animator.GetCurrentAnimatorStateInfo(0);
writer.Write(animatorStateInfo.fullPathHash);
writer.Write(animatorStateInfo.normalizedTime);
}
WriteParameters(writer, false);
return true;
}
public override void OnDeserialize(NetworkReader reader, bool initialState)
{
if (!initialState)
{
return;
}
var stateNameHash = reader.ReadInt32();
var normalizedTime = reader.ReadSingle();
ReadParameters(reader, false);
m_Animator.Play(stateNameHash, 0, normalizedTime);
}
public void SetTrigger(string triggerName)
{
SetTrigger(Animator.StringToHash(triggerName));
}
public void SetTrigger(int hash)
{
var animationTriggerMessage = new QSBAnimationTriggerMessage
{
2020-12-02 09:51:53 +00:00
netId = NetId,
2020-11-08 14:41:16 +00:00
hash = hash
};
2020-12-02 09:51:53 +00:00
if (HasAuthority && LocalPlayerAuthority)
2020-11-08 14:41:16 +00:00
{
2020-12-02 18:40:38 +00:00
if (QSBNetworkClient.allClients.Count <= 0)
2020-11-08 14:41:16 +00:00
{
return;
}
2020-12-02 12:42:26 +00:00
var readyConnection = QSBClientScene.readyConnection;
2020-11-08 14:41:16 +00:00
if (readyConnection == null)
{
return;
}
readyConnection.Send(42, animationTriggerMessage);
}
else
{
2020-12-02 09:51:53 +00:00
if (!IsServer || LocalPlayerAuthority)
2020-11-08 14:41:16 +00:00
{
return;
}
2020-12-02 18:40:38 +00:00
QSBNetworkServer.SendToReady(gameObject, 42, animationTriggerMessage);
2020-11-08 14:41:16 +00:00
}
}
2020-12-02 12:42:26 +00:00
internal static void OnAnimationServerMessage(QSBNetworkMessage netMsg)
2020-11-08 14:41:16 +00:00
{
2020-11-08 16:10:10 +00:00
netMsg.ReadMessage(AnimationMessage);
2020-12-02 18:40:38 +00:00
var localObject = QSBNetworkServer.FindLocalObject(AnimationMessage.netId);
2020-11-08 16:10:10 +00:00
if (localObject == null)
{
2020-11-08 14:41:16 +00:00
return;
2020-11-08 16:10:10 +00:00
}
var component = localObject.GetComponent<QSBNetworkAnimator>();
var reader = new NetworkReader(AnimationMessage.parameters);
component?.HandleAnimMsg(AnimationMessage, reader);
2020-12-02 18:40:38 +00:00
QSBNetworkServer.SendToReady(localObject, 40, AnimationMessage);
2020-11-08 14:41:16 +00:00
}
2020-12-02 12:42:26 +00:00
internal static void OnAnimationParametersServerMessage(QSBNetworkMessage netMsg)
2020-11-08 14:41:16 +00:00
{
2020-11-08 16:10:10 +00:00
netMsg.ReadMessage(ParametersMessage);
2020-12-02 18:40:38 +00:00
var localObject = QSBNetworkServer.FindLocalObject(ParametersMessage.netId);
2020-11-08 16:10:10 +00:00
if (localObject == null)
{
2020-11-08 14:41:16 +00:00
return;
2020-11-08 16:10:10 +00:00
}
var component = localObject.GetComponent<QSBNetworkAnimator>();
var reader = new NetworkReader(ParametersMessage.parameters);
component?.HandleAnimParamsMsg(ParametersMessage, reader);
2020-12-02 18:40:38 +00:00
QSBNetworkServer.SendToReady(localObject, 41, ParametersMessage);
2020-11-08 14:41:16 +00:00
}
2020-12-02 12:42:26 +00:00
internal static void OnAnimationTriggerServerMessage(QSBNetworkMessage netMsg)
2020-11-08 14:41:16 +00:00
{
2020-11-08 16:10:10 +00:00
netMsg.ReadMessage(TriggersMessage);
2020-12-02 18:40:38 +00:00
var localObject = QSBNetworkServer.FindLocalObject(TriggersMessage.netId);
2020-11-08 16:10:10 +00:00
if (localObject == null)
{
2020-11-08 14:41:16 +00:00
return;
2020-11-08 16:10:10 +00:00
}
var component = localObject.GetComponent<QSBNetworkAnimator>();
component?.HandleAnimTriggerMsg(TriggersMessage.hash);
2020-12-02 18:40:38 +00:00
QSBNetworkServer.SendToReady(localObject, 42, TriggersMessage);
2020-11-08 14:41:16 +00:00
}
2020-12-02 12:42:26 +00:00
internal static void OnAnimationClientMessage(QSBNetworkMessage netMsg)
2020-11-08 14:41:16 +00:00
{
2020-11-08 16:10:10 +00:00
netMsg.ReadMessage(AnimationMessage);
2020-12-02 12:42:26 +00:00
var localObject = QSBClientScene.FindLocalObject(AnimationMessage.netId);
2020-11-09 21:05:50 +00:00
if (localObject == null)
2020-11-08 14:41:16 +00:00
return;
2020-11-09 21:05:50 +00:00
var component = localObject.GetComponent<QSBNetworkAnimator>();
if (component == null)
2020-11-08 14:41:16 +00:00
return;
2020-11-09 21:05:50 +00:00
var reader = new NetworkReader(AnimationMessage.parameters);
component.HandleAnimMsg(AnimationMessage, reader);
2020-11-08 14:41:16 +00:00
}
2020-12-02 12:42:26 +00:00
internal static void OnAnimationParametersClientMessage(QSBNetworkMessage netMsg)
2020-11-08 14:41:16 +00:00
{
2020-11-09 21:05:50 +00:00
netMsg.ReadMessage(ParametersMessage);
2020-12-02 12:42:26 +00:00
var localObject = QSBClientScene.FindLocalObject(ParametersMessage.netId);
2020-11-09 21:05:50 +00:00
if (localObject == null)
2020-11-08 14:41:16 +00:00
return;
2020-11-09 21:05:50 +00:00
var component = localObject.GetComponent<QSBNetworkAnimator>();
if (component == null)
2020-11-08 14:41:16 +00:00
return;
2020-11-09 21:05:50 +00:00
var reader = new NetworkReader(ParametersMessage.parameters);
component.HandleAnimParamsMsg(ParametersMessage, reader);
2020-11-08 14:41:16 +00:00
}
2020-12-02 12:42:26 +00:00
internal static void OnAnimationTriggerClientMessage(QSBNetworkMessage netMsg)
2020-11-08 14:41:16 +00:00
{
2020-11-09 21:05:50 +00:00
netMsg.ReadMessage(TriggersMessage);
2020-12-02 12:42:26 +00:00
var localObject = QSBClientScene.FindLocalObject(TriggersMessage.netId);
2020-11-09 21:05:50 +00:00
if (localObject == null)
2020-11-08 14:41:16 +00:00
return;
2020-11-09 21:05:50 +00:00
var component = localObject.GetComponent<QSBNetworkAnimator>();
if (component == null)
2020-11-08 14:41:16 +00:00
return;
2020-11-09 21:05:50 +00:00
component.HandleAnimTriggerMsg(TriggersMessage.hash);
2020-11-08 14:41:16 +00:00
}
}
2020-11-08 16:13:10 +00:00
}