online fix
This commit is contained in:
@@ -30,12 +30,14 @@ namespace MegaKoop.Game.Networking
|
||||
private Vector3 remoteTargetVelocity;
|
||||
private bool haveRemoteState;
|
||||
private bool localOverrideSet;
|
||||
private int expectedNetworkId;
|
||||
|
||||
public void AssignOwner(ulong steamId, bool localPlayer)
|
||||
{
|
||||
ownerSteamId = steamId;
|
||||
isLocalPlayer = localPlayer;
|
||||
localOverrideSet = true;
|
||||
CaptureExpectedNetworkId();
|
||||
UpdateAuthority();
|
||||
ConfigureController();
|
||||
}
|
||||
@@ -87,6 +89,7 @@ namespace MegaKoop.Game.Networking
|
||||
private void Start()
|
||||
{
|
||||
networkManager = SteamCoopNetworkManager.Instance;
|
||||
CaptureExpectedNetworkId();
|
||||
UpdateAuthority();
|
||||
ConfigureController();
|
||||
}
|
||||
@@ -94,6 +97,7 @@ namespace MegaKoop.Game.Networking
|
||||
private void OnEnable()
|
||||
{
|
||||
networkManager = SteamCoopNetworkManager.Instance;
|
||||
CaptureExpectedNetworkId();
|
||||
RegisterHandlers();
|
||||
}
|
||||
|
||||
@@ -221,6 +225,11 @@ namespace MegaKoop.Game.Networking
|
||||
return;
|
||||
}
|
||||
|
||||
if (identity.NetworkId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var unityController = GetComponent<CharacterController>();
|
||||
Vector3 velocity = unityController != null ? unityController.velocity : Vector3.zero;
|
||||
SteamCharacterStateCache.ReportLocalState(identity.NetworkId, rootTransform.position, rootTransform.rotation, velocity);
|
||||
@@ -238,6 +247,11 @@ namespace MegaKoop.Game.Networking
|
||||
return;
|
||||
}
|
||||
|
||||
if (identity.NetworkId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float moveX = animator.GetFloat("MoveX");
|
||||
float moveZ = animator.GetFloat("MoveZ");
|
||||
float speed = animator.GetFloat("Speed");
|
||||
@@ -285,24 +299,9 @@ namespace MegaKoop.Game.Networking
|
||||
}
|
||||
|
||||
CharacterTransformMessage transformMessage = CharacterTransformMessage.Deserialize(message.Payload);
|
||||
if (identity != null && transformMessage.NetworkId != identity.NetworkId)
|
||||
if (!EnsureMatchingNetworkId(transformMessage.NetworkId, message.Sender))
|
||||
{
|
||||
if (ownerSteamId != 0 && message.Sender != ownerSteamId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var existing = NetworkIdRegistry.GetById(transformMessage.NetworkId);
|
||||
if (existing != null && existing != identity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
identity.SetNetworkId(transformMessage.NetworkId);
|
||||
if (identity.NetworkId != transformMessage.NetworkId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
remoteTargetPosition = transformMessage.Position;
|
||||
@@ -319,24 +318,9 @@ namespace MegaKoop.Game.Networking
|
||||
}
|
||||
|
||||
CharacterAnimMessage anim = CharacterAnimMessage.Deserialize(message.Payload);
|
||||
if (identity != null && anim.NetworkId != identity.NetworkId)
|
||||
if (!EnsureMatchingNetworkId(anim.NetworkId, message.Sender))
|
||||
{
|
||||
if (ownerSteamId != 0 && message.Sender != ownerSteamId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var existing = NetworkIdRegistry.GetById(anim.NetworkId);
|
||||
if (existing != null && existing != identity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
identity.SetNetworkId(anim.NetworkId);
|
||||
if (identity.NetworkId != anim.NetworkId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (animator == null)
|
||||
@@ -358,6 +342,93 @@ namespace MegaKoop.Game.Networking
|
||||
animator.SetBool("IsJumping", anim.IsJumping);
|
||||
}
|
||||
|
||||
private void CaptureExpectedNetworkId()
|
||||
{
|
||||
if (identity == null)
|
||||
{
|
||||
identity = GetComponent<NetworkIdentity>();
|
||||
}
|
||||
|
||||
if (identity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (identity.NetworkId != 0)
|
||||
{
|
||||
expectedNetworkId = identity.NetworkId;
|
||||
if (ownerSteamId != 0)
|
||||
{
|
||||
var mapped = NetworkIdRegistry.GetNetworkIdForSteamId(ownerSteamId);
|
||||
if (mapped != expectedNetworkId)
|
||||
{
|
||||
NetworkIdRegistry.MapSteamIdToNetworkId(ownerSteamId, expectedNetworkId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool EnsureMatchingNetworkId(int incomingId, ulong sender)
|
||||
{
|
||||
if (identity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (incomingId == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int currentId = identity.NetworkId;
|
||||
if (currentId == incomingId)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (currentId != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ownerSteamId != 0 && sender != ownerSteamId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int mappedId = ownerSteamId != 0 ? NetworkIdRegistry.GetNetworkIdForSteamId(ownerSteamId) : expectedNetworkId;
|
||||
if (mappedId != 0)
|
||||
{
|
||||
identity.SetNetworkId(mappedId);
|
||||
if (identity.NetworkId == mappedId)
|
||||
{
|
||||
expectedNetworkId = mappedId;
|
||||
return mappedId == incomingId;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!NetworkIdAllocator.IsPlayerId(incomingId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
identity.SetNetworkId(incomingId);
|
||||
if (identity.NetworkId != incomingId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
expectedNetworkId = incomingId;
|
||||
if (ownerSteamId != 0)
|
||||
{
|
||||
NetworkIdRegistry.MapSteamIdToNetworkId(ownerSteamId, incomingId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SendLocalInput(Vector2 moveInput, bool jump)
|
||||
{
|
||||
if (networkManager == null || identity == null || !isAuthority)
|
||||
@@ -370,6 +441,11 @@ namespace MegaKoop.Game.Networking
|
||||
return;
|
||||
}
|
||||
|
||||
if (identity.NetworkId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
networkManager.SendToAll(NetworkMessageType.PlayerInput, PlayerInputMessage.Serialize(new PlayerInputMessage(identity.NetworkId, moveInput, jump)), EP2PSend.k_EP2PSendUnreliableNoDelay);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user