This commit is contained in:
azw 2024-05-19 12:26:19 +08:00
parent ee123745b1
commit 60cb29e75a
4 changed files with 120 additions and 59 deletions

38
Managers/Battle.cs Normal file
View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Protobuf;
class Battle
{
private JoinInfo joinInfo;
private Net.ClientNet conn;
public Battle(JoinInfo joinInfo)
{
this.conn = new Net.ClientNet("ws://192.168.100.21:7601/websocket?server_id=2");
this.conn.OnOpen += (Net.ClientNet conn) =>
{
return;
var joinMsg = new battle.cs.CMJoin();
joinMsg.AccountId = joinInfo.account_id;
joinMsg.SessionId = joinInfo.session_id;
joinMsg.TeamUuid = joinInfo.team_uuid;
joinMsg.PayloadData = joinInfo.payload_data;
this.conn.SendMsg(joinMsg);
};
this.conn.Open();
{
var joinMsg = new battle.cs.CMJoin();
joinMsg.AccountId = joinInfo.account_id;
joinMsg.SessionId = joinInfo.session_id;
joinMsg.TeamUuid = joinInfo.team_uuid;
joinMsg.PayloadData = joinInfo.payload_data;
this.conn.SendMsg(joinMsg);
}
}
}

View File

@ -40,6 +40,9 @@ public class BattleMgr
TransformExtension.localEulerAngles(terrainObj.transform, 0, 0, 0);
TransformExtension.localPosition(terrainObj.transform, 0, 0, 0);
}
{
var b = new Battle(join_info);
}
}

View File

@ -8,14 +8,18 @@ namespace Mt
{
class Table
{
public static string RES_PATH = @"D:\opensource\pubgv4\branch\config_v4_dev3d_10_9\upload_files\equip@equip.json";
private static bool loadEd;
public static string RES_PATH = @"D:\opensource\pubgv4\branch\config_v4_dev3d_10_9\upload_files\";
public static EquipTable Equip = new EquipTable(RES_PATH + "equip@equip.json", "id");
public static MapTable Map = new MapTable(RES_PATH + "map@map.json", "map_id");
public static void Load()
{
Equip.Load();
Map.Load();
if (!loadEd)
{
Equip.Load();
Map.Load();
}
}
}

View File

@ -10,96 +10,112 @@ using pbr = global::Google.Protobuf.Reflection;
namespace Net
{
public delegate void OnNetOpen(ClientNet conn);
public delegate void OnNetError(ClientNet conn);
public delegate void OnNetClosed(ClientNet conn);
public class ClientNet
{
private string uri;
private WebSocket ws;
private byte[] recvBuf = new byte[1024 * 64 * 3];
private int recvBufLen = 0;
public OnNetOpen OnOpen;
public OnNetError OnError;
public OnNetClosed OnClosed;
public ClientNet(string uri)
{
this.ws = new WebSocket(new Uri(uri));
this.ws.OnOpen += this.OnOpen;
this.ws.OnBinary += this.OnBinaryReceived;
this.ws.OnError += this.OnError;
this.ws.OnClosed += this.OnClosed;
this.ws.OnOpen += this.OnWsOpen;
this.ws.OnBinary += this.OnWsBinaryReceived;
this.ws.OnError += this.OnWsError;
this.ws.OnClosed += this.OnWsClosed;
}
public void Open()
{
this.ws.Open();
battle.cs.CMJoin joinMsg = new battle.cs.CMJoin();
this.SendMsg(joinMsg);
battle.cs.CMMove moveMsg = new battle.cs.CMMove();
this.SendMsg(moveMsg);
}
public int GetCMMsgId(string msgName)
{
return this.GetPbMsgId(typeof(battle.cs.CMMessageId_e), "_" + msgName);
}
public int GetSMMsgId(string msgName)
{
return this.GetPbMsgId(typeof(battle.cs.SMMessageId_e), "_" + msgName);
}
private int GetPbMsgId(Type enumType,string msgName)
{
foreach (var f in enumType.GetFields())
{
var tAttrs = f.GetCustomAttributes(typeof(pbr::OriginalNameAttribute), false);
if (tAttrs.Length > 0)
{
var tAttr = (pbr::OriginalNameAttribute)tAttrs[0];
if (tAttr.Name.Equals(msgName))
{
var val = Enum.Parse(enumType, f.Name);
return Convert.ToInt32(val);
}
}
}
return 0;
}
public void SendMsg<T>(T msg) where T : IMessage<T>
{
int msgId = 0;
Type enumType = typeof(battle.cs.CMMessageId_e);
foreach(var f in enumType.GetFields())
try
{
var tAttrs = f.GetCustomAttributes(typeof(pbr::OriginalNameAttribute), false);
if (tAttrs.Length > 0) {
var tAttr = (pbr::OriginalNameAttribute)tAttrs[0];
if (tAttr.Name.Equals("")) {
}
}
int msgId = this.GetCMMsgId(typeof(T).Name);
int packLen = msg.CalculateSize();
const int magicCode = 21323;
byte[] data = new byte[12 + packLen];
data[0] = (byte)(packLen & 0xFF);
data[1] = (byte)((packLen & 0xFFFF) >> 8);
data[2] = (byte)(msgId & 0xFF);
data[3] = (byte)((msgId & 0xFFFF) >> 8);
data[4] = 0;
data[5] = 0;
data[6] = 0;
data[7] = 0;
data[8] = (byte)(magicCode & 0xFF);
data[9] = (byte)((magicCode & 0xFFFF) >> 8);
data[10] = 0;
data[11] = 0;
msg.ToByteArray().CopyTo(data, 12);
this.ws.Send(data);
}catch(Exception e)
{
int i = 0;
}
/*
var b = by.ToBytes();
by.Close();
ByteBuffer buffer = new ByteBuffer();
buffer.WriteShort((ushort)b.Length);
buffer.WriteShort((ushort)msgid);
buffer.WriteInt(0);
buffer.WriteShort(21323);
buffer.WriteShort(0);
buffer.WriteBytesNo(b);
var buf = buffer.ToBytes();
m_webSocket.Send(buf);
*/
int packLen = msg.CalculateSize();
const int magicCode = 21323;
byte[] data = new byte[12 + packLen];
data[0] = (byte)(packLen & 0xFF);
data[1] = (byte)((packLen & 0xFFFF) >> 8);
data[2] = (byte)(msgId & 0xFF);
data[3] = (byte)((msgId & 0xFFFF) >> 8);
data[4] = 0;
data[5] = 0;
data[6] = 0;
data[7] = 0;
data[8] = (byte)(magicCode & 0xFF);
data[9] = (byte)((magicCode & 0xFFFF) >> 8);
data[10] = 0;
data[11] = 0;
msg.ToByteArray().CopyTo(data, 12);
this.ws.Send(data);
}
private void OnOpen(WebSocket ws)
private void OnWsOpen(WebSocket ws)
{
Debug.Log("ClientNet.OnOpen");
this.OnOpen(this);
}
private void OnBinaryReceived(WebSocket ws, byte[] data)
private void OnWsBinaryReceived(WebSocket ws, byte[] data)
{
Debug.Log("ClientNet.OnBinaryReceived");
}
private void OnError(WebSocket ws, string reason)
private void OnWsError(WebSocket ws, string reason)
{
Debug.Log("ClientNet.OnError");
this.OnError(this);
}
private void OnClosed(WebSocket ws, UInt16 code, string message)
private void OnWsClosed(WebSocket ws, UInt16 code, string message)
{
Debug.Log("ClientNet.OnClosed");
this.OnClosed(this);
}
}