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

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

protobuf生成的协议,有挺多协议的.利用反射生成dto进行伪协议的响应 和 发送请求
应用场景:
请求(CS)_后端先写完了,前端还搞完时,可使用此请求,可自测
响应(SC)_可自行构建一个响应,对数据进行测试

// 请求 使用物品  CS
message ReqUseItem{
    optional Opcodes MessageID=1[default = CG_MSG_REQ_USE_ITEM];
    optional uint32 itemId = 2;
    optional uint32 num = 3;
    optional string strparam = 4;  //buff{"castBuff":"1"}        {"rewardIdx":"1"} 礼包选择,从1开始     
}
//响应 使用物品 SC
message RetUseItem{
    optional Opcodes MessageID=1[default = GC_MSG_RET_USE_ITEM];
    optional uint32 errorCode = 2;
    optional uint32 itemId = 3;
    optional uint32 num = 4;
    repeated ItemInfo rewards = 5;
    optional string strparam = 6;
}

Unity使用Odin,写的页面,Json格式 dtoName{json内容}
在这里插入图片描述

using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Reflection;
using QQu;
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 && t.Namespace == "Pb").ToList();
            foreach (var itemClass in entityValues)
            {
                var key = itemClass.FullName.Replace("Pb.", "");
                mEntityPbClass[key] = itemClass;
            }
        }
        Debug.LogError($"初始化_Pb_总条数{mEntityPbClass.Count}");
    }

    [LabelText("只知道dto名字"), HorizontalGroup("module"), PropertyOrder(Order = 1), Space(5)]
    public string mDtoNameTxt = "ReqUseItem";

    [Button("先 生成一个Json_Dto,然后手动自行修改赋值", ButtonSizes.Medium, ButtonHeight = 30), HorizontalGroup("module"), PropertyOrder(Order = 2)]
    public void GenOneDto()
    {
        if (mEntityPbClass.TryGetValue(mDtoNameTxt, out var itemClass))
        {
            var properties = itemClass.GetProperties();
            var defaultValues = new Dictionary<string, object>();
            foreach (var property in properties)
            {
                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;
                }
            }
            var jsonContent = Newtonsoft.Json.JsonConvert.SerializeObject(defaultValues);
            mDtoJsonTxt = itemClass.Name.ToString() + "\n" + jsonContent.ToString();
        }
        else
        {
            Debug.LogError("确定左边 输入正确了?");
        }
    }

    [TextArea(20, 50), HideLabel, Space(30), PropertyOrder(Order = 3), InfoBox("应用场景: \r\n请求(CS)_后端先写完了,前端还搞完时,可使用此请求,可自测\r\n响应(SC)_可自行构建一个响应,对数据进行测试\r\nPS:Console面板的Log可以直接copy内容,再对内容进行小修改")]
    public string mDtoJsonTxt = @"SyncCurrency{
  ""InfoLists"": [
    {
      ""Ntype"": 11,
      ""Val"": 101660500
    }
  ]
}";
    [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("数据格式 不对哦   dtoName{jsonString}   eg:SyncCurrency{  \"InfoLists\": [    {      \"Ntype\": 11,      \"Val\": 101660500    }  ]}");
            return;
        }
        if (mEntityPbClass.TryGetValue(typeName, out var itemClass))
        {
            var syncCurrency = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContent, itemClass); // 使用非泛型的方法
            PropertyInfo messageIDProperty = itemClass.GetProperty("MessageID");
            object messageIDValue = messageIDProperty.GetValue(syncCurrency);
            var cmdValue = (int)messageIDValue;
            NetMgr.Ins.EditorReceive_Test((uint)cmdValue, syncCurrency);
        }
        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("数据格式 不对哦   dtoName{jsonString}   eg:SyncCurrency{  \"InfoLists\": [    {      \"Ntype\": 11,      \"Val\": 101660500    }  ]}");
            return;
        }
        if (mEntityPbClass.TryGetValue(typeName, out var itemClass))
        {
            var syncCurrency = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContent, itemClass); // 使用非泛型的方法
            PropertyInfo messageIDProperty = itemClass.GetProperty("MessageID");
            object messageIDValue = messageIDProperty.GetValue(syncCurrency);
            var cmdValue = (int)messageIDValue;
            NetMgr.Ins.Send((uint)cmdValue, syncCurrency);//此行 调用 自己的业务 
        }
        else
        {
            Debug.LogError("查查吧~1.DTO没写对? 2.协议不是最新的?");
        }
    }
}

相关文章:

  • 【Audio】Android 10车载音频路由
  • Qt:Qt网络
  • 智能背后的阴影:LLM安全风险
  • C语言中隐式类型转换 截断和整型提升
  • 电脑出现蓝屏英文怎么办?查看修复过程
  • 介绍几款免费的显示器辅助工具!
  • Windows系统中常用的命令
  • 如何使用ADB进行WIFI调试
  • 零售行业线下门店的AI知识库应用实践
  • 可视化实操记录(自用)
  • uniapp + vite + 使用多个 ui 库
  • 2025Java面试题超详细整理《微服务篇》
  • 【音视频】RTSP拉流: RTP负载AAC详解(三)
  • redis cluster测试
  • Linux笔记:Vim编辑器基本操作笔记
  • BDD风格测试
  • Golang协程调度模型MPG
  • 基于Swift实现仿IOS闹钟
  • .Net使用EF Core框架如何连接Oracle
  • Django 创建表 choices的妙用:get_<field_name>_display()
  • 官方通报汕头违建豪宅“英之园”将强拆:对有关人员严肃追责问责
  • 查幽门螺杆菌的这款同位素长期被海外垄断,秦山核电站实现突破
  • “走进书适圈”:一周城市生活
  • “朱雀玄武敕令”改名“周乔治华盛顿”?警方称未通过审核
  • 年在沪纳税350亿人民币,这些全球头部企业表示“对上海承诺不会变”
  • 京东回应外卖系统崩溃:订单暴涨所致,已恢复