summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Rayhawk <jrayhawk@omgwallhack.org>2017-09-27 15:31:20 -0700
committerJoe Rayhawk <jrayhawk@omgwallhack.org>2017-09-27 15:31:20 -0700
commitdb638057fb77eb81020803aab0cee9763be36425 (patch)
tree39d5cbd0bc3652fb990466e8681e39a3e2a0b703
parentafb194162345630d75420a7311d47ae7698b54e3 (diff)
parent7f191a09be1c122c0a890cf7c5e87efc84a17beb (diff)
downloadtwitchtools-db638057fb77eb81020803aab0cee9763be36425.tar.gz
twitchtools-db638057fb77eb81020803aab0cee9763be36425.zip
Merge action:twitchtools
-rwxr-xr-xapb/apbobsbridge.rb186
-rw-r--r--ircobsbridge.rb81
2 files changed, 229 insertions, 38 deletions
diff --git a/apb/apbobsbridge.rb b/apb/apbobsbridge.rb
new file mode 100755
index 0000000..de5426b
--- /dev/null
+++ b/apb/apbobsbridge.rb
@@ -0,0 +1,186 @@
+#!/usr/bin/env ruby
+
+# APB/OBS-websocket scene management bridge
+
+require 'json'
+require 'socket'
+require 'openssl'
+require 'websocket'
+require 'net/http'
+require 'pp'
+
+confdir = Dir.home + '/.config/twitch/'
+ENV['APPDATA'] && confdir = ENV['APPDATA'] + "\\twitch\\"
+ENV['XDG_CONFIG_HOME'] && confdir = ENV['XDG_CONFIG_HOME'] + '/twitch/'
+
+access_token = IO.read( confdir + 'access_token' ).chomp
+channel = IO.read( confdir + 'channel' ).chomp
+client_id = IO.read( confdir + 'client_id' ).chomp
+unix_socket_path = confdir + 'chat_socket'
+obs_password = IO.read( confdir + 'obs_password' ).chomp
+
+def apbsocketwrite( unix_socket_path, text )
+ unix_socket = UNIXSocket.new( unix_socket_path )
+ unix_socket.print( text )
+rescue
+ensure
+ unix_socket.close
+end
+
+def websocketwrite( json )
+ $tcp_socket.write( WebSocket::Frame::Outgoing::Client.new( data: json, type: :text ).to_s )
+rescue IOError, EOFError
+ $tcp_socket.close
+ websocket_create()
+ retry
+end
+
+# the obsreader thread specially interprets unusual message-id-keyed responses; '1' will be a safe default.
+
+def SetSourceRender( source, render = 'true', id = '1' )
+ websocketwrite( '{ "request-type": "SetSourceRender", "message-id" : "' + id.to_s + '", "source" : "' + source + '", "render" : ' + render.to_s + ' }' )
+end
+
+def GetCurrentScene( id = '1' )
+ websocketwrite( '{ "request-type": "GetCurrentScene", "message-id" : "' + id.to_s + '" }' )
+end
+
+def GetSceneList( id = '1' )
+ websocketwrite( '{ "request-type": "GetSceneList", "message-id" : "' + id.to_s + '" }' )
+end
+
+def SetCurrentScene( scene, id = '1' )
+ websocketwrite( '{ "request-type": "SetCurrentScene", "message-id" : "' + id.to_s + '", "scene-name": "' + scene + '" }' )
+end
+
+def GetStreamingStatus( id = '1' )
+ websocketwrite( '{ "request-type": "GetStreamingStatus", "message-id" : "' + id.to_s + '" }' )
+end
+
+def transientsource( source )
+ Thread.new {
+ SetSourceRender( 'media-' + source , false )
+ sleep 0.1
+ SetSourceRender( 'media-' + source , true )
+ sleep 60
+ SetSourceRender( 'media-' + source , false )
+ }
+end
+
+#json["subscriptions"][0]["user"]["name"]
+
+def websocket_create()
+ begin
+ $tcp_socket = TCPSocket.new( '127.0.0.1', 4444 )
+ $tcp_socket.write( WebSocket::Handshake::Client.new( url: 'ws://localhost/' ).to_s )
+ IO.select( [ $tcp_socket ] )
+ print $tcp_socket.read_nonblock( 4096 )
+ websocketwrite( '{ "request-type": "GetAuthRequired", "message-id" : "1" }' )
+ # Let the obsreader thread do the actual authentication.
+ rescue IOError, EOFError
+ $tcp_socket.close
+ sleep 1
+ retry
+ end
+end
+
+websocket_create()
+frame = WebSocket::Frame::Incoming::Client.new
+frame_decoded = String.new
+status = Hash.new
+$lastworldenter = String.new
+
+def randomyoudied()
+ if rand(5) > 1
+ transientsource( 'youdied' )
+ else
+ transientsource( [ 'youdied-tiggs', 'youdied-nerf', 'youdied-thanksobama' ][rand( 3 )] )
+ end
+end
+
+def log_dispatch( line )
+# 15:22:25 - APB_UI: UcUIAction_WorldEnter::Activated() LastPlayedCharacter = BungMonkey@3002
+# 06:48:07 - APB_UI: Unrecognised tag "Color" in markup text "<col:Action_Enemy>Krezz</col> <col:Action_Enemy_Assist><Color:R=0 G=1 B=0>(+ <Color:R=1 G=0 B=0>Witherr<Color:R=0 G=1 B=0> )</col><hudtexture:KillIcon_SMG_PMG> <col:Action_Team>BungoTheClown</col>" (HUD Message AM_CombatKillOpp)
+# 07:51:34 - APB_UI: Unrecognised tag "Color" in markup text "<col:Action_Enemy>Andromgzeda</col> <col:Action_Enemy_Assist><Color:R=0 G=1 B=0>(+ <Color:R=1 G=0 B=0>Violetta94<Color:R=0 G=1 B=0> )</col><hudtexture:KillIcon_AssaultRifle_NTEC_Scope> <col:Action_Team>Bung</col>" (HUD Message AM_CombatKillOpp)
+ # arg1 may be empty String
+ if ( match = line.match( /^\d\d:\d\d:\d\d\s+-\s+APB_UI:\s+UcUIAction_WorldEnter::Activated\(\)\s+LastPlayedCharacter\s+=\s+([0-9a-zA-Z]+)@\d+/ ) )
+ $lastworldenter = match.to_a[1]
+ puts "Last world enter updated: #{$lastworldenter}"
+ elsif line =~ /\d\d:\d\d:\d\d\s+-\s+APB_UI:\s+Unrecognised\s+tag\s+"Color"\s+in\s+markup\s+text\s+"<col:Action_Enemy>[0-9a-zA-Z]+<\/col>\s+<col:Action_Enemy_Assist><Color:R=0\s+G=1\s+B=0>\(\+\s+<Color:R=1\s+G=0\s+B=0>[0-9a-zA-Z]+<Color:R=0\s+G=1\s+B=0>\s+\)<\/col><hudtexture:[0-9a-zA-Z_]+>\s+<col:Action_Team>#{$lastworldenter}<\/col>"\s+\(HUD\s+Message\s+AM_CombatKillOpp\)/
+ puts 'You died!'
+ randomyoudied()
+ end
+end
+
+# apblog read loop event thread
+apbreader = Thread.new {
+begin
+ lastline = String.new
+ apblog = IO.popen('/usr/bin/tail -n0 -F /cygdrive/c/Program\ Files\ \(x86\)/Steam/SteamApps/common/APB\ Reloaded/APBGame/Logs/Current.log' )
+# apblog = IO.popen('/usr/bin/tail -n0 -F /cygdrive/c/cygwin64/home/user/test.log' )
+ apblog.each_line do |line|
+ if lastline != line
+# puts line
+ log_dispatch( line )
+ lastline = line
+ end
+ end
+rescue
+ensure
+ apblog.close
+end
+}
+
+# websocket read loop event thread
+#obsreader = Thread.new {
+ begin
+ while true
+ frame << $tcp_socket.read_nonblock( 4096 )
+ while ( frame_decoded = frame.next.to_s ) != ""
+ frame_parsed = JSON.parse(frame_decoded)
+ if frame_parsed["update-type"] == "StreamStatus"
+ status = frame_parsed
+ elsif frame_parsed['current-scene'] && frame_parsed['scenes'] # GetSceneList
+ text = '| Current scene: ' + frame_parsed['current-scene'] + ' | Available: ' + frame_parsed['scenes'].map{|v| v['name'] }.sort.join(' ') + "\n"
+ print text
+ #apbsocketwrite( unix_socket_path, text )
+ elsif frame_parsed['name'] && frame_parsed['sources'] # GetCurrentScene
+ pp frame_parsed
+ text = '| Sources: ' + frame_parsed['sources'].map{|v| v['name'] }.sort.join(' ') + "\n"
+ print text
+ if frame_parsed['message-id'] == '1'
+ #apbsocketwrite( unix_socket_path, text )
+ else # other message-ids are a source we want to toggle
+ frame_parsed['sources'].each do |v|
+ if v['name'] == frame_parsed['message-id']
+ if v['render'] == true
+ SetSourceRender( v['name'], 'false', '2' )
+ #apbsocketwrite( unix_socket_path, '| Source ' + v['name'] + " is now unrendered\n" )
+ elsif v['render'] == false
+ SetSourceRender( v['name'], 'true', '2' )
+ #apbsocketwrite( unix_socket_path, '| Source ' + v['name'] + " is now visible\n" )
+ end
+ end
+ end
+ end
+ elsif frame_parsed['update-type'] == 'SwitchScenes'
+ #apbsocketwrite( unix_socket_path, '| Scene is now ' + frame_parsed['scene-name'] + "\n" )
+ #elsif frame_parsed['update-type'] == 'SceneItemVisibilityChanged'
+ # #apbsocketwrite( unix_socket_path, '| Source ' + frame_parsed['item-name'] + ' visibility is now ' + frame_parsed['item-visible'].to_s + "\n" )
+ elsif frame_parsed['authRequired'] == true # GetAuthRequired
+ websocketwrite( '{ "request-type": "Authenticate", "message-id" : "2", "auth" : "' + Digest::SHA256.base64digest(Digest::SHA256.base64digest(obs_password + frame_parsed['salt'])+frame_parsed['challenge']) + '" }' )
+ else
+ print frame_decoded
+ end
+ end
+ #pp JSON.parse(frame_decoded)
+ end
+ rescue IO::WaitReadable, IO::EAGAINWaitReadable, JSON::ParserError
+ IO.select([$tcp_socket])
+ retry
+ rescue IOError, EOFError
+ $tcp_socket.close
+ websocket_create()
+ retry
+ end
+#}
diff --git a/ircobsbridge.rb b/ircobsbridge.rb
index 124f540..103c7a2 100644
--- a/ircobsbridge.rb
+++ b/ircobsbridge.rb
@@ -166,10 +166,6 @@ def websocket_create()
end
end
-websocket_create()
-frame = WebSocket::Frame::Incoming::Client.new
-frame_decoded = String.new
-status = Hash.new
$lastused = Hash.new
$globallastused = 0
@@ -219,6 +215,43 @@ def command_dispatch( mode, user, command, arg1 )
end
end
+def frame_dispatch( frame )
+ if frame["update-type"] == "StreamStatus"
+ $status = frame
+ elsif frame['current-scene'] && frame['scenes'] # GetSceneList
+ text = '| Current scene: ' + frame['current-scene'] + ' | Available: ' + frame['scenes'].map{|v| v['name'] }.sort.join(' ') + "\n"
+ print text
+ ircsocketwrite( text )
+ elsif frame['name'] && frame['sources'] # GetCurrentScene
+ pp frame
+ text = '| Sources: ' + frame['sources'].map{|v| v['name'] }.sort.join(' ') + "\n"
+ print text
+ if frame['message-id'] == '1'
+ ircsocketwrite( text )
+ else # other message-ids are a source we want to toggle
+ frame['sources'].each do |v|
+ if v['name'] == frame['message-id']
+ if v['render'] == true
+ SetSourceRender( v['name'], 'false', '2' )
+ ircsocketwrite( '| Source ' + v['name'] + " is now unrendered\n" )
+ elsif v['render'] == false
+ SetSourceRender( v['name'], 'true', '2' )
+ ircsocketwrite( '| Source ' + v['name'] + " is now visible\n" )
+ end
+ end
+ end
+ end
+ elsif frame['update-type'] == 'SwitchScenes'
+ ircsocketwrite( '| Scene is now ' + frame['scene-name'] + "\n" )
+ #elsif frame['update-type'] == 'SceneItemVisibilityChanged'
+ # ircsocketwrite( '| Source ' + frame['item-name'] + ' visibility is now ' + frame['item-visible'].to_s + "\n" )
+ elsif frame['authRequired'] == true # GetAuthRequired
+ websocketwrite( '{ "request-type": "Authenticate", "message-id" : "2", "auth" : "' + Digest::SHA256.base64digest(Digest::SHA256.base64digest($OBS_PASSWORD + frame['salt'])+frame['challenge']) + '" }' )
+ else
+ print frame
+ end
+end
+
# irclog read loop event thread
ircreader = Thread.new {
begin
@@ -234,6 +267,11 @@ ensure
end
}
+websocket_create()
+frame = WebSocket::Frame::Incoming::Client.new
+frame_decoded = String.new
+$status = Hash.new
+
# websocket read loop event thread
obsreader = Thread.new {
begin
@@ -241,40 +279,7 @@ obsreader = Thread.new {
frame << $tcp_socket.read_nonblock( 4096 )
while ( frame_decoded = frame.next.to_s ) != ""
frame_parsed = JSON.parse(frame_decoded)
- if frame_parsed["update-type"] == "StreamStatus"
- status = frame_parsed
- elsif frame_parsed['current-scene'] && frame_parsed['scenes'] # GetSceneList
- text = '| Current scene: ' + frame_parsed['current-scene'] + ' | Available: ' + frame_parsed['scenes'].map{|v| v['name'] }.sort.join(' ') + "\n"
- print text
- ircsocketwrite( text )
- elsif frame_parsed['name'] && frame_parsed['sources'] # GetCurrentScene
- pp frame_parsed
- text = '| Sources: ' + frame_parsed['sources'].map{|v| v['name'] }.sort.join(' ') + "\n"
- print text
- if frame_parsed['message-id'] == '1'
- ircsocketwrite( text )
- else # other message-ids are a source we want to toggle
- frame_parsed['sources'].each do |v|
- if v['name'] == frame_parsed['message-id']
- if v['render'] == true
- SetSourceRender( v['name'], 'false', '2' )
- ircsocketwrite( '| Source ' + v['name'] + " is now unrendered\n" )
- elsif v['render'] == false
- SetSourceRender( v['name'], 'true', '2' )
- ircsocketwrite( '| Source ' + v['name'] + " is now visible\n" )
- end
- end
- end
- end
- elsif frame_parsed['update-type'] == 'SwitchScenes'
- ircsocketwrite( '| Scene is now ' + frame_parsed['scene-name'] + "\n" )
- #elsif frame_parsed['update-type'] == 'SceneItemVisibilityChanged'
- # ircsocketwrite( '| Source ' + frame_parsed['item-name'] + ' visibility is now ' + frame_parsed['item-visible'].to_s + "\n" )
- elsif frame_parsed['authRequired'] == true # GetAuthRequired
- websocketwrite( '{ "request-type": "Authenticate", "message-id" : "2", "auth" : "' + Digest::SHA256.base64digest(Digest::SHA256.base64digest($OBS_PASSWORD + frame_parsed['salt'])+frame_parsed['challenge']) + '" }' )
- else
- print frame_decoded
- end
+ frame_dispatch( frame_parsed )
end
#pp JSON.parse(frame_decoded)
end