SHOW:
|
|
- or go back to the newest paste.
1 | #include <proxy.hpp> | |
2 | #include <mcnetadept.hpp> | |
3 | ||
4 | //Modified versions from libmcnet/src/read.c | |
5 | //These use the socketbuffer to allow for easier writing. | |
6 | ||
7 | //Important defines from my include: | |
8 | /* | |
9 | typedef uint8_t byte; | |
10 | typedef queue<byte> byteBuffer; | |
11 | //typedef all the things! (using info from libmcnet/include/mcnet/structs.h, which i assume is correct) | |
12 | //Note: some of these are useless, but make code more consistent. | |
13 | namespace mcVal { | |
14 | typedef int8_t _bool; | |
15 | typedef int8_t _byte; | |
16 | typedef uint8_t _ubyte; | |
17 | typedef int16_t _short; | |
18 | typedef uint16_t _ushort; | |
19 | typedef int32_t _int; | |
20 | typedef uint32_t _uint; | |
21 | typedef int64_t _long; | |
22 | typedef uint64_t _ulong; | |
23 | typedef float _float; | |
24 | typedef double _double; | |
25 | typedef byteBuffer _blob; | |
26 | typedef _blob _string; | |
27 | typedef _blob _string8; | |
28 | typedef _blob _string16; | |
29 | typedef _blob _metadata; | |
30 | typedef _blob _slot; | |
31 | typedef _blob _slots; | |
32 | } | |
33 | */ | |
34 | ||
35 | using namespace mcVal; | |
36 | ||
37 | //I have to undef it here because crypto++ uses a macro with the same name | |
38 | #undef GETBYTE | |
39 | //I use a separate variable to keep track of the byteBuffer's length because for some reason queue::size sometimes does weird stuff on my system, i really don't know why. (It's like it goes INT_MAX-1 sometimes when it's empty, i should really check out why this happens, but until then, this works.) | |
40 | #define SOCKBUF byteBuffer *buf, int* remainingBytes | |
41 | #define GETBYTE getByte(buf, remainingBytes) | |
42 | ||
43 | //LOGGER, WARN and TRACE are from my logger, you can safely ignore them, they are defines which encapsule some log4cplus functions to make logging just that one bit easier. | |
44 | byte getByte(SOCKBUF) | |
45 | { | |
46 | LOGGER("mcnetadept.cpp::getByte") | |
47 | if (!((*remainingBytes)>0)) | |
48 | { | |
49 | //Just because minecraft isn't consistent. | |
50 | WARN("Not enough data. Returning 0") | |
51 | return 0x00; | |
52 | } | |
53 | byte rc = buf->front(); | |
54 | buf->pop(); | |
55 | TRACE("Returning: " << hex << (int)rc << dec << " C: (" << (char)rc << ")") | |
56 | (*remainingBytes)--; | |
57 | return rc; | |
58 | } | |
59 | ||
60 | _bool mcnet_read_bool(SOCKBUF) { | |
61 | return (((int8_t)(GETBYTE)) == 0) ? 0 : 1; | |
62 | } | |
63 | ||
64 | _byte mcnet_read_byte(SOCKBUF) { | |
65 | _byte a = ((int8_t)(GETBYTE)); | |
66 | - | GETBYTE; //I've had it, i'll just junk it, i don't care anymore! |
66 | + | |
67 | } | |
68 | ||
69 | _ubyte mcnet_read_ubyte(SOCKBUF) { | |
70 | return ((uint8_t)(GETBYTE)); | |
71 | } | |
72 | ||
73 | _short mcnet_read_short(SOCKBUF) { | |
74 | LOGGER("mcnetadept.cpp::mcnet_read_short") | |
75 | _short rc = GETBYTE + (GETBYTE << 8); | |
76 | TRACE("Returning: " << hex << (int)rc << dec << " C:(" << (char)rc << ")") | |
77 | return rc; | |
78 | } | |
79 | ||
80 | _ushort mcnet_read_ushort(SOCKBUF) { | |
81 | return GETBYTE + (GETBYTE << 8); | |
82 | } | |
83 | ||
84 | _int mcnet_read_int(SOCKBUF) { | |
85 | return GETBYTE + (GETBYTE << 8) + (GETBYTE << 16) + (GETBYTE << 24); | |
86 | } | |
87 | ||
88 | _uint mcnet_read_uint(SOCKBUF) { | |
89 | return (_uint)mcnet_read_int(buf,remainingBytes); | |
90 | } | |
91 | ||
92 | _long mcnet_read_long(SOCKBUF) { | |
93 | return ((int64_t)(GETBYTE)) + (((int64_t)(GETBYTE)) << 8) + (((int64_t)(GETBYTE)) << 16) + (((int64_t)(GETBYTE)) << 24) + (((int64_t)(GETBYTE)) << 32) + (((int64_t)(GETBYTE)) << 40) + (((int64_t)(GETBYTE)) << 48) + (((int64_t)(GETBYTE)) << 56); | |
94 | } | |
95 | ||
96 | _ulong mcnet_read_ulong(SOCKBUF) { | |
97 | return ((uint64_t)(GETBYTE)) + (((uint64_t)(GETBYTE)) << 8) + (((uint64_t)(GETBYTE)) << 16) + (((uint64_t)(GETBYTE)) << 24) + (((uint64_t)(GETBYTE)) << 32) + (((uint64_t)(GETBYTE)) << 40) + (((uint64_t)(GETBYTE)) << 48) + (((uint64_t)(GETBYTE)) << 56); | |
98 | } | |
99 | ||
100 | _float mcnet_read_float(SOCKBUF) { | |
101 | _float out; | |
102 | ||
103 | byte* t = (byte*)&out; | |
104 | ||
105 | t[0] = GETBYTE; | |
106 | t[1] = GETBYTE; | |
107 | t[2] = GETBYTE; | |
108 | t[3] = GETBYTE; | |
109 | ||
110 | return out; | |
111 | } | |
112 | ||
113 | _double mcnet_read_double(SOCKBUF) { | |
114 | _double out; | |
115 | ||
116 | byte* t = (byte*)&out; | |
117 | ||
118 | t[0] = GETBYTE; | |
119 | t[1] = GETBYTE; | |
120 | t[2] = GETBYTE; | |
121 | t[3] = GETBYTE; | |
122 | t[4] = GETBYTE; | |
123 | t[5] = GETBYTE; | |
124 | t[6] = GETBYTE; | |
125 | t[7] = GETBYTE; | |
126 | ||
127 | return out; | |
128 | } | |
129 | ||
130 | //The following function(s) are actually original by me, they're just here for code cleanness (i am not sure that was a word). | |
131 | ||
132 | _blob mcnet_read_blob(SOCKBUF, int len) { | |
133 | LOGGER("mcnetadept.cpp::mcnet_read_blob") | |
134 | _blob rc; | |
135 | TRACE("Need to read " << len << " bytes...") | |
136 | for (int i = 0; i < len ; i++) | |
137 | rc.push((char)(int)mcnet_read_short(buf,remainingBytes)); | |
138 | TRACE("Finished reading...") | |
139 | return rc; | |
140 | } | |
141 | ||
142 | mcVal::_string mcnet_read_string(SOCKBUF) | |
143 | { | |
144 | //As far as i gather a string is prefixed with a short | |
145 | return mcnet_read_blob(buf,remainingBytes,(int)mcnet_read_short(buf,remainingBytes)); | |
146 | } | |
147 | ||
148 | _short mcnet_read_packetid(SOCKBUF,bool *isWeird) { | |
149 | //Side note: Why oh why aren't client and server using the same format for packet ids? | |
150 | LOGGER("mcnetadept.cpp::mcnet_read_packetid") | |
151 | (*isWeird)=true; | |
152 | _short rc = GETBYTE; | |
153 | if (buf->front()==0x0) | |
154 | { | |
155 | rc += (GETBYTE << 8); | |
156 | (*isWeird)=false; | |
157 | } | |
158 | TRACE("Returning: " << hex << (int)rc << dec << " C:(" << (char)rc << ")") | |
159 | return rc; | |
160 | } | |
161 | ||
162 | //The following functions are the reverse of the mcnet functions (they write instead of read) and were written by me | |
163 | ||
164 | //Plenty 'o defines make coding easy | |
165 | #undef SOCKBUF | |
166 | //Yeah, i'm too lazy to spec all these with proper parameter, so i use a boost::any. | |
167 | #define SOCKBUF byteBuffer *buf, boost::any value | |
168 | //Easy 'nough | |
169 | #define ACAST(type) boost::any_cast<type>(value) | |
170 | //Again, easy 'enough | |
171 | #define ADDBYTE(byte) buf->push(byte) | |
172 | #define BYTE(x,y) (x & (((uint64_t)0xFF)<<y)) | |
173 | ||
174 | void mcnet_write_bool(SOCKBUF) { | |
175 | ADDBYTE(ACAST(_bool)); | |
176 | } | |
177 | ||
178 | void mcnet_write_byte(SOCKBUF) { | |
179 | ADDBYTE(ACAST(_byte)); | |
180 | } | |
181 | ||
182 | void mcnet_write_ubyte(SOCKBUF) { | |
183 | ADDBYTE(ACAST(_ubyte)); | |
184 | } | |
185 | ||
186 | void mcnet_write_short(SOCKBUF) { | |
187 | ADDBYTE(BYTE(ACAST(_short),0)); | |
188 | ADDBYTE(BYTE(ACAST(_short),8)); | |
189 | } | |
190 | ||
191 | void mcnet_write_ushort(SOCKBUF) { | |
192 | mcnet_write_short(buf,(short)ACAST(_ushort)); | |
193 | } | |
194 | ||
195 | void mcnet_write_int(SOCKBUF) { | |
196 | ADDBYTE(BYTE(ACAST(_int),0)); | |
197 | ADDBYTE(BYTE(ACAST(_int),8)); | |
198 | ADDBYTE(BYTE(ACAST(_int),16)); | |
199 | ADDBYTE(BYTE(ACAST(_int),24)); | |
200 | } | |
201 | ||
202 | void mcnet_write_uint(SOCKBUF) { | |
203 | mcnet_write_int(buf,(int)ACAST(_uint)); | |
204 | } | |
205 | ||
206 | void mcnet_write_long(SOCKBUF) { | |
207 | ADDBYTE(BYTE(ACAST(_long),0)); | |
208 | ADDBYTE(BYTE(ACAST(_long),8)); | |
209 | ADDBYTE(BYTE(ACAST(_long),16)); | |
210 | ADDBYTE(BYTE(ACAST(_long),24)); | |
211 | ADDBYTE(BYTE(ACAST(_long),32)); | |
212 | ADDBYTE(BYTE(ACAST(_long),40)); | |
213 | ADDBYTE(BYTE(ACAST(_long),48)); | |
214 | ADDBYTE(BYTE(ACAST(_long),56)); | |
215 | } | |
216 | ||
217 | void mcnet_write_ulong(SOCKBUF) { | |
218 | mcnet_write_ulong(buf,(long)ACAST(_ulong)); | |
219 | } | |
220 | ||
221 | void mcnet_write_float(SOCKBUF) { | |
222 | _float in = ACAST(_float); | |
223 | ||
224 | byte* t = (byte*)∈ | |
225 | ||
226 | for(int i = 0; i < 4 ; i++) | |
227 | ADDBYTE(t[i]); | |
228 | } | |
229 | ||
230 | void mcnet_write_double(SOCKBUF) { | |
231 | _double in = ACAST(_double); | |
232 | ||
233 | byte* t = (byte*)∈ | |
234 | ||
235 | for(int i = 0; i < 8 ; i++) | |
236 | ADDBYTE(t[i]); | |
237 | } | |
238 | ||
239 | void mcnet_write_blob(SOCKBUF) { | |
240 | LOGGER("mcnetadept.cpp::mcnet_write_blob") | |
241 | _blob data = ACAST(_blob); | |
242 | _short len = data.size(); | |
243 | TRACE("Writing length (" << len << ")...") | |
244 | mcnet_write_short(buf,len); | |
245 | ||
246 | TRACE("Writing data...") | |
247 | for (int i = 0; i < len; i++) | |
248 | { | |
249 | // TRACE("Writing byte " << i << "...") | |
250 | _short b = data.front(); //My compiler was whining... | |
251 | mcnet_write_short(buf,b); | |
252 | data.pop(); | |
253 | } | |
254 | TRACE("Finished writing...") | |
255 | } | |
256 | ||
257 | void mcnet_write_packetid(SOCKBUF, bool isWeird) { | |
258 | ADDBYTE(BYTE(ACAST(_short),0)); | |
259 | if (!isWeird) | |
260 | ADDBYTE(BYTE(ACAST(_short),8)); | |
261 | } |