当前位置: 首页 > news >正文

Unity使用反射进行Protobuf(CS/SC)协议,json格式_002

上篇文章
与此家公司的protobuf有点小差异,记录一下咯

在这里插入图片描述

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Reflection;
using Google.Protobuf;
using Sirenix.OdinInspector;
using UnityEditor;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;/// <summary> 协议 辅助工具  一般用来 1.测试请求   2.自己给自己下发数据</summary>
public class EditorSendReceiveNet : OdinEditorWindow
{[MenuItem("Tools/协议工具_请求_伪收到 &#%N", priority = 199)] public static void ShowNetTool(){var win = GetWindow<EditorSendReceiveNet>("协议___C2S___S2C(伪)");win.position = GUIHelper.GetEditorWindowRect().AlignCenter(680, 520);}private Dictionary<string, Type> mEntityPbClass;public EditorSendReceiveNet(){Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); // 获取当前应用程序域中所有已加载的程序集mEntityPbClass = new Dictionary<string, Type>();foreach (var assembly in assemblies){Type[] types = assembly.GetTypes(); // 筛选出所有 Pb 命名空间中的公共类var entityValues = types.Where(t => t.IsClass && t.IsPublic).ToList();foreach (var itemClass in entityValues){var key = itemClass.FullName;mEntityPbClass[key] = itemClass;}}Debug.LogError($"初始化__总条数{mEntityPbClass.Count}");}[LabelText("只知道【接口】名字"), HorizontalGroup("module"), PropertyOrder(Order = 1), Space(5)]public string mDtoNameTxt = "CFight_C2GSFightReq";[Button("先 生成一个Json_Dto,然后手动自行修改赋值", ButtonSizes.Medium, ButtonHeight = 30), HorizontalGroup("module",Width= 0.38f), PropertyOrder(Order = 2)]public void GenOneDto(){var c2s_interface = mDtoNameTxt.Split("_");if (c2s_interface.Length != 2){Debug.LogError($"理论上 接口名 {mDtoNameTxt} 含有一个下划线  _");return;}if (mEntityPbClass.TryGetValue(c2s_interface[1], out var itemClass)){var properties = itemClass.GetProperties();var defaultValues = new Dictionary<string, object>();foreach (var property in properties){if (property.Name.Contains("Parser") || property.Name.Contains("Descriptor")){//过滤掉 Parser Descriptor}else if (property.PropertyType == typeof(int) || property.PropertyType == typeof(uint)){defaultValues[property.Name] = 0;}else if (property.PropertyType == typeof(string)){defaultValues[property.Name] = "string_null";}else if (property.PropertyType == typeof(decimal) || property.PropertyType == typeof(float)){defaultValues[property.Name] = 0;}else if (property.PropertyType == typeof(bool)){defaultValues[property.Name] = false;}else if (property.PropertyType.IsClass){defaultValues[property.Name] = null;}}if (Enum.TryParse(mDtoNameTxt, out Protobuf.ProtoIdDefinedGS2C protoSCId)){var jsonContent = Newtonsoft.Json.JsonConvert.SerializeObject(defaultValues);mDtoJsonTxt = mDtoNameTxt + "\n" + jsonContent.ToString();}if (Enum.TryParse(mDtoNameTxt, out Protobuf.ProtoIdDefinedC2GS protoCSId)){var jsonContent = Newtonsoft.Json.JsonConvert.SerializeObject(defaultValues);mDtoJsonTxt = mDtoNameTxt + "\n" + jsonContent.ToString();}}else{Debug.LogError("确定左边 输入正确了?");}}[TextArea(20, 50), HideLabel, Space(30), PropertyOrder(Order = 3), InfoBox("应用场景: \r\n请求(CS)_后端先写完了(有proto接口了),前端还没搞完时(暂还没接入proto),可使用此请求,可自测\r\n响应(SC)_可自行构建一个响应(有些特殊数据后端下发不了的),对数据进行测试\r\nPS:Console面板的Log可以直接copy内容,再对内容进行小修改")]public string mDtoJsonTxt = @"Formation_GS2CFormationDefaultUpdate{  ""DefaultTeam"": {    ""FormationId"": 10020,    ""TeamId"": 4  }}";[HorizontalGroup("Split", 0.5f)][Button("伪_接收到协议", ButtonSizes.Medium, ButtonHeight = 30), PropertyOrder(Order = 4)]public void ReceiveNet(){int index = mDtoJsonTxt.IndexOf('{');string typeName = mDtoJsonTxt.Substring(0, index).Trim();string jsonContent = mDtoJsonTxt.Substring(index).Trim();if (string.IsNullOrEmpty(typeName) || string.IsNullOrEmpty(jsonContent)){Debug.LogError(@"数据格式 不对哦  eg:  Formation_GS2CFormationDefaultUpdate{  ""DefaultTeam"": {    ""FormationId"": 10020,    ""TeamId"": 4  }}");return;}if (Enum.TryParse(typeName, out Protobuf.ProtoIdDefinedGS2C protoSCId) == false){Debug.LogError($"ProtoIdDefinedGS2C的枚举 转义不了 typeName={typeName}");return;}var c2s_interface = typeName.Split("_");if (c2s_interface.Length != 2){Debug.LogError($"理论上 接口名 {typeName} 含有一个下划线  _");return;}if (mEntityPbClass.TryGetValue(c2s_interface[1], out var itemClass)){var dtoMsg = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContent, itemClass); // 使用非泛型的方法Nikke.hotUpdate.NikkeEntryHotupdate.WebSocket.ReceiveFakeSCPacket(protoSCId, (IMessage)dtoMsg);}else{Debug.LogError("查查吧~1.DTO没写对? 2.协议不是最新的?");}}[VerticalGroup("Split/right")][Button("发送请求", ButtonSizes.Medium, ButtonHeight = 30), PropertyOrder(Order = 5)]public void SendNet(){int index = mDtoJsonTxt.IndexOf('{');string typeName = mDtoJsonTxt.Substring(0, index).Trim();string jsonContent = mDtoJsonTxt.Substring(index).Trim();if (string.IsNullOrEmpty(typeName) || string.IsNullOrEmpty(jsonContent)){Debug.LogError(@"数据格式 不对哦  eg:  CFormation_C2GSFormationDefaultTeamUpdate{  ""DefaultTeam"": {    ""FormationId"": 10020,    ""TeamId"": 2  }}");return;}if (Enum.TryParse(typeName, out Protobuf.ProtoIdDefinedC2GS protoCSId) == false){Debug.LogError($"ProtoIdDefinedC2GS的枚举 转义不了 typeName={typeName}");return;}var c2s_interface = typeName.Split("_");if (c2s_interface.Length != 2){Debug.LogError($"理论上 接口名 {typeName} 含有一个下划线  _");return;}if (mEntityPbClass.TryGetValue(c2s_interface[1], out var itemClass)){var dtoMsg = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContent, itemClass); // 使用非泛型的方法Debug.Log($"发送协议:{protoCSId}");Nikke.hotUpdate.NikkeEntryHotupdate.WebSocket.Send(protoCSId, (IMessage)dtoMsg);}else{Debug.LogError("查查吧~1.DTO没写对? 2.协议不是最新的?");}}
}
#endif
http://www.xdnf.cn/news/89191.html

相关文章:

  • Python 常用Web框架对比
  • 乐视系列玩机---乐视2 x620 x628等系列线刷救砖以及刷写第三方twrp 卡刷第三方固件步骤解析
  • Spring 中 @Component, @Repository, @Service的区别
  • 电商场景下Elasticsearch集群与分片(Sharding)的ELK安装配置指南
  • qemu如何支持vpxor %xmm0,%xmm0,%xmm0(百度AI)
  • ACI multipod 一、组网概要
  • 【自然语言处理与大模型】如何知道自己部署的模型的最大并行访问数呢?
  • 「数据可视化 D3系列」入门第十二章:树状图详解与实践
  • Docker 快速入门教程
  • XPath 介绍
  • Ubuntu与Linux的关系
  • Linux虚拟机中 编译Linux源码 记录
  • 给 20GB 文件“排排坐”——详解外部排序
  • 鸿蒙NEXT开发定位工具类 (WGS-84坐标系)(ArkTs)
  • ios开发中xxx.debug.dylib not found
  • MySQL终章(8)JDBC
  • OpenCV --- 图像预处理(六)
  • 小白工具视频转MPG, 功能丰富齐全,无需下载软件,在线使用,超实用
  • 基于Spring Security 6的OAuth2 系列之二十六 - 终章
  • 2537. 统计好子数组的数目
  • AI深度伪造视频用于诈骗的法律定性与风险防范
  • 【Vue】路由管理(Vue Router)
  • Java ByteBuf解析和进制转换汇总
  • Spark-SQL 项目
  • Linux安装后无法启动24天
  • 数据集 | 柑橘果目标检测数据集
  • 大数据开发的基本流程
  • 基于机器学习的房租影响因素分析系统
  • 安卓模拟器绕过检测全解析:雷电、MuMu、蓝叠、逍遥、夜神与WSA完整指南
  • 3.1.1 MaterialDesign中DrawerHost使用案例