# encoding: UTF-8 # TabLocationStatus client 1.0.2 MOBILE SAFE # Adds explicit home/clan-home location labels to the multiplayer TAB overlay. # Server companion: TabLocationStatus 1.0.0 (unchanged). # # 1.0.1: # - removes the per-frame Scene_Map#update wrapper from 1.0.0; # - adds a re-entry guard around status sending; # - sends location only on map/scene transitions, ClanEstate entry and TAB refresh; # - avoids calling WebSocket.connected? from the location sender. # This prevents SystemStackError / "stack level too deep" caused by update/alias # chains in large ModScripts installations. module MPTabLocationStatus VERSION = '1.0.2-mobile-safe' class MapRef attr_reader :map_id, :label def initialize(map_id, label) @map_id = map_id.to_i @label = label.to_s end def to_i @map_id end def to_s "tabloc:#{@map_id}:#{@label}" end end def self.hex_encode(text) text.to_s.encode('UTF-8').unpack1('H*') rescue '' end def self.hex_decode(hex) raw = hex.to_s return '' unless raw =~ /\A[0-9A-Fa-f]*\z/ && raw.size.even? value = [raw].pack('H*').force_encoding('UTF-8') return '' unless value.valid_encoding? value.gsub(/[\r\n\t\0]/, ' ').strip[0, 32].to_s rescue '' end def self.clan_context return nil unless defined?(MPClanEstate) return nil unless MPClanEstate.respond_to?(:active?) && MPClanEstate.active? kind = MPClanEstate.respond_to?(:home_kind) ? MPClanEstate.home_kind.to_s : '' return nil unless kind == 'house' || kind == 'yard' clan_id = MPClanEstate.respond_to?(:local_clan_id) ? MPClanEstate.local_clan_id.to_i : 0 return nil if clan_id <= 0 clan_name = MPClanEstate.respond_to?(:local_clan_name) ? MPClanEstate.local_clan_name.to_s.strip : '' [kind == 'yard' ? 'clan_yard' : 'clan_house', clan_name] rescue nil end def self.private_context return nil unless defined?(NoerHomeEstate) return nil unless defined?($game_map) && $game_map return nil unless NoerHomeEstate.respond_to?(:home_kind_for_local) kind = NoerHomeEstate.home_kind_for_local($game_map.map_id).to_s return ['private_house', ''] if kind == 'house' return ['private_yard', ''] if kind == 'yard' nil rescue nil end def self.camp_context return nil unless defined?(LonaCraft::Bases) && defined?($game_map) && $game_map return nil if $game_map.respond_to?(:isOverMap) && $game_map.isOverMap rec = LonaCraft::Bases.record_for_map($game_map) return nil unless rec.is_a?(Hash) && rec['om'].is_a?(Array) name = LonaCraft::Bases.name_of(rec).to_s ['camp', name] rescue StandardError nil end def self.current_context clan_context || private_context || camp_context || ['none', ''] rescue ['none', ''] end # Do not call WebSocket.connected? here. This add-on can sit at the tail of a # very long alias chain, so reading the core connection flag directly is the # safest possible test and cannot recurse back into client wrappers. def self.connected_raw? return false unless defined?(WebSocket) !!WebSocket.instance_variable_get(:@connected) rescue false end def self.send_current(force=false) return false if @sending return false unless connected_raw? @sending = true kind, name = current_context key = "#{kind}|#{name}" return false if !force && key == @last_key WebSocket.send_line("tabloc_set:#{kind}:#{hex_encode(name)}") @last_key = key true rescue Exception => e # Do not allow this cosmetic TAB add-on to take the game down. The re-entry # guard is always cleared below. Logging is intentionally tiny. begin puts "[TABLOC] send failed #{e.class}: #{e.message}" rescue end false ensure @sending = false end def self.label(kind, name) case kind.to_s when 'camp' n = name.to_s.strip n.empty? ? 'Лагерь' : n when 'private_house' 'Частный дом' when 'private_yard' 'Огород' when 'clan_house' n = name.to_s.strip n.empty? ? 'Клановый дом' : "Клановый дом - #{n}" when 'clan_yard' n = name.to_s.strip n.empty? ? 'Клановый двор' : "Клановый двор - #{n}" else nil end end end if defined?(WebSocket) module WebSocket @mp_tab_locations ||= {} class << self unless method_defined?(:tabloc_entries_without_location_status) alias tabloc_entries_without_location_status mp_tab_entries if method_defined?(:mp_tab_entries) end unless method_defined?(:tabloc_request_who_without_location_status) alias tabloc_request_who_without_location_status mp_tab_request_who if method_defined?(:mp_tab_request_who) end end def self.tabloc_apply_status(msg) return false unless msg.to_s.start_with?('tabloc_status:') parts = msg.to_s.split(':', 4) return true if parts.size < 3 pid = parts[1].to_i return true if pid <= 0 kind = parts[2].to_s name = MPTabLocationStatus.hex_decode(parts[3].to_s) @mp_tab_locations ||= {} if kind == 'none' || kind.empty? @mp_tab_locations.delete(pid) else @mp_tab_locations[pid] = {:kind => kind, :name => name} end true rescue true end # parse is installed below with prepend. This avoids aliasing an already # prepended ClanEstate WebSocket parser on mobile during ModScripts loading. def self.mp_tab_location_label(pid) @mp_tab_locations ||= {} data = @mp_tab_locations[pid.to_i] return nil unless data.is_a?(Hash) MPTabLocationStatus.label(data[:kind], data[:name]) rescue nil end if respond_to?(:tabloc_entries_without_location_status) def self.mp_tab_entries(current_map_id) total, entries = tabloc_entries_without_location_status(current_map_id) wrapped = (entries || []).map do |row| pid, nick, map, same = row label = mp_tab_location_label(pid) if label && !label.to_s.empty? [pid, nick, MPTabLocationStatus::MapRef.new(map, label), same] else [pid, nick, map, same] end end [total, wrapped] rescue tabloc_entries_without_location_status(current_map_id) end end if respond_to?(:tabloc_request_who_without_location_status) def self.mp_tab_request_who # Refresh our own location immediately before the TAB list is requested. MPTabLocationStatus.send_current(false) tabloc_request_who_without_location_status send_line('tabloc_who') if respond_to?(:send_line) && MPTabLocationStatus.connected_raw? rescue end end end end module MPTabLocationStatusWebSocketPatch def parse(msg) return if tabloc_apply_status(msg) super(msg) end end if defined?(WebSocket) begin sc = WebSocket.singleton_class sc.prepend(MPTabLocationStatusWebSocketPatch) unless sc.ancestors.include?(MPTabLocationStatusWebSocketPatch) rescue => e puts "[TABLOC] parser prepend failed #{e.class}: #{e.message}" rescue nil end end if defined?(MPMapInfo) module MPMapInfo class << self unless method_defined?(:tabloc_display_name_without_location_status) alias tabloc_display_name_without_location_status display_name end def display_name(map_id) if map_id.is_a?(MPTabLocationStatus::MapRef) return map_id.label.to_s end tabloc_display_name_without_location_status(map_id) end end end end # Event-driven refresh only. 1.0.0 wrapped Scene_Map#update every frame; in a # heavily patched client that can complete an alias cycle and overflow the Ruby # stack. Map setup + Scene_Map start + TAB refresh is sufficient for this label. if defined?(Game_Map) class Game_Map unless method_defined?(:tabloc_setup_without_location_status) alias tabloc_setup_without_location_status setup def setup(map_id) result = tabloc_setup_without_location_status(map_id) MPTabLocationStatus.send_current(true) result end end end end if defined?(Scene_Map) class Scene_Map < Scene_Base unless method_defined?(:tabloc_start_without_location_status) alias tabloc_start_without_location_status start def start result = tabloc_start_without_location_status MPTabLocationStatus.send_current(true) result end end end end # ClanEstate can enter its virtual house/yard after ordinary map setup. Send a # fresh marker right after the existing enter method completes. if defined?(MPClanEstate) module MPClanEstate class << self if method_defined?(:enter) && !method_defined?(:tabloc_enter_without_location_status) alias tabloc_enter_without_location_status enter def enter(value) result = tabloc_enter_without_location_status(value) MPTabLocationStatus.send_current(true) result end end end end end