180 lines
7.0 KiB
C#
180 lines
7.0 KiB
C#
using Logic.Action;
|
|
using Logic.CrashSight;
|
|
using RuntimeData;
|
|
using Steamworks;
|
|
using TH1_Logic.Core;
|
|
using TH1_Logic.Net;
|
|
|
|
namespace TH1_Logic.Steam
|
|
{
|
|
public class GameNetReceiver
|
|
{
|
|
public static GameNetReceiver Instance { get; } = new GameNetReceiver();
|
|
|
|
|
|
public void OnMessageReceived(CSteamID cSteamID, byte[] data)
|
|
{
|
|
if (Main.MapData.Net.Mode != NetMode.Multi) return;
|
|
P2PMsgType msgType = GetMessageType(data);
|
|
if (msgType == P2PMsgType.String) OnReceivedString(data);
|
|
if (msgType == P2PMsgType.GameStart) OnReceivedGameStart(data);
|
|
if (msgType == P2PMsgType.ActionConfirm) OnReceivedActionConfirm(data);
|
|
if (msgType == P2PMsgType.ActionExcute) OnReceivedActionExcute(data);
|
|
if (msgType == P2PMsgType.TurnEnd) OnReceivedTurnEnd(data);
|
|
if (msgType == P2PMsgType.MapConfirm) OnReceivedMapConfirm(data);
|
|
if (msgType == P2PMsgType.ForceUpdate) OnReceivedForceUpdate(data);
|
|
}
|
|
|
|
// 基础字符串消息
|
|
private void OnReceivedString(byte[] data)
|
|
{
|
|
var message = MemoryPack.MemoryPackSerializer.Deserialize<StringMessage>(data);
|
|
if (message == null)
|
|
{
|
|
LogSystem.LogError($"消息解析失败: OnReceivedString");
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(message.Content)) return;
|
|
LogSystem.LogInfo($"收到消息 : {message.Content}");
|
|
}
|
|
|
|
// 只有玩家会收到
|
|
private void OnReceivedGameStart(byte[] data)
|
|
{
|
|
var message = MemoryPack.MemoryPackSerializer.Deserialize<GameStartMessage>(data);
|
|
if (message == null)
|
|
{
|
|
LogSystem.LogError($"消息解析失败: OnReceivedGameStart");
|
|
return;
|
|
}
|
|
|
|
if (message.MapData == null) return;
|
|
if (LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
|
|
Main.Instance.NetStartGame(message.MapData);
|
|
}
|
|
|
|
// 只有房主会收到
|
|
private void OnReceivedActionConfirm(byte[] data)
|
|
{
|
|
var message = MemoryPack.MemoryPackSerializer.Deserialize<ActionConfirmMessage>(data);
|
|
if (message == null)
|
|
{
|
|
LogSystem.LogError($"消息解析失败: OnReceivedActionConfirm");
|
|
return;
|
|
}
|
|
|
|
if (!LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
|
|
if (message.ActionData == null) return;
|
|
if (message.ActionData.MapHash != Main.MapData.Net.MapHash)
|
|
{
|
|
LogSystem.LogError($"OnReceivedActionConfirm MapHash不一致");
|
|
return;
|
|
}
|
|
if (message.ActionData.Version != Main.MapData.Net.GetActionVersion())
|
|
{
|
|
LogSystem.LogError($"OnReceivedActionConfirm Version 不一致");
|
|
return;
|
|
}
|
|
message.ActionData.Param.MapData = Main.MapData;
|
|
message.ActionData.Param.RefreshParams();
|
|
var action = ActionLogicFactory.GetActionLogic(message.ActionData.ActionId);
|
|
if (action == null)
|
|
{
|
|
LogSystem.LogError($"OnReceivedActionConfirm 找不到对应 Action");
|
|
return;
|
|
}
|
|
action.CompleteExecute(message.ActionData.Param);
|
|
}
|
|
|
|
// 只有玩家会收到
|
|
private void OnReceivedActionExcute(byte[] data)
|
|
{
|
|
var message = MemoryPack.MemoryPackSerializer.Deserialize<ActionExcuteMessage>(data);
|
|
if (message == null)
|
|
{
|
|
LogSystem.LogError($"消息解析失败: OnReceivedActionExcute");
|
|
return;
|
|
}
|
|
if (LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
|
|
if (message.ActionData == null) return;
|
|
if (message.ActionData.MapHash != Main.MapData.Net.MapHash)
|
|
{
|
|
LogSystem.LogError($"OnReceivedActionConfirm MapHash不一致");
|
|
return;
|
|
}
|
|
if (message.ActionData.Version != Main.MapData.Net.GetActionVersion())
|
|
{
|
|
LogSystem.LogError($"OnReceivedActionConfirm Version 不一致");
|
|
return;
|
|
}
|
|
message.ActionData.Param.MapData = Main.MapData;
|
|
message.ActionData.Param.RefreshParams();
|
|
var action = ActionLogicFactory.GetActionLogic(message.ActionData.ActionId);
|
|
if (action == null)
|
|
{
|
|
LogSystem.LogError($"OnReceivedActionConfirm 找不到对应 Action");
|
|
return;
|
|
}
|
|
action.NetCompleteExecute(message.ActionData.Param);
|
|
}
|
|
|
|
// 只有房主会收到
|
|
private void OnReceivedTurnEnd(byte[] data)
|
|
{
|
|
var message = MemoryPack.MemoryPackSerializer.Deserialize<TurnEndMessage>(data);
|
|
if (message == null)
|
|
{
|
|
LogSystem.LogError($"消息解析失败: OnReceivedTurnEnd");
|
|
return;
|
|
}
|
|
if (!LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
|
|
Main.PlayerLogic.EndPlayerTurn(Main.MapData, message.PlayerId);
|
|
}
|
|
|
|
// 只有房主会收到 心跳校验
|
|
private void OnReceivedMapConfirm(byte[] data)
|
|
{
|
|
var message = MemoryPack.MemoryPackSerializer.Deserialize<MapConfirmMessage>(data);
|
|
if (message == null)
|
|
{
|
|
LogSystem.LogError($"消息解析失败: OnReceivedMapConfirm");
|
|
return;
|
|
}
|
|
if (!LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
|
|
if (Main.MapData.Net.Actions.Count == 0) return;
|
|
if (message.Index > Main.MapData.Net.Actions.Count)
|
|
{
|
|
GameNetSender.Instance.ForceUpdate(message.PlayerId);
|
|
return;
|
|
}
|
|
|
|
if (message.Index > 0 && !Main.MapData.Net.Actions[message.Index - 1].IsEqual(message.ActionData))
|
|
{
|
|
GameNetSender.Instance.ForceUpdate(message.PlayerId);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 只有玩家会收到
|
|
private void OnReceivedForceUpdate(byte[] data)
|
|
{
|
|
var message = MemoryPack.MemoryPackSerializer.Deserialize<ForceUpdateMessage>(data);
|
|
if (message == null)
|
|
{
|
|
LogSystem.LogError($"消息解析失败: OnReceivedActionExcute");
|
|
return;
|
|
}
|
|
if (LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
|
|
Main.MapData = message.MapData;
|
|
}
|
|
|
|
private P2PMsgType GetMessageType(byte[] data)
|
|
{
|
|
// 临时反序列化只为获取消息类型
|
|
var tempMessage = MemoryPack.MemoryPackSerializer.Deserialize<BaseMessage>(data);
|
|
if (tempMessage == null) return P2PMsgType.None;
|
|
return tempMessage.MessageType;
|
|
}
|
|
}
|
|
} |