rrb 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5fa8cdaebd91d028bc2a93d59e00c45c7d61090f4fb8547b224ed2d78aa7fea9
4
- data.tar.gz: 69968f16213b56e75f9a96e18a1d5f2a56c187a783820de396c2453398657a9d
3
+ metadata.gz: aff2ff620677328d8da0ff0b455bdfbe02d27d5dd581799c21e782fc6e6f5cca
4
+ data.tar.gz: 2ca970bb43d6977c7a460dbc1f4b5792d2698ea98a53bba4e90e8a0ed152fa38
5
5
  SHA512:
6
- metadata.gz: 91e576b8fbf50b633175d3084f18a7abd5d0b24ab1a71a951e4144d889de7078ae66fc7bace3f8aeb384b793d67b11805dbf536cfcf13da1b42325298bd1345c
7
- data.tar.gz: '09dcd69d25de941f26dd0551276b45170bbd8464675a95ddeb6dbed6adb145aabc536549db28cff504f049c2fc5b7f55e8603dc48652a0a2b5c7e6144c28dfff'
6
+ metadata.gz: 6a498fc882576d04b55ac092d440191f7a00efeaaa3651f048731d6f72c506ada61491ebc7f4665e6e779c7e333edb27062b736d490840868265b3239237dcfb
7
+ data.tar.gz: c41876eea5064a79bfedacdf6988fcdec61ccc97a87ed078f239a849dcaab06083337e7f243e23782293cbdb427e9e6af071a873c2138b3dd092cc2af0b504a1
@@ -5,9 +5,11 @@ module RuneRb::IO
5
5
  class Message
6
6
  include RuneRb::Utils::Logging
7
7
 
8
+ # @!attribute [r] header
8
9
  # @return [Struct] the header for the message.
9
10
  attr :header
10
11
 
12
+ # @!attribute [r] body
11
13
  # @return [Buffer] the access mode for the message.
12
14
  attr :body
13
15
 
@@ -75,13 +77,19 @@ module RuneRb::IO
75
77
  # @param type [Symbol] the type of data to read
76
78
  # @param signed [Boolean] should the value be signed
77
79
  # @param mutation [Symbol] the mutation that should be applied to the data
78
- # @param order [String] the byte order to read the data in.
79
- def read(type: :byte, signed: false, mutation: :STD, order: 'BIG')
80
+ # @param order [Symbol] the byte order to read the data in.
81
+ def read(type: :byte, signed: false, mutation: :STD, order: :BIG)
80
82
  @body.read(type: type, signed: signed, mutation: mutation, order: order)
81
83
  end
82
84
 
83
85
  # Write data to the {Message#body}
84
- def write(value, type: :byte, mutation: :STD, order: 'BIG', options: {})
86
+ # @param value [Integer] the value
87
+ # @param type [Symbol] the type of data to write
88
+ # @param mutation [Symbol] the mutation that should be applied to the data
89
+ # @param order [Symbol] the byte order to write data in
90
+ # @param options [Hash] options for the write operation.
91
+ # @return [RuneRb::IO::Message]
92
+ def write(value, type: :byte, mutation: :STD, order: :BIG, options: {})
85
93
  @body.write(value, type: type, mutation: mutation, order: order, options: options)
86
94
  update_length
87
95
  self
@@ -5,90 +5,95 @@ module RuneRb::IO
5
5
  module NativeReadable
6
6
  # Read a byte value from the {Buffer#data}
7
7
  # @param signed [Boolean] should the value be signed.
8
- # @param mutation [String] mutation that should be applied to the byte value.
8
+ # @param mutation [Symbol] mutation that should be applied to the byte value.
9
+ # @return [Integer]
9
10
  def read_byte(mutation: :STD, signed: false)
10
11
  mutate(@data.slice!(0)&.unpack1(signed ? 'c' : 'C') || 0, mutation)
11
12
  end
12
13
 
13
14
  # Reads a short value from the {Buffer#data}
14
15
  # @param signed [Boolean] should the value be signed.
15
- # @param mutation [String] mutation that should be applied to the short value
16
- # @param order [String] they byte order to read the short value
17
- def read_short(signed: false, mutation: :STD, order: 'BIG')
16
+ # @param mutation [Symbol] mutation that should be applied to the short value
17
+ # @param order [Symbol] they byte order to read the short value
18
+ # @return [Integer]
19
+ def read_short(signed: false, mutation: :STD, order: :BIG)
18
20
  val = 0
19
21
  case order
20
- when 'BIG'
22
+ when :BIG
21
23
  val += mutate(@data.slice!(0..1).unpack1(signed ? 's>' : 'S>'), mutation)
22
- when 'LITTLE'
24
+ when :LITTLE
23
25
  val += mutate(@data.slice!(0..1).unpack1(signed ? 's<' : 'S<'), mutation)
24
- else read_short(signed: signed, mutation: mutation, order: 'BIG')
26
+ else read_short(signed: signed, mutation: mutation, order: :BIG)
25
27
  end
26
28
  val
27
29
  end
28
30
 
29
31
  # Reads a medium value from the {Buffer#data}
30
32
  # @param signed [Boolean] should the value be signed.
31
- # @param mutation [String] mutation that should be applied to the medium value
32
- # @param order [String] they byte order to read the medium value
33
- def read_medium(signed: false, mutation: :STD, order: 'BIG')
33
+ # @param mutation [Symbol] mutation that should be applied to the medium value
34
+ # @param order [Symbol] they byte order to read the medium value
35
+ # @return [Integer]
36
+ def read_medium(signed: false, mutation: :STD, order: :BIG)
34
37
  val = 0
35
38
  case order
36
- when 'BIG'
39
+ when :BIG
37
40
  val += read_byte(signed: signed) << 16
38
41
  val += read_byte(signed: signed) << 8
39
42
  val += read_byte(signed: signed, mutation: mutation)
40
- when 'MIDDLE'
43
+ when :MIDDLE
41
44
  val += read_byte(signed: signed) << 8
42
45
  val += read_byte(signed: signed, mutation: mutation)
43
46
  val += read_byte(signed: signed) << 16
44
- when 'LITTLE'
47
+ when :LITTLE
45
48
  val += read_byte(signed: signed, mutation: mutation)
46
49
  val += read_byte(signed: signed) << 8
47
50
  val += read_byte(signed: signed) << 16
48
- else read_medium(signed: signed, mutation: mutation, order: 'BIG')
51
+ else read_medium(signed: signed, mutation: mutation, order: :BIG)
49
52
  end
50
53
  val
51
54
  end
52
55
 
53
56
  # Reads a integer value from the {Buffer#data}
54
57
  # @param signed [Boolean] should the value be signed.
55
- # @param mutation [String] mutation that should be applied to the integer value
56
- # @param order [String] they byte order to read the integer value
57
- def read_int(signed: false, mutation: :STD, order: 'BIG')
58
+ # @param mutation [Symbol] mutation that should be applied to the integer value
59
+ # @param order [Symbol] they byte order to read the integer value
60
+ # @return [Integer]
61
+ def read_int(signed: false, mutation: :STD, order: :BIG)
58
62
  val = 0
59
63
  case order
60
- when 'BIG'
64
+ when :BIG
61
65
  val += mutate(@data.slice!(0..3).unpack1(signed ? 'i>' : 'I>'), mutation)
62
- when 'MIDDLE'
66
+ when :MIDDLE
63
67
  val += read_byte(signed: signed) << 8
64
68
  val += read_byte(signed: signed, mutation: mutation)
65
69
  val += read_byte(signed: signed) << 24
66
70
  val += read_byte(signed: signed) << 16
67
71
  return val
68
- when 'INVERSE_MIDDLE'
72
+ when :INVERSE_MIDDLE
69
73
  val += read_byte(signed: signed) << 16
70
74
  val += read_byte(signed: signed) << 24
71
75
  val += read_byte(signed: signed, mutation: mutation)
72
76
  val += read_byte(signed: signed) << 8
73
77
  return val
74
- when 'LITTLE'
78
+ when :LITTLE
75
79
  val += mutate(@data.slice!(0..3).unpack1(signed ? 'i<' : 'I<'), mutation)
76
- else read_int(signed: signed, mutation: mutation, order: 'BIG')
80
+ else read_int(signed: signed, mutation: mutation, order: :BIG)
77
81
  end
78
82
  val
79
83
  end
80
84
 
81
85
  # Reads a long value from the {Buffer#data}
82
86
  # @param signed [Boolean] should the value be signed.
83
- # @param mutation [String] mutation that should be applied to the long value
84
- # @param order [String] they byte order to read the long value
85
- def read_long(signed: false, mutation: :STD, order: 'BIG')
87
+ # @param mutation [Symbol] mutation that should be applied to the long value
88
+ # @param order [Symbol] they byte order to read the long value
89
+ # @return [Integer]
90
+ def read_long(signed: false, mutation: :STD, order: :BIG)
86
91
  case order
87
- when 'BIG'
92
+ when :BIG
88
93
  mutate(@data.slice!(0..7).unpack1(signed ? 'q>' : 'Q>'), mutation)
89
- when 'LITTLE'
94
+ when :LITTLE
90
95
  mutate(@data.slice!(0..7).unpack1(signed ? 'q<' : 'Q<'), mutation)
91
- else read_long(signed: signed, mutation: mutation, order: 'BIG')
96
+ else read_long(signed: signed, mutation: mutation, order: :BIG)
92
97
  end
93
98
  end
94
99
  end
@@ -8,11 +8,10 @@ module RuneRb::IO
8
8
  # Read data from the payload according to the option parameter.
9
9
  # @param type [Symbol] the type of database to read
10
10
  # @param mutation [Symbol] an option mutation to apply to the read value.
11
- def read(type: :byte, signed: false, mutation: :STD, order: 'BIG', options: {})
11
+ def read(type: :byte, signed: false, mutation: :STD, order: :BIG, options: {})
12
12
  return unless RuneRb::IO::Validation.validate(self, 'read', { bit_access: @bit_access, mutation: mutation, order: order })
13
13
 
14
14
  case type
15
- when :bits then read_bits(options[:amount])
16
15
  when :byte then read_byte(signed: signed, mutation: mutation)
17
16
  when :bytes then read_bytes(options[:amount] || 1, mutation: mutation)
18
17
  when :short then read_short(signed: signed, mutation: mutation, order: order)
@@ -22,7 +21,7 @@ module RuneRb::IO
22
21
  when :smart then read_smart(signed: signed, mutation: mutation)
23
22
  when :string then read_string
24
23
  when :reverse_bytes then read_bytes_reverse(options[:amount] || 1, mutation: mutation)
25
- else raise "Unrecognized read type! #{type}"
24
+ else raise TypeError, "Unrecognized read type! Expected: [byte bytes short medium int long smart string reverse_bytes] | Received: #{type}"
26
25
  end
27
26
  end
28
27
 
@@ -31,6 +30,7 @@ module RuneRb::IO
31
30
  # Read multiple bytes from the {Buffer#data}
32
31
  # @param amount [Integer] the amount of bytes to read
33
32
  # @param mutation [Symbol] the mutation to apply to read bytes.
33
+ # @return [Array]
34
34
  def read_bytes(amount, mutation)
35
35
  amount.times.each_with_object([]) { |_idx, arr| arr << read_byte(signed: false, mutation: mutation) }
36
36
  end
@@ -38,6 +38,7 @@ module RuneRb::IO
38
38
  # Probably did this wrong
39
39
  # @param amount [Integer] the amount of bytes to read
40
40
  # @param mutation [Symbol] an optional mutation to apply to the result.
41
+ # @return [Array]
41
42
  def read_bytes_reverse(amount, mutation)
42
43
  @data.reverse
43
44
  value = read_bytes(amount, mutation)
@@ -47,7 +48,8 @@ module RuneRb::IO
47
48
 
48
49
  # Read a byte value from the {Buffer#data}
49
50
  # @param signed [Boolean] should the value be signed.
50
- # @param mutation [String] mutation that should be applied to the byte value.
51
+ # @param mutation [Symbol] mutation that should be applied to the byte value.
52
+ # @return [Integer]
51
53
  def read_byte(mutation: :STD, signed: false)
52
54
  val = mutate(@data.slice!(0)&.unpack1(signed ? 'c' : 'C') || 0, mutation)
53
55
  signed ? val.signed(:byte) : val.unsigned(:byte)
@@ -55,104 +57,110 @@ module RuneRb::IO
55
57
 
56
58
  # Reads a short value from the {Buffer#data}
57
59
  # @param signed [Boolean] should the value be signed.
58
- # @param mutation [String] mutation that should be applied to the short value
59
- # @param order [String] they byte order to read the short value
60
- def read_short(signed: false, mutation: :STD, order: 'BIG')
60
+ # @param mutation [Symbol] mutation that should be applied to the short value
61
+ # @param order [Symbol] they byte order to read the short value
62
+ # @return [Integer]
63
+ def read_short(signed: false, mutation: :STD, order: :BIG)
61
64
  val = 0
62
65
  case order
63
- when 'BIG'
66
+ when :BIG
64
67
  val += read_byte(signed: signed) << 8
65
68
  val += read_byte(mutation: mutation, signed: signed)
66
- when 'LITTLE'
69
+ when :LITTLE
67
70
  val += read_byte(mutation: mutation, signed: signed)
68
71
  val += read_byte(signed: signed) << 8
69
- else read_short(signed: signed, mutation: mutation, order: 'BIG')
72
+ else read_short(signed: signed, mutation: mutation, order: :BIG)
70
73
  end
71
74
  val
72
75
  end
73
76
 
74
77
  # Reads a medium value from the {Buffer#data}
75
78
  # @param signed [Boolean] should the value be signed.
76
- # @param mutation [String] mutation that should be applied to the medium value
77
- # @param order [String] they byte order to read the medium value
78
- def read_medium(signed: false, mutation: :STD, order: 'BIG')
79
+ # @param mutation [Symbol] mutation that should be applied to the medium value
80
+ # @param order [Symbol] they byte order to read the medium value
81
+ # @return [Integer]
82
+ def read_medium(signed: false, mutation: :STD, order: :BIG)
79
83
  val = 0
80
84
  case order
81
- when 'BIG'
85
+ when :BIG
82
86
  val += read_byte(signed: signed) << 16
83
87
  val += read_byte(signed: signed) << 8
84
88
  val += read_byte(signed: signed, mutation: mutation)
85
- when 'MIDDLE'
89
+ when :MIDDLE
86
90
  val += read_byte(signed: signed) << 8
87
91
  val += read_byte(signed: signed, mutation: mutation)
88
92
  val += read_byte(signed: signed) << 16
89
- when 'LITTLE'
93
+ when :LITTLE
90
94
  val += read_byte(signed: signed, mutation: mutation)
91
95
  val += read_byte(signed: signed) << 8
92
96
  val += read_byte(signed: signed) << 16
93
- else read_medium(signed: signed, mutation: mutation, order: 'BIG')
97
+ else read_medium(signed: signed, mutation: mutation, order: :BIG)
94
98
  end
95
99
  val
96
100
  end
97
101
 
98
102
  # Reads a integer value from the {Buffer#data}
99
103
  # @param signed [Boolean] should the value be signed.
100
- # @param mutation [String] mutation that should be applied to the integer value
101
- # @param order [String] they byte order to read the integer value
102
- def read_int(signed: false, mutation: :STD, order: 'BIG')
104
+ # @param mutation [Symbol] mutation that should be applied to the integer value
105
+ # @param order [Symbol] they byte order to read the integer value
106
+ # @return [Integer]
107
+ def read_int(signed: false, mutation: :STD, order: :BIG)
103
108
  val = 0
104
109
  case order
105
- when 'BIG'
110
+ when :BIG
106
111
  val += read_byte(signed: signed) << 24
107
112
  val += read_byte(signed: signed) << 16
108
113
  val += read_byte(signed: signed) << 8
109
114
  val += read_byte(signed: signed, mutation: mutation)
110
- when 'MIDDLE'
115
+ when :MIDDLE
111
116
  val += read_byte(signed: signed) << 8
112
117
  val += read_byte(signed: signed, mutation: mutation)
113
118
  val += read_byte(signed: signed) << 24
114
119
  val += read_byte(signed: signed) << 16
115
- when 'INVERSE_MIDDLE'
120
+ when :INVERSE_MIDDLE
116
121
  val += read_byte(signed: signed) << 16
117
122
  val += read_byte(signed: signed) << 24
118
123
  val += read_byte(signed: signed, mutation: mutation)
119
124
  val += read_byte(signed: signed) << 8
120
- when 'LITTLE'
125
+ when :LITTLE
121
126
  val += read_byte(signed: signed, mutation: mutation)
122
127
  val += read_byte(signed: signed) << 8
123
128
  val += read_byte(signed: signed) << 16
124
129
  val += read_byte(signed: signed) << 24
125
- else read_int(signed: signed, mutation:mutation, order: 'BIG')
130
+ else read_int(signed: signed, mutation:mutation, order: :BIG)
126
131
  end
127
132
  val
128
133
  end
129
134
 
130
135
  # Reads a long value from the {Buffer#data}
131
136
  # @param signed [Boolean] should the value be signed.
132
- # @param mutation [String] mutation that should be applied to the long value
133
- # @param order [String] they byte order to read the long value
134
- def read_long(signed: false, mutation: :STD, order: 'BIG')
137
+ # @param mutation [Symbol] mutation that should be applied to the long value
138
+ # @param order [Symbol] they byte order to read the long value
139
+ # @return [Integer]
140
+ def read_long(signed: false, mutation: :STD, order: :BIG)
135
141
  val = 0
136
142
  case order
137
- when 'BIG'
143
+ when :BIG
138
144
  (RuneRb::IO::BYTE_SIZE * 7).downto(0) { |div| ((div % 8).zero? and div.positive?) ? val |= read_byte(signed: signed) << div : next }
139
145
  val += read_byte(signed: signed, mutation: mutation)
140
- when 'LITTLE'
146
+ when :LITTLE
141
147
  val += read_byte(signed: signed, mutation: mutation)
142
148
  (0).upto(RuneRb::IO::BYTE_SIZE * 7) { |div| ((div % 8).zero? and div.positive?) ? val |= read_byte(signed: signed) << div: next }
143
- else read_long(signed: signed, mutation: mutation, order: 'BIG')
149
+ else read_long(signed: signed, mutation: mutation, order: :BIG)
144
150
  end
145
151
  val
146
152
  end
147
153
 
148
154
  # Read a smart value from the {Buffer#data}
149
155
  # @param signed [Boolean] should the value be signed.
150
- # @param mutation [String] mutation that should be applied to the long value
156
+ # @param mutation [Symbol] mutation that should be applied to the long value
157
+ # @return [Integer]
151
158
  def read_smart(signed: false, mutation: :STD)
152
159
  val = peek.slice(0).unpack1(signed ? 'c' : 'C')
153
- case signed
154
- when true then val < 128 ? read_byte(mutation: mutation, signed: signed) - 64 : read_short(mutation: mutation, signed: signed, order: 'BIG') - 49_152
155
- when false then val < 128 ? read_byte(mutation: mutation, signed: signed) : read_short(mutation: mutation, signed: signed, order: 'BIG') - 32_768
160
+ if signed
161
+ val < 128 ? read_byte(mutation: mutation, signed: signed) - 64 : read_short(mutation: mutation, signed: signed, order: :BIG) - 49_152
162
+ else
163
+ val < 128 ? read_byte(mutation: mutation, signed: signed) : read_short(mutation: mutation, signed: signed, order: :BIG) - 32_768
156
164
  end
157
165
  end
158
166
 
@@ -66,7 +66,7 @@ module RuneRb::IO
66
66
  # Validates the byte order to read for the operation
67
67
  # @param order [String] the order in which to read bytes in the operation.
68
68
  def valid_order?(order)
69
- unless RuneRb::IO::BYTE_ORDERS.match?(order)
69
+ unless RuneRb::IO::BYTE_ORDERS.include?(order)
70
70
  err "Unrecognized byte order! #{order}"
71
71
  return false
72
72
  end
@@ -5,7 +5,11 @@ module RuneRb::IO
5
5
  # Write data to the payload.
6
6
  # @param type [Symbol] the type of value to write.
7
7
  # @param value [Integer, String, Message, Array] the value to write.
8
- def write(value, type: :byte, mutation: :STD, order: 'BIG', options: {})
8
+ # @param mutation [Symbol] mutation that should be applied to the data.
9
+ # @param order [Symbol] the order to write the data in
10
+ # @param options [Hash] options for the operation
11
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
12
+ def write(value, type: :byte, mutation: :STD, order: :BIG, options: {})
9
13
  return unless RuneRb::IO::Validation.validate(self, 'write', { mutation: mutation, order: order })
10
14
 
11
15
  case type
@@ -29,144 +33,166 @@ module RuneRb::IO
29
33
  # Enables or Disables bit writing by setting the {Buffer#bit_access} variable.
30
34
  def switch_access
31
35
  @bit_access = !@bit_access
36
+ self
32
37
  end
33
38
 
39
+ # Completes bit access.
34
40
  def finish_access
35
41
  @bit_position = (@bit_position + 7) / 8
36
42
  switch_access
43
+ self
37
44
  end
38
45
 
39
46
  private
40
47
 
41
48
  # Write a byte value to the payload.
42
49
  # @param value [Integer] the byte value to write.
43
- # @param mutation [String] mutations made to the byte.
50
+ # @param mutation [Symbol] mutations made to the byte.
51
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
44
52
  def write_byte(value, mutation: :STD)
45
53
  @data.concat([mutate(value, mutation)].pack('C'))
54
+ self
46
55
  end
47
56
 
48
57
  # Write a short value to the payload.
49
58
  # @param value [Integer] the short value to write.
50
- # @param mutation [String] the type of byte to write.
51
- # @param order [String] the order in which bytes will be written.
52
- def write_short(value, mutation: :STD, order: 'BIG')
59
+ # @param mutation [Symbol] the type of byte to write.
60
+ # @param order [Symbol] the order in which bytes will be written.
61
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
62
+ def write_short(value, mutation: :STD, order: :BIG)
53
63
  case order
54
- when 'BIG'
64
+ when :BIG
55
65
  write_byte(value >> 8)
56
66
  write_byte(value, mutation: mutation)
57
- when 'LITTLE'
67
+ when :LITTLE
58
68
  write_byte(value, mutation: mutation)
59
69
  write_byte(value >> 8)
60
70
  else raise "Unrecognized bit order: #{order}"
61
71
  end
72
+ self
62
73
  end
63
74
 
64
75
  # Write a medium value to the payload.
65
76
  # @param value [Integer] the medium value to write.
66
- # @param mutation [String] the mutation made to the byte.
67
- # @param order [String] the order in which bytes will be written.
68
- def write_medium(value, mutation: :STD, order: 'BIG')
77
+ # @param mutation [Symbol] the mutation made to the byte.
78
+ # @param order [Symbol] the order in which bytes will be written.
79
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
80
+ def write_medium(value, mutation: :STD, order: :BIG)
69
81
  case order
70
- when 'BIG'
82
+ when :BIG
71
83
  write_byte(value >> 16)
72
84
  write_byte(value >> 8)
73
85
  write_byte(value, mutation: mutation)
74
- when 'MIDDLE'
86
+ when :MIDDLE
75
87
  write_byte(value >> 8)
76
88
  write_byte(value, mutation: mutation)
77
89
  write_byte(value >> 16)
78
- when 'LITTLE'
90
+ when :LITTLE
79
91
  write_byte(value, mutation: mutation)
80
92
  write_byte(value >> 8)
81
93
  write_byte(value >> 16)
82
94
  else raise "Unrecognized bit order: #{order}"
83
95
  end
96
+ self
84
97
  end
85
98
 
86
99
  # Write a integer value to the payload.
87
100
  # @param value [Integer] the integer value to write.
88
- # @param mutation [String] the type of byte to write.
89
- # @param order [String] the order in which bytes will be written.
90
- def write_int(value, mutation: :STD, order: 'BIG')
101
+ # @param mutation [Symbol] the type of byte to write.
102
+ # @param order [Symbol] the order in which bytes will be written.
103
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
104
+ def write_int(value, mutation: :STD, order: :BIG)
91
105
  case order
92
- when 'BIG'
106
+ when :BIG
93
107
  write_byte(value >> 24)
94
108
  write_byte(value >> 16)
95
109
  write_byte(value >> 8)
96
110
  write_byte(value, mutation: mutation)
97
- when 'MIDDLE'
111
+ when :MIDDLE
98
112
  write_byte(value >> 8)
99
113
  write_byte(value, mutation: mutation)
100
114
  write_byte(value >> 24)
101
115
  write_byte(value >> 16)
102
- when 'INVERSE_MIDDLE'
116
+ when :INVERSE_MIDDLE
103
117
  write_byte(value >> 16)
104
118
  write_byte(value >> 24)
105
119
  write_byte(value, mutation: mutation)
106
120
  write_byte(value >> 8)
107
- when 'LITTLE'
121
+ when :LITTLE
108
122
  write_byte(value, mutation: mutation)
109
123
  write_byte(value >> 8)
110
124
  write_byte(value >> 16)
111
125
  write_byte(value >> 24)
112
126
  else raise "Unrecognized bit order: #{order}"
113
127
  end
128
+ self
114
129
  end
115
130
 
116
131
  # Write a long value to the payload.
117
132
  # @param value [Integer] the long value to write.
118
- # @param mutation [String] the type of byte to write.
119
- # @param order [String] the order in which bytes will be written.
120
- def write_long(value, mutation: :STD, order: 'BIG')
133
+ # @param mutation [Symbol] the type of byte to write.
134
+ # @param order [Symbol] the order in which bytes will be written.
135
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
136
+ def write_long(value, mutation: :STD, order: :BIG)
121
137
  case order
122
- when 'BIG'
138
+ when :BIG
123
139
  (RuneRb::IO::BYTE_SIZE * 7).downto(0) { |div| ((div % 8).zero? and div.positive?) ? write_byte(value >> div) : next }
124
140
  write_byte(value, mutation: mutation)
125
- when 'LITTLE'
141
+ when :LITTLE
126
142
  write_byte(value, mutation: mutation)
127
143
  (0).upto(RuneRb::IO::BYTE_SIZE * 7) { |div| ((div % 8).zero? and div.positive?) ? write_byte(value >> div) : next }
128
144
  else raise "Unrecognized bit order: #{order}"
129
145
  end
146
+ self
130
147
  end
131
148
 
132
149
  # Write a string value to the payload. This will be escaped/terminated with a \n[10] value.
133
150
  # @param string [String] the byte to write to the payload.
151
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
134
152
  def write_string(string)
135
153
  @data.concat(string.force_encoding(Encoding::BINARY))
136
154
  write_byte(10)
155
+ self
137
156
  end
138
157
 
139
158
  # Write a 'smart' value to the payload.
140
159
  #
141
160
  # @param value [Integer] the smart value to write.
142
- # @param mutation [String] an optional mutation to apply to the written smart value.
161
+ # @param mutation [Symbol] an optional mutation to apply to the written smart value.
162
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
143
163
  def write_smart(value, mutation: :STD, signed: false)
144
- case signed
145
- when true
164
+ if signed
146
165
  value > 128 ? write_byte(value, mutation: mutation) + 64 : write_short(value, mutation: mutation) + 49_152
147
- when false
166
+ else
148
167
  value > 128 ? write_byte(value, mutation: mutation) : write_short(value, mutation: mutation) + 32_768
149
168
  end
169
+ self
150
170
  end
151
171
 
152
172
  # Writes multiple bytes to the payload.
153
173
  # @param values [String, Array, RuneRb::IO::Buffer] the values whose bytes will be written.
174
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
154
175
  def write_bytes(values)
155
176
  case values
156
177
  when Array then values.each { |byte| write_byte(byte.to_i) }
157
178
  when RuneRb::IO::Message then @data.concat(values.body.data)
158
179
  when RuneRb::IO::Buffer then @data.concat(values.data)
159
180
  when String then @data.concat(values)
181
+ else raise TypeError, "Invalid type for operation! Expecting: [Array, Message, Buffer, String] | Received: #{values.class}"
160
182
  end
183
+ self
161
184
  end
162
185
 
163
186
  # Write a collection of bytes in reverse. *Not sure if I did this right.
164
187
  # @param values [Array, String] the values to write.
188
+ # @return [RuneRb::IO::Message, RuneRb::IO::Writeable]
165
189
  def write_reverse_bytes(values)
166
190
  case values
167
191
  when Array then write(values.reverse, type: :bytes)
168
192
  when String then write(values.bytes.reverse, type: :bytes)
193
+ else raise TypeError, "Invalid type for operation! Expecting: [Array, String] | Received: #{values.class}"
169
194
  end
195
+ self
170
196
  end
171
197
 
172
198
  # Write a single bit with a value of 1 or 0 depending on the flag parameter.
@@ -2,8 +2,10 @@
2
2
  module RuneRb::Patches::IntegerRefinements
3
3
 
4
4
  refine Integer do
5
+
5
6
  # Adjusts the value of the Integer to be unsigned.
6
7
  # @param type [Symbol] the type of primitive the value will be returned as
8
+ # @return [Integer, RuneRb::Patches::IntegerRefinements]
7
9
  def unsigned(type)
8
10
  return 0 if negative?
9
11
 
@@ -20,6 +22,7 @@ module RuneRb::Patches::IntegerRefinements
20
22
 
21
23
  # Adjusts the value of the Integer to be signed.
22
24
  # @param type [Symbol] the type of primitive the value will be returned as
25
+ # @return [Integer, RuneRb::Patches::IntegerRefinements]
23
26
  def signed(type)
24
27
  case type
25
28
  when :Byte, :byte, :b, :B then adjust(:byte)
@@ -32,11 +35,14 @@ module RuneRb::Patches::IntegerRefinements
32
35
 
33
36
  alias_method :s, :signed
34
37
 
38
+ # The first 4 bits of the integer.
39
+ # @return [Integer, RuneRb::Patches::IntegerRefinements]
35
40
  def nibble
36
41
  adjust(:nibble)
37
42
  end
38
43
 
39
44
  # Returns a string with a formatted representation of the Integer as a timestamp.
45
+ # @return [String] the formatted time.
40
46
  def to_ftime
41
47
  mm, ss = divmod(60)
42
48
  hh, mm = mm.divmod(60)
@@ -48,6 +54,7 @@ module RuneRb::Patches::IntegerRefinements
48
54
 
49
55
  # Adjusts the integer based on the passed type
50
56
  # @param type [Symbol] the type of adjustment to make to the integer.
57
+ # @return [Integer, RunRb::Patches::IntegerRefinements]
51
58
  def adjust(type)
52
59
  case type
53
60
  when :Byte, :byte, :b, :B
@@ -12,33 +12,25 @@ module RuneRb::Utils
12
12
  using RuneRb::Patches::IntegerRefinements
13
13
 
14
14
  include RuneRb::Utils::Logging
15
-
16
15
  include Singleton
17
16
 
18
17
  # @!attribute [r] sessions
19
18
  # @return [Hash] a collection of sessions.
20
19
  attr :sessions
21
20
 
22
- # @!attribute [r] servers
23
- # @return [Hash] a collection of server objects and signatures.
24
- attr :servers
25
-
26
- # @!attribute [r] worlds
27
- # @return [Array] a map of World instances.
28
- attr :worlds
29
-
30
21
  # Constructs a new Controller object
31
22
  def initialize
32
- @worlds = {}
33
23
  @start = { time: Process.clock_gettime(Process::CLOCK_MONOTONIC), stamp: Time.now }
24
+ @pids = { gateway: nil, world: nil, controller: Process.pid }
25
+ super()
34
26
  end
35
27
 
36
28
  def autorun
37
29
  # Setup signal trapping
38
30
  setup_trapping
39
31
 
40
- # Deploy server process
41
- deploy_server
32
+ # Deploy gateway process
33
+ deploy_gateway
42
34
 
43
35
  # Deploy Game world
44
36
  deploy_world
@@ -51,8 +43,9 @@ module RuneRb::Utils
51
43
 
52
44
  # Logs information about the worlds, servers, and session states.
53
45
  def about
54
- log COLORS.cyan("[Server]:\n#{@server_pid.nil? ? COLORS.green.bold(@server_pid) : COLORS.red.bold('nil')}")
55
- log COLORS.cyan("[World]:\n#{@world_pid.nil? ? COLORS.green.bold(@world_pid) : COLORS.red.bold('nil')}")
46
+ log COLORS.cyan.bold("[Controller]: #{@pids[:controller]}"),
47
+ COLORS.magenta.bold("[Server]: #{@pids[:gateway].nil? ? COLORS.red.bold('nil') : COLORS.green.bold(@pids[:gateway])}"),
48
+ COLORS.yellow.bold("[World]: #{@pids[:world].nil? ? COLORS.red.bold('nil') : COLORS.green.bold(@pids[:world])}")
56
49
  end
57
50
 
58
51
  # Deploy a console session.
@@ -60,25 +53,25 @@ module RuneRb::Utils
60
53
  Pry.start(self)
61
54
  end
62
55
 
63
- # Deploys a single TCP Socket Server.
64
- # @return [Integer, NilClass] the process ID for the server process.
65
- def deploy_server
66
- @server_pid ||= Process.fork { RuneRb::Network::Server.instance.autorun }
56
+ # Construct a singleton instance of the {RuneRb::Network::Gateway} in a forked process.
57
+ # @return [Integer, NilClass] the process ID for the gateway process.
58
+ def deploy_gateway
59
+ # @pids[:gateway] ||= Process.fork { RuneRb::Network::Gateway.instance.autorun }
67
60
  end
68
61
 
69
- # Deploys a single instance of {RuneRb::Game::World::Instance}
70
- # @return [Integer, NilClass] the constructed World
62
+ # Construct a singleton instance of {RuneRb::Game::World::Instance} in a forked process.
63
+ # @return [Integer, NilClass] the process ID for the world process.
71
64
  def deploy_world
72
- @world_pid ||= Process.fork { RuneRb::Game::World::Instance.instance }
65
+ # @pids[:world] ||= Process.fork { RuneRb::Game::World::Instance.instance }
73
66
  end
74
67
 
75
68
  # Stop all server instances and close all connected sessions.
76
69
  def shutdown(graceful: true)
77
70
  # Kill server process
78
- Process.kill(graceful ? 'INT' : 'TERM', @server_pid) unless @server_pid.nil?
71
+ Process.kill(graceful ? 'INT' : 'TERM', @pids[:gateway]) unless @pids[:gateway].nil?
79
72
 
80
73
  # Kill World processes
81
- Process.kill(graceful ? 'INT' : 'TERM', @world_pid) unless @world_pid.nil?
74
+ Process.kill(graceful ? 'INT' : 'TERM', @pids[:world]) unless @pids[:world].nil?
82
75
 
83
76
  # Log up-time
84
77
  log! COLORS.blue.bold("Total Controller Up-time: #{up_time.to_i.to_ftime}")
data/lib/rrb.rb CHANGED
@@ -24,17 +24,18 @@ module RuneRb
24
24
  module IO
25
25
  # Acceptable byte orders in which multi-byte values can be read.
26
26
  # @return [Array] the orders.
27
- BYTE_ORDERS = /(?i)\bBIG|MIDDLE|INVERSE_MIDDLE|LITTLE/.freeze
27
+ BYTE_ORDERS = %i[BIG MIDDLE INVERSE_MIDDLE LITTLE].freeze
28
28
 
29
29
  # The size of a byte
30
30
  # @type [Integer] the size
31
31
  BYTE_SIZE = 8
32
32
 
33
33
  # Valid byte mutations
34
- # @return [Hash]
34
+ # @return [Array]
35
35
  BYTE_MUTATIONS = %i[ADD NEG SUB STD].freeze
36
36
 
37
37
  # Types that can be read.
38
+ # @return [Array]
38
39
  RW_TYPES = %i[bits bit byte bytes medium int long reverse_bytes short smart string].freeze
39
40
 
40
41
  # Bit masks for bit packing
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick W.