begin unless defined?(Ruby310Compat) compat_candidates = [] begin cwd = Dir.pwd.to_s unless cwd.nil? || cwd.empty? compat_candidates << File.expand_path('ModScripts/000_Ruby310_Compat.rb', cwd) compat_candidates << File.expand_path('000_Ruby310_Compat.rb', cwd) end rescue StandardError end compat_candidates.compact.uniq.each do |path| begin next unless File.file?(path) load path break if defined?(Ruby310Compat) rescue StandardError end end end rescue StandardError end # encoding: UTF-8 return unless defined?(WebSocket) module WebSocket def self.voice_supported? @caps ||= {} @caps["voice"].to_i == 1 rescue false end end module MPVoice ROOT_DIR = File.expand_path(File.join(Dir.pwd, "UserData", "voice", "pid_#{(Process.pid rescue 0).to_i}")) TX_DIR = File.join(ROOT_DIR, "tx") RX_DIR = File.join(ROOT_DIR, "rx") CONTROL_FILE = File.join(ROOT_DIR, "control.txt") HELPER_SCRIPT = File.expand_path(File.join(Dir.pwd, "ModScripts", "multiplayer", "voice_helper.ps1")) HELPER_LAUNCHER = File.expand_path(File.join(Dir.pwd, "ModScripts", "multiplayer", "voice_helper_launcher.vbs")) POWERSHELL_EXE = "powershell.exe" WSCRIPT_EXE = "wscript.exe" BRIDGE_LIBRARY_NAME = "VoiceBridge" BRIDGE_DLL_PATH = "System/VoiceBridge.dll" BRIDGE_LIBRARY_CANDIDATES = [BRIDGE_LIBRARY_NAME, BRIDGE_DLL_PATH].freeze BRIDGE_SAMPLE_RATE = 16_000 BRIDGE_FRAME_MS = 40 BRIDGE_BUFFER_BYTES = 4096 PUSH_TO_TALK_VK = 0x75 # F6 CHUNK_MS = 100 MIN_WAV_BYTES = 32 MAX_WAV_BYTES = 4096 MAX_FILES_PER_TICK = 2 # Drain one PCM frame at a time so packets do not bunch up and get dropped in bursts. MAX_CAPTURE_PACKETS_PER_TICK = 1 UPDATE_EVERY_FRAMES = 2 HELPER_CHECK_FRAMES = 20 CLEANUP_EVERY_FRAMES = 300 STALE_FILE_AGE_SEC = 30 @record_helper_pid = nil @play_helper_pid = nil @last_capture = nil @warned_missing = false @tx_seq = 1 @last_update_frame = -1 @last_helper_check_frame = -1 @last_cleanup_frame = -1 @bridge_loaded = false @bridge_failed = false @bridge_inited = false def self.ensure_dir(path) value = path.to_s.gsub("\\", "/") parts = value.split("/") return if parts.empty? built = "" if parts[0].to_s =~ /\A[A-Za-z]:\z/ built = parts.shift + "/" elsif value.start_with?("/") built = "/" parts.shift if parts[0].to_s == "" end parts.each do |segment| next if segment.nil? || segment == "" if built == "" || built.end_with?("/") built += segment else built += "/" + segment end begin Dir.mkdir(built) unless File.directory?(built) rescue end end end def self.ensure_paths ensure_dir(ROOT_DIR) ensure_dir(TX_DIR) ensure_dir(RX_DIR) end def self.bridge_library_candidates seen = {} BRIDGE_LIBRARY_CANDIDATES.compact.map { |entry| entry.to_s }.reject { |entry| entry == "" }.select { |entry| seen[entry] ? false : (seen[entry] = true) } rescue [BRIDGE_LIBRARY_NAME, BRIDGE_DLL_PATH] end def self.try_bind_bridge_library(library_name) bindings = {} bindings[:vb_init] = Ruby310Compat.native_api(library_name, 'vb_init', 'PII', 'I') bindings[:vb_shutdown] = Ruby310Compat.native_api(library_name, 'vb_shutdown', 'V', 'V') bindings[:vb_set_capture_enabled] = Ruby310Compat.native_api(library_name, 'vb_set_capture_enabled', 'I', 'V') bindings[:vb_pop_capture] = Ruby310Compat.native_api(library_name, 'vb_pop_capture', 'PI', 'I') bindings[:vb_push_remote] = Ruby310Compat.native_api(library_name, 'vb_push_remote', 'IPI', 'I') bindings[:vb_clear_remote] = Ruby310Compat.native_api(library_name, 'vb_clear_remote', 'I', 'V') bindings[:vb_clear_all_remote] = Ruby310Compat.native_api(library_name, 'vb_clear_all_remote', 'V', 'V') bindings[:vb_get_state] = Ruby310Compat.native_api(library_name, 'vb_get_state', 'V', 'I') bindings[:vb_get_last_error] = Ruby310Compat.native_api(library_name, 'vb_get_last_error', 'PI', 'I') bindings end def self.load_bridge_library return true if @bridge_loaded return false if @bridge_failed begin last_error = nil bridge_library_candidates.each do |library_name| begin bindings = try_bind_bridge_library(library_name) @vb_init = bindings[:vb_init] @vb_shutdown = bindings[:vb_shutdown] @vb_set_capture_enabled = bindings[:vb_set_capture_enabled] @vb_pop_capture = bindings[:vb_pop_capture] @vb_push_remote = bindings[:vb_push_remote] @vb_clear_remote = bindings[:vb_clear_remote] @vb_clear_all_remote = bindings[:vb_clear_all_remote] @vb_get_state = bindings[:vb_get_state] @vb_get_last_error = bindings[:vb_get_last_error] @bridge_library_name = library_name @bridge_loaded = true return true rescue => e last_error = e end end raise(last_error || LoadError.new('VoiceBridge library is unavailable')) rescue => e @bridge_failed = true puts "[VOICE] VoiceBridge load failed: #{e.class}: #{e.message}" false end end def self.bridge_last_error_text return "" unless @bridge_loaded && @vb_get_last_error buf = "\0" * 1024 len = @vb_get_last_error.call(buf, buf.bytesize) rescue 0 return "" if len.to_i <= 0 buf.byteslice(0, len.to_i).to_s.delete("\0") rescue "" end def self.ensure_bridge_initialized return true if @bridge_inited return false unless load_bridge_library result = @vb_init.call(ROOT_DIR.to_s, BRIDGE_SAMPLE_RATE, BRIDGE_FRAME_MS) rescue -1 if result.to_i >= 0 @bridge_inited = true return true end @bridge_failed = true msg = bridge_last_error_text msg = "init returned #{result}" if msg.to_s == "" puts "[VOICE] VoiceBridge init failed: #{msg}" false rescue => e @bridge_failed = true puts "[VOICE] VoiceBridge init exception: #{e.class}: #{e.message}" false end def self.bridge_active? ensure_bridge_initialized rescue false end def self.bridge_shutdown return unless @bridge_loaded begin @vb_set_capture_enabled.call(0) if @vb_set_capture_enabled rescue end begin @vb_clear_all_remote.call if @vb_clear_all_remote rescue end begin @vb_shutdown.call if @bridge_inited && @vb_shutdown rescue end @bridge_inited = false end def self.bridge_push_remote(from_pid, raw) return false unless bridge_active? return false if raw.nil? || raw.bytesize <= 0 result = @vb_push_remote.call(from_pid.to_i, raw, raw.bytesize).to_i return true if result > 0 msg = bridge_last_error_text.to_s msg = "return=#{result}" if msg == "" puts "[VOICE] VoiceBridge push rejected pid=#{from_pid.to_i} bytes=#{raw.bytesize}: #{msg}" false rescue => e puts "[VOICE] VoiceBridge push failed: #{e.class}: #{e.message}" false end def self.bridge_drain_capture return unless bridge_active? 0.upto(MAX_CAPTURE_PACKETS_PER_TICK - 1) do buf = "\0" * BRIDGE_BUFFER_BYTES len = @vb_pop_capture.call(buf, buf.bytesize) rescue 0 break if len.to_i <= 0 raw = buf.byteslice(0, len.to_i) next if raw.nil? || raw.bytesize <= 0 b64 = [raw].pack("m0") rescue nil next if b64.nil? || b64.to_s == "" || b64.bytesize > 3800 WebSocket.send_line("voice:#{next_tx_seq}:#{b64}") end rescue => e puts "[VOICE] VoiceBridge capture drain failed: #{e.class}: #{e.message}" end def self.process_alive?(pid) return false if pid.nil? || pid.to_i <= 0 Process.kill(0, pid.to_i) true rescue false end def self.spawn_helper(mode_name) return nil unless File.exist?(HELPER_SCRIPT) pid = nil if File.exist?(HELPER_LAUNCHER) pid = spawn( WSCRIPT_EXE, "//B", "//Nologo", HELPER_LAUNCHER, HELPER_SCRIPT, mode_name.to_s, ROOT_DIR.to_s, ((Process.pid rescue 0).to_i.to_s) ) else pid = spawn( POWERSHELL_EXE, "-NoProfile", "-ExecutionPolicy", "Bypass", "-WindowStyle", "Hidden", "-File", HELPER_SCRIPT, "-Mode", mode_name.to_s, "-Root", ROOT_DIR.to_s, "-ParentPid", ((Process.pid rescue 0).to_i.to_s) ) end begin Process.detach(pid) rescue end pid rescue => e puts "[VOICE] helper spawn failed (#{mode_name}): #{e.class}: #{e.message}" nil end def self.ensure_helpers ensure_paths unless File.exist?(HELPER_SCRIPT) unless @warned_missing @warned_missing = true puts "[VOICE] missing helper: #{HELPER_SCRIPT}" end return false end @record_helper_pid = spawn_helper("record") unless process_alive?(@record_helper_pid) @play_helper_pid = spawn_helper("play") unless process_alive?(@play_helper_pid) process_alive?(@record_helper_pid) && process_alive?(@play_helper_pid) end def self.ensure_helpers_throttled frame = (Graphics.frame_count rescue 0).to_i ready = process_alive?(@record_helper_pid) && process_alive?(@play_helper_pid) return true if ready && @last_helper_check_frame >= 0 && (frame - @last_helper_check_frame) < HELPER_CHECK_FRAMES @last_helper_check_frame = frame ensure_helpers rescue false end def self.control_payload(capture_enabled) "run=1\ncapture=#{capture_enabled ? 1 : 0}\nchunk_ms=#{CHUNK_MS}\n" end def self.write_control(capture_enabled) ensure_paths capture_enabled = !!capture_enabled return if !@last_capture.nil? && @last_capture == capture_enabled && File.exist?(CONTROL_FILE) tmp = CONTROL_FILE + ".tmp" File.open(tmp, "wb") { |f| f.write(control_payload(capture_enabled)) } begin File.delete(CONTROL_FILE) if File.exist?(CONTROL_FILE) rescue end File.rename(tmp, CONTROL_FILE) @last_capture = capture_enabled rescue => e puts "[VOICE] control write failed: #{e.class}: #{e.message}" end def self.ui_blocked? return true if defined?(MPChat) && MPChat.respond_to?(:active?) && MPChat.active? return true if defined?(WebSocket) && WebSocket.respond_to?(:trade_active?) && WebSocket.trade_active? return true if defined?(WebSocket) && WebSocket.respond_to?(:player_menu_open?) && WebSocket.player_menu_open? false rescue false end def self.capture_allowed? return false unless defined?(SceneManager) && SceneManager.scene_is?(Scene_Map) return false unless WebSocket.connected? return false unless WebSocket.voice_supported? return false if ui_blocked? true rescue false end def self.capture_pressed? begin return true if defined?(MPVoiceAction) && MPVoiceAction.respond_to?(:slot_capture_pressed?) && MPVoiceAction.slot_capture_pressed? rescue end begin if defined?(MPVoiceAction) return true if Input.respond_to?(:trigger?) && Input.trigger?(MPVoiceAction.key_symbol) return true if Input.respond_to?(:press?) && Input.press?(MPVoiceAction.key_symbol) end rescue end return false unless defined?(MPKeyboard) && MPKeyboard.respond_to?(:press?) MPKeyboard.press?(PUSH_TO_TALK_VK) rescue false end def self.hud_visible? capture_allowed? && capture_pressed? rescue false end def self.next_tx_seq @tx_seq ||= 1 seq = @tx_seq.to_i @tx_seq = seq + 1 @tx_seq = 1 if @tx_seq > 999_999_999 seq rescue @tx_seq = 2 1 end def self.send_wav_file(path) raw = nil begin raw = File.open(path, "rb") { |f| f.read } rescue raw = nil end begin File.delete(path) if File.exist?(path) rescue end return if raw.nil? raw = wav_to_pcm(raw) size = raw.bytesize.to_i return if size < MIN_WAV_BYTES || size > MAX_WAV_BYTES b64 = nil begin b64 = [raw].pack("m0") rescue b64 = nil end return if b64.nil? || b64.to_s == "" || b64.bytesize > 3800 WebSocket.send_line("voice:#{next_tx_seq}:#{b64}") rescue => e puts "[VOICE] send failed: #{e.class}: #{e.message}" end def self.flush_tx_queue return unless WebSocket.connected? && WebSocket.voice_supported? ensure_paths files = [] begin files = Dir.glob(File.join(TX_DIR, "*.wav")).sort rescue files = [] end files[0, MAX_FILES_PER_TICK].each do |path| send_wav_file(path) end end def self.pending_wav_files?(dir) begin Dir.glob(File.join(dir, "*.wav")).any? rescue false end end def self.wav_to_pcm(raw) data = raw.to_s return data unless data.bytesize >= 12 return data unless data.byteslice(0, 4) == "RIFF" && data.byteslice(8, 4) == "WAVE" offset = 12 while (offset + 8) <= data.bytesize chunk_id = data.byteslice(offset, 4).to_s chunk_size = (data.byteslice(offset + 4, 4).to_s.unpack("V").first rescue 0).to_i offset += 8 break if chunk_size < 0 if chunk_id == "data" payload = data.byteslice(offset, chunk_size) return payload unless payload.nil? break end offset += chunk_size offset += 1 if (chunk_size & 1) == 1 end data rescue raw end def self.pcm_to_wav(raw) pcm = raw.to_s data_size = pcm.bytesize byte_rate = BRIDGE_SAMPLE_RATE * 2 block_align = 2 riff_size = 36 + data_size header = [ "RIFF", riff_size, "WAVE", "fmt ", 16, 1, 1, BRIDGE_SAMPLE_RATE, byte_rate, block_align, 16, "data", data_size ].pack("A4VA4A4VvvVVvvA4V") header + pcm rescue raw end def self.enqueue_rx(from_pid, seq, b64_payload) raw = nil begin raw = b64_payload.to_s.unpack("m0").first rescue raw = nil end return if raw.nil? size = raw.bytesize.to_i return if size < MIN_WAV_BYTES || size > MAX_WAV_BYTES return if bridge_push_remote(from_pid, raw) ensure_paths stamp = ((Time.now.to_f * 1000.0).to_i rescue 0) base = format("%013d_%06d_%010d", stamp, from_pid.to_i.abs, seq.to_i.abs) tmp = File.join(RX_DIR, base + ".tmp") dst = File.join(RX_DIR, base + ".wav") wav = pcm_to_wav(raw) File.open(tmp, "wb") { |f| f.write(wav) } begin File.delete(dst) if File.exist?(dst) rescue end File.rename(tmp, dst) rescue => e puts "[VOICE] rx enqueue failed: #{e.class}: #{e.message}" begin File.delete(tmp) if tmp && File.exist?(tmp) rescue end end def self.cleanup_stale_files frame = (Graphics.frame_count rescue 0).to_i return if @last_cleanup_frame >= 0 && (frame - @last_cleanup_frame) < CLEANUP_EVERY_FRAMES @last_cleanup_frame = frame now = Time.now.to_f rescue 0.0 [TX_DIR, RX_DIR].each do |dir| begin Dir.glob(File.join(dir, "*")).each do |path| begin age = now - File.mtime(path).to_f File.delete(path) if age >= STALE_FILE_AGE_SEC rescue end end rescue end end end def self.purge_pending_files [TX_DIR, RX_DIR].each do |dir| begin Dir.glob(File.join(dir, "*")).each do |path| begin File.delete(path) if File.file?(path) rescue end end rescue end end end def self.stop_capture begin @vb_set_capture_enabled.call(0) if @bridge_inited && @vb_set_capture_enabled rescue end write_control(false) rescue end def self.update begin MPVoiceHUD.sync(hud_visible?) if defined?(MPVoiceHUD) rescue end frame = (Graphics.frame_count rescue 0).to_i return if @last_update_frame >= 0 && (frame - @last_update_frame) < UPDATE_EVERY_FRAMES @last_update_frame = frame if WebSocket.connected? && WebSocket.voice_supported? && bridge_active? pressed = capture_allowed? && capture_pressed? begin @vb_set_capture_enabled.call(pressed ? 1 : 0) if @vb_set_capture_enabled rescue => e puts "[VOICE] VoiceBridge capture toggle failed: #{e.class}: #{e.message}" end bridge_drain_capture if WebSocket.connected? && WebSocket.voice_supported? elsif capture_allowed? pressed = capture_pressed? need_helpers = pressed || pending_wav_files?(RX_DIR) || pending_wav_files?(TX_DIR) || process_alive?(@record_helper_pid) || process_alive?(@play_helper_pid) ensure_helpers_throttled if need_helpers write_control(pressed) if process_alive?(@record_helper_pid) || process_alive?(@play_helper_pid) flush_tx_queue if pressed || pending_wav_files?(TX_DIR) else stop_capture flush_tx_queue if WebSocket.connected? && WebSocket.voice_supported? end cleanup_stale_files rescue => e puts "[VOICE] update failed: #{e.class}: #{e.message}" end end module MPVoiceHUD module_function @vp = nil @spr = nil @bmp = nil def on_map_scene? return false unless defined?(SceneManager) && defined?(Scene_Map) SceneManager.scene_is?(Scene_Map) rescue false end def load_bitmap begin return Bitmap.new("Graphics/Online/voice") rescue end begin return Bitmap.new("Graphics/Online/voice.png") rescue end nil end def ensure_hud return unless on_map_scene? return if @spr && !@spr.disposed? dispose rescue nil @bmp = load_bitmap return if @bmp.nil? @vp = Viewport.new(0, 0, Graphics.width, Graphics.height) @vp.z = 200_000 @spr = Sprite.new(@vp) @spr.bitmap = @bmp @spr.z = 200_000 @spr.ox = (@bmp.width / 2) rescue 0 @spr.oy = 0 @spr.visible = false update_position rescue dispose rescue nil end def update_position return unless @spr && !@spr.disposed? @spr.x = (Graphics.width / 2) rescue 0 @spr.y = 6 rescue end def show return unless on_map_scene? ensure_hud return unless @spr && !@spr.disposed? update_position @spr.visible = true rescue end def hide return unless @spr && !@spr.disposed? @spr.visible = false rescue end def sync(visible) if visible show else hide end rescue end def dispose begin if @spr && !@spr.disposed? @spr.bitmap = nil rescue nil @spr.dispose end rescue end begin @vp.dispose if @vp && !@vp.disposed? rescue end begin @bmp.dispose if @bmp && !@bmp.disposed? rescue end @spr = nil @vp = nil @bmp = nil end end module MPVoiceAction KEY_SYMBOL = :VOICE KEY_NAME = 'VOICE' KEY_PATH = 'Graphics/Online/voice.png' ITEM_NAME = 'mp_voice_skill' TEXT_NAME = 'mp/voice/name' TEXT_DESC = 'mp/voice/desc' KEY_ROW = [KEY_SYMBOL, 'DataInput:Key/Voice', 'Voice', [:F6]] PAD_ROW = [KEY_SYMBOL, 'DataInput:Key/Voice', 'Voice', -1] class << self attr_accessor :voice_skill_id end @capture_keys = [] def self.assign_attr(obj, attr, value) return if obj.nil? setter = (attr.to_s + '=').to_sym return unless obj.respond_to?(setter) obj.send(setter, value) rescue end def self.install_key_rows begin list = InputUtils.keyList unless list.any? { |row| row && row[0] == KEY_SYMBOL } idx = list.index { |row| row && row[0] == :D8 } || list.length list.insert(idx, KEY_ROW.dup) end rescue end begin list = InputUtils.gamepadList unless list.any? { |row| row && row[0] == KEY_SYMBOL } idx = list.index { |row| row && row[0] == :D8 } || list.length list.insert(idx, PAD_ROW.dup) end rescue end end def self.read_ini_key(index) $LonaINI['Keyboard']["#{KEY_NAME}_#{index}"] rescue nil end def self.write_ini_key(index, value) $LonaINI['Keyboard']["#{KEY_NAME}_#{index}"] = value rescue end def self.ensure_keyboard_binding install_key_rows begin Input::SYM_KEYS[KEY_SYMBOL] ||= [0, 0, 0] rescue return end defaults = [:F6, nil, nil] changed = false 0.upto(2) do |i| raw = read_ini_key(i) if raw.nil? || raw == '' || raw == 0 || raw == '0' sym = defaults[i] code = 0 if sym begin code = Input::KEYMAP[sym] || 0 rescue code = 0 end write_ini_key(i, sym.to_s) else write_ini_key(i, '0') end Input::SYM_KEYS[KEY_SYMBOL][i] = code changed = true else begin Input::SYM_KEYS[KEY_SYMBOL][i] = Input::KEYMAP[raw.to_sym] || 0 rescue Input::SYM_KEYS[KEY_SYMBOL][i] = 0 end end end begin if defined?(WolfPad) && WolfPad.respond_to?(:keys) pad_val = nil begin pad_val = $LonaINI['GamePad'][KEY_NAME] rescue pad_val = nil end if pad_val.nil? || pad_val == '' WolfPad.keys[KEY_SYMBOL] = -1 begin $LonaINI['GamePad'][KEY_NAME] = '-1' rescue end changed = true else WolfPad.keys[KEY_SYMBOL] = pad_val.to_i end end rescue end begin $LonaINI.save if changed rescue end end def self.install_text begin $game_text[TEXT_NAME] = 'Голос' $game_text[TEXT_DESC] = 'Голосовой чат по нажатию для игроков на одной карте. Это действие можно назначить в слот навыка или использовать отдельной клавишей.' rescue end end def self.ensure_voice_skill return nil unless defined?($data_skills) && $data_skills if @voice_skill_id && $data_skills[@voice_skill_id] && voice_skill?($data_skills[@voice_skill_id]) install_text return @voice_skill_id end existing = nil begin existing = $data_skills.compact.find { |s| voice_skill?(s) } rescue existing = nil end if existing begin @voice_skill_id = existing.id rescue @voice_skill_id = nil end install_text return @voice_skill_id end template = nil begin template = $data_skills.compact.find { |s| s && s.respond_to?(:item_name) && s.respond_to?(:id) } rescue template = nil end return nil unless template skill = nil begin skill = template.clone rescue begin skill = template.dup rescue skill = nil end end return nil unless skill id = $data_skills.length assign_attr(skill, :id, id) assign_attr(skill, :item_name, ITEM_NAME) assign_attr(skill, :name, TEXT_NAME) assign_attr(skill, :description, TEXT_DESC) assign_attr(skill, :icon_index, KEY_PATH) assign_attr(skill, :type, 'other') begin $data_skills[id] = skill rescue return nil end begin if defined?($data_SkillName) && $data_SkillName.respond_to?(:[]=) $data_SkillName[ITEM_NAME] = skill end rescue end @voice_skill_id = id install_text id end def self.voice_skill_id ensure_voice_skill unless @voice_skill_id @voice_skill_id end def self.voice_skill id = voice_skill_id return nil if id.nil? begin $data_skills[id] rescue nil end end def self.voice_skill?(obj) return false if obj.nil? begin return true if obj.respond_to?(:item_name) && obj.item_name == ITEM_NAME rescue end begin return true if !@voice_skill_id.nil? && obj.respond_to?(:id) && obj.id == @voice_skill_id rescue end false end def self.voice_slot?(slot) sid = nil begin sid = slot.id if slot.respond_to?(:id) rescue sid = nil end sid = slot if sid.nil? sid = sid.to_i rescue 0 return false if sid <= 0 id = voice_skill_id.to_i return true if id > 0 && sid == id begin skill = $data_skills[sid] if defined?($data_skills) && $data_skills return voice_skill?(skill) rescue end false end def self.key_symbol KEY_SYMBOL end def self.key_text begin InputUtils.getKeyAndTranslate(key_symbol) rescue 'F6' end end def self.draw_icon_to(bitmap, x, y) begin src = Cache.normal_bitmap(KEY_PATH) bitmap.blt(x, y, src, Rect.new(0, 0, 24, 24)) return true rescue begin bitmap.fill_rect(x, y, 24, 24, Color.new(32, 32, 32, 180)) bitmap.font.size = 14 bitmap.font.outline = false bitmap.draw_text(x, y - 1, 24, 24, 'V', 1) rescue end return false end end def self.hud_font_name begin return MPSafeFont.skill_menu_name if defined?(MPSafeFont) && MPSafeFont.respond_to?(:skill_menu_name) return MPSafeFont.name if defined?(MPSafeFont) && MPSafeFont.respond_to?(:name) rescue end "VL Gothic" end def self.skill_info_font_name begin return MPSafeFont.skill_menu_info_name if defined?(MPSafeFont) && MPSafeFont.respond_to?(:skill_menu_info_name) return MPSafeFont.skill_menu_name if defined?(MPSafeFont) && MPSafeFont.respond_to?(:skill_menu_name) rescue end hud_font_name end def self.skill_font_size begin return FONT_MOD::MENU_M_S if defined?(FONT_MOD) && FONT_MOD.const_defined?(:MENU_M_S) rescue end 16 end def self.skill_info_font_size begin return FONT_MOD::MENU_L_S if defined?(FONT_MOD) && FONT_MOD.const_defined?(:MENU_L_S) rescue end skill_font_size end def self.voice_key_pairs(player = nil) player ||= (defined?($game_player) ? $game_player : nil) return [] if player.nil? [ [player.hotkey_skill_normal, player.slot_skill_normal], [player.hotkey_skill_heavy, player.slot_skill_heavy], [player.hotkey_skill_control, player.slot_skill_control], [player.skill_hotkey_0, player.slot_hotkey_0], [player.skill_hotkey_1, player.slot_hotkey_1], [player.skill_hotkey_2, player.slot_hotkey_2], [player.skill_hotkey_3, player.slot_hotkey_3], [player.skill_hotkey_4, player.slot_hotkey_4], [player.hotkey_other, player.slot_hotkey_other] ] rescue [] end def self.register_capture_key(key) return if key.nil? @capture_keys ||= [] @capture_keys << key unless @capture_keys.include?(key) rescue end def self.release_stale_capture_keys @capture_keys ||= [] @capture_keys = @capture_keys.select do |key| begin (Input.respond_to?(:trigger?) && Input.trigger?(key)) || (Input.respond_to?(:press?) && Input.press?(key)) rescue false end end rescue @capture_keys = [] end def self.slot_capture_pressed? begin keys = release_stale_capture_keys return true if keys && !keys.empty? rescue end voice_key_pairs.any? do |key, slot| next false unless voice_slot?(slot) begin (Input.respond_to?(:trigger?) && Input.trigger?(key)) || (Input.respond_to?(:press?) && Input.press?(key)) rescue false end end rescue false end def self.consume_as_voice_action(key) return false if key.nil? return false unless defined?($game_player) && $game_player triggered = false begin triggered = Input.trigger?(key) || (Input.press?(key) && (($game_player.instance_variable_get(:@prevSkillKey) rescue nil) != key)) rescue triggered = false end return false unless triggered begin $game_player.instance_variable_set(:@prevSkillKey, key) rescue end register_capture_key(key) begin SndLib.sys_ok rescue end true end end MPVoiceAction.ensure_keyboard_binding MPVoiceAction.ensure_voice_skill class Game_Player < Game_Character alias mp_voice_action_process_hotkey_input process_hotkey_input def process_hotkey_input(key, slot) return if MPVoiceAction.voice_slot?(slot) && MPVoiceAction.consume_as_voice_action(key) mp_voice_action_process_hotkey_input(key, slot) end alias mp_voice_action_process_skill_roster_input process_skill_roster_input def process_skill_roster_input(key, whichroster = 0, which_skill = 0) if !key.nil? && @slot_RosterArray && whichroster < @slot_RosterArray.size begin skill_id = @slot_RosterArray[whichroster][which_skill] return if MPVoiceAction.voice_slot?(skill_id) && MPVoiceAction.consume_as_voice_action(key) rescue end end mp_voice_action_process_skill_roster_input(key, whichroster, which_skill) end alias mp_voice_action_process_custom_skill process_custom_skill def process_custom_skill(key, skill_id) return if MPVoiceAction.voice_slot?(skill_id) && MPVoiceAction.consume_as_voice_action(key) mp_voice_action_process_custom_skill(key, skill_id) end end class Map_Hud def mp_special_skill_object(ref) return nil if ref.nil? if ref.respond_to?(:item_name) && ref.respond_to?(:icon_index) return ref end sid = 0 begin sid = ref.to_i rescue sid = 0 end return nil if sid <= 0 begin if [101, 102, 103].include?(sid) && @actor && @actor.respond_to?(:get_system_skill) && defined?($data_skills) && $data_skills base = $data_skills[sid] resolved = @actor.get_system_skill(base) return resolved unless resolved.nil? end rescue end skill = nil begin skill = $data_skills[sid] if defined?($data_skills) && $data_skills rescue skill = nil end return skill unless skill.nil? nil rescue nil end def mp_special_chat_or_voice_icon(skill_id, raw_slot = nil) refs = [raw_slot, skill_id] refs.each do |ref| begin sid = ref.to_i rescue sid = 0 end begin if defined?(MPChatAction) && MPChatAction.respond_to?(:chat_slot?) && MPChatAction.chat_slot?(ref) return MPChatAction::KEY_PATH end rescue end begin if MPVoiceAction.respond_to?(:voice_slot?) && MPVoiceAction.voice_slot?(ref) return MPVoiceAction::KEY_PATH end rescue end begin if sid > 0 && defined?(MPChatAction) && MPChatAction.respond_to?(:chat_slot?) && MPChatAction.chat_slot?(sid) return MPChatAction::KEY_PATH end rescue end begin if sid > 0 && MPVoiceAction.respond_to?(:voice_slot?) && MPVoiceAction.voice_slot?(sid) return MPVoiceAction::KEY_PATH end rescue end skill_obj = mp_special_skill_object(ref) next if skill_obj.nil? begin if defined?(MPChatAction) && MPChatAction.respond_to?(:chat_skill?) && MPChatAction.chat_skill?(skill_obj) return MPChatAction::KEY_PATH end rescue end begin if MPVoiceAction.respond_to?(:voice_skill?) && MPVoiceAction.voice_skill?(skill_obj) return MPVoiceAction::KEY_PATH end rescue end begin item_name = skill_obj.item_name.to_s return MPChatAction::KEY_PATH if item_name == "mp_chat_skill" && defined?(MPChatAction) return MPVoiceAction::KEY_PATH if item_name == "mp_voice_skill" rescue end begin icon = skill_obj.icon_index.to_s return icon if icon != "" && icon.downcase.include?("graphics/online/chat") return icon if icon != "" && icon.downcase.include?("graphics/online/voice") rescue end end nil rescue nil end def mp_voice_action_raw_slot_for_hud(hotkey_sprite, sort_index) return nil if !defined?(@player) || @player.nil? idx = sort_index.to_i begin if hotkey_sprite.equal?(@hotkey_asdf) return @player.slot_skill_normal if idx == 0 return @player.slot_skill_heavy if idx == 1 return @player.slot_skill_control if idx == 2 return @player.slot_hotkey_0 if idx == 3 end if hotkey_sprite.equal?(@hotkey_qwer) return @player.slot_hotkey_1 if idx == 0 return @player.slot_hotkey_2 if idx == 1 return @player.slot_hotkey_3 if idx == 2 return @player.slot_hotkey_4 if idx == 3 end rescue end nil rescue nil end alias mp_voice_action_draw_hotkey_icon_get_icon_index draw_hotkey_icon_get_icon_index def draw_hotkey_icon_get_icon_index(original_bitmap, hotkey_sprite, skill_id, sort_index, sym) raw_slot = mp_voice_action_raw_slot_for_hud(hotkey_sprite, sort_index) special_icon = mp_special_chat_or_voice_icon(skill_id, raw_slot) return special_icon unless special_icon.nil? || special_icon.to_s == "" mp_voice_action_draw_hotkey_icon_get_icon_index(original_bitmap, hotkey_sprite, skill_id, sort_index, sym) end alias mp_voice_action_draw_hotkey_icon_from_skill_base draw_hotkey_icon_from_skill def draw_hotkey_icon_from_skill(original_bitmap, hotkey_sprite, icon_index, sort_index, sym) raw_slot = mp_voice_action_raw_slot_for_hud(hotkey_sprite, sort_index) special_icon = mp_special_chat_or_voice_icon(icon_index, raw_slot) if !(special_icon.nil? || special_icon.to_s == "") icon_num = 0 begin icon_num = icon_index.to_i rescue icon_num = 0 end begin no_item = @icon_no_item.to_i rescue no_item = -99999 end if icon_num == -1 || icon_num == no_item return draw_hotkey_icon_from_string(original_bitmap, hotkey_sprite, special_icon, sort_index, sym) end end mp_voice_action_draw_hotkey_icon_from_skill_base(original_bitmap, hotkey_sprite, icon_index, sort_index, sym) end alias mp_voice_action_draw_hotkey_icon_from_string_base draw_hotkey_icon_from_string def draw_hotkey_icon_from_string(original_bitmap, hotkey_sprite, icon_index, sort_index, sym) unless mp_voice_action_skill_like_string_icon?(icon_index) return mp_voice_action_draw_hotkey_icon_from_string_base(original_bitmap, hotkey_sprite, icon_index, sort_index, sym) end @cachedBitmapICON = Cache.normal_bitmap(icon_index) cel = 27 y = 0 w = 26 skill_y = (defined?(@yh) ? @yh.to_i : 0) icon_src_rect = Rect.new(0, 0, 24, 24) board_src_rect = Rect.new(sort_index * cel, 0, 24, 32 + skill_y) hotkey_sprite.bitmap.fill_rect(board_src_rect, Color.new(255, 255, 255, 0)) hotkey_sprite.bitmap.blt(sort_index * cel, skill_y, @cachedBitmapICON, icon_src_rect) hotkey_sprite.bitmap.blt(sort_index * cel, skill_y, original_bitmap, board_src_rect) if @roster_need_display skillRosterIcon = 641 + @player.slot_RosterCurrent skillRosterIcon_src_rect = Rect.new(0, 0, 24, 24) hotkey_sprite.bitmap.blt(sort_index * cel, skill_y, @cachedBitmapICON, skillRosterIcon_src_rect) end if @display_X_icon iconX_src = Rect.new(@icon_no_item % 16 * 24, @icon_no_item / 16 * 24, 24, 24) hotkey_sprite.bitmap.blt(sort_index * cel, skill_y, @cachedBitmapICON, iconX_src) @display_X_icon = false end begin hotkey_sprite.bitmap.font.name = FONT_MOD::HUD_SKIL hotkey_sprite.bitmap.font.size = FONT_MOD::HUD_SKIL_S hotkey_sprite.bitmap.font.color = FM_ST::SKY rescue hotkey_sprite.bitmap.font.size = 14 hotkey_sprite.bitmap.font.color.set(255, 255, 255, 180) end hotkey_sprite.bitmap.draw_text(sort_index * cel, y, w, [skill_y, 16].max, sym.to_s.gsub("Num", "n"), 1) end def mp_voice_action_skill_like_string_icon?(icon_index) path = icon_index.to_s return true if defined?(MPChatAction) && path == MPChatAction::KEY_PATH return true if path == MPVoiceAction::KEY_PATH false rescue false end end class Menu_SkillAndHotkey < Menu_ContentBase alias mp_voice_action_initialize initialize def initialize(*args) MPVoiceAction.ensure_voice_skill mp_voice_action_initialize(*args) begin @skill_icons.bitmap.font.size = 16 if @skill_icons && @skill_icons.bitmap rescue end end def mp_special_skill?(skill) return false if skill.nil? return true if defined?(MPChatAction) && MPChatAction.respond_to?(:chat_skill?) && MPChatAction.chat_skill?(skill) return true if MPVoiceAction.voice_skill?(skill) false rescue false end def mp_special_skill_order(skill) return 0 if defined?(MPChatAction) && MPChatAction.respond_to?(:chat_skill?) && MPChatAction.chat_skill?(skill) return 1 if MPVoiceAction.voice_skill?(skill) 99 rescue 99 end def mp_special_skill_label(skill) return 'Чат' if defined?(MPChatAction) && MPChatAction.respond_to?(:chat_skill?) && MPChatAction.chat_skill?(skill) return 'Голос' if MPVoiceAction.voice_skill?(skill) begin return $game_text[skill.name] rescue end begin return skill.name.to_s rescue end '' end def mp_special_redraw_skill_icons! return if !@buff_icons @buff_icons.bitmap.dispose if @buff_icons.bitmap @buff_icons.bitmap = Bitmap.new(130, 27 * @skills.length) @last_icon_index = @skills.size % 9 @buff_icon_rect_record = [] begin font_name = @mp_special_skill_font_name.to_s font_name = MPVoiceAction.hud_font_name if font_name.strip == "" @buff_icons.bitmap.font.name = font_name @buff_icons.bitmap.font.size = MPVoiceAction.skill_font_size @buff_icons.bitmap.font.outline = false @buff_icons.bitmap.font.color = Color.new(224, 128, 64) rescue end for index in 0...@skills.length skill = @skills[index] icon_index = real_skill_icon(skill) @buff_icon_rect_record << [@icon_viewport_default_x, @icon_viewport_default_y + 27 * index, 123, 32] if @buff_icon_rect_record.size < 9 if skill.item_name == "sys_normal" equip_icon_index = getPlayerEquipIcon(0) if icon_index.is_a?(String) @buff_icons.bitmap.blt(0, index * 27, Cache.normal_bitmap(icon_index), Rect.new(0, 0, 24, 24)) if equip_icon_index.is_a?(String) @buff_icons.bitmap.blt(24, index * 27, Cache.normal_bitmap(equip_icon_index), Rect.new(0, 0, 24, 24)) else @buff_icons.bitmap.blt(24, index * 27, Cache.system("Iconset"), Rect.new(equip_icon_index % 16 * 24, equip_icon_index / 16 * 24, 24, 24)) end else @buff_icons.bitmap.blt(0, index * 27, Cache.system("Iconset"), Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)) if equip_icon_index.is_a?(String) @buff_icons.bitmap.blt(24, index * 27, Cache.normal_bitmap(equip_icon_index), Rect.new(0, 0, 24, 24)) else @buff_icons.bitmap.blt(24, index * 27, Cache.system("Iconset"), Rect.new(equip_icon_index % 16 * 24, equip_icon_index / 16 * 24, 24, 24)) end end draw_text_on_canvas(@buff_icons, 5 + 21 + 24, 27 * index, mp_special_skill_label(skill), true) elsif skill.item_name == "sys_heavy" equip_icon_index = getPlayerEquipIcon(1) if icon_index.is_a?(String) @buff_icons.bitmap.blt(0, index * 27, Cache.normal_bitmap(icon_index), Rect.new(0, 0, 24, 24)) if equip_icon_index.is_a?(String) @buff_icons.bitmap.blt(24, index * 27, Cache.normal_bitmap(equip_icon_index), Rect.new(0, 0, 24, 24)) else @buff_icons.bitmap.blt(24, index * 27, Cache.system("Iconset"), Rect.new(equip_icon_index % 16 * 24, equip_icon_index / 16 * 24, 24, 24)) end else @buff_icons.bitmap.blt(0, index * 27, Cache.system("Iconset"), Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)) if equip_icon_index.is_a?(String) @buff_icons.bitmap.blt(24, index * 27, Cache.normal_bitmap(equip_icon_index), Rect.new(0, 0, 24, 24)) else @buff_icons.bitmap.blt(24, index * 27, Cache.system("Iconset"), Rect.new(equip_icon_index % 16 * 24, equip_icon_index / 16 * 24, 24, 24)) end end draw_text_on_canvas(@buff_icons, 5 + 21 + 24, 27 * index, mp_special_skill_label(skill), true) elsif skill.item_name == "sys_control" equip_icon_index = getPlayerEquipIcon(1) if icon_index.is_a?(String) @buff_icons.bitmap.blt(0, index * 27, Cache.normal_bitmap(icon_index), Rect.new(0, 0, 24, 24)) if equip_icon_index.is_a?(String) @buff_icons.bitmap.blt(24, index * 27, Cache.normal_bitmap(equip_icon_index), Rect.new(0, 0, 24, 24)) else @buff_icons.bitmap.blt(24, index * 27, Cache.system("Iconset"), Rect.new(equip_icon_index % 16 * 24, equip_icon_index / 16 * 24, 24, 24)) end else @buff_icons.bitmap.blt(0, index * 27, Cache.system("Iconset"), Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)) if equip_icon_index.is_a?(String) @buff_icons.bitmap.blt(24, index * 27, Cache.normal_bitmap(equip_icon_index), Rect.new(0, 0, 24, 24)) else @buff_icons.bitmap.blt(24, index * 27, Cache.system("Iconset"), Rect.new(equip_icon_index % 16 * 24, equip_icon_index / 16 * 24, 24, 24)) end end draw_text_on_canvas(@buff_icons, 5 + 21 + 24, 27 * index, mp_special_skill_label(skill), true) else draw_text_on_canvas(@buff_icons, 5 + 21, 27 * index, mp_special_skill_label(skill), true) if icon_index.is_a?(String) @buff_icons.bitmap.blt(0, index * 27, Cache.normal_bitmap(icon_index), Rect.new(0, 0, 24, 24)) else @buff_icons.bitmap.blt(0, index * 27, Cache.system("Iconset"), Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)) end end end rescue end def mp_voice_action_voice_skill_selected? return false if !@skills || @cursor_real_index.nil? MPVoiceAction.voice_skill?(@skills[@cursor_real_index]) rescue false end alias mp_voice_action_refresh_skill_icons refresh_skill_icons def refresh_skill_icons MPVoiceAction.ensure_voice_skill mp_voice_action_refresh_skill_icons return if !@buff_icons || !@buff_icons.bitmap return if !@skills voice_skill = MPVoiceAction.voice_skill return if voice_skill.nil? return if @skills.any? { |s| MPVoiceAction.voice_skill?(s) } old_bitmap = @buff_icons.bitmap begin @mp_special_skill_font_name = old_bitmap.font.name rescue @mp_special_skill_font_name = nil end new_bitmap = Bitmap.new(old_bitmap.width, old_bitmap.height + 27) new_bitmap.blt(0, 0, old_bitmap, old_bitmap.rect) old_bitmap.dispose rescue nil @buff_icons.bitmap = new_bitmap begin @buff_icons.bitmap.font.name = MPVoiceAction.hud_font_name @buff_icons.bitmap.font.size = MPVoiceAction.skill_font_size @buff_icons.bitmap.font.outline = false @buff_icons.bitmap.font.color = Color.new(224, 128, 64) rescue end @skills = @skills.dup @skills << voice_skill specials = @skills.select { |skill| mp_special_skill?(skill) }.sort_by { |skill| mp_special_skill_order(skill) } regulars = @skills.reject { |skill| mp_special_skill?(skill) } @skills = specials + regulars @cursor_real_index = 0 if @cursor_real_index && @cursor_real_index >= @skills.length mp_special_redraw_skill_icons! begin @skill_icons.bitmap.font.size = 16 if @skill_icons && @skill_icons.bitmap rescue end end alias mp_voice_action_refresh_skill_info refresh_skill_info def refresh_skill_info if mp_voice_action_voice_skill_selected? @reuqest_refresh_info = false @reuqest_refresh_info_delay = 0 @buff_info.bitmap.dispose if @buff_info.bitmap @buff_info.bitmap = Bitmap.new(299,312) begin @buff_info.bitmap.font.name = MPVoiceAction.skill_info_font_name rescue end @buff_info.bitmap.font.size = MPVoiceAction.skill_info_font_size @buff_info.bitmap.font.outline = false draw_text_on_canvas(@buff_info, 13, 13, 'Голосовой чат') @buff_info.bitmap.font.size = MPVoiceAction.skill_font_size @buff_info.bitmap.font.outline = false desc = "Голосовой чат по нажатию для игроков на одной карте.\nВыбери эту строку и нажми любой слот навыка\n(A/S/D/F/Q/W/E/R/SP), чтобы назначить туда голос.\nОтдельная клавиша: #{MPVoiceAction.key_text}." draw_text_on_canvas(@buff_info, 13, 33, desc) return end mp_voice_action_refresh_skill_info end alias mp_voice_action_update_input update_input def update_input MPVoiceAction.ensure_voice_skill mp_voice_action_update_input end end module WebSocket class << self alias mp_voice_parse parse end def self.parse(msg) if msg.start_with?("voice:") parts = msg.split(":", 4) if parts.size >= 4 begin MPVoice.enqueue_rx(parts[1].to_i, parts[2].to_i, parts[3].to_s) rescue => e puts "[VOICE] parse failed: #{e.class}: #{e.message}" end end return end mp_voice_parse(msg) end class << self alias mp_voice_update update end def self.update mp_voice_update MPVoice.update end class << self alias mp_voice_disconnect disconnect end def self.disconnect begin MPVoice.stop_capture MPVoice.purge_pending_files MPVoice.bridge_shutdown MPVoiceHUD.dispose if defined?(MPVoiceHUD) rescue end mp_voice_disconnect end end if defined?(Scene_Map) class Scene_Map unless method_defined?(:mp_voice_terminate) alias mp_voice_terminate terminate end def terminate begin MPVoice.stop_capture MPVoice.bridge_shutdown MPVoiceHUD.dispose if defined?(MPVoiceHUD) rescue end mp_voice_terminate end end end