Files
LonaRPG-Online-Games/ModScripts/100005_DialogControlNilGuard.rb
T

97 lines
3.2 KiB
Ruby
Raw Normal View History

2026-10-29 21:59:06 +04:00
# encoding: UTF-8
#==============================================================================
# Dialog_Control_System NIL guard
#------------------------------------------------------------------------------
# Fixes a crash in Dialog_Control_System#process_escape_character when a dialog
# escape command tries to use a map/event/portrait object that is already nil.
# The most common case in this mod pack is a portrait/camera escape from
# NoerHome text being processed while its target event is missing after a map
# transition/load race.
#==============================================================================
module MPDialogControlNilGuard
LOG_FILE = "dialog_escape_nil_guard.log"
def self.safe_dialog_nil_error?(error)
return false unless error.is_a?(NoMethodError)
msg = error.message.to_s
return false unless msg.include?("for nil:NilClass")
return false unless msg.include?("`id'") || msg.include?("`stat'")
bt = error.backtrace || []
bt.any? do |line|
s = line.to_s
s.include?("121_Dialog_Control_System.rb") ||
s.include?("09_Lona_Portrait.rb") ||
s.include?("Modules/Mood.rb")
end
rescue
false
end
def self.log_skip(error, owner, args)
File.open(LOG_FILE, "ab") do |f|
f.write("=== #{Time.now} ===\r\n")
f.write("#{error.class}: #{error.message}\r\n")
begin
f.write("window=#{owner.class}\r\n")
rescue
end
begin
f.write("escape_code=#{args[0].inspect}\r\n")
rescue
end
begin
f.write("map_id=#{$game_map.map_id}\r\n") if defined?($game_map) && $game_map
rescue
end
begin
f.write("player_xy=#{$game_player.x},#{$game_player.y}\r\n") if defined?($game_player) && $game_player
rescue
end
begin
(error.backtrace || []).first(12).each { |line| f.write("#{line}\r\n") }
rescue
end
f.write("\r\n")
end
rescue
end
end
if defined?(Window_Message)
class Window_Message < Window_Base
if method_defined?(:process_escape_character) && !method_defined?(:mp_dialog_nil_guard_process_escape_character__orig)
alias mp_dialog_nil_guard_process_escape_character__orig process_escape_character
def process_escape_character(*args)
mp_dialog_nil_guard_process_escape_character__orig(*args)
rescue NoMethodError => e
if defined?(MPDialogControlNilGuard) && MPDialogControlNilGuard.safe_dialog_nil_error?(e)
MPDialogControlNilGuard.log_skip(e, self, args)
return nil
end
raise
end
end
end
end
if defined?(Window_Base)
class Window_Base < Window
if method_defined?(:process_escape_character) && !method_defined?(:mp_dialog_nil_guard_base_process_escape_character__orig)
alias mp_dialog_nil_guard_base_process_escape_character__orig process_escape_character
def process_escape_character(*args)
mp_dialog_nil_guard_base_process_escape_character__orig(*args)
rescue NoMethodError => e
if defined?(MPDialogControlNilGuard) && MPDialogControlNilGuard.safe_dialog_nil_error?(e)
MPDialogControlNilGuard.log_skip(e, self, args)
return nil
end
raise
end
end
end
end