Last active
February 12, 2016 23:38
-
-
Save keratagpro/d4d4a49caa2f98f81a0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name TagPro Packet Recorder | |
// @version 0.0.1 | |
// @author Kera | |
// @namespace https://github.com/keratagpro/tagpro-packet-recorder/ | |
// @include http://*.newcompte.fr:* | |
// ==/UserScript== | |
tagpro.ready(function() { | |
recordAndSavePackets(1000, true); | |
}); | |
function removeCallbacks(callbacks) { | |
Object.keys(callbacks).forEach(k => { | |
tagpro.rawSocket.removeListener(k, callbacks[k]); | |
}); | |
} | |
function saveData(data) { | |
var url = 'data:text/json;charset=utf8,' + encodeURIComponent(data); | |
window.open(url, '_blank'); | |
} | |
function recordPackets(count, debug, callback) { | |
if (debug) { | |
console.log('Started recording packets.'); | |
} | |
var packets = []; | |
var callbacks = {}; | |
var keys = Object.keys(tagpro.rawSocket._callbacks); | |
keys.forEach(k => { | |
var func = (d) => { | |
var packet = { | |
type: k, | |
data: d, | |
time: performance.now() | |
}; | |
// NOTE: Have to stringify, since the TagPro game modifies the packets and adds circular references. | |
packets.push(JSON.stringify(packet)); | |
if (debug && packets.length % 100 === 0) { | |
var progress = ((packets.length / count) * 100).toFixed(0); | |
console.log(`recorded ${packets.length} packets (${progress}%).`); | |
} | |
if (packets.length === count || k === 'end') { | |
removeCallbacks(callbacks); | |
callback(packets); | |
} | |
}; | |
callbacks[`record-${k}`] = func; | |
tagpro.rawSocket.on(k, func); | |
}); | |
} | |
function recordAndSavePackets(count, debug) { | |
recordPackets(count, debug, function(packets) { | |
saveData(packets.join('\n')); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment