Add rubocop to CI (closed #2)

This commit is contained in:
ChillerDragon 2022-11-05 17:57:12 +01:00
parent 742b665f26
commit 39389ae379
8 changed files with 89 additions and 16 deletions

27
.github/workflows/style.yml vendored Normal file
View file

@ -0,0 +1,27 @@
name: Style
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby 3.1
uses: actions/setup-ruby@v1
with:
ruby-version: 3.1.x
- name: Prepare
run: |
gem install bundler
gem install rubocop:1.31.2
bundle install --jobs 4 --retry 3
- name: Check ruby with rubocop
run: |
rubocop

47
.rubocop.yml Normal file
View file

@ -0,0 +1,47 @@
AllCops:
TargetRubyVersion: 3.1.2
NewCops: enable
Metrics/BlockLength:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/AbcSize:
Enabled: false
Style/GlobalVars:
Enabled: false
Naming/MethodParameterName:
Enabled: false
Metrics/ClassLength:
Enabled: false
Layout/LineContinuationLeadingSpace:
Enabled: false
# TODO: enable
Metrics/ParameterLists:
Enabled: false
Lint/DuplicateBranch:
Enabled: false
Style/Documentation:
Enabled: false
Naming/AccessorMethodName:
Enabled: false
Style/ClassVars:
Enabled: false

View file

@ -11,9 +11,9 @@ def str_bytes(str)
end end
def bytes_to_str(data) def bytes_to_str(data)
data.unpack('H*').join('') data.unpack('H*').join
end end
def get_byte(data, start = 0, num = 1) def get_byte(data, start = 0, num = 1)
data[start...(start + num)].unpack('H*').join('').upcase data[start...(start + num)].unpack('H*').join.upcase
end end

View file

@ -75,7 +75,7 @@ class NetChunk
size_bits[6..] + size_bits[6..] +
seq_bits[2..] seq_bits[2..]
header_bits.chars.groups_of(8).map do |eigth_bits| header_bits.chars.groups_of(8).map do |eigth_bits|
eigth_bits.join('').to_i(2) eigth_bits.join.to_i(2)
end end
end end
@ -91,8 +91,8 @@ class NetChunk
size_bytes = size.chars.groups_of(8) size_bytes = size.chars.groups_of(8)
# trim first 2 bits of both bytes # trim first 2 bits of both bytes
# Size: 2 bytes (..00 0000 ..00 0010) # Size: 2 bytes (..00 0000 ..00 0010)
size_bytes.map! { |b| b[2..].join('') } size_bytes.map! { |b| b[2..].join }
@size = size_bytes.join('').to_i(2) @size = size_bytes.join.to_i(2)
# sequence number # sequence number
# in da third byte but who needs seq?! # in da third byte but who needs seq?!

View file

@ -13,7 +13,7 @@ class NetBase
@port = nil @port = nil
@s = nil @s = nil
@ack = 0 @ack = 0
@server_token = [0xFF, 0xFF, 0xFF, 0xFF].map { |b| b.to_s(16) }.join('') @server_token = [0xFF, 0xFF, 0xFF, 0xFF].map { |b| b.to_s(16) }.join
end end
def connect(socket, ip, port) def connect(socket, ip, port)
@ -47,7 +47,7 @@ class NetBase
header_bits = "00#{flags_bits}#{@ack.to_s(2).rjust(10, '0')}#{num_chunks.to_s(2).rjust(8, '0')}" header_bits = "00#{flags_bits}#{@ack.to_s(2).rjust(10, '0')}#{num_chunks.to_s(2).rjust(8, '0')}"
header = header_bits.chars.groups_of(8).map do |eight_bits| header = header_bits.chars.groups_of(8).map do |eight_bits|
eight_bits.join('').to_i(2) eight_bits.join.to_i(2)
end end
header += str_bytes(@server_token) header += str_bytes(@server_token)

View file

@ -53,7 +53,7 @@ class Packer
bytes = [] bytes = []
num_bits.chars.groups_of(7).each do |seven_bits| num_bits.chars.groups_of(7).each do |seven_bits|
# mark all as extended # mark all as extended
bytes << "1#{seven_bits.join('').rjust(7, '0')}" bytes << "1#{seven_bits.join.rjust(7, '0')}"
end end
# least significant first # least significant first
bytes = bytes.reverse bytes = bytes.reverse
@ -105,7 +105,6 @@ class Unpacker
# because bigger ints are not sent anyways # because bigger ints are not sent anyways
bytes = @data.map { |byte| byte.to_s(2).rjust(8, '0') } bytes = @data.map { |byte| byte.to_s(2).rjust(8, '0') }
first = bytes[0] first = bytes[0]
other = bytes[1..]
sign = first[1] == '1' ? -1 : 1 sign = first[1] == '1' ? -1 : 1
bits = [] bits = []
@ -127,7 +126,7 @@ class Unpacker
bits = [first[2..]] bits = [first[2..]]
@data = @data[1..] @data = @data[1..]
end end
bits.join('').to_i(2) * sign bits.join.to_i(2) * sign
end end
end end

View file

@ -61,11 +61,11 @@ class Packet
flags_byte = @data[0].unpack('B*') flags_byte = @data[0].unpack('B*')
@flags = PacketFlags.new(flags_byte.first[2..5]).hash @flags = PacketFlags.new(flags_byte.first[2..5]).hash
@payload = @data[PACKET_HEADER_SIZE..] @payload = @data[PACKET_HEADER_SIZE..]
if flags_compressed return unless flags_compressed
@payload = @huffman.decompress(@payload.unpack('C*')) @payload = @huffman.decompress(@payload.unpack('C*'))
@payload = @payload.pack('C*') @payload = @payload.pack('C*')
end end
end
def annotate_first_row(bytes) def annotate_first_row(bytes)
header = bytes[0..2].join(' ').yellow header = bytes[0..2].join(' ').yellow
@ -81,7 +81,7 @@ class Packet
def to_s def to_s
puts "#{@prefix}Packet" puts "#{@prefix}Packet"
puts @prefix + " flags: #{@flags}" puts @prefix + " flags: #{@flags}"
bytes = str_hex(@data).split(' ') bytes = str_hex(@data).split
# TODO: check terminal size? # TODO: check terminal size?
max_width = 14 max_width = 14
rows = bytes.groups_of(max_width) rows = bytes.groups_of(max_width)

View file

@ -97,7 +97,7 @@ class TeeworldsClient
@game_client = GameClient.new(self) @game_client = GameClient.new(self)
# me trying to write cool code # me trying to write cool code
@client_token = (1..4).to_a.map { |_| rand(0..255) } @client_token = (1..4).to_a.map { |_| rand(0..255) }
@client_token = @client_token.map { |b| b.to_s(16) }.join('') @client_token = @client_token.map { |b| b.to_s(16) }.join
puts "client token #{@client_token}" puts "client token #{@client_token}"
@netbase = NetBase.new @netbase = NetBase.new
@netbase.client_token = @client_token @netbase.client_token = @client_token