From 9e162e1a3cbf4ea75953bb1f4e2b9440c9456348 Mon Sep 17 00:00:00 2001 From: hovertank3d Date: Thu, 13 Nov 2025 23:07:41 +0100 Subject: make clang-format happy --- .clang-format | 33 ------------- include/net/client.hpp | 2 +- include/net/conn.hpp | 129 +++++++++++++++++++++++++----------------------- include/net/packet.hpp | 6 +-- include/net/server.hpp | 25 +++++----- include/net/types.hpp | 32 ++++++------ include/util/bitset.hpp | 71 +++++++++++++------------- include/version.hpp | 2 +- src/kill_proxy.cpp | 20 ++++---- 9 files changed, 149 insertions(+), 171 deletions(-) diff --git a/.clang-format b/.clang-format index 60d5ee5..b182cc9 100644 --- a/.clang-format +++ b/.clang-format @@ -35,29 +35,7 @@ AlignConsecutiveShortCaseStatements: Enabled: false AcrossEmptyLines: false AcrossComments: false - AlignCaseArrows: false AlignCaseColons: false -AlignConsecutiveTableGenBreakingDAGArgColons: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveTableGenCondOperatorColons: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveTableGenDefinitionColons: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false AlignEscapedNewlines: Right AlignOperands: DontAlign AlignTrailingComments: @@ -67,7 +45,6 @@ AllowAllArgumentsOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowBreakBeforeNoexceptSpecifier: Never AllowShortBlocksOnASingleLine: Empty -AllowShortCaseExpressionOnASingleLine: true AllowShortCaseLabelsOnASingleLine: false AllowShortCompoundRequirementOnASingleLine: true AllowShortEnumsOnASingleLine: true @@ -104,7 +81,6 @@ BraceWrapping: BreakAdjacentStringLiterals: true BreakAfterAttributes: Leave BreakAfterJavaFieldAnnotations: false -BreakAfterReturnType: None BreakArrays: true BreakBeforeBinaryOperators: All BreakBeforeBraces: WebKit @@ -112,10 +88,8 @@ BreakBeforeConceptDeclarations: Always BreakBeforeInlineASMColon: OnlyMultiline BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeComma -BreakFunctionDefinitionParameters: false BreakInheritanceList: BeforeColon BreakStringLiterals: true -BreakTemplateDeclarations: MultiLine ColumnLimit: 0 CommentPragmas: "^ IWYU pragma:" CompactNamespaces: false @@ -171,15 +145,10 @@ IntegerLiteralSeparator: HexMinDigits: 0 JavaScriptQuotes: Leave JavaScriptWrapImports: true -KeepEmptyLines: - AtEndOfFile: false - AtStartOfBlock: true - AtStartOfFile: true LambdaBodyIndentation: Signature LineEnding: DeriveLF MacroBlockBegin: "" MacroBlockEnd: "" -MainIncludeChar: Quote MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCBinPackProtocolList: Auto @@ -248,7 +217,6 @@ SpacesInLineCommentPrefix: Maximum: -1 SpacesInParens: Never SpacesInParensOptions: - ExceptDoubleParentheses: false InConditionalStatements: false InCStyleCasts: false InEmptyParentheses: false @@ -261,7 +229,6 @@ StatementMacros: - Q_UNUSED - QT_REQUIRE_VERSION TabWidth: 4 -TableGenBreakInsideDAGArg: DontBreak UseTab: ForIndentation VerilogBreakBetweenInstancePorts: true WhitespaceSensitiveMacros: diff --git a/include/net/client.hpp b/include/net/client.hpp index 8f81638..234ca90 100644 --- a/include/net/client.hpp +++ b/include/net/client.hpp @@ -22,7 +22,7 @@ namespace net { class client { - struct sockaddr_in server_address {}; + struct sockaddr_in server_address { }; public: client(std::string hostname, int port) diff --git a/include/net/conn.hpp b/include/net/conn.hpp index b69dac6..6569973 100644 --- a/include/net/conn.hpp +++ b/include/net/conn.hpp @@ -16,9 +16,9 @@ class conn { sockaddr_in conn_addr; socklen_t conn_addr_size; - using handler_list = std::vector)>>; - std::array handlers; - std::map netmodule_handlers; + using handler_list = std::vector)>>; + std::array handlers; + std::map netmodule_handlers; private: packet::packet_header read_header() @@ -29,14 +29,14 @@ private: recv(packet_size); recv(id); - return { id, packet_size - 3}; + return { id, packet_size - 3 }; } public: conn(int sock_fd, sockaddr_in conn_addr, socklen_t conn_addr_size) - : sock_fd(sock_fd) - , conn_addr(conn_addr) - , conn_addr_size(conn_addr_size) + : sock_fd(sock_fd) + , conn_addr(conn_addr) + , conn_addr_size(conn_addr_size) { } @@ -67,9 +67,9 @@ public: ssize_t recv(std::span data) { - if (data.size_bytes() == 0) { - return 0; - } + if (data.size_bytes() == 0) { + return 0; + } auto bytes = ::recv(sock_fd, data.data(), data.size_bytes(), 0); if (bytes <= 0) { @@ -98,34 +98,36 @@ public: auto [id, len] = read_header(); assert(id == T::packet_id); - if(len > 0) { + if (len > 0) { std::vector payload(len); recv(payload); decode_packet(payload, t); } } - template - void reg_handler(H h) { - handlers[P::packet_id].push_back([this, h](std::span payload) { - P t; - if (payload.size() > 0) { - packet::decode_packet(payload, t); + template + void reg_handler(H h) + { + handlers[P::packet_id].push_back([this, h](std::span payload) { + P t; + if (payload.size() > 0) { + packet::decode_packet(payload, t); } - return h(t); - }); - } - - template - void reg_handler(H h) { - netmodule_handlers[P::module_id].push_back([this, h](std::span payload) { - P t; - if (payload.size() > 0) { - packet::decode_packet(payload, t); + return h(t); + }); + } + + template + void reg_handler(H h) + { + netmodule_handlers[P::module_id].push_back([this, h](std::span payload) { + P t; + if (payload.size() > 0) { + packet::decode_packet(payload, t); } - return h(t); - }); - } + return h(t); + }); + } template void send_packet(T p) @@ -137,52 +139,53 @@ public: if (payload.size() > 0) { send(payload); } - } - - bool handle_netmodule(std::span payload) { + } + + bool handle_netmodule(std::span payload) + { uint16_t id; std::memcpy(&id, payload.data(), sizeof(id)); id = to_little(id); - if (netmodule_handlers[id].size() <= 0) { + if (netmodule_handlers[id].size() <= 0) { // currently i won't bother implementing netmodules // FIXME: return false - return true; - } - - for (auto &h : handlers[id]) { - if (h(payload.subspan(2)) == true) { - return false; - } - } - return true; + return true; + } + + for (auto& h : handlers[id]) { + if (h(payload.subspan(2)) == true) { + return false; + } + } + return true; } - bool handle() { - auto [id, len] = read_header(); - if (id == 0) { - return false; - } + bool handle() + { + auto [id, len] = read_header(); + if (id == 0) { + return false; + } - std::vector payload(len); - recv(payload); + std::vector payload(len); + recv(payload); - //got netmodule - if (id == 82) { + // got netmodule + if (id == 82) { return handle_netmodule(payload); } - if (handlers[id].size() <= 0) { - return false; - } - - for (auto &h : handlers[id]) { - if (h(payload) == true) { - break; - } - } - return true; - } + if (handlers[id].size() <= 0) { + return false; + } + for (auto& h : handlers[id]) { + if (h(payload) == true) { + break; + } + } + return true; + } }; } \ No newline at end of file diff --git a/include/net/packet.hpp b/include/net/packet.hpp index f2094c0..117fc15 100644 --- a/include/net/packet.hpp +++ b/include/net/packet.hpp @@ -62,7 +62,7 @@ std::vector encode_packet(T& t) template std::vector encode_packet(T& t) { - static constexpr auto chunk_size = 1024; + static constexpr auto chunk_size = 1024; std::array buffer; std::vector payload; @@ -82,10 +82,10 @@ std::vector encode_packet(T& t) strm.next_in = raw_payload.data(); do { strm.avail_out = chunk_size; - strm.next_out = reinterpret_cast(buffer.data()); + strm.next_out = reinterpret_cast(buffer.data()); ret = deflate(&strm, flush); assert(ret != Z_STREAM_ERROR); - payload.insert(payload.end(), buffer.begin(), buffer.end()-strm.avail_out); + payload.insert(payload.end(), buffer.begin(), buffer.end() - strm.avail_out); } while (strm.avail_out == 0); assert(strm.avail_in == 0); /* all input will be used */ diff --git a/include/net/server.hpp b/include/net/server.hpp index 14a5d1d..9a27a25 100644 --- a/include/net/server.hpp +++ b/include/net/server.hpp @@ -22,11 +22,10 @@ namespace net { class server { - struct sockaddr_in server_address {}; + struct sockaddr_in server_address { }; int sock_fd; public: - server(std::string hostname, int port) { server_address.sin_family = AF_INET; @@ -34,30 +33,34 @@ public: server_address.sin_port = htons(port); } - ~server() { + ~server() + { close(sock_fd); } - void bind() { + void bind() + { sock_fd = socket(AF_INET, SOCK_STREAM, 0); - if (sock_fd < 0) + if (sock_fd < 0) throw std::runtime_error(strerror(errno)); - + int bindStatus = ::bind(sock_fd, (struct sockaddr*)&server_address, sizeof(server_address)); if (bindStatus < 0) throw std::runtime_error(strerror(errno)); } - void listen(int max_connections) { + void listen(int max_connections) + { ::listen(sock_fd, max_connections); } - - conn accept() { + + conn accept() + { sockaddr_in conn_addr {}; socklen_t conn_addr_size = sizeof(conn_addr); - int conn_fd = ::accept(sock_fd, (struct sockaddr *)&conn_addr, &conn_addr_size); - if (conn_fd < 0) + int conn_fd = ::accept(sock_fd, (struct sockaddr*)&conn_addr, &conn_addr_size); + if (conn_fd < 0) throw std::runtime_error(strerror(errno)); return conn { conn_fd, conn_addr, conn_addr_size }; } diff --git a/include/net/types.hpp b/include/net/types.hpp index 9ebc56c..8f0e383 100644 --- a/include/net/types.hpp +++ b/include/net/types.hpp @@ -25,7 +25,7 @@ template concept packet = requires(T x) { x.packet_id; }; template -concept compressed_packet = requires(T x) { packet && x.compressed; }; +concept compressed_packet = requires(T x) { packet&& x.compressed; }; template concept netmodule = requires(T x) { x.module_id; }; @@ -59,7 +59,7 @@ struct rgb { }; struct death_reason { - enum class reason : int { + enum class reason : int { Player = 0, NPC, Projectile, @@ -71,7 +71,7 @@ struct death_reason { }; bitset<8> reasons; - + int16_t player_index; int16_t npc_index; int16_t projectile_index; @@ -126,7 +126,7 @@ void encode_field(std::vector& data, std::array& f) } template -void encode_field(std::vector& data, std::vector &f) +void encode_field(std::vector& data, std::vector& f) { for (auto& e : f) { encode_field(data, e); @@ -134,7 +134,7 @@ void encode_field(std::vector& data, std::vector &f) } template -void encode_field(std::vector& data, std::span &f) +void encode_field(std::vector& data, std::span& f) { for (auto& e : f) { encode_field(data, e); @@ -142,14 +142,14 @@ void encode_field(std::vector& data, std::span &f) } template -void encode_field(std::vector& data, bitset &f) +void encode_field(std::vector& data, bitset& f) { for (auto& e : f) { encode_field(data, e); } } -void encode_field(std::vector& data, death_reason &f) +void encode_field(std::vector& data, death_reason& f) { encode_field(data, f.reasons); if (f.reasons[death_reason::reason::Player]) { @@ -199,14 +199,12 @@ ssize_t decode_field(std::span data, T& f) return sizeof(T); } - ssize_t decode_field(std::span data, std::vector& f) -{ +{ f.insert(f.begin(), data.begin(), data.end()); return f.size(); } - template ssize_t decode_field(std::span data, std::array& f) { @@ -220,7 +218,7 @@ ssize_t decode_field(std::span data, std::array& f) return offset; } -ssize_t decode_field(std::span data, death_reason &f) +ssize_t decode_field(std::span data, death_reason& f) { int offset = 0; offset += decode_field(data, f.reasons); @@ -372,7 +370,7 @@ struct send_tile_data { static constexpr uint8_t packet_id = 10; static constexpr packet_flag compressed {}; - //FIXME: implement + // FIXME: implement }; struct spawn_player { @@ -480,12 +478,12 @@ struct player_loadout { struct damage_player { static constexpr uint8_t packet_id = 117; - uint8_t client_id; + uint8_t client_id; death_reason reason; - int16_t damage; - uint8_t hit_direction; - uint8_t ty; - int8_t cooldown_counter; + int16_t damage; + uint8_t hit_direction; + uint8_t ty; + int8_t cooldown_counter; }; template diff --git a/include/util/bitset.hpp b/include/util/bitset.hpp index 8798a4a..de5bd49 100644 --- a/include/util/bitset.hpp +++ b/include/util/bitset.hpp @@ -4,45 +4,50 @@ #include #include -constexpr int bits_ceil(int bits) { - return bits / 8 + (bits % 8 > 0 ? 1 : 0); +constexpr int bits_ceil(int bits) +{ + return bits / 8 + (bits % 8 > 0 ? 1 : 0); } template struct bitset : std::array { - using array_type = std::array; + using array_type = std::array; private: - struct bit_ref { - bitset &a; - int referenced_byte; - uint8_t referenced_bit_mask; - - bool operator=(bool b) { - if (b) - a.at(referenced_byte) |= referenced_bit_mask; - else - a.at(referenced_byte) &= ~referenced_bit_mask; - return b; - } - - constexpr operator bool() const { - return (a.at(referenced_byte) & referenced_bit_mask) > 0; - } - }; + struct bit_ref { + bitset& a; + int referenced_byte; + uint8_t referenced_bit_mask; + + bool operator=(bool b) + { + if (b) + a.at(referenced_byte) |= referenced_bit_mask; + else + a.at(referenced_byte) &= ~referenced_bit_mask; + return b; + } + + constexpr operator bool() const + { + return (a.at(referenced_byte) & referenced_bit_mask) > 0; + } + }; public: - template - constexpr bool operator [](T idx) const { - unsigned int i = (unsigned int)(idx); - assert(i < flags); - return (array_type::at(i/8) & (1 << (i%8))) > 0; - } - - template - bit_ref operator[](T idx) { - unsigned int i = (unsigned int)(idx); - assert(i < flags); - return bit_ref(*this, i/8, (1 << (i%8))); - } + template + constexpr bool operator[](T idx) const + { + unsigned int i = (unsigned int)(idx); + assert(i < flags); + return (array_type::at(i / 8) & (1 << (i % 8))) > 0; + } + + template + bit_ref operator[](T idx) + { + unsigned int i = (unsigned int)(idx); + assert(i < flags); + return bit_ref(*this, i / 8, (1 << (i % 8))); + } }; \ No newline at end of file diff --git a/include/version.hpp b/include/version.hpp index 0371595..b5f4856 100644 --- a/include/version.hpp +++ b/include/version.hpp @@ -1,3 +1,3 @@ #pragma once -constexpr auto max_buffs = 44; \ No newline at end of file +constexpr auto max_buffs = 44; \ No newline at end of file diff --git a/src/kill_proxy.cpp b/src/kill_proxy.cpp index a2a2e27..82723e1 100644 --- a/src/kill_proxy.cpp +++ b/src/kill_proxy.cpp @@ -26,11 +26,13 @@ using net::packet::operator""_ns; template -void forward_packets(net::conn &dst, net::conn &src, std::integer_sequence int_seq) { - (src.reg_handler>([&dst](auto &a) { +void forward_packets(net::conn& dst, net::conn& src, std::integer_sequence int_seq) +{ + (src.reg_handler>([&dst](auto& a) { dst.send_packet(a); return true; - }), ...); + }), + ...); } int main(int argc, char* argv[]) @@ -50,25 +52,25 @@ int main(int argc, char* argv[]) return false; }); - proxy_conn.reg_handler([&slot, &server_conn](auto &dmg) { + proxy_conn.reg_handler([&slot, &server_conn](auto& dmg) { if (dmg.client_id != slot) { dmg.damage *= 100; dmg.reason.reasons.at(0) = 0; dmg.reason.reasons[net::packet::death_reason::reason::Custom] = true; dmg.reason.custom = "killed by server insecurity"; - + server_conn.send_packet(dmg); return true; } return false; }); - forward_packets(server_conn, proxy_conn, std::make_integer_sequence{}); - forward_packets(proxy_conn, server_conn, std::make_integer_sequence{}); + forward_packets(server_conn, proxy_conn, std::make_integer_sequence {}); + forward_packets(proxy_conn, server_conn, std::make_integer_sequence {}); - auto handle_loop = [] (net::conn &c) { - for(;;) { + auto handle_loop = [](net::conn& c) { + for (;;) { c.handle(); } }; -- cgit v1.2.3