unityzgy/Assets/Scripts/PublicTool/TCP/Base/ProtocolByte.cs
ayuan9957 bf12e02276 feat: 多语言本地化系统 - 支持中/英/法/俄实时切换
- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg)
- LoginPanel: InputField placeholder本地化、字体颜色保持
- HistoryPanel: 用时数据本地化、placeholder本地化
- RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建)
- AppraiseWindowBase: 评价等级本地化、操作消息重建
- EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized
- StudentOperateRecorder: 新增InjectOperateMsgLocalized方法
- LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选
- 字体切换时保留颜色和verticalOverflow
2026-07-16 10:05:59 +08:00

213 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class ProtocolByte : ProtocolBase
{
public ProtocolByte()
{
}
//创建时指定消息标识字符串
public ProtocolByte(string protocolName)
{
AddString(protocolName);
}
public byte[] bytes;
public override ProtocolBase Decode(byte[] readBuff, int start, int length)
{
ProtocolByte protocol = new ProtocolByte();
protocol.bytes = new byte[length];
Array.Copy(readBuff, start, protocol.bytes, 0, length);
return protocol;
}
public override byte[] Encode()
{
return bytes;
}
//获取每个字节的编码
public override string GetDesc()
{
string str = "";
if (bytes == null) return str;
for (int i = 0; i < bytes.Length; i++)
{
int b = (int)bytes[i];
str += b.ToString() + " ";
}
return str;
}
public override string GetProtocolName()
{
return GetString(0);
}
public void AddStruct<T>(T obj) where T : struct
{
byte[] objBytes = BytesHelper.StructToBytes<T>(obj);
byte[] lenBytes = BitConverter.GetBytes(objBytes.Length);
if (bytes == null)
{
bytes = lenBytes.Concat(objBytes).ToArray();
}
else
bytes = bytes.Concat(lenBytes).Concat(objBytes).ToArray();
}
public T GetStruct<T>(int start, ref int end) where T : struct
{
if (bytes == null) return default(T);
int msgLen = BitConverter.ToInt32(bytes, start);
if (bytes.Length < (start + sizeof(Int32) + msgLen))
return default(T);
byte[] objBytes = new byte[msgLen];
Array.Copy(bytes, start + sizeof(Int32), objBytes, 0, msgLen);
T obj = BytesHelper.BytesToStuct<T>(objBytes);
end = start + sizeof(Int32) + msgLen;
return obj;
}
/// <summary>
/// 添加的类中 尽量不要存在float 或限制float的精度后使用 否则转成字符串后占用空间较大
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
public void AddClass<T>(T obj) where T : class
{
byte[] objBytes = BytesHelper.ClassToBytes<T>(obj);
byte[] lenBytes = BitConverter.GetBytes(objBytes.Length);
if (bytes == null)
{
bytes = lenBytes.Concat(objBytes).ToArray();
}
else
bytes = bytes.Concat(lenBytes).Concat(objBytes).ToArray();
}
public T GetClass<T>(int start, ref int end) where T : class
{
if (bytes == null) return default(T);
int msgLen = BitConverter.ToInt32(bytes, start);
if (bytes.Length < (start + sizeof(Int32) + msgLen))
return default(T);
byte[] objBytes = new byte[msgLen];
Array.Copy(bytes, start + sizeof(Int32), objBytes, 0, msgLen);
T obj = BytesHelper.BytesToClass<T>(objBytes);
end = start + sizeof(Int32) + msgLen;
return obj;
}
public void AddString(string str)
{
//byte[] lenBytes = BitConverter.GetBytes(str.Length);
byte[] strBytes = System.Text.Encoding.UTF8.GetBytes(str);
byte[] lenBytes = BitConverter.GetBytes(strBytes.Length);
if (bytes == null)
{
bytes = lenBytes.Concat(strBytes).ToArray();
}
else
bytes = bytes.Concat(lenBytes).Concat(strBytes).ToArray();
}
public string GetString(int start, ref int end)
{
if (bytes == null) return "";
int msgLen = BitConverter.ToInt32(bytes, start);
if (bytes.Length < (start + sizeof(Int32) + msgLen))
return "";
string str = System.Text.Encoding.UTF8.GetString(bytes, start + sizeof(Int32), msgLen);
end = start + sizeof(Int32) + msgLen;
return str;
}
public string GetString(int start)
{
int end = 0;
return GetString(start, ref end);
}
public void AddBool(bool value)
{
byte[] numbytes = BitConverter.GetBytes(value);
if (bytes == null)
bytes = numbytes;
bytes = bytes.Concat(numbytes).ToArray();
}
public bool GetBool(int start, ref int end)
{
if (bytes == null) return false;
if (bytes.Length < sizeof(bool) + start)
return false;
bool value = BitConverter.ToBoolean(bytes, start);
end = start + sizeof(bool);
return value;
}
public void AddInt(int num)
{
byte[] numbytes = BitConverter.GetBytes(num);
if (bytes == null)
bytes = numbytes;
bytes = bytes.Concat(numbytes).ToArray();
}
public int GetInt(int start, ref int end)
{
if (bytes == null) return 0;
if (bytes.Length < sizeof(Int32) + start)
return 0;
int num = BitConverter.ToInt32(bytes, start);
end = start + sizeof(Int32);
return num;
}
public int GetInt(int start)
{
int end = 0;
return GetInt(start, ref end);
}
public void AddFloat(float num)
{
byte[] numbytes = BitConverter.GetBytes(num);
if (bytes == null)
bytes = numbytes;
bytes = bytes.Concat(numbytes).ToArray();
}
public float GetFloat(int start, ref int end)
{
if (bytes == null) return 0;
if (bytes.Length < sizeof(float) + start)
return 0;
float num = BitConverter.ToSingle(bytes, start);
end = start + sizeof(float);
return num;
}
public int GetFloat(int start)
{
int end = 0;
return GetInt(start, ref end);
}
}