mcp 0.0.1 → 0.0.2

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.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in mcp.gemspec
4
- gemspec
4
+ gemspec
data/fail.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'mcp'
2
+ include MCP
3
+ file = 'listaserwerow'
4
+ map = MCP::Util.mapping
5
+ File.readlines(file).each do |line|
6
+ server,port = line.split(":")
7
+ sock = TCPSocket.new server,port
8
+ msg = Msg.new
9
+ login1 = Msg.new
10
+ login1.msg = "/register werwerdf werwerdf"
11
+ login2 = Msg.new
12
+ login2.msg = "/register werwerdf"
13
+ login3 = Msg.new
14
+ login3.msg = "/login werwerdf"
15
+ msg.msg = "Testujemy"
16
+ login = LoginRequest.new
17
+ login.nick = "testujemyxd1"
18
+ sock << login.with_id
19
+ packet_id = MCUbyte.read(sock).to_i
20
+ klass = map[packet_id]
21
+ if !klass
22
+ puts "nieznany pakiet jedziemy dalej nastepny serwer"
23
+ next
24
+ end
25
+ packet = klass.read(sock)
26
+ if packet.instance_of? Kick
27
+ puts "Fail got kick: #{packet.reason} //jedziemy dalej"
28
+ next
29
+ elsif packet.instance_of? LoginResponse
30
+ puts "OK"
31
+ sock << login1.with_id
32
+ sock << login2.with_id
33
+ sock << login3.with_id
34
+ sock << msg.with_id
35
+ end
36
+
37
+ break
38
+ end
39
+ #p MCP::MCMetadata.read "asd"
data/lib/mcp.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "mcp/version"
2
-
3
- module Mcp
2
+ require 'mcp/connection.rb'
3
+ module MCP
4
4
  # Your code goes here...
5
5
  end
@@ -0,0 +1,44 @@
1
+ require 'socket'
2
+ require 'mcp/packets.rb'
3
+
4
+ module MCP
5
+ class Connection
6
+
7
+ def initialize(opts = {}, &block)
8
+ host = opts[:host] || "localhost"
9
+ port = opts[:port] || 25565
10
+ @socket = TCPSocket.new host,port
11
+ login = LoginRequest.new
12
+ login.nick = "testing221"
13
+ @socket << login.with_id
14
+ map = Util.mapping
15
+
16
+ while true
17
+ id = MCUbyte.read(@socket).to_i
18
+ klass = map[id]
19
+ if !klass
20
+ @socket.close
21
+ raise Exception, "Unknown packet: 0x#{id.to_s(16).upcase}"
22
+ return
23
+ end
24
+ p klass
25
+ a = klass.read(@socket)
26
+ p a
27
+ if a.instance_of? LoginResponse
28
+ puts "Polaczono z #{host}:#{port}, entity_id=#{a.entity_id}"
29
+ elsif a.instance_of? Kick
30
+ @socket.close
31
+ raise Exception, "Got kick: #{a.reason}"
32
+ return
33
+ elsif a.instance_of? Keepalive
34
+ k = Keepalive.new
35
+ k.id = a.id
36
+ @socket << k.with_id
37
+ end
38
+ puts "Naste[ny packiet"
39
+
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,177 @@
1
+ require 'mcp/types.rb'
2
+ require 'mcp/packets_util.rb'
3
+
4
+ module MCP
5
+ class PluginMessage < Packet
6
+ Id = 0xFA
7
+ Type = :to_client
8
+
9
+ mc_string :channel
10
+ mc_byte_array :data
11
+ end
12
+
13
+ class LoginRequest < Packet
14
+ Id = 0x01
15
+ Type = :to_server
16
+
17
+ mc_int :protocol_version, value: 29 # int
18
+
19
+ mc_string :nick
20
+ mc_string :notused
21
+
22
+ mc_int :notused1, value: 0 #int
23
+ mc_int :notused2, value: 0 # int
24
+ mc_byte :notused3, value: 0 # byte
25
+ mc_ubyte :notused4, value: 0 #unsigned byte
26
+ mc_ubyte :notused5, value: 0 # ungsigned byte
27
+ end
28
+
29
+ class LoginResponse < Packet
30
+ Id = 0x01
31
+ Type = :to_client # u can only receive it @_@
32
+
33
+ mc_int :entity_id
34
+
35
+ mc_string :comment
36
+ mc_string :level_type
37
+
38
+
39
+ mc_int :server_mode
40
+ mc_int :dimension
41
+ mc_byte :difficulty
42
+ mc_ubyte :notused
43
+ mc_ubyte :max_players
44
+ end
45
+
46
+ class SpawnPosition < Packet
47
+ Id = 0x06
48
+ Type = :to_client
49
+ mc_int :x
50
+ mc_int :y
51
+ mc_int :z
52
+ end
53
+ class Msg < Packet
54
+ Id = 0x03
55
+ Type = :both
56
+
57
+ mc_string :msg
58
+ end
59
+
60
+ class Kick < Packet
61
+ Id = 0xFF
62
+ Type = :to_client
63
+
64
+ mc_string :reason
65
+ end
66
+
67
+ class PlayerAbilities < Packet
68
+ Id = 0xCA
69
+ Type = :both
70
+
71
+ mc_bool :god
72
+ mc_bool :flying
73
+ mc_bool :can_fly
74
+ mc_bool :instant_destroy
75
+ end
76
+
77
+ class TimeUpdate < Packet
78
+ Id = 0x04
79
+ Type = :to_client
80
+
81
+ mc_long :time
82
+ end
83
+
84
+ class SpawnItem < Packet
85
+ Id = 0x15
86
+ Type = :to_client
87
+
88
+ mc_int :entity_id
89
+ mc_short :item
90
+ mc_byte :item_count
91
+ mc_short :damage
92
+ mc_ab_int :x
93
+ mc_ab_int :y
94
+ mc_ab_int :z
95
+ mc_byte :rotation
96
+ mc_byte :pitch
97
+ mc_byte :roll
98
+ end
99
+
100
+ class EntityVelocity < Packet
101
+ Id = 0x1C
102
+ Type = :to_client
103
+
104
+ mc_int :eid
105
+ mc_short :x
106
+ mc_short :y
107
+ mc_short :z
108
+ end
109
+
110
+ class EntityHeadLook < Packet
111
+ Id = 0x23
112
+ Type = :to_client
113
+ mc_int :eid
114
+ mc_byte :head_yaw
115
+ end
116
+
117
+ class ChunkAllocation < Packet
118
+ Id = 0x32
119
+ Type = :to_client
120
+ mc_int :x
121
+ mc_int :z
122
+ mc_bool :mode
123
+ end
124
+
125
+ class SpawnMob < Packet
126
+ Id = 0x18
127
+ Type = :to_client
128
+
129
+ mc_int :eid
130
+ mc_byte :type
131
+ mc_int :x
132
+ mc_int :y
133
+ mc_int :z
134
+ mc_byte :yaw
135
+ mc_byte :pitch
136
+ mc_byte :head_yaw
137
+ mc_short :velocity_z
138
+ mc_short :velocity_x
139
+ mc_short :velocity_y
140
+ mc_metadata :meta
141
+ end
142
+
143
+ class SetWindowItems < Packet
144
+ Id = 0x68
145
+ Type = :to_client
146
+
147
+ mc_byte :window_id
148
+ mc_short :item_count
149
+ end
150
+
151
+ class SpawnNamedEntity < Packet
152
+ Id = 0x14
153
+ Type = :to_client
154
+
155
+ mc_int :eid
156
+ mc_string :name
157
+ mc_ab_int :x
158
+ mc_ab_int :y
159
+ mc_ab_int :z
160
+ mc_byte :yaw
161
+ mc_byte :pitch
162
+ mc_short :item
163
+ mc_metadata :meta
164
+ end
165
+
166
+ class Keepalive < Packet
167
+ Id = 0x00
168
+ Type = :both
169
+ mc_int :id
170
+ end
171
+ class ChangeGamestate < Packet
172
+ Id = 0x46
173
+ Type = :to_client
174
+ mc_byte :reason
175
+ mc_byte :gm
176
+ end
177
+ end
@@ -0,0 +1,24 @@
1
+ require 'mcp/types.rb'
2
+ module MCP
3
+ class Util
4
+ def self.mapping
5
+ MCP.constants.each_with_object({}) do |constant_name, result|
6
+ klass = MCP.const_get(constant_name)
7
+ next unless klass.is_a?(Class)
8
+ next unless klass < Packet
9
+ next unless (klass::Type || :both) != :to_server
10
+ result[klass::Id] = klass
11
+ end
12
+ end
13
+ end
14
+
15
+ class Packet < BinData::Record
16
+ Id = -1
17
+ Type = :both
18
+ def with_id
19
+ packet = self.class
20
+ id = packet::Id rescue 0
21
+ MCUbyte.new(id).to_binary_s + to_binary_s
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,101 @@
1
+ #packet id to U(!)byte
2
+ #byte => int8
3
+ #short => int16
4
+ #int => int32
5
+ require 'bindata'
6
+ module MCP
7
+ class MCByte < BinData::Primitive
8
+ endian :big
9
+ int8 :data
10
+ def get; return self.data; end
11
+ def set(a); self.data = a; end
12
+ end
13
+
14
+ class MCUbyte < BinData::Primitive
15
+ endian :big
16
+ uint8 :data
17
+ def get; return self.data; end
18
+ def set(a); self.data = a; end
19
+ end
20
+
21
+ class MCShort < BinData::Primitive
22
+ endian :big
23
+ int16 :data
24
+ def get; return self.data; end
25
+ def set(a); self.data = a; end
26
+ end
27
+
28
+ class MCInt < BinData::Primitive
29
+ endian :big
30
+ int32 :data
31
+ def get; return self.data; end
32
+ def set(a); self.data = a; end
33
+ end
34
+
35
+ class MCAbInt < BinData::Primitive
36
+ endian :big
37
+ int32 :data
38
+ def get; return self.data/32; end
39
+ def set(a); self.data = a; end
40
+ end
41
+
42
+ class MCString < BinData::Primitive
43
+ endian :big
44
+ int16 :len, value: lambda { length }
45
+ string :data, read_length: lambda { 2*len}
46
+
47
+ def get; return self.data.force_encoding("utf-16be").encode("utf-8"); end
48
+ def set(a)
49
+ length = a.length
50
+ self.data = a.encode("utf-16be")
51
+ end
52
+ end
53
+
54
+ class MCByteArray < BinData::Primitive
55
+ endian :big
56
+ int16 :len
57
+ array :data, :type => :mc_byte, :initial_length => lambda {len}
58
+
59
+ def get; return self.data; end
60
+ def set(a); self.data = a; end
61
+ end
62
+
63
+ class MCBool < BinData::Primitive
64
+ endian :big
65
+ int8 :data
66
+ def get; return self.data != 0; end
67
+ def set(a); self.data = a; end
68
+ end
69
+
70
+ class MCLong < BinData::BasePrimitive
71
+ BinData::Int.define_methods(self, 64, :big, :signed)
72
+ end
73
+
74
+ class MCMetadata < BinData::Primitive
75
+ endian :big
76
+ # array :data, :type => :mc_byte#, :initial_length => 1
77
+ def get; return self.datax; end
78
+ def set(a); puts "nie ma setania"; end
79
+
80
+ def read(stream)
81
+ self.datax = []
82
+ while (x = MCByte.read(stream)) != 127
83
+ #data.initial_length = data.initial_length+1
84
+ index = x & 0x1F
85
+ ty = x >> 5
86
+
87
+ val = case ty
88
+ when 0 then MCByte.read(stream)
89
+ when 1 then MCShort.read(stream)
90
+ when 2 then MCInt.read(stream)
91
+ when 3 then nil # TODO: MC FLOAT!!!
92
+ when 4 then MCString.read(stream)
93
+ when 5 then {id: MCShort.read(stream), count: MCByte.read(stream), damage: MCShort.read(stream)}
94
+ when 6 then [MCInt.read(stream),MCInt.read(stream),MCInt.read(stream)]
95
+ else nil
96
+ end
97
+ self.datax << val
98
+ end
99
+ end
100
+ end
101
+ end
@@ -1,3 +1,3 @@
1
- module Mcp
2
- VERSION = "0.0.1"
1
+ module MCP
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1 @@
1
+ metropolic.pl:25565
@@ -13,5 +13,5 @@ Gem::Specification.new do |gem|
13
13
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
14
  gem.name = "mcp"
15
15
  gem.require_paths = ["lib"]
16
- gem.version = Mcp::VERSION
16
+ gem.version = MCP::VERSION
17
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-31 00:00:00.000000000 Z
12
+ date: 2012-08-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Minecraft Protocol implementation
15
15
  email:
@@ -23,11 +23,14 @@ files:
23
23
  - LICENSE
24
24
  - README.md
25
25
  - Rakefile
26
+ - fail.rb
26
27
  - lib/mcp.rb
27
- - lib/mcp/protocol/packets.rb
28
- - lib/mcp/protocol/types.rb
29
- - lib/mcp/protocol29/packets.rb
28
+ - lib/mcp/connection.rb
29
+ - lib/mcp/packets.rb
30
+ - lib/mcp/packets_util.rb
31
+ - lib/mcp/types.rb
30
32
  - lib/mcp/version.rb
33
+ - listaserwerow
31
34
  - mcp.gemspec
32
35
  homepage: ''
33
36
  licenses: []
@@ -1,27 +0,0 @@
1
- require 'mcp/protocol/types'
2
- module MCP
3
- module Protocol
4
- module Packets
5
-
6
- def mapping
7
- constants.each_with_object({}) do |constant_name, result|
8
- klass = const_get(constant_name)
9
- next unless klass.is_a?(Class)
10
- next unless klass < Packet
11
- next unless klass::Type != :to_server
12
- result[klass::Id] = klass
13
- end
14
- end
15
-
16
- private
17
- class Packet < BinData::Record
18
- def with_id
19
- packet = self.class
20
- id = packet::Id rescue 0
21
- MCUbyte.new(id).to_binary_s + to_binary_s
22
- end
23
- end
24
-
25
- end
26
- end
27
- end
@@ -1,70 +0,0 @@
1
- #packet id to U(!)byte
2
- #byte => int8
3
- #short => int16
4
- #int => int32
5
- require 'bindata'
6
- module MCP
7
- module Protocol
8
- module Types
9
- class MCByte < BinData::Primitive
10
- endian :big
11
- int8 :data
12
- def get; return self.data; end
13
- def set(a); self.data = a; end
14
- end
15
-
16
- class MCUbyte < BinData::Primitive
17
- endian :big
18
- uint8 :data
19
- def get; return self.data; end
20
- def set(a); self.data = a; end
21
- end
22
-
23
- class MCShort < BinData::Primitive
24
- endian :big
25
- int16 :data
26
- def get; return self.data; end
27
- def set(a); self.data = a; end
28
- end
29
-
30
- class MCInt < BinData::Primitive
31
- endian :big
32
- int32 :data
33
- def get; return self.data; end
34
- def set(a); self.data = a; end
35
- end
36
-
37
- class MCString < BinData::Primitive
38
- endian :big
39
- int16 :len, value: lambda { length }
40
- string :data, read_length: lambda { 2*len}
41
-
42
- def get; return self.data.force_encoding("utf-16be").encode("utf-8"); end
43
- def set(a)
44
- length = a.length
45
- self.data = a.encode("utf-16be")
46
- end
47
- end
48
-
49
- class MCByteArray < BinData::Primitive
50
- endian :big
51
- int16 :len
52
- array :data, :type => :mc_byte, :initial_length => lambda {len}
53
-
54
- def get; return self.data; end
55
- def set(a); self.data = a; end
56
- end
57
-
58
- class MCBool < BinData::Primitive
59
- endian :big
60
- int8 :data
61
- def get; return self.data != 0; end
62
- def set(a); self.data = a; end
63
- end
64
-
65
- class MCLong < BinData::BasePrimitive
66
- BinData::Int.define_methods(self, 64, :big, :signed)
67
- end
68
- end
69
- end
70
-
@@ -1,87 +0,0 @@
1
- require 'mcp/protocol/packets'
2
- require 'mcp/protocol/types'
3
- module MCP
4
- module Protocol29
5
- module Packets
6
- include Protocol
7
- class Plugin < Packet
8
- Id = 0xFA
9
- Type = :to_client
10
-
11
- mc_string :channel
12
- mc_byte_array :data#, read_length: lambda {data_length}
13
- end
14
-
15
- class LoginRequest < Packet
16
- Id = 0x01
17
- Type = :to_server
18
-
19
- mc_int :protocol_version, value: 29 # int
20
-
21
- mc_string :nick
22
- mc_string :notused
23
-
24
- mc_int :notused1, value: 0 #int
25
- mc_int :notused2, value: 0 # int
26
- mc_byte :notused3, value: 0 # byte
27
- mc_ubyte :notused4, value: 0 #unsigned byte
28
- mc_ubyte :notused5, value: 0 # ungsigned byte
29
- end
30
-
31
- class LoginResponse < Packet
32
- Id = 0x01
33
- Type = :to_client # u can only receive it @_@
34
-
35
- mc_int :entity_id
36
-
37
- mc_string :comment
38
- mc_string :level_type
39
-
40
-
41
- mc_int :server_mode
42
- mc_int :dimension
43
- mc_byte :difficulty
44
- mc_ubyte :notused
45
- mc_ubyte :max_players
46
- end
47
-
48
- class SpawnPosition < Packet
49
- Id = 6
50
- Type = :to_client
51
- mc_int :x
52
- mc_int :y
53
- mc_int :z
54
- end
55
- class Msg < Packet
56
- Id = 3
57
- Type = :both
58
-
59
- mc_string :msg
60
- end
61
-
62
- class Kick < Packet
63
- Id = 0xFF
64
- Type = :to_client
65
-
66
- mc_string :reason
67
- end
68
-
69
- class PlayerAbilities < Packet
70
- Id = 0xCA
71
- Type = :both
72
-
73
- mc_bool :god
74
- mc_bool :flying
75
- mc_bool :can_fly
76
- mc_bool :instant_destroy
77
- end
78
-
79
- class TimeUpdate < Packet
80
- Id = 0x04
81
- Type = :to_client
82
-
83
- mc_long :time
84
- end
85
- end
86
- end
87
- end