-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft.js
150 lines (136 loc) · 3.65 KB
/
minecraft.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var hexdump = document.getElementById('hexdump').contentDocument;
var css = hexdump.createElement('link');
css.href = window.location.origin + '/hexdump.css';
css.rel = 'stylesheet';
css.type = 'text/css';
hexdump.head.appendChild(css);
hexdump = hexdump.body;
var packetlog = document.getElementById('packetlog').contentDocument;
css = packetlog.createElement('link');
css.href = window.location.origin + '/packetlog.css';
css.rel = 'stylesheet';
css.type = 'text/css';
packetlog.head.appendChild(css);
packetlog = packetlog.body;
var log = (function() {
var msgs = [];
var add_to_dump = (function() {
var current_index = 0;
var _add_to_dump = function(msgid) {
var line = msgs[msgid]['raw'];
child = document.createElement('div');
if(msgs[msgid]['from'] == 'server') {
child.className = 'server';
} else {
child.className = 'client';
}
child.title = 'message id ' + msgid;
while(line.length > 0) {
if(child.innerHTML != '') {
child.innerHTML += '<br/>';
}
var pos = Number(current_index).toString(16);
var pad = "";
switch(pos.length) {
case 1: pad += '0';
case 2: pad += '0';
case 3: pad += '0';
case 4: pad += '0';
case 5: pad += '0';
case 6: pad += '0';
case 7: pad += '0';
}
pos = pad + pos;
var text = pos.substr(0,4) + ' ' + pos.substr(4) + ' | ';
var text2 = '';
for(var j=0; j<8; ++j) {
if(line.length > 0) {
text += line.substr(0, 2) + ' ';
var n = Number('0x' + line.substr(0, 2)).valueOf();
if(n < 33 || n > 126) {
text2 += '.';
} else {
text2 += String.fromCharCode(n);
}
line = line.substr(2);
current_index++;
} else {
text += ' ';
}
}
text += ' ';
text2 += ' ';
for(; j<16; ++j) {
if(line.length > 0) {
text += line.substr(0, 2) + ' ';
var n = Number('0x' + line.substr(0, 2)).valueOf();
if(n < 33 || n > 126) {
text2 += '.';
} else {
text2 += String.fromCharCode(n);
}
line = line.substr(2);
current_index++;
} else {
text += ' ';
}
}
text += ' ' + text2;
// console.log(text);
child.innerHTML += text;
}
hexdump.appendChild(child);
};
return _add_to_dump;
})();
var _log = function(msg) {
var child;
if(!(msg instanceof Object)) {
msg = {'packet':msg, 'from':'info'};
}
var msgid = msgs.length;
msgs.push(msg);
if(hexdump && msg['raw']) {
add_to_dump(msgid);
}
child = document.createElement('div');
child.className = 'entry';
child.title = 'message id ' + msgid;
if(msg['from'] == 'server' || msg['from'] == 'info') {
child.className += ' ' + msg['from'];
} else {
child.className += ' client';
}
child.id = 'msg' + msgid;
var legend = document.createElement('div');
legend.className = 'title';
legend.innerText = msg['packet'] + ' :: ' + msg['from'] + (msg['to']?' => ' + msg['to']:'');
child.appendChild(legend);
var tab = document.createElement('div');
var first = true;
for(var k in msg) {
if(k != 'packet' && k != 'from' && k != 'to') {
if(k != 'raw') {
if(!first) {
first = false;
var comma = document.createElement('span');
comma.innerText = ', ';
tab.appendChild(comma);
}
var th = document.createElement('span');
th.className = 'key';
th.innerText = k;
tab.appendChild(th);
var td = document.createElement('span');
td.innerText = ' => ' + msg[k];
tab.appendChild(td);
}
}
}
child.appendChild(tab);
//document.body.appendChild(child);
packetlog.appendChild(child);
//if(!hexdump) window.scrollTo(0, document.body.scrollHeight);
};
return _log;
})();