95 lines
3.4 KiB
C#
95 lines
3.4 KiB
C#
/*
|
|
* @Author: 白哉
|
|
* @Description:
|
|
* @Date: 2025年09月08日 星期一 17:09:18
|
|
* @Modify:
|
|
*/
|
|
|
|
|
|
using System;
|
|
using Logic.AI;
|
|
using RuntimeData;
|
|
using TH1_Logic.Core;
|
|
|
|
|
|
namespace TH1_Logic.Steam
|
|
{
|
|
public class GameNetSender
|
|
{
|
|
public static GameNetSender Instance { get; } = new GameNetSender();
|
|
|
|
// 游戏开始
|
|
public void GameStart()
|
|
{
|
|
// 序列化 MapData
|
|
byte[] mapDataBytes = MemoryPack.MemoryPackSerializer.Serialize(Main.MapData);
|
|
|
|
// 创建完整消息:消息头(1字节) + 数据长度(4字节) + MapData数据
|
|
byte[] result = new byte[1 + 4 + mapDataBytes.Length];
|
|
|
|
int offset = 0;
|
|
|
|
// 写入消息类型
|
|
result[offset] = (byte)P2PMsgType.GameStart;
|
|
offset += 1;
|
|
|
|
// 写入数据长度
|
|
byte[] lengthBytes = BitConverter.GetBytes(mapDataBytes.Length);
|
|
Array.Copy(lengthBytes, 0, result, offset, 4);
|
|
offset += 4;
|
|
|
|
// 写入 MapData 数据
|
|
Array.Copy(mapDataBytes, 0, result, offset, mapDataBytes.Length);
|
|
SteamLobbyManager.Instance.BroadcastMessage(result);
|
|
}
|
|
|
|
//行为确认 (成员 => 房主)
|
|
public void ActionConfirm(AIActionBase action)
|
|
{
|
|
var actionData = new ActionNetData();
|
|
actionData.Version = Main.MapData.Net.GetActionVersion();
|
|
actionData.MapHash = NetData.GetMapDataHash(Main.MapData);
|
|
actionData.Param = action.Param;
|
|
actionData.ActionId = action.ActionLogic.ActionId;
|
|
|
|
byte[] data = MemoryPack.MemoryPackSerializer.Serialize(actionData);
|
|
byte[] result = new byte[1 + 4 + data.Length];
|
|
int offset = 0;
|
|
// 写入消息类型
|
|
result[offset] = (byte)P2PMsgType.ActionConfirm;
|
|
offset += 1;
|
|
// 写入数据长度
|
|
byte[] lengthBytes = BitConverter.GetBytes(data.Length);
|
|
Array.Copy(lengthBytes, 0, result, offset, 4);
|
|
offset += 4;
|
|
// 写入 data
|
|
Array.Copy(data, 0, result, offset, data.Length);
|
|
|
|
SteamLobbyManager.Instance.SendMessageToPeer(SteamLobbyManager.Instance.CachedOwner, result);
|
|
}
|
|
|
|
//行为执行 (房主 => 所有成员)
|
|
public void ActionExcute(AIActionBase action)
|
|
{
|
|
var actionData = new ActionNetData();
|
|
actionData.Version = Main.MapData.Net.GetActionVersion();
|
|
actionData.MapHash = NetData.GetMapDataHash(Main.MapData);
|
|
actionData.Param = action.Param;
|
|
actionData.ActionId = action.ActionLogic.ActionId;
|
|
|
|
byte[] data = MemoryPack.MemoryPackSerializer.Serialize(actionData);
|
|
byte[] result = new byte[1 + 4 + data.Length];
|
|
int offset = 0;
|
|
// 写入消息类型
|
|
result[offset] = (byte)P2PMsgType.ActionExcute;
|
|
offset += 1;
|
|
// 写入数据长度
|
|
byte[] lengthBytes = BitConverter.GetBytes(data.Length);
|
|
Array.Copy(lengthBytes, 0, result, offset, 4);
|
|
offset += 4;
|
|
// 写入 data
|
|
Array.Copy(data, 0, result, offset, data.Length);
|
|
SteamLobbyManager.Instance.BroadcastMessage(result);
|
|
}
|
|
}
|
|
} |