469 lines
13 KiB
Ruby
469 lines
13 KiB
Ruby
# encoding: UTF-8
|
|
# Цвет текста над ником:
|
|
# global -> золотой, видят все, кому сервер прислал сообщение
|
|
# clan -> зелёный, видят только члены клана
|
|
# system -> не отображается
|
|
#
|
|
# Текст находится над ником (и над иконкой паузы/чата, если она уже там).
|
|
|
|
module MPChatOverhead
|
|
VERSION = "1.0.0"
|
|
MAX_CHARS = 200
|
|
MAX_WIDTH = 280
|
|
MAX_LINES = 3
|
|
HOLD_SEC = 7.2
|
|
FADE_SEC = 0.8
|
|
FRESH_MS = 8000
|
|
SKEW_MS = 5000
|
|
GAP = 2
|
|
|
|
@lines = {}
|
|
@seen = {}
|
|
@seen_order = []
|
|
@parser_installed = false
|
|
@measure_bitmap = nil
|
|
|
|
def self.parser_installed?
|
|
@parser_installed
|
|
end
|
|
|
|
def self.install_parser!
|
|
return true if @parser_installed
|
|
return false unless defined?(WebSocket)
|
|
sc = WebSocket.singleton_class
|
|
unless sc.ancestors.include?(MPChatOverheadWebSocketPatch)
|
|
sc.prepend(MPChatOverheadWebSocketPatch)
|
|
end
|
|
@parser_installed = true
|
|
true
|
|
rescue => e
|
|
puts "[ChatOverhead] parser prepend failed #{e.class}: #{e.message}" rescue nil
|
|
false
|
|
end
|
|
|
|
def self.normalize_channel(channel)
|
|
if defined?(MPChatChannels) && MPChatChannels.respond_to?(:normalize)
|
|
MPChatChannels.normalize(channel)
|
|
else
|
|
ch = channel.to_s.strip.downcase
|
|
return "clan" if ch == "clan" || ch == "клан"
|
|
return "system" if ch == "system" || ch == "система"
|
|
"global"
|
|
end
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.color_for(channel)
|
|
if defined?(MPChatChannels) && MPChatChannels.respond_to?(:color)
|
|
color = MPChatChannels.color(channel)
|
|
return color if color.is_a?(Color)
|
|
end
|
|
normalize_channel(channel) == "clan" ? Color.new(120, 230, 150) : Color.new(255, 213, 128)
|
|
rescue
|
|
Color.new(255, 213, 128)
|
|
end
|
|
|
|
def self.font_name
|
|
FONT_MOD::POPUP
|
|
rescue
|
|
"Digital Pixel"
|
|
end
|
|
|
|
def self.font_size
|
|
size = FONT_MOD::POPUP_S.to_i
|
|
size > 0 ? size : 20
|
|
rescue
|
|
20
|
|
end
|
|
|
|
def self.system_message?(channel, pid, nick)
|
|
return true if normalize_channel(channel) == "system"
|
|
n = nick.to_s.strip.upcase
|
|
return true if pid.to_i <= 0
|
|
return true if n == "SERVER" || n == "[SERVER]" || n == "SYSTEM" || n == "СИСТЕМА"
|
|
false
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.clean_text(text)
|
|
t = text.to_s.dup
|
|
begin
|
|
t.force_encoding("UTF-8")
|
|
t = t.scrub("") if t.respond_to?(:scrub) && !t.valid_encoding?
|
|
rescue
|
|
end
|
|
t = t.gsub(/\r\n?/, " ").gsub("\n", " ").gsub("\0", "").strip
|
|
t = t[0, MAX_CHARS] if t.length > MAX_CHARS
|
|
t
|
|
rescue
|
|
""
|
|
end
|
|
|
|
def self.fresh?(ts_ms)
|
|
ts = ts_ms.to_i
|
|
now = (Time.now.to_f * 1000.0).to_i
|
|
if ts >= 1_000_000_000_000
|
|
age = now - ts
|
|
elsif ts >= 1_000_000_000
|
|
age = now - (ts * 1000)
|
|
else
|
|
return false
|
|
end
|
|
age >= -SKEW_MS && age <= FRESH_MS
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.seen?(key)
|
|
return true if @seen[key]
|
|
@seen[key] = true
|
|
@seen_order << key
|
|
if @seen_order.size > 400
|
|
old = @seen_order.shift(@seen_order.size - 400)
|
|
old.each { |k| @seen.delete(k) }
|
|
end
|
|
false
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.capture(raw)
|
|
msg = raw.to_s
|
|
if msg.start_with?("chat_msg2:")
|
|
parts = msg.split(":", 7)
|
|
return if parts.size < 7
|
|
channel = normalize_channel(parts[1])
|
|
msg_id = parts[2].to_i
|
|
ts_ms = parts[3].to_i
|
|
pid = parts[4].to_i
|
|
nick = unescape_nick(parts[5])
|
|
text = parts[6].to_s
|
|
show(channel, msg_id, ts_ms, pid, nick, text)
|
|
return
|
|
end
|
|
|
|
return unless msg.start_with?("chat_msg:")
|
|
parts = msg.split(":", 6)
|
|
return if parts.size < 6
|
|
msg_id = parts[1].to_i
|
|
ts_ms = parts[2].to_i
|
|
pid = parts[3].to_i
|
|
nick = unescape_nick(parts[4])
|
|
text = parts[5].to_s
|
|
channel = legacy_channel(pid, nick)
|
|
show(channel, msg_id, ts_ms, pid, nick, text)
|
|
rescue => e
|
|
puts "[ChatOverhead] capture error #{e.class}: #{e.message}" rescue nil
|
|
end
|
|
|
|
def self.unescape_nick(nick)
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:unesc)
|
|
WebSocket.unesc(nick.to_s)
|
|
else
|
|
nick.to_s
|
|
end
|
|
rescue
|
|
nick.to_s
|
|
end
|
|
|
|
def self.legacy_channel(pid, nick)
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:legacy_chat_channel)
|
|
WebSocket.legacy_chat_channel(pid, nick)
|
|
else
|
|
pid.to_i < 0 ? "system" : "global"
|
|
end
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.show(channel, msg_id, ts_ms, pid, nick, text)
|
|
ch = normalize_channel(channel)
|
|
return false unless ch == "global" || ch == "clan"
|
|
return false if system_message?(ch, pid, nick)
|
|
return false unless fresh?(ts_ms)
|
|
body = clean_text(text)
|
|
return false if body.empty?
|
|
|
|
id = msg_id.to_i
|
|
key = id > 0 ? "#{ch}:#{id}" : "#{ch}:#{pid.to_i}:#{ts_ms.to_i}:#{body}"
|
|
return false if seen?(key)
|
|
|
|
@lines[pid.to_i] = {
|
|
:channel => ch,
|
|
:text => body,
|
|
:key => key,
|
|
:born => Time.now.to_f
|
|
}
|
|
true
|
|
rescue => e
|
|
puts "[ChatOverhead] show error #{e.class}: #{e.message}" rescue nil
|
|
false
|
|
end
|
|
|
|
def self.prune!
|
|
limit = HOLD_SEC + FADE_SEC
|
|
@lines.delete_if { |_pid, line| age_of(line) >= limit }
|
|
rescue
|
|
@lines ||= {}
|
|
end
|
|
|
|
def self.line_for(pid)
|
|
prune!
|
|
id = pid.to_i
|
|
return nil if id <= 0
|
|
@lines[id]
|
|
rescue
|
|
nil
|
|
end
|
|
|
|
def self.age_of(line)
|
|
Time.now.to_f - line[:born].to_f
|
|
rescue
|
|
HOLD_SEC + FADE_SEC
|
|
end
|
|
|
|
def self.opacity_for(line)
|
|
age = age_of(line)
|
|
return 0 if age >= (HOLD_SEC + FADE_SEC)
|
|
return 255 if age <= HOLD_SEC
|
|
remain = (HOLD_SEC + FADE_SEC) - age
|
|
value = (255.0 * remain / FADE_SEC).to_i
|
|
value < 0 ? 0 : (value > 255 ? 255 : value)
|
|
rescue
|
|
255
|
|
end
|
|
|
|
def self.measure_bitmap
|
|
bmp = @measure_bitmap
|
|
if bmp.nil? || (bmp.respond_to?(:disposed?) && bmp.disposed?)
|
|
bmp = Bitmap.new(8, 8)
|
|
@measure_bitmap = bmp
|
|
end
|
|
bmp.font.name = font_name
|
|
bmp.font.size = font_size
|
|
bmp.font.bold = false
|
|
bmp.font.outline = false
|
|
bmp.font.shadow = true
|
|
bmp
|
|
end
|
|
|
|
def self.wrap_lines(text)
|
|
bmp = measure_bitmap
|
|
words = text.to_s.split(/\s+/)
|
|
lines = []
|
|
current = ""
|
|
words.each do |word|
|
|
if bmp.text_size(word).width > MAX_WIDTH
|
|
lines << current unless current.empty?
|
|
current = ""
|
|
chunk = ""
|
|
word.each_char do |ch|
|
|
trial = chunk + ch
|
|
if !chunk.empty? && bmp.text_size(trial).width > MAX_WIDTH
|
|
lines << chunk
|
|
chunk = ch
|
|
else
|
|
chunk = trial
|
|
end
|
|
end
|
|
current = chunk
|
|
next
|
|
end
|
|
trial = current.empty? ? word : "#{current} #{word}"
|
|
if current.empty? || bmp.text_size(trial).width <= MAX_WIDTH
|
|
current = trial
|
|
else
|
|
lines << current
|
|
current = word
|
|
end
|
|
end
|
|
lines << current unless current.empty?
|
|
lines = [text.to_s] if lines.empty?
|
|
|
|
if lines.size > MAX_LINES
|
|
lines = lines[0, MAX_LINES]
|
|
last = lines[-1].to_s
|
|
ellipsis = "..."
|
|
while !last.empty? && bmp.text_size(last + ellipsis).width > MAX_WIDTH
|
|
last = last[0, last.length - 1].to_s
|
|
end
|
|
lines[-1] = last + ellipsis
|
|
end
|
|
lines
|
|
rescue
|
|
[text.to_s[0, MAX_CHARS]]
|
|
end
|
|
|
|
def self.build_bitmap(channel, text)
|
|
lines = wrap_lines(text)
|
|
bmp_measure = measure_bitmap
|
|
line_h = font_size + 2
|
|
width = 1
|
|
lines.each do |line|
|
|
w = bmp_measure.text_size(line.to_s).width.to_i
|
|
width = w if w > width
|
|
end
|
|
width += 4
|
|
width = MAX_WIDTH + 4 if width > MAX_WIDTH + 4
|
|
width = 1 if width < 1
|
|
height = line_h * lines.size
|
|
height = line_h if height < 1
|
|
|
|
bitmap = Bitmap.new(width, height)
|
|
bitmap.font.name = font_name
|
|
bitmap.font.size = font_size
|
|
bitmap.font.bold = false
|
|
bitmap.font.outline = false
|
|
bitmap.font.shadow = true
|
|
bitmap.font.color = color_for(channel)
|
|
lines.each_with_index do |line, index|
|
|
bitmap.draw_text(0, index * line_h, width, line_h, line.to_s, 1)
|
|
end
|
|
bitmap
|
|
end
|
|
end
|
|
|
|
# Installed from the first character sprite, after later ModScripts have
|
|
# already wrapped WebSocket.parse. Outermost prepend always calls super,
|
|
# so both chat_msg2 and legacy chat_msg are seen.
|
|
module MPChatOverheadWebSocketPatch
|
|
def parse(msg)
|
|
MPChatOverhead.capture(msg) if defined?(MPChatOverhead)
|
|
super(msg)
|
|
end
|
|
end
|
|
|
|
class Sprite_Character < Sprite_Base
|
|
unless method_defined?(:mp_chat_overhead_init_without)
|
|
alias mp_chat_overhead_init_without initialize
|
|
end
|
|
|
|
def initialize(*args, &block)
|
|
MPChatOverhead.install_parser! if defined?(MPChatOverhead)
|
|
mp_chat_overhead_init_without(*args, &block)
|
|
end
|
|
|
|
unless method_defined?(:mp_chat_overhead_update_without)
|
|
alias mp_chat_overhead_update_without update
|
|
end
|
|
|
|
def update(*args, &block)
|
|
MPChatOverhead.install_parser! if defined?(MPChatOverhead) && !MPChatOverhead.parser_installed?
|
|
mp_chat_overhead_update_without(*args, &block)
|
|
mp_chat_overhead_update
|
|
end
|
|
|
|
unless method_defined?(:mp_chat_overhead_dispose_without)
|
|
alias mp_chat_overhead_dispose_without dispose
|
|
end
|
|
|
|
def dispose(*args, &block)
|
|
mp_chat_overhead_dispose
|
|
mp_chat_overhead_dispose_without(*args, &block)
|
|
end
|
|
|
|
def mp_chat_overhead_pid
|
|
return 0 unless @character
|
|
if @character.is_a?(Game_Player)
|
|
return 0 unless defined?(WebSocket) && WebSocket.respond_to?(:my_id) && WebSocket.my_id
|
|
return WebSocket.my_id.to_i
|
|
end
|
|
if @character.instance_variable_defined?(:@net_player_id)
|
|
return @character.instance_variable_get(:@net_player_id).to_i
|
|
end
|
|
0
|
|
rescue
|
|
0
|
|
end
|
|
|
|
def mp_chat_overhead_dispose
|
|
if @mp_chat_overhead_sprite
|
|
begin
|
|
@mp_chat_overhead_sprite.bitmap.dispose if @mp_chat_overhead_sprite.bitmap && !@mp_chat_overhead_sprite.bitmap.disposed?
|
|
rescue
|
|
end
|
|
begin
|
|
@mp_chat_overhead_sprite.dispose unless @mp_chat_overhead_sprite.disposed?
|
|
rescue
|
|
end
|
|
end
|
|
@mp_chat_overhead_sprite = nil
|
|
@mp_chat_overhead_key = nil
|
|
rescue
|
|
@mp_chat_overhead_sprite = nil
|
|
@mp_chat_overhead_key = nil
|
|
end
|
|
|
|
def mp_chat_overhead_nick_sprite
|
|
sprite = @character.is_a?(Game_Player) ? @nickname_sprite : @mp_nick_sprite
|
|
return nil unless sprite
|
|
return nil if sprite.respond_to?(:disposed?) && sprite.disposed?
|
|
return nil unless sprite.bitmap && !sprite.bitmap.disposed?
|
|
return nil unless sprite.visible
|
|
sprite
|
|
rescue
|
|
nil
|
|
end
|
|
|
|
def mp_chat_overhead_anchor_y
|
|
anchor = self.y - self.height
|
|
nick = mp_chat_overhead_nick_sprite
|
|
if nick
|
|
nick_top = nick.y - nick.bitmap.height
|
|
anchor = nick_top if nick_top < anchor
|
|
end
|
|
icon = @mp_presence_status_sprite
|
|
if icon && !(icon.respond_to?(:disposed?) && icon.disposed?) && icon.bitmap && !icon.bitmap.disposed? && icon.visible
|
|
gap = defined?(MPPlayerPresenceVersionGate) ? MPPlayerPresenceVersionGate::STATUS_GAP : 2
|
|
anchor -= gap + icon.bitmap.height
|
|
end
|
|
anchor
|
|
rescue
|
|
self.y - self.height
|
|
end
|
|
|
|
def mp_chat_overhead_update
|
|
pid = mp_chat_overhead_pid
|
|
line = (pid > 0 && defined?(MPChatOverhead)) ? MPChatOverhead.line_for(pid) : nil
|
|
if line.nil?
|
|
mp_chat_overhead_dispose if @mp_chat_overhead_sprite
|
|
return
|
|
end
|
|
unless self.visible
|
|
@mp_chat_overhead_sprite.visible = false if @mp_chat_overhead_sprite
|
|
return
|
|
end
|
|
|
|
if @mp_chat_overhead_sprite.nil? || (@mp_chat_overhead_sprite.respond_to?(:disposed?) && @mp_chat_overhead_sprite.disposed?)
|
|
@mp_chat_overhead_sprite = Sprite.new(self.viewport)
|
|
@mp_chat_overhead_key = nil
|
|
end
|
|
|
|
if @mp_chat_overhead_key != line[:key]
|
|
bitmap = MPChatOverhead.build_bitmap(line[:channel], line[:text])
|
|
old = @mp_chat_overhead_sprite.bitmap
|
|
@mp_chat_overhead_sprite.bitmap = bitmap
|
|
@mp_chat_overhead_sprite.ox = bitmap.width / 2
|
|
@mp_chat_overhead_sprite.oy = bitmap.height
|
|
@mp_chat_overhead_key = line[:key]
|
|
begin
|
|
old.dispose if old && !old.disposed?
|
|
rescue
|
|
end
|
|
end
|
|
|
|
sprite = @mp_chat_overhead_sprite
|
|
sprite.x = self.x
|
|
sprite.y = mp_chat_overhead_anchor_y - MPChatOverhead::GAP
|
|
sprite.z = self.z + 40
|
|
sprite.opacity = MPChatOverhead.opacity_for(line)
|
|
sprite.visible = self.visible && sprite.opacity > 0
|
|
rescue
|
|
mp_chat_overhead_dispose
|
|
end
|
|
end
|
|
|
|
puts "[ChatOverhead] loaded v#{MPChatOverhead::VERSION}; global=gold clan=green above-nick" rescue nil
|