diff options
author | chai <chaifix@163.com> | 2021-01-04 08:27:46 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-01-04 08:27:46 +0800 |
commit | b0f6c6df9e8ae1688646ab8c8934708123e4e979 (patch) | |
tree | 3c77641aaf068d50b4d5503c52bb65518d4a1afe | |
parent | 89d302af2b9f84befd7497e52f1d72162721cd70 (diff) |
*网络
-rw-r--r-- | Client/Assembly-CSharp/CustomNetworkTransform.cs | 9 | ||||
-rw-r--r-- | Client/Assembly-CSharp/InnerNet/InnerNetClient.cs | 17 | ||||
-rw-r--r-- | Client/Assembly-CSharp/InnerNet/MessageExtensions.cs | 4 | ||||
-rw-r--r-- | Client/Assembly-CSharp/Msg.cs | 12 | ||||
-rw-r--r-- | Client/Assembly-CSharp/PlayerControl.cs | 50 | ||||
-rw-r--r-- | Client/Assembly-CSharp/PlayerPhysics.cs | 4 | ||||
-rw-r--r-- | Client/Assembly-CSharp/ShipStatus.cs | 8 |
7 files changed, 62 insertions, 42 deletions
diff --git a/Client/Assembly-CSharp/CustomNetworkTransform.cs b/Client/Assembly-CSharp/CustomNetworkTransform.cs index 56113b1..5a15a11 100644 --- a/Client/Assembly-CSharp/CustomNetworkTransform.cs +++ b/Client/Assembly-CSharp/CustomNetworkTransform.cs @@ -58,13 +58,13 @@ public class CustomNetworkTransform : InnerNetObject public void Halt() { - ushort minSid = this.lastSequenceId + 1; + ushort minSid = (ushort)(this.lastSequenceId + 1); this.SnapTo(base.transform.position, minSid); } public void RpcSnapTo(Vector2 position) { - ushort minSid = this.lastSequenceId + 5; + ushort minSid = (ushort)(this.lastSequenceId + 5); if (AmongUsClient.Instance.AmClient) { this.SnapTo(position, minSid); @@ -77,7 +77,7 @@ public class CustomNetworkTransform : InnerNetObject public void SnapTo(Vector2 position) { - ushort minSid = this.lastSequenceId + 3; + ushort minSid = (ushort)(this.lastSequenceId + 3); this.SnapTo(position, minSid); } @@ -220,6 +220,7 @@ public class CustomNetworkTransform : InnerNetObject this.targetSyncPosition = this.ReadVector2(reader); this.targetSyncVelocity = this.ReadVector2(reader); // 当本地计算的位置数据和网络传输的位置数据差别大于snapThreshold的时候直接用消息里的位置和速度数据 + // 实际上是一种预测的策略 if (Vector2.Distance(this.body.position, this.targetSyncPosition) > this.snapThreshold) { if (this.body) @@ -240,7 +241,7 @@ public class CustomNetworkTransform : InnerNetObject private static bool SidGreaterThan(ushort newSid, ushort prevSid) { - ushort num = prevSid + 32767; + ushort num = (ushort)(prevSid + 32767); if (prevSid < num) { return newSid > prevSid && newSid <= num; diff --git a/Client/Assembly-CSharp/InnerNet/InnerNetClient.cs b/Client/Assembly-CSharp/InnerNet/InnerNetClient.cs index 4a68332..5806605 100644 --- a/Client/Assembly-CSharp/InnerNet/InnerNetClient.cs +++ b/Client/Assembly-CSharp/InnerNet/InnerNetClient.cs @@ -635,6 +635,7 @@ namespace InnerNet } // 结束游戏 + // MsgWrite:EndGame public MessageWriter StartEndGame() { MessageWriter messageWriter = MessageWriter.Get(SendOption.Reliable); @@ -819,7 +820,7 @@ namespace InnerNet this.EnqueueDisconnect(reason3, null); return; } - case 4: + case Tags.RemovePlayer: break; case (int)TagAlias.GameData: case (int)TagAlias.GameDataTo: // 把这类消息存在队列里,主线程后续调用 @@ -1665,11 +1666,15 @@ namespace InnerNet { uint netId = reader.ReadPackedUInt32(); InnerNetObject innerNetObject; - if (this.allObjectsFast.TryGetValue(netId, out innerNetObject)) + if (this.allObjectsFast.TryGetValue(netId, out innerNetObject)) // 如果有这个netObject { + // 反序列化,更新数据 innerNetObject.Deserialize(reader, false); return; } + // 如果没有这个netObject且DestroyedObjects里也没有记录这个netId已经被销毁,那么 + // 意味着SpawnMsg还没过来,因为Udp没法保证收到包的顺序,所以这里延迟执行这个消息,等到对应的netId收到后执行 + // 如果下次执行的时候还是没有,那么继续调用DeferMessage,cnt>10次的时候退出这个递归 if (!this.DestroyedObjects.Contains(netId)) { this.DeferMessage(cnt, reader, "Stored data for " + netId); @@ -1765,7 +1770,7 @@ namespace InnerNet case DespawnFlag: // MsgRead:DespawnMsg,收到消息后,销毁对应的netObject { uint netId = reader.ReadPackedUInt32(); - this.DestroyedObjects.Add(netId); + this.DestroyedObjects.Add(netId); // 回收这个netId InnerNetObject innerNetObject5 = this.FindObjectByNetId<InnerNetObject>(netId); if (innerNetObject5) { @@ -1819,7 +1824,9 @@ namespace InnerNet }) + string.Join<byte>(" ", reader.Buffer)); } - // 延迟到下一帧调用回调? + // 延迟到下一帧调用回调,因为Udp没法保证消息顺序,对于那些需要netObject存在的消息,比如同步netObject内容,可能 + // 会出现netObject还没有创建的情况(因为没有受到SpawnMsg),这里会先缓存起来,每帧去检查一下,但超过10次后就放弃了 + // cnt是递归次数 private void DeferMessage(int cnt, MessageReader reader, string logMsg) { if (cnt > 10) @@ -1830,7 +1837,7 @@ namespace InnerNet int cnt2 = cnt; cnt = cnt2 + 1; Debug.Log(logMsg); - MessageReader copy = MessageReader.CopyMessageIntoParent(reader); + MessageReader copy = MessageReader.CopyMessageIntoParent(reader); // 拷贝 List<Action> preSpawnDispatcher = this.PreSpawnDispatcher; lock (preSpawnDispatcher) { diff --git a/Client/Assembly-CSharp/InnerNet/MessageExtensions.cs b/Client/Assembly-CSharp/InnerNet/MessageExtensions.cs index 9e9bd49..d1aca88 100644 --- a/Client/Assembly-CSharp/InnerNet/MessageExtensions.cs +++ b/Client/Assembly-CSharp/InnerNet/MessageExtensions.cs @@ -5,7 +5,7 @@ namespace InnerNet { public static class MessageExtensions { - public static void WriteNetObject(this MessageWriter self, InnerNetObject obj) + public static void WriteNetObject(MessageWriter self, InnerNetObject obj) { if (!obj) { @@ -15,7 +15,7 @@ namespace InnerNet self.WritePacked(obj.NetId); } - public static T ReadNetObject<T>(this MessageReader self) where T : InnerNetObject + public static T ReadNetObject<T>(MessageReader self) where T : InnerNetObject { uint netId = self.ReadPackedUInt32(); return AmongUsClient.Instance.FindObjectByNetId<T>(netId); diff --git a/Client/Assembly-CSharp/Msg.cs b/Client/Assembly-CSharp/Msg.cs index bb99f82..5976a0e 100644 --- a/Client/Assembly-CSharp/Msg.cs +++ b/Client/Assembly-CSharp/Msg.cs @@ -1,6 +1,16 @@ /* 所有网络消息 +////////////////////////////////////////////// 不需要同步的消息 ////////////////////////////////////////////// +EndGame // 本局结束 +{ + +} + + +////////////////////////////////////////////// 高频率的同步消息 ////////////////////////////////////////////// +// 对应 GameData(5)和GameDataTo(6) + GameData // 所有在游戏内的广播消息的通用结构,可以包含Rpc、SpawnMsg等等 { flag // GameData 或 GameDataTo @@ -8,6 +18,8 @@ GameData // 所有在游戏内的广播消息的通用结构,可以 msgData // 具体的网络消息 } +// SyncNetObject和Rpc有很多,在NetObject中实现 + SyncNetObject // 定时同步innnerNetObject的数据 { DataFlag // 1 diff --git a/Client/Assembly-CSharp/PlayerControl.cs b/Client/Assembly-CSharp/PlayerControl.cs index 753e127..4f2505e 100644 --- a/Client/Assembly-CSharp/PlayerControl.cs +++ b/Client/Assembly-CSharp/PlayerControl.cs @@ -160,31 +160,6 @@ public class PlayerControl : InnerNetObject } } - //c 角色相关的所有的Rpc调用 - public enum RpcCalls : byte - { - PlayAnimation, - CompleteTask, - SyncSettings, - SetInfected, - Exiled, - CheckName, - SetName, - CheckColor, - SetColor, - SetHat, - SetSkin, - ReportDeadBody, - MurderPlayer, - SendChat, - TimesImpostor, - StartMeeting, - SetScanner, - SendChatNote, - SetPet, - SetStartCounter - } - public void SetKillTimer(float time) { this.killTimer = time; @@ -1376,6 +1351,31 @@ public class PlayerControl : InnerNetObject AmongUsClient.Instance.FinishRpcImmediately(messageWriter); } + //c 角色相关的所有的Rpc调用 + public enum RpcCalls : byte + { + PlayAnimation, + CompleteTask, + SyncSettings, + SetInfected, + Exiled, + CheckName, + SetName, + CheckColor, + SetColor, + SetHat, + SetSkin, + ReportDeadBody, + MurderPlayer, + SendChat, + TimesImpostor, + StartMeeting, + SetScanner, + SendChatNote, + SetPet, + SetStartCounter + } + //c 对应的角色执行对应发过来的Rpc调用 public override void HandleRpc(byte callId, MessageReader reader) { diff --git a/Client/Assembly-CSharp/PlayerPhysics.cs b/Client/Assembly-CSharp/PlayerPhysics.cs index c35e21d..c751f0d 100644 --- a/Client/Assembly-CSharp/PlayerPhysics.cs +++ b/Client/Assembly-CSharp/PlayerPhysics.cs @@ -212,7 +212,7 @@ public class PlayerPhysics : InnerNetObject base.transform.position = spawnPos + new Vector3(amFlipped ? -0.3f : 0.3f, -0.24f); this.ResetAnim(false); Vector2 b = (-spawnPos).normalized; - yield return this.WalkPlayerTo(spawnPos + b, 0.01f); + yield return this.WalkPlayerTo((Vector2)spawnPos + b, 0.01f); this.myPlayer.Collider.enabled = true; KillAnimation.SetMovement(this.myPlayer, true); this.myPlayer.nameText.gameObject.SetActive(true); @@ -267,7 +267,7 @@ public class PlayerPhysics : InnerNetObject do { Vector2 vector2; - Vector2 vector = vector2 = worldPos - base.transform.position; + Vector2 vector = vector2 = worldPos - (Vector2)base.transform.position; if (vector2.sqrMagnitude <= tolerance) { break; diff --git a/Client/Assembly-CSharp/ShipStatus.cs b/Client/Assembly-CSharp/ShipStatus.cs index 2e9cb2f..89fd8da 100644 --- a/Client/Assembly-CSharp/ShipStatus.cs +++ b/Client/Assembly-CSharp/ShipStatus.cs @@ -197,7 +197,7 @@ public class ShipStatus : InnerNetObject Vector2 vector = Vector2.up; vector = vector.Rotate((float)(playerId - 1) * (360f / (float)numPlayer)); vector *= this.SpawnRadius; - return this.SpawnCenter.position + vector + new Vector2(0f, 0.3636f); + return (Vector2)this.SpawnCenter.position + vector + new Vector2(0f, 0.3636f); } public void StartShields() @@ -211,7 +211,7 @@ public class ShipStatus : InnerNetObject public void FireWeapon() { - if (!this.WeaponsImage.IsPlaying(null)) + if (!this.WeaponsImage.IsPlaying((string)null)) { this.WeaponsImage.Play(this.WeaponFires[this.WeaponFireIdx], 1f); this.WeaponFireIdx = (this.WeaponFireIdx + 1) % 2; @@ -230,7 +230,7 @@ public class ShipStatus : InnerNetObject public void OpenHatch() { - if (!this.Hatch.IsPlaying(null)) + if (!this.Hatch.IsPlaying((string)null)) { this.Hatch.Play(this.HatchActive, 1f); this.HatchParticles.Play(); @@ -389,7 +389,7 @@ public class ShipStatus : InnerNetObject { GameData.Instance.RecomputeTaskCounts(); } - if (AmongUsClient.Instance.AmHost) + if (AmongUsClient.Instance.AmHost) //c 只有host检查是否结束游戏,然后广播给其他玩家 { this.CheckEndCriteria(); } |