188 lines
5.8 KiB
Ruby
188 lines
5.8 KiB
Ruby
# encoding: UTF-8
|
||
# Replace player speaker label ("Lona"/"\\u041b\\u043e\\u043d\\u0430") with nickname in dialogs.
|
||
|
||
module MPDialogNickname
|
||
STATIC_LABELS = ["Lona", "\u041b\u043e\u043d\u0430"]
|
||
|
||
def self.sanitize(text)
|
||
text.to_s.gsub(/\uFEFF/, "").gsub(/\0/, "").gsub(/[\r\n\t]/, " ").strip
|
||
end
|
||
|
||
def self.player_nickname
|
||
nick = ""
|
||
|
||
if defined?(WebSocket) && WebSocket.respond_to?(:local_nickname)
|
||
begin
|
||
nick = WebSocket.local_nickname.to_s
|
||
rescue StandardError
|
||
nick = ""
|
||
end
|
||
end
|
||
|
||
if nick.strip.empty? && defined?(NicknameInput) && NicknameInput.respond_to?(:nickname)
|
||
begin
|
||
nick = NicknameInput.nickname.to_s
|
||
rescue StandardError
|
||
nick = ""
|
||
end
|
||
end
|
||
|
||
if nick.strip.empty? && defined?($game_player) && $game_player && $game_player.respond_to?(:actor)
|
||
begin
|
||
nick = $game_player.actor.name.to_s
|
||
rescue StandardError
|
||
nick = ""
|
||
end
|
||
end
|
||
|
||
sanitize(nick)
|
||
end
|
||
|
||
def self.player_labels
|
||
labels = STATIC_LABELS.dup
|
||
|
||
begin
|
||
if defined?($game_text) && $game_text
|
||
labels << $game_text["menu:main_stats/name"].to_s
|
||
end
|
||
rescue StandardError
|
||
end
|
||
|
||
begin
|
||
if defined?($game_player) && $game_player && $game_player.respond_to?(:actor)
|
||
labels << $game_player.actor.name.to_s
|
||
end
|
||
rescue StandardError
|
||
end
|
||
|
||
labels.map { |x| sanitize(x) }.reject { |x| x.empty? }.uniq
|
||
end
|
||
|
||
def self.player_label?(text)
|
||
target = sanitize(text)
|
||
return false if target.empty?
|
||
player_labels.any? { |label| target.casecmp(label).zero? }
|
||
end
|
||
|
||
def self.replace_label(text)
|
||
original = text.to_s
|
||
return original if original.empty?
|
||
return original unless player_label?(original)
|
||
nick = player_nickname
|
||
nick.empty? ? original : nick
|
||
rescue StandardError
|
||
text.to_s
|
||
end
|
||
|
||
# Some text files use "Name: line" converted to "Name\\nline".
|
||
def self.replace_leading_name_line(text)
|
||
out = text.to_s
|
||
return out if out.empty?
|
||
nick = player_nickname
|
||
return out if nick.empty?
|
||
|
||
player_labels.each do |label|
|
||
next if label.empty?
|
||
esc = Regexp.escape(label)
|
||
out = out.gsub(/\A[ \t]*#{esc}[ \t]*\n/u, "#{nick}\n")
|
||
out = out.gsub(/\f[ \t]*#{esc}[ \t]*\n/u, "\f#{nick}\n")
|
||
end
|
||
out
|
||
rescue StandardError
|
||
text.to_s
|
||
end
|
||
|
||
# Replace the first token on each line/page if it is player label,
|
||
# preserving leading escape formatting tags.
|
||
def self.replace_line_start_label_token(text)
|
||
out = text.to_s
|
||
return out if out.empty?
|
||
nick = player_nickname
|
||
return out if nick.empty?
|
||
|
||
out.gsub(/(^|[\n\f])((?:\e[A-Za-z]+(?:\[[^\]]*\])?)*)[ \t]*([^\s::]+)(?=(?:[ \t]|[::]|$))/u) do
|
||
line_sep = Regexp.last_match(1)
|
||
prefix = Regexp.last_match(2)
|
||
token = Regexp.last_match(3)
|
||
player_label?(token) ? "#{line_sep}#{prefix}#{nick}" : Regexp.last_match(0)
|
||
end
|
||
rescue StandardError
|
||
text.to_s
|
||
end
|
||
end
|
||
|
||
if defined?(Window_NameMessage)
|
||
class Window_NameMessage < Window_Base
|
||
unless method_defined?(:mp_dialog_nick_start_without_patch)
|
||
alias mp_dialog_nick_start_without_patch start
|
||
end
|
||
|
||
def start(text, x_position)
|
||
mp_dialog_nick_start_without_patch(MPDialogNickname.replace_label(text), x_position)
|
||
end
|
||
end
|
||
end
|
||
|
||
if defined?(Window_Message)
|
||
class Window_Message < Window_Base
|
||
if method_defined?(:namewindow) && !method_defined?(:mp_dialog_nick_namewindow_without_patch)
|
||
alias mp_dialog_nick_namewindow_without_patch namewindow
|
||
def namewindow(text, position)
|
||
mp_dialog_nick_namewindow_without_patch(MPDialogNickname.replace_label(text), position)
|
||
end
|
||
end
|
||
|
||
if method_defined?(:draw_board_title) && !method_defined?(:mp_dialog_nick_draw_board_title_without_patch)
|
||
alias mp_dialog_nick_draw_board_title_without_patch draw_board_title
|
||
def draw_board_title(title_text, pos)
|
||
mp_dialog_nick_draw_board_title_without_patch(MPDialogNickname.replace_label(title_text), pos)
|
||
end
|
||
end
|
||
|
||
if method_defined?(:convert_escape_characters) && !method_defined?(:mp_dialog_nick_convert_escape_without_patch)
|
||
alias mp_dialog_nick_convert_escape_without_patch convert_escape_characters
|
||
def convert_escape_characters(text)
|
||
converted = mp_dialog_nick_convert_escape_without_patch(text)
|
||
converted = MPDialogNickname.replace_leading_name_line(converted)
|
||
MPDialogNickname.replace_line_start_label_token(converted)
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
if defined?(Window_Base)
|
||
class Window_Base < Window
|
||
if method_defined?(:actor_name) && !method_defined?(:mp_dialog_nick_actor_name_without_patch)
|
||
alias mp_dialog_nick_actor_name_without_patch actor_name
|
||
def actor_name(n)
|
||
name = mp_dialog_nick_actor_name_without_patch(n)
|
||
begin
|
||
actor = (n >= 1 ? $game_actors[n] : nil)
|
||
if defined?($game_player) && $game_player && actor && actor == $game_player.actor
|
||
nick = MPDialogNickname.player_nickname
|
||
return nick unless nick.empty?
|
||
end
|
||
rescue StandardError
|
||
end
|
||
MPDialogNickname.replace_label(name)
|
||
end
|
||
end
|
||
|
||
if method_defined?(:party_member_name) && !method_defined?(:mp_dialog_nick_party_member_name_without_patch)
|
||
alias mp_dialog_nick_party_member_name_without_patch party_member_name
|
||
def party_member_name(n)
|
||
name = mp_dialog_nick_party_member_name_without_patch(n)
|
||
begin
|
||
actor = (n >= 1 ? $game_party.members[n - 1] : nil)
|
||
if defined?($game_player) && $game_player && actor && actor == $game_player.actor
|
||
nick = MPDialogNickname.player_nickname
|
||
return nick unless nick.empty?
|
||
end
|
||
rescue StandardError
|
||
end
|
||
MPDialogNickname.replace_label(name)
|
||
end
|
||
end
|
||
end
|
||
end
|