Example #1
0
    public static void GenerateCsRpc()
    {
        if (EditorApplication.isCompiling)
        {
            EditorUtility.DisplayDialog("警告", "请等待编辑器完成编译再执行此功能", "确定");
            return;
        }

        usingList.Clear();
        usingList.Add("System");
        //usingList.Add("System.Diagnostics");
        usingList.Add("System.Collections.Generic");
        usingList.Add("Debug = UnityEngine.Debug");
        //usingList.Add("System.Runtime.InteropServices");
        usingList.Add("LuaInterface");
        usingList.Add("WCG");

        sb = new StringBuilder();
        sb.AppendLine("\npublic static class CsRpcWrap");
        sb.AppendLine("{");

        RegisteredRcvAndSend();

        Add_Call_Bytes2Handler_To_Lua();

        //1导出嵌套协议
        for (int i = 0; i < Types_Nesting.Length; i++)
        {
            ConvertCsRpcToByteFunc(Types_Nesting[i]);
            ConvertCsRpcByteToFunc(Types_Nesting[i]);
        }

        //2导出接收协议
        DispatcherC <byte> tempDispatcher = new DispatcherC <byte>();

        Type[] cs2RpcRcv = tempDispatcher.HandlerRcvType;
        for (int i = 0; i < cs2RpcRcv.Length; i++)
        {
            //ConvertCsRpcToByteFunc(cs2RpcRcv[i]);
            ConvertCsRpcByteToFunc(cs2RpcRcv[i]);
        }

        //3导出发送协议
        Type[] cs2RpcSend = tempDispatcher.HandlerSendType;
        for (int i = 0; i < cs2RpcSend.Length; i++)
        {
            ConvertCsRpcToByteFunc(cs2RpcSend[i]);
            //ConvertCsRpcByteToFunc(cs2RpcSend[i]);
        }

        sb.AppendLine("\r\n");
        EndCodeGen(Application.dataPath + "/Source/RpcCore/");

        Debug.Log("GenerateCsRpc Over");
        AssetDatabase.Refresh();
    }
Example #2
0
    public static bool RegisteredRcvAndSend()
    {
        sb.AppendLine("\tstatic int iBufSize = 1;");
        sb.AppendLine("\tstatic Byte[] byteBuf = new Byte[iBufSize];");
        sb.AppendLine("\tstatic void BufReset()");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\tArray.Clear(byteBuf, 0, iBufSize);");
        sb.AppendLine("\t}");

        sb.AppendLine("");
        sb.AppendLine("\tdelegate bool Bytes_To_Handler(ref Byte[] pData, uint iOffset, ref uint uProcessedOnce, ref LuaFunction luafunc);");
        DispatcherC <byte> tempDispatcher = new DispatcherC <byte>();

        //3导出接收协议
        sb.AppendLine("\tstatic Bytes_To_Handler[] bytes_to_pHander = new Bytes_To_Handler[]");
        sb.AppendLine("\t{");
        Type[] cs2RpcSend = tempDispatcher.HandlerRcvType;
        for (int i = 0; i < cs2RpcSend.Length; i++)
        {
            sb.AppendFormat("\t\t{0}_Bytes_To,\n", cs2RpcSend[i].Name);
        }
        sb.AppendLine("\t};");
        sb.AppendLine("");


        //2导出接收协议
        sb.AppendLine("\tpublic delegate Byte[] To_Bytes_Handler(ref object[] valObjs, uint iOffset);");
        sb.AppendLine("\tpublic static To_Bytes_Handler[] to_bytes_pHander = new To_Bytes_Handler[]");
        sb.AppendLine("\t{");
        Type[] cs2RpcRcv = tempDispatcher.HandlerSendType;
        for (int i = 0; i < cs2RpcRcv.Length; i++)
        {
            sb.AppendFormat("\t\t{0}_To_Bytes,\n", cs2RpcRcv[i].Name);
        }
        sb.AppendLine("\t};");
        sb.AppendLine("");

        //3导出注册和释放CSLua的二级协议接口函数
        sb.AppendFormat("\tstatic LuaFunction[] csRpc_to_LuaFunc = new LuaFunction[{0}];\n", cs2RpcSend.Length);
        sb.AppendLine("\tpublic static void RegisterCSLuaFunc()");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\tDispatcherC<byte> tempDispatcher = new DispatcherC<byte>();");
        sb.AppendLine("\t\tType[] cs2RpcSend = tempDispatcher.HandlerRcvType;");
        sb.AppendLine("\t\tfor (int i = 0; i < cs2RpcSend.Length; i++)");
        sb.AppendLine("\t\t{");
        sb.AppendLine("\t\t\tDebug.Assert(csRpc_to_LuaFunc[i] == null);");
        sb.AppendLine("\t\t\tstring sLuaFunc = cs2RpcSend[i].Name.Replace(\"CS2CG\", \"Gas2GacCs.\");");
        sb.AppendLine("\t\t\tLuaFunction luafunc = GameMgr.ms_luaMgr.GetLuaFunction(sLuaFunc, false);");
        sb.AppendLine("\t\t\tDebug.AssertFormat(luafunc!=null, \"RegisterCSLuaFunc Error: {0}\", sLuaFunc);");
        sb.AppendLine("\t\t\tcsRpc_to_LuaFunc[i] = luafunc;");
        sb.AppendLine("\t\t}");
        sb.AppendLine("\t}");
        sb.AppendLine("");
        sb.AppendLine("\tpublic static void DisposeCSLuaFunc()");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\tfor (int i = 0; i < csRpc_to_LuaFunc.Length; i++)");
        sb.AppendLine("\t\t{");
        sb.AppendLine("\t\t\tLuaFunction luafunc = csRpc_to_LuaFunc[i];");
        sb.AppendLine("\t\t\tif (luafunc != null)");
        sb.AppendLine("\t\t\t{");
        sb.AppendLine("\t\t\t\tluafunc.Dispose();");
        sb.AppendLine("\t\t\t\tluafunc = null;");
        sb.AppendLine("\t\t\t\tcsRpc_to_LuaFunc[i] = null;");
        sb.AppendLine("\t\t\t}");
        sb.AppendLine("\t\t}");
        sb.AppendLine("\t}");

        return(true);
    }