Skip to content

Instantly share code, notes, and snippets.

@at15four2020
Last active July 3, 2021 20:59
Show Gist options
  • Save at15four2020/9e1d51ce92a66afd1868a11ab4c6c724 to your computer and use it in GitHub Desktop.
Save at15four2020/9e1d51ce92a66afd1868a11ab4c6c724 to your computer and use it in GitHub Desktop.
Record outgoing packages and save/replay later.
var GPlaceRecorder = (function() {
if (typeof setTimeout == 'undefined') {
load("https://gist.githubusercontent.com/kayhadrin/4bf141103420b79126e434dfcf6d4826/raw/a9174bacb2e4d0d8a619d2575b8de55a291c8786/js-timeout-polyfill.js")
}
if (typeof RoomChatManager == 'undefined') {
load("https://gist.github.com/Alynva/7f5dd840fc54fa49e0c538f51b69ac93/raw/RoomChatManager.gprog.js")
}
var headersToRecord = ['PlaceObject', 'MoveObject'] // PlaceObject 1905, MoveObject 2379
var replayInterval = 500
var entries = []
var recording, stopReplay
function savePacket(message) {
if (recording)
entries.push(message.getPacket().stringify())
}
for (var i = 0; i < headersToRecord.length; i++) {
interceptToServer(headersToRecord[i], savePacket)
}
function tick(entries) {
if (stopReplay) {
manager.sendChat("Replay stoped.")
return
} else if (!entries.length) {
manager.sendChat("Replay ended.")
return
}
var packet = HPacket(0)
packet.constructFromString(entries[0])
sendToServer(packet)
setTimeout(function() {
tick(entries.slice(1))
}, replayInterval)
}
function replay() {
manager.sendChat("Replay started.")
tick(entries.slice())
}
var manager = RoomChatManager({
inChatHeaderId: "Chat", // Chat 1918
outChatHeaderId: "Chat" // Chat 3351
})
manager.handleCommand("startRecording", function () {
manager.sendChat("Recording headers "+headersToRecord+".")
entries = []
recording = true
}, "/")
manager.handleCommand("stopRecording", function () {
manager.sendChat("Just recorded "+entries.length+" entries.")
recording = false
}, "/")
manager.handleCommand("startReplay", function () {
stopReplay = false
replay()
}, "/")
manager.handleCommand("stopReplay", function () {
stopReplay = true
}, "/")
manager.handleCommand("exportRecording", function (message, command) {
manager.sendChat("Recording output printed in the log.")
var output = "GPlaceRecorder.setEntries("+JSON.stringify(entries, null, 2)+")"
print(output)
}, "/")
print("\
======= GPlaceRecorder loaded! =======\n\
\n\
Record outgoing packages and save/replay later.\
\n\
Available in-room chat commands:\n\
\n\
- \"/startRecording\": start saving the packages.\n\
- \"/stopRecording\": stop saving the packages.\n\
\n\
- \"/startReplay\": start sending the saved packages.\n\
- \"/stopReplay\": stop sending the saved packages.\n\
\n\
- \"/exportRecording\": print the saved packages to load with `setEntries`.\n\
\n\
Available in-script methods:\n\
\n\
- \"GPlaceRecorder.setHeadersToRecord(list?: (string | number)[]): void\": \n\
Change the list of packages tha will be saved while recording.\n\
The initial list is "+headersToRecord+".\n\
The default value of the paramter is an empty list.\n\
\n\
- \"GPlaceRecorder.setReplayInterval(interval?: number): void\": \n\
Change the interval (in miliseconds) between each package while replaying.\n\
The initial interval and default value of the paramter is 500.\n\
\n\
- \"GPlaceRecorder.setEntries(newEntries?: string[]): void\": \n\
Load pre-recorded entries to be replayed.\n\
The initial interval and default value of the paramter is an empty list.\n\
\n\
======================================\n\
")
return {
/** @param {Array<String|Number>} [list] */
setHeadersToRecord: function(list) {
list = list || []
if (!Array.isArray(list))
throw new Error("list is not array")
headersToRecord = list
},
/** @param {Number} [interval] */
setReplayInterval: function(interval) {
interval = interval || 500
if (typeof interval != 'number')
throw new Error("interval is not number")
replayInterval = interval
},
/** @param {Array<String>} [newEntries] */
setEntries: function(newEntries) {
newEntries = newEntries || []
if (!Array.isArray(newEntries))
throw new Error("newEntries is not array")
entries = newEntries
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment