using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; using XLua; using BF; namespace BFEditor { public class GenProtoConfig { static private string ProtoToolDir = Application.dataPath + "/../Tools/proto"; static private string PbSaveDir = Application.dataPath + "/proto"; static private string LuaSaveDir = Application.dataPath + "/Developer/lua/app/proto/"; static private string MsgIdsProto = PbSaveDir + "/bf_msgids.bytes"; static public void GenProtoLuaConfig() { try { var protoPath = LocalData.GetProtoPath(); if (string.IsNullOrEmpty(protoPath) || !Directory.Exists(protoPath)) { EditorUtility.DisplayDialog("提示", "没有正确配置Proto路径!", "确定"); return; } BFEditorUtils.RunCommond("python", ProtoToolDir + "/proto.py " + protoPath + " " + LuaSaveDir, ProtoToolDir, (msg) => { }, (err) => { } ); var dirInfo = new DirectoryInfo(protoPath); var files = dirInfo.GetFiles("*.pb", SearchOption.AllDirectories); for (var i = 0; i < files.Length; ++i) { var fileInfo = files[i]; var filePath = fileInfo.FullName; var outPath = Path.Combine(PbSaveDir, filePath.Substring(protoPath.Length + 1)).Replace(".pb", ".bytes"); if (File.Exists(outPath)) { var inMD5 = GameLaunchUtils.GetFileMD5(filePath); var outMD5 = GameLaunchUtils.GetFileMD5(outPath); if (inMD5.CompareTo(outMD5) == 0) { continue; } } File.Copy(filePath, outPath, true); } // LuaEnv luaEnv = new LuaEnv(); // luaEnv.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf); // luaEnv.DoString(@"allNames = {}"); // var pbFullName = MsgIdsProto; // var pbPath = BF.Utils.GetAssetPathByFullPath(pbFullName); // var luaText = CreateLuaString(pbPath); // luaEnv.DoString(luaText); // var luaDir = LuaSaveDir; // if (!Directory.Exists(luaDir)) // Directory.CreateDirectory(luaDir); // var luaPath = luaDir + "protomsgtype.lua"; // luaEnv.DoString(SaveLuaFile(luaPath)); // luaEnv.Dispose(); // AssetDatabase.ImportAsset(pbPath); // AssetDatabase.Refresh(); // AssetDatabase.DeleteAsset(pbPath); // Proto2MarkDown.Pb2Markdown(); EditorUtility.DisplayDialog("提示", "生成Proto配置成功!", "确定"); } catch (Exception e) { throw; } finally { } } static private string CreateLuaString(string protoPath) { var lua = @" local pb = require 'pb' assert(pb.loadfile '" + protoPath + @"') local function split(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if (delimiter=='') then return false end local pos,arr = 0, {} -- for each divider found for st,sp in function() return string.find(input, delimiter, pos, true) end do table.insert(arr, string.sub(input, pos, st - 1)) pos = sp + 1 end table.insert(arr, string.sub(input, pos)) return arr end local function unique(number) for _,info in ipairs(allNames) do if info.number == number then return false end end return true end for name, number, type in pb.fields 'MsgId' do -- print(name, number) if number > 0 and unique(number) then table.insert(allNames, {number = number, name = name}) end end table.sort(allNames, function(a, b) return a.number < b.number end) "; return lua; } static private string SaveLuaFile(string luaPath) { var lua = @" local output = 'local ProtoMsgType = {\n' output = output .. ' FromMsgId = {\n' for i, info in ipairs(allNames) do output = output .. ' ' .. '[' .. info.number .. ']' .. ' = \'' .. info.name .. '\',\n' end output = output .. ' },\n' output = output .. ' FromMsgToId = {\n' for i, info in ipairs(allNames) do output = output .. ' ' .. info.name .. ' = ' .. info.number .. ',\n' end output = output .. ' },\n' output = output .. ' FromMsgEnum = {\n' for i, info in ipairs(allNames) do output = output .. ' ' .. 'req' .. info.name .. ' = \'' .. 'req' .. info.name .. '\',\n' output = output .. ' ' .. 'rsp' .. info.name .. ' = \'' .. 'rsp' .. info.name .. '\',\n' end output = output .. ' },\n' output = output .. '}\n\n' output = output .. 'return ProtoMsgType' local file = assert(io.open('" + luaPath + @"', 'w')) file:write(output) file:close() -- print(output) "; return lua; } } }