Skip to content
Snippets Groups Projects
outvoke.rb 14.65 KiB
#!/usr/bin/env ruby
#
# ==========================================================================
# Outvoke -- version 0.0.99.20240817
#
# written by cleemy desu wayo / Licensed under CC0 1.0
#
# official repository: https://gitlab.com/cleemy-desu-wayo/outvoke
# ==========================================================================
#
# * requirements:
# * Ruby 3.0 or later
#
require 'time'
class Outvoke
include Enumerable
attr_accessor :sources, :wait, :version
def initialize
@version = Struct.new(:branch, :body).new(
'0.1',
'0.0.99.20240817'
)
@sources = Hash.new
@wait = 0.5
@hooks = []
@is_quiet_mode = false
end
def quiet_mode?
@is_quiet_mode
end
def quiet_mode=(x)
@is_quiet_mode = x
end
def each
return @hooks.enum_for unless block_given?
@hooks.each {|x| yield x }
end
def <<(x)
@hooks << x
end
def mainloop
loop do
@sources.each do |dslabel, ds|
ds.before_each_event
ds.each_event do |event|
next unless ds.enabled?
if ds.is_first_time_log_check
ds.first_time_log_check(ds, event)
else
hooks_exec(ds, event)
end
end
ds.after_each_event
end
sleep @wait
end
end
def hooks_exec(ds, event)
@hooks.each do |hook|
# check data source
next unless hook['source'] == ds.label # TODO
# check condition
hook_cond_array = [hook['cond']]
hook_cond_array = hook['cond'] if hook['cond'].respond_to?(:each)
hook_cond_array.each do |cond|
if cond.is_a?(String)
event.m = [event.body] if cond == event.body
elsif cond.respond_to?(:match)
event.m = cond.match(event.body)
elsif cond.respond_to?(:call)
event.m = cond.call(event)
end
break if event.m
end
next unless event.m
hook_result = nil
hook_result = hook['proc'].call(event) if hook['proc'] # execute
is_log_outputted = false
if hook_result.is_a?(String)
if not @is_quiet_mode
puts "[#{event.time}] #{hook_result}".gsub("\n", " ")
else
puts hook_result
end
is_log_outputted = true
elsif hook_result.is_a?(Hash)
if hook_result['log']
if not @is_quiet_mode
puts "[#{event.time}] #{hook_result['log']}".gsub("\n", " ")
else
puts hook_result['log']
end
is_log_outputted = true
end
if hook_result['ignore']
event.status.ignore_list[hook_result['ignore'][0]] = [event.time, hook_result['ignore'][1]]
end
end
if not is_log_outputted
if (not hook['proc']) || (hook['proc'] && hook_result)
if not @is_quiet_mode
puts "[#{event.time}] #{event.body}".gsub("\n", " ")
else
puts event.body
end
end
end
end
end
# extracts only the labels of the appropriate data sources from $sources
def extract_ds(dslabel)
return @sources.keys if dslabel == true
label_cond_array = [dslabel]
label_cond_array = dslabel if dslabel.respond_to?(:each)
result = []
label_cond_array.each do |label_cond|
next unless label_cond.respond_to?(:===)
@sources.keys.each do |label|
result << label if label_cond === label
end
end
result.uniq
end
def loputs(x)
@sources['lo'].mutex_lo.synchronize do
@sources['lo'].lo_data << x
end
nil
end
end
module OutvokeDSL
refine Kernel do
def hook(ds_cond, event_cond, &block)
raise '$outvoke is not an Outvoke object' unless $outvoke.is_a?(Outvoke)
$outvoke.extract_ds(ds_cond).each do |label|
if label == 'stdin'
$outvoke.sources['stdin'].enabled = true
end
$outvoke << {
'source' => ds_cond, # TODO
'cond' => event_cond,
'proc' => block
}
end
end
# if block is given, execute in another thread (concurrent computing)
def hookcc(ds_cond, event_cond, &block)
if block_given?
hook(ds_cond, event_cond) {|x| Thread.start { yield x } ; nil }
else
hook(ds_cond, event_cond)
end
end
def loputs(x)
$outvoke.loputs(x)
end
# TODO (stub)
def ignore?(dslabel, value, sec)
raise '$outvoke is not an Outvoke object' unless $outvoke.is_a?(Outvoke)
is_empty = false
ignore_limit = nil
if not $outvoke.sources[dslabel].status.ignore_list[value]
is_empty = true
$outvoke.sources[dslabel].status.ignore_list[value] = []
else
ignore_limit = $outvoke.sources[dslabel].status.ignore_list[value][0] +
$outvoke.sources[dslabel].status.ignore_list[value][1]
end
$outvoke.sources[dslabel].status.ignore_list[value][0] = Time.now
$outvoke.sources[dslabel].status.ignore_list[value][1] = sec
return false if is_empty
#p "ignore_limit: #{ignore_limit} ---- now: #{Time.now}" # DEBUG
return ignore_limit > Time.now
end
end
end
class OutvokeDataSource
attr_accessor :label, :status, :log_lines, :is_first_time_log_check
def initialize(label)
@label = label
@is_enabled = true
@log_lines = []
@is_first_time_log_check = false
end
def enabled?
@is_enabled
end
def enabled=(x)
@is_enabled = x
end
def each_event
raise NotImplementedError
end
def before_each_event
raise NotImplementedError
end
def after_each_event
raise NotImplementedError
end
def first_time_log_check
raise NotImplementedError
end
end
#
# TODO: experimental and incomplete implementation
#
# (specifications are subject to sudden change without notice)
#
class OutvokeDataSourceStdin < OutvokeDataSource
def initialize(label)
super
@status = Struct.new(:now, :last_time).new
@status.last_time = Time.now.floor
@stdin = $stdin.each_line
@is_enabled = false
@stdin_new_lines = []
@mutex_stdin = Mutex.new
@thread_stdin = Thread.new do
Thread.stop
loop do
tmp = @stdin.next
@mutex_stdin.synchronize do
@stdin_new_lines << tmp
end
end
end
end
def enabled=(x)
@is_enabled = x
if @is_enabled
sleep 0.05 # TODO
@thread_stdin.run
else
@thread_stdin.stop
end
end
def each_event
@log_lines.each do |line|
event = Struct.new(:status, :time, :body, :m).new
event.time = Time.now
event.body = line.to_s
event.status = @status
event.status.now = event.time
yield event
@status.last_time = event.time
end
self
end
def before_each_event
@log_lines = []
return unless @is_enabled
@mutex_stdin.synchronize do
@log_lines = @stdin_new_lines.map { _1.dup } # something like deep_dup
@stdin_new_lines = []
end
end
def after_each_event
nil
end
def first_time_log_check(ds, event)
nil
end
end
#
# TODO: experimental and incomplete implementation
#
# (specifications are subject to sudden change without notice)
#
class OutvokeDataSourceLoopback < OutvokeDataSource
attr_accessor :lo_data, :mutex_lo
def initialize(label)
super
@status = Struct.new(:now, :last_time).new
@status.last_time = Time.now.floor
@lo_data = []
@mutex_lo = Mutex.new
end
def each_event
@log_lines.each do |line|
event = Struct.new(:status, :time, :body, :m).new
event.time = Time.now
event.body = line.to_s
event.status = @status
event.status.now = event.time
yield event
@status.last_time = event.time
end
self
end
def before_each_event
@log_lines = []
@mutex_lo.synchronize do
@log_lines = @lo_data.map { _1.dup } # something like deep_dup
@lo_data = []
end
end
def after_each_event
nil
end
def first_time_log_check(ds, event)
nil
end
end
class OutvokeDataSourceVrchat < OutvokeDataSource
attr_accessor :log_dir
def initialize(label)
super
@status = Struct.new(:now, :logfile, :file_size,
:last_joined_time, :elapsed, :ignore_list, :count, :lineno).new
@status.logfile = nil
@status.file_size = 0
@status.ignore_list = Hash.new
@status.count = 0
@status.lineno = 0
@log_dir = ''
@is_first_time_log_check = true
@has_new_log_lines = false
end
def each_event
return unless @has_new_log_lines
loop do
cnt = 0
line_raw = ''
m = nil
@log_lines[@status.lineno..].each do |line|
cnt += 1
line_raw = line
m = line.chomp.match(/\A([0-9]{4}\.[0-9]{2}\.[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) +([-_a-zA-Z0-9]+) +(.*)\z/)
break if m # if found
end
@status.lineno += cnt
@has_new_log_lines = false
return self unless m
event = Struct.new(:status, :raw, :time, :type, :body, :m).new
event.raw = line_raw
event.time = Time.parse(m[1]) # unlike event.status.now, a value taken from log data
event.type = m[2]
event.body = m[3].sub(/\A *- */, '')
event.status = @status
event.status.now = Time.now
if event.body =~ /\A\[Behaviour\] Joining or Creating Room:/
event.status.last_joined_time = event.time
end
event.status.elapsed = event.status.now - event.status.last_joined_time
yield event
end
self
end
def before_each_event
Dir.glob("#{@log_dir}/output_log_*.txt").last.then do
if _1 != @status.logfile
@status.logfile = _1
@status.lineno = 0
if not $outvoke.quiet_mode? # TODO
puts "[#{Time.now}] [outvoke-system] #{@label}: a new log file was found." if @status.logfile
end
end
end
# return if not found
unless @status.logfile
if @is_first_time_log_check
puts "[#{Time.now}] [outvoke-system] #{@label}: ERROR: VRChat log file not found"
end
return
end
# return if not readable
return unless File.readable?(@status.logfile)
# return if the file size has not changed
return if @status.file_size == File.size(@status.logfile)
if @is_first_time_log_check
tmp = @status.logfile.sub(
/\A.*output_log_([0-9]{4}-[0-9]{2}-[0-9]{2})_([0-9]{2})-([0-9]{2})-([0-9]{2})\.txt\z/,
"\\1 \\2:\\3:\\4")
@status.last_joined_time = Time.parse(tmp)
end
# read VRChat log file
File.read(@status.logfile).then do |file_body|
@log_lines = file_body.split("\n")
@status.file_size = file_body.bytesize
end
@has_new_log_lines = true
end
def after_each_event
if @is_first_time_log_check
if not $outvoke.quiet_mode? # TODO
puts "[#{Time.now}] [outvoke-system] #{@label}: first time log check has done."
end
@is_first_time_log_check = false
end
end
def first_time_log_check(ds, event)
if event.body =~ /\A\[Behaviour\] entering room:/i
@status.count = 0
elsif event.body =~ /\A\[Behaviour\] onplayerjoined /i
@status.count += 1
elsif event.body =~ /\A\[Behaviour\] onplayerleft /i
@status.count -= 1
@status.count = 0 if @status.count < 0
end
end
end
class OutvokeDataSourceEverySec < OutvokeDataSource
def initialize(label)
super
@status = Struct.new(:now, :last_time).new
@status.last_time = Time.now.floor
end
def each_event
@log_lines.each do |line|
event = Struct.new(:status, :time, :body, :m).new
event.time = line
event.body = line.to_s
event.status = @status
event.status.now = Time.now
yield event
@status.last_time = line
end
self
end
def before_each_event
@log_lines = []
tmp_time = @status.last_time.floor
loop do
tmp_time += 1
return if tmp_time >= Time.now
@log_lines << tmp_time
end
end
def after_each_event
nil
end
def first_time_log_check(ds, event)
nil
end
end
# --------------------------------------------------------------------
# from this point forward, it will only be executed when this file run
# as a command, not when this file included by require.
if $0 == __FILE__
$stdout.sync = true
def log_puts(str, line_head = '')
return if $outvoke.quiet_mode?
if line_head == ''
puts "#{str.gsub("\n", " ")}"
else
puts "#{line_head}#{str.gsub("\n", "\n" + line_head)}"
end
end
$outvoke = Outvoke.new
version_str = "#{$outvoke.version.branch} (version #{$outvoke.version.body})"
require 'optparse'
is_ruby_code_given = false
ruby_code = nil
mainloop_interval = nil
webui_port = nil
opts = OptionParser.new
opts.default_argv = ['main.rb']
opts.on('-e CODE') { |optvalue| ruby_code = optvalue }
opts.on('-q', '--quiet') { $outvoke.quiet_mode = true }
opts.on('--webui-port PORT') { |optvalue| webui_port = optvalue }
opts.on('--wait INTERVAL') do |optvalue|
raise "--wait option allows only numeric" if optvalue !~ /\A[0-9]+(\.[0-9]+)?\z/
mainloop_interval = optvalue.to_f
end
if ARGV[0]
specified_file = opts.parse!(ARGV)[0]
else
specified_file = opts.parse![0]
end
log_puts "# starting Outvoke #{version_str} ---- #{Time.now}"
log_puts "# ----"
$outvoke.wait = mainloop_interval if mainloop_interval
'stdin'.then do
$outvoke.sources[_1] = OutvokeDataSourceStdin.new(_1)
end
'lo'.then do
$outvoke.sources[_1] = OutvokeDataSourceLoopback.new(_1)
end
'every-sec'.then do
$outvoke.sources[_1] = OutvokeDataSourceEverySec.new(_1)
end
'vrchat-001'.then do
vrchat_log_dir = "#{Dir.home}/.steam/debian-installation/steamapps/compatdata/438100/pfx/drive_c/users/steamuser/AppData/LocalLow/VRChat/VRChat"
$outvoke.sources[_1] = OutvokeDataSourceVrchat.new(_1)
$outvoke.sources[_1].log_dir = vrchat_log_dir
end
'./outvoke.conf.rb'.then do
if File.readable?(_1)
log_puts "# loading #{_1} ..."
require _1
end
end
if ruby_code
log_puts '# given ruby code:'
log_puts ruby_code, '# '
eval "using OutvokeDSL;#{ruby_code}"
else
file_prefix = ''
file_prefix = './' if not specified_file.start_with?('/')
specified_file = "#{file_prefix }#{specified_file}"
log_puts "# loading #{specified_file} ..."
if File.readable?(specified_file)
require specified_file
else
log_puts "# ERROR: #{specified_file} does not exist or is not readable"
exit 1
end
end
log_puts '# ----'
log_puts ''
# execute mainloop
begin
$outvoke.mainloop
rescue Interrupt
if not $outvoke.quiet_mode?
puts "[#{Time.now}] [outvoke-system] interrupted"
end
end
end