Last active
February 23, 2023 06:54
-
-
Save notpeelz/9c923ee2d6e89133f13aecb7e2115b34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# obs-save-replay | |
# Copyright (C) 2023 notpeelz | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
WS_ENDPOINT="ws://127.0.0.1:4444" | |
REQUEST_ID="$(uuidgen -r)" | |
coproc wspipe { | |
websocat "$WS_ENDPOINT" 2>/dev/null | |
} | |
info() { | |
printf "%s\n" "$1" | |
} | |
error() { | |
printf "%s\n" "$1" | |
finish 1 | |
} | |
finish() { | |
[[ -n "${wspipe[0]}" ]] && { | |
{wspipe[0]}<&- | |
} | |
[[ -n "${wspipe[1]}" ]] && { | |
{wspipe[1]}>&- | |
} | |
exit "$1" | |
} | |
write() { | |
echo "$1" >&"${wspipe[1]}" | |
} | |
success=0 | |
while true; do | |
IFS= read -r line <&"${wspipe[0]}" || { | |
error "WebSocket was closed too early" | |
} | |
op="$(echo "$line" | jq -r .op)" | |
case "$op" in | |
0) | |
rpc_version="$(echo "$line" | jq -r .d.rpcVersion)" | |
if [[ "$rpc_version" != "1" ]]; then | |
error "Invalid rpc version: $rpc_version" | |
fi | |
write '{"op": 1, "d": {"rpcVersion": 1}}' | |
;; | |
2) | |
msg="$(jq -c \ | |
--null-input \ | |
--arg requestId "$REQUEST_ID" \ | |
'{"op": 6, "d": {"requestType": "SaveReplayBuffer", "requestId": $requestId}}' | |
)" | |
write "$msg" | |
;; | |
5) | |
event_type="$(echo "$line" | jq -r .d.eventType)" | |
if [[ "$event_type" == "ReplayBufferSaved" ]]; then | |
if [[ "$success" -ne 1 ]]; then | |
# not ours, so we keep listening | |
continue | |
fi | |
replay_path="$(echo "$line" | jq -r .d.eventData.savedReplayPath)" | |
echo "Saved replay: $replay_path" | |
finish 0 | |
fi | |
;; | |
7) | |
request_type="$(echo "$line" | jq -r .d.requestType)" | |
request_id="$(echo "$line" | jq -r .d.requestId)" | |
case "$request_type" in | |
SaveReplayBuffer) | |
if [[ "$request_id" == "$REQUEST_ID" ]]; then | |
request_status_code="$(echo "$line" | jq -r .d.requestStatus.code)" | |
request_status_result="$(echo "$line" | jq -r .d.requestStatus.result)" | |
if [[ "$request_status_code" == "100" && "$request_status_result" == "true" ]]; then | |
success=1 | |
elif [[ "$request_status_result" == "true" ]]; then | |
info "Unexpected result (succeeded anyway?)" | |
success=1 | |
else | |
error "Failed to save replay buffer ($request_status_code)" | |
fi | |
fi | |
;; | |
*) | |
;; | |
esac | |
;; | |
*) | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment