using UnityEngine;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
//using ILCrossBinding;
|
|
public class PackageRegedit
|
{
|
#region 主工程原生的封包
|
static object lockojb1 = new object();
|
static Dictionary<ushort, Type> m_PackageTable = new Dictionary<ushort, Type>();
|
static Dictionary<ushort, Type> packageTable
|
{
|
get
|
{
|
lock (lockojb1) { return m_PackageTable; }
|
}
|
}
|
static object lockojb2 = new object();
|
static Dictionary<ushort, DtcBasic> m_PackToBusinessTable = new Dictionary<ushort, DtcBasic>();
|
static Dictionary<ushort, DtcBasic> packToBusinessTable
|
{
|
get
|
{
|
lock (lockojb2) { return m_PackToBusinessTable; }
|
}
|
}
|
#endregion
|
|
public static void Init()
|
{
|
// 登记相应的数据体及对应的数据转逻辑类
|
Register(typeof(H0102_tagCDBPlayer), typeof(DTC0102_tagCDBPlayer));
|
Register(typeof(HA004_tagServerDateTime), typeof(DTCA004_tagServerDateTime));
|
Register(typeof(HA005_tagOpenServerDay), typeof(DTCA005_tagOpenServerDay));
|
Register(typeof(H0403_tagPlayerLoginLoadOK), typeof(DTC0403_tagPlayerLoginLoadOK));
|
Register(typeof(H0101_tagServerPrepared), typeof(DTC0101_tagServerPrepared));
|
Register(typeof(H0104_tagServerDisconnect), typeof(DTC0104_tagServerDisconnect));
|
|
|
}
|
|
|
//主工程注册封包
|
public static void Register(Type _pack, Type _business)
|
{
|
var packInstance = _pack.Assembly.CreateInstance(_pack.Name) as GameNetPackBasic;
|
var businessInstance = _business.Assembly.CreateInstance(_business.Name) as DtcBasic;
|
if (packToBusinessTable.ContainsKey(packInstance.cmd))
|
{
|
Debug.LogErrorFormat("重复封包登记: {0}!", _pack.Name);
|
}
|
else
|
{
|
packToBusinessTable[packInstance.cmd] = businessInstance;
|
packageTable[packInstance.cmd] = _pack;
|
}
|
}
|
|
|
|
public static bool Contain(ushort _cmd)
|
{
|
return packToBusinessTable.ContainsKey(_cmd);
|
}
|
public static void Distribute(GameNetPackBasic _package)
|
{
|
try
|
{
|
//执行主工程的封包
|
if (packToBusinessTable.ContainsKey(_package.cmd))
|
packToBusinessTable[_package.cmd].Done(_package);
|
}
|
catch (Exception ex)
|
{
|
Debug.LogError(ex.Message + "\r\n" + ex.StackTrace);
|
Debug.LogErrorFormat("封包是否为Null:{0};", _package == null);
|
if (_package != null)
|
{
|
Debug.LogErrorFormat("封包编号是:{0};", _package.cmd);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 转换一个协议包
|
/// </summary>
|
/// <param name="vBytes"></param>
|
/// <returns></returns>
|
public static GameNetPackBasic TransPack(ServerType socketType, ushort cmd, byte[] vBytes)
|
{
|
#if UNITY_EDITOR
|
int length = vBytes.Length;
|
string vStr = "";
|
for (int i = 0; i < length; i++)
|
{
|
vStr += vBytes[i].ToString("x2").ToUpper() + ",";
|
}
|
#endif
|
// 主工程有登记
|
if (packageTable.ContainsKey(cmd))
|
{
|
var packageType = packageTable[cmd];
|
// 实例化并强转为协议包基类
|
var packageInstance = packageType.Assembly.CreateInstance(packageType.Name) as GameNetPackBasic;
|
packageInstance.ReadFromBytes(vBytes);// 解析内容
|
if (packageInstance.cmd == (ushort)0x03F0 || packageInstance.cmd == (ushort)0x1801)
|
{
|
byte[] vPyBody = new byte[vBytes.Length - 4];
|
Array.Copy(vBytes, 4, vPyBody, 0, vPyBody.Length);
|
packageInstance = TransForPyPack(vPyBody);
|
}
|
|
packageInstance.socketType = socketType;
|
#if UNITY_EDITOR
|
NetPkgCtl.RecordPackage(socketType, vStr, NetPackagetType.Server, packageInstance.ToString(), FieldPrint.PrintFields(packageInstance), FieldPrint.PrintFieldsExpand(packageInstance, true));
|
#endif
|
return packageInstance;
|
}
|
else //未注册
|
{
|
#if UNITY_EDITOR
|
NetPkgCtl.RecordPackage(socketType, vStr, NetPackagetType.Server, string.Empty, string.Empty, null);
|
#endif
|
}
|
return null;
|
}
|
|
|
/// <summary>
|
/// 解析PY的模拟包
|
/// </summary>
|
/// <param name="vBytes"></param>
|
/// <returns></returns>
|
private static GameNetPackBasic TransForPyPack(byte[] vBytes)
|
{
|
GameNetPackBasic vNetPackInst = null;
|
ushort vPackCmd = (ushort)((ushort)(vBytes[0] << 8) + vBytes[1]);
|
// 主工程封包
|
if (packageTable.ContainsKey(vPackCmd))
|
{
|
Type vNetPackType = packageTable[vPackCmd];
|
vNetPackInst = vNetPackType.Assembly.CreateInstance(vNetPackType.Name) as GameNetPackBasic;
|
vNetPackInst.ReadFromBytes(vBytes);// 解析内容
|
}
|
else
|
{
|
Debug.Log("收到未定义的解包协议:" + vBytes[0].ToString("x2").ToUpper() + "," + vBytes[1].ToString("x2").ToUpper());
|
}
|
return vNetPackInst;
|
}
|
|
}
|