import hashlib import os def calculate_md5(file_path): """计算文件的MD5值""" hash_md5 = hashlib.md5() with open(file_path, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() def write_file_code(output_file, proto_md5): output_file.write("///\n") output_file.write("///auto generated by gen_ServerScriptHotfixCheck.py\n") output_file.write("///\n") output_file.write("\n") output_file.write("using System;\n") output_file.write("using ProtoCSStruct;\n") output_file.write("\n") output_file.write("namespace Sog\n{\n") output_file.write("\n") output_file.write(" public class ServerScriptHotfixCheck : IScriptHotfixCheck\n") output_file.write(" {\n") output_file.write(" public const int DBRoleData_Size = DBRoleData.SizeOf;\n") output_file.write(" public const string preString = \"SOGSCHFCMAGICSTR:[\";\n") output_file.write(f" public const string proto_md5String = \"{proto_md5}\";\n") output_file.write(" public string GetCheckString()\n") output_file.write(" {\n") output_file.write(" return preString + DBRoleData_Size + \" protomd5:\" + proto_md5String + ']';\n") output_file.write(" }\n") output_file.write(" }\n") output_file.write("}\n") output_file.write("\n") return def write_ServerScriptHotfixCheck(proto_md5): serverrootpath="../../u2china_svr_proj/" allserverdir = [ #account "VersionServer", "AccountServer", "GateServer", "DBServer", "RealmlistServer", "HttpProxy", "HttpProxyPay", "OperationServer", "OperationLogicServer", "ArenaKFServer", #world "MailServer", "BillLogServer", "WorldServer", "GameDBServer", "HttpProxyWorld", "GameServer", "RankServer", "ChatServer", "BattleServer", "ArenaServer", "FriendServer", "UnionServer", "GVGServer" ] for server in allserverdir: output_filename= serverrootpath + server + "/ServerScriptHotfixCheck.cs" with open(output_filename, 'w') as output_file: write_file_code(output_file,proto_md5) return def main(): # 获取当前目录下所有以.proto为后缀的文件 proto_files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.proto')] # 计算每个文件的MD5并连接 concatenated_md5 = ''.join(calculate_md5(file) for file in proto_files) # 计算连接后的字符串的MD5 final_proto_md5 = hashlib.md5(concatenated_md5.encode()).hexdigest() # 写文件 write_ServerScriptHotfixCheck(final_proto_md5) if __name__ == "__main__": main()