def replace_fun_param(str, mod_name, protocol): # 替换record参数 # 注意()前面的\,会影响正则匹配 begin_str = "\nhandle_c2s_1\(#%s_%s_c2s{" % (mod_name, protocol.name) # 正则匹配函数 a = r"(.*)" + begin_str + "(.*?)}, RoleId(.*)" matchObj = re.match(a, str, re.M | re.S) if matchObj: begin_str = "\nhandle_c2s_1(#%s_%s_c2s{" % (mod_name, protocol.name) str = matchObj.group(1) + begin_str + HelpLib.get_record_param( protocol.c2s) + "}, RoleId" + matchObj.group(3) else: # 匹配不到,说明没有这个函数 return False, str # 替换lib参数 "luck_draw_internal:recruit_one(RoleId, Type);" begin_str = "%s_internal:%s\(RoleId" % (mod_name, protocol.name) a = r"(.*)" + begin_str + "(.*?)\)(.*)" matchObj = re.match(a, str, re.M | re.S) if matchObj: begin_str = "%s_internal:%s(RoleId" % (mod_name, protocol.name) str = matchObj.group(1) + begin_str + HelpLib.get_fun_param( protocol.c2s) + ")" + matchObj.group(3) return True, str
def get_protocol_fun_str(mod_name, protocol): # 协议函数结构 protocol_fun = """ %% {0} handle_c2s_1(#{1}_{2}_c2s{{{3}}}, RoleId) -> {1}_internal:{2}(RoleId{4}); """ # 生成协议函数 tmp_protocol_fun = protocol_fun.format( protocol.desc, mod_name, protocol.name, HelpLib.get_record_param(protocol.c2s), HelpLib.get_fun_param(protocol.c2s)) return tmp_protocol_fun
def get_check_fun_str(c2s_record): protocol_fun_str = """ %% @doc 检查{0} check_{1}(RoleId{2}) -> ok. """ # 函数参数 protocol_fun_str = protocol_fun_str.format( c2s_record.desc , c2s_record.name , HelpLib.get_fun_param(c2s_record) ) return protocol_fun_str
def get_protocol_fun_str(mod_name, c2s_record): protocol_fun_str = """ %% @doc {0} {1}(RoleId{2}) -> case check_{1}(RoleId{2}) of {{false, Code}} -> {{false, Code}}; ok -> #{3} end. """ # 函数参数 protocol_fun_str = protocol_fun_str.format( c2s_record.desc , c2s_record.name , HelpLib.get_fun_param(c2s_record) , mod_name + c2s_record.name + "s2c{}" ) return protocol_fun_str
def replace_protocol_fun_param(str, protocol): # fun_str是否存在 fun_str = "\n" + protocol.name + "(" if str.find(fun_str) == -1: return False,str # 替换rpc接口函数参数 # 正则匹配函数 fun_str_1 = "\n" + protocol.name + "\(" a = r"(.*)" + fun_str_1 + "RoleId(.*?)\) ->(.*)" matchObj = re.match(a, str, re.M|re.S) if matchObj: str = "{0}{1}RoleId{2}) ->{3}".format( matchObj.group(1) , fun_str , HelpLib.get_fun_param(protocol.c2s) , matchObj.group(3) ) else: # 匹配不到 print("替换rpc接口函数参数 err : %s"%(protocol.name)) return True,str