Files
LonaRPG-Online-Games/ModScripts/multiplayer/91_fix_nil.rb
T

609 lines
21 KiB
Ruby
Raw Normal View History

2026-10-29 21:59:06 +04:00
# encoding: UTF-8
#==============================================================================
# Multiplayer NIL-guards (merged fixes)
#------------------------------------------------------------------------------
# Цель: не трогать Scripts.zip, а мягко защитить движок от nil в редких состояниях
# (синхронизация/удаление remote-объектов, скриптовые move route команды и т.п.)
#==============================================================================
#--------------------------------------------------------------------------
# 1) Battle_System#update_stun : @stun_count может стать nil при редких гонках
#--------------------------------------------------------------------------
if defined?(Battle_System)
module Battle_System
unless instance_methods(false).include?(:mp__update_stun__orig)
alias mp__update_stun__orig update_stun
def update_stun(*args)
if @action_state == :stun && (@stun_count.nil? || !@stun_count.is_a?(Numeric))
# Если счётчик стана потерян — безопасно снимаем стан
begin
remove_stun_effect
rescue
begin
set_action_state(:none, true)
rescue
@action_state = :none
end
end
return
end
mp__update_stun__orig(*args)
end
end
unless instance_methods(false).include?(:mp__over_energized__orig)
alias mp__over_energized__orig over_energized?
def over_energized?(*args)
return false if @skill.nil?
mp__over_energized__orig(*args)
rescue NoMethodError => e
return false if e.message.include?("NilClass")
raise
end
end
unless instance_methods(false).include?(:mp__update_energizing__orig)
alias mp__update_energizing__orig update_energizing
def update_energizing(*args)
return if @skill.nil?
@holding_count = @holding_count.to_i
mp__update_energizing__orig(*args)
rescue NoMethodError => e
if e.message.include?("NilClass")
begin
reset_skill(true) if respond_to?(:reset_skill)
rescue
begin
set_action_state(:none, true)
rescue
@action_state = :none
end
end
return
end
raise
end
end
end
end
#--------------------------------------------------------------------------
# Cache CHS rendering : remote/dynamic NPC can briefly have use_chs?=true
# before valid chs_type/parts are fully prepared. Fall back to the raw
# character sheet instead of crashing in create_chs_character_bitmap.
#--------------------------------------------------------------------------
if defined?(Cache)
module Cache
class << self
unless method_defined?(:mp__chs_bitmap_fallback)
def mp__chs_bitmap_fallback(character)
raw_name = ""
begin
raw_name = character.instance_variable_get(:@character_name).to_s
rescue
raw_name = ""
end
if raw_name.nil? || raw_name == ""
begin
raw_name = character.instance_variable_get(:@manual_character_name).to_s
rescue
raw_name = ""
end
end
if !raw_name.nil? && raw_name != ""
begin
return Cache.character(raw_name)
rescue
end
end
Bitmap.new(384, 256)
end
end
unless method_defined?(:mp__create_chs_character_bitmap__orig)
alias mp__create_chs_character_bitmap__orig create_chs_character_bitmap
def create_chs_character_bitmap(character)
chs_data = nil
begin
key = character.chs_type
chs_data = $chs_data[key] if defined?($chs_data) && !$chs_data.nil? && !key.nil?
rescue
chs_data = nil
end
return mp__chs_bitmap_fallback(character) if chs_data.nil? || !chs_data.respond_to?(:parts)
parts = nil
begin
parts = chs_data.parts[character.charset_index]
rescue
parts = nil
end
return mp__chs_bitmap_fallback(character) unless parts.respond_to?(:each)
mp__create_chs_character_bitmap__orig(character)
rescue NoMethodError => e
return mp__chs_bitmap_fallback(character) if e.message.include?("parts") || e.message.include?("NilClass")
raise
end
end
unless method_defined?(:mp__chs_character__orig)
alias mp__chs_character__orig chs_character
def chs_character(character, force_update=false)
mp__chs_character__orig(character, force_update)
rescue NoMethodError => e
return mp__chs_bitmap_fallback(character) if e.message.include?("parts") || e.message.include?("NilClass")
raise
end
end
end
end
end
#--------------------------------------------------------------------------
# 1b) Sex#sex_receiver? : @chs_configuration может быть nil у remote/временных объектов
#--------------------------------------------------------------------------
if defined?(Sex)
module Sex
unless instance_methods(false).include?(:mp__sex_receiver__orig)
alias mp__sex_receiver__orig sex_receiver?
def sex_receiver?(*args)
cfg = (@chs_configuration rescue nil)
return false if cfg.nil?
return false unless cfg.respond_to?(:[])
cfg["sex_position"] == 0
rescue NoMethodError => e
return false if e.message.include?("NilClass")
raise
end
end
end
end
#--------------------------------------------------------------------------
# 2) Game_Event#move_npc_movement : @npc может быть nil (event без NPC данных)
#--------------------------------------------------------------------------
if defined?(Game_Event)
class Game_Event
unless method_defined?(:mp__safe_page_for_self_move)
def mp__safe_page_for_self_move
page = nil
begin
page = @page
rescue
page = nil
end
page
end
def mp__reset_broken_self_move_type
manual = nil
begin
manual = @manual_move_type
rescue
manual = nil
end
@move_type = manual.nil? ? 0 : manual
rescue
@move_type = 0
end
def mp__broken_self_move_event?
page = mp__safe_page_for_self_move
manual = nil
begin
manual = @manual_move_type
rescue
manual = nil
end
page.nil? && manual.nil?
end
end
if method_defined?(:update_self_movement) && !method_defined?(:mp__update_self_movement_nil_guard__orig)
alias mp__update_self_movement_nil_guard__orig update_self_movement
def update_self_movement(*args)
if mp__broken_self_move_event?
mp__reset_broken_self_move_type
return
end
if (@move_type rescue nil) == :move_type_return && mp__broken_self_move_event?
mp__reset_broken_self_move_type
return
end
mp__update_self_movement_nil_guard__orig(*args)
rescue NoMethodError => e
if e.message.include?("move_type") || e.message.include?("NilClass")
mp__reset_broken_self_move_type
return
end
raise
end
end
if method_defined?(:move_type_return) && !method_defined?(:mp__move_type_return_nil_guard__orig)
alias mp__move_type_return_nil_guard__orig move_type_return
def move_type_return(*args)
if (@original_x.nil? && @original_y.nil?) && mp__broken_self_move_event?
mp__reset_broken_self_move_type
return
end
if mp__broken_self_move_event?
mp__reset_broken_self_move_type
return
end
mp__move_type_return_nil_guard__orig(*args)
rescue NoMethodError => e
if e.message.include?("move_type") || e.message.include?("NilClass")
mp__reset_broken_self_move_type
return
end
raise
end
end
if method_defined?(:update_stop) && !method_defined?(:mp__update_stop_nil_guard__orig)
alias mp__update_stop_nil_guard__orig update_stop
def update_stop(*args)
mp__update_stop_nil_guard__orig(*args)
rescue NoMethodError => e
if e.message.include?("move_type") || e.message.include?("NilClass")
mp__reset_broken_self_move_type
return
end
raise
end
end
if instance_methods(false).include?(:move_npc_movement) && !method_defined?(:mp__move_npc_movement__orig)
alias mp__move_npc_movement__orig move_npc_movement
def move_npc_movement(*args)
return if @npc.nil?
mp__move_npc_movement__orig(*args)
end
end
if instance_methods(false).include?(:sex_mode?) && !method_defined?(:mp__sex_mode__orig)
alias mp__sex_mode__orig sex_mode?
def sex_mode?(*args)
return false if !use_chs?
cfg = (@chs_configuration rescue nil)
return false unless cfg.respond_to?(:[])
sex_pos = nil
begin
sex_pos = cfg.has_key?("sex_pos") ? cfg["sex_pos"] : cfg["sex_position"]
rescue
sex_pos = cfg["sex_pos"] rescue nil
end
sex_pos.to_i != -1
rescue NoMethodError => e
return false if e.message.include?("NilClass")
raise
end
end
if instance_methods(false).include?(:get_chs_sex_pose) && !method_defined?(:mp__get_chs_sex_pose__orig)
alias mp__get_chs_sex_pose__orig get_chs_sex_pose
def get_chs_sex_pose(*args)
cfg = (@chs_configuration rescue nil)
return -1 unless cfg.respond_to?(:[])
return cfg["sex_pos"] if cfg.has_key?("sex_pos")
return cfg["sex_position"] if cfg.has_key?("sex_position")
-1
rescue NoMethodError => e
return -1 if e.message.include?("NilClass")
raise
end
end
end
end
#--------------------------------------------------------------------------
# 2b) Game_Map#npcs : in rare timing windows @npcs can be nil.
# Many event scripts call `$game_map.npcs.any? { ... }` and crash.
#--------------------------------------------------------------------------
if defined?(Game_Map)
class Game_Map
if method_defined?(:npcs) && !method_defined?(:mp__npcs_nil_guard__orig)
alias mp__npcs_nil_guard__orig npcs
def npcs(*args)
list = mp__npcs_nil_guard__orig(*args)
return list if list.is_a?(Array)
@npcs = [] unless @npcs.is_a?(Array)
@npcs
rescue
@npcs = [] unless @npcs.is_a?(Array)
@npcs
end
end
end
end
#--------------------------------------------------------------------------
# 2b) Game_NonPlayerCharacter#aggro_allowed? : attacker/skill/map_token могут быть nil
#--------------------------------------------------------------------------
if defined?(Game_NonPlayerCharacter)
class Game_NonPlayerCharacter
if instance_methods(false).include?(:aggro_allowed?) && !method_defined?(:mp__aggro_allowed__orig)
alias mp__aggro_allowed__orig aggro_allowed?
def aggro_allowed?(attacker, skill, *args)
return false if attacker.nil? || skill.nil?
mp__aggro_allowed__orig(attacker, skill, *args)
rescue NoMethodError => e
return false if e.message.include?("NilClass")
raise
end
end
end
end
#--------------------------------------------------------------------------
# 3) Game_Character#distance_x_from / distance_y_from : аргумент может быть nil
#--------------------------------------------------------------------------
if defined?(Game_Character)
class Game_Character
if instance_methods(false).include?(:distance_x_from) && !method_defined?(:mp__distance_x_from__orig)
alias mp__distance_x_from__orig distance_x_from
def distance_x_from(x)
return 0 if x.nil?
mp__distance_x_from__orig(x)
end
end
if instance_methods(false).include?(:distance_y_from) && !method_defined?(:mp__distance_y_from__orig)
alias mp__distance_y_from__orig distance_y_from
def distance_y_from(y)
return 0 if y.nil?
mp__distance_y_from__orig(y)
end
end
end
end
#--------------------------------------------------------------------------
# 6) Game_Interpreter#command_355 : script-call может догнаться уже после
# удаления события. В базе это показывает popup "Tried calling script from
# deleted event ...", а нам нужно просто тихо завершить такой event.
#--------------------------------------------------------------------------
if defined?(Game_Interpreter)
class Game_Interpreter
unless method_defined?(:mp__deleted_event_script_running?)
def mp__deleted_event_script_running?
return false if @event_id.to_i == 0
begin
get_character(0).nil?
rescue
true
end
end
def mp__collect_script_355
script = @list[@index].parameters[0].to_s + "\n"
while next_event_code == 655
@index += 1
script += @list[@index].parameters[0].to_s + "\n"
end
script
end
def mp__stop_deleted_event_script
begin
@list = []
rescue
end
nil
end
def command_355
begin
$game_temp.mp_last_transfer_event_id = @event_id
rescue
end
return mp__stop_deleted_event_script if mp__deleted_event_script_running?
script = mp__collect_script_355
begin
eval(script)
rescue => ex
if ex.is_a?(NoMethodError) && ex.message.to_s.include?("undefined method `any?' for nil:NilClass")
return mp__stop_deleted_event_script
end
if ex.is_a?(NoMethodError) && ex.message.to_s.include?("undefined method `erase' for nil:NilClass") && script.to_s.include?("get_character(0).erase")
return mp__stop_deleted_event_script
end
ev = nil
begin
ev = get_character(0)
rescue
ev = nil
end
if ev.nil? && @event_id.to_i != 0
return mp__stop_deleted_event_script
end
if ev != nil
raise $!, "#{$!.message} \nProblem Eval Event Script \n name=>#{ev.name} \n id=>#{ev.id} x=#{ev.x} y=#{ev.y}", $!.backtrace
end
raise ex
end
end
end
end
end
#--------------------------------------------------------------------------
# 4) Game_CharacterBase#moveto : x/y могут быть nil из Script-команд маршрута
# (иначе падает на `x % $game_map.width`)
#--------------------------------------------------------------------------
if defined?(Game_CharacterBase)
class Game_CharacterBase
if instance_methods(false).include?(:moveto) && !method_defined?(:mp__moveto__orig)
alias mp__moveto__orig moveto
def moveto(x, y)
return if x.nil? || y.nil?
mp__moveto__orig(x, y)
end
end
unless method_defined?(:mp__chs_data_entry_safe)
def mp__chs_data_entry_safe
return nil unless respond_to?(:use_chs?) && use_chs?
data = ($chs_data rescue nil)
return nil if data.nil?
key = nil
begin
key = chs_type
rescue
key = nil
end
return nil if key.nil?
begin
data[key]
rescue
nil
end
end
end
if instance_methods(false).include?(:screen_y) && !method_defined?(:mp__screen_y__orig)
alias mp__screen_y__orig screen_y
def screen_y(*args)
entry = mp__chs_data_entry_safe
return screen_y_common if respond_to?(:use_chs?) && use_chs? && entry.nil?
mp__screen_y__orig(*args)
rescue NoMethodError => e
return screen_y_common if e.message.include?("cell_y_adjust") || e.message.include?("sex_cell_y_adjust")
raise
end
end
if instance_methods(false).include?(:check_bush_depth) && !method_defined?(:mp__check_bush_depth__orig)
alias mp__check_bush_depth__orig check_bush_depth
def check_bush_depth(*args)
entry = mp__chs_data_entry_safe
return 12 if respond_to?(:use_chs?) && use_chs? && entry.nil?
mp__check_bush_depth__orig(*args)
rescue NoMethodError => e
return 12 if e.message.include?("cell_y_adjust") || e.message.include?("sex_cell_y_adjust")
raise
end
end
end
end
#--------------------------------------------------------------------------
# 5) Game_Character#process_move_command : script-route eval может уронить игру
# Не ломаем Scripts.zip, а мягко логируем проблемную команду
# и пропускаем один битый ROUTE_SCRIPT вместо полного краша.
#--------------------------------------------------------------------------
if defined?(Game_Character)
class Game_Character
if instance_methods(false).include?(:process_move_command) && !method_defined?(:mp__process_move_command__orig)
alias mp__process_move_command__orig process_move_command
def mp__append_move_route_eval_log(error, command)
File.open("move_route_eval_error.log", "ab") do |f|
f.write("=== #{Time.now} ===\r\n")
f.write("#{error.class}: #{error.message}\r\n")
begin
if instance_variable_defined?(:@event) && @event
f.write("event_name=#{@event.name}\r\n")
f.write("event_id=#{@event.id}\r\n")
f.write("event_x=#{@event.x}\r\n")
f.write("event_y=#{@event.y}\r\n")
else
f.write("event_name=\r\n")
end
rescue
end
begin
f.write("map_id=#{$game_map.map_id}\r\n") if defined?($game_map) && $game_map
rescue
end
begin
f.write("move_route_index=#{@move_route_index}\r\n")
rescue
end
begin
if command && command.parameters.is_a?(Array)
f.write("script=#{command.parameters[0].to_s}\r\n")
end
rescue
end
begin
if error.backtrace
error.backtrace.each do |line|
f.write("#{line}\r\n")
end
end
rescue
end
f.write("\r\n")
end
rescue
end
def process_move_command(command)
if command && command.code == ROUTE_SCRIPT
begin
mp__process_move_command__orig(command)
rescue => e
mp__append_move_route_eval_log(e, command)
return
end
return
end
mp__process_move_command__orig(command)
end
end
end
end
#--------------------------------------------------------------------------
# 6) Game_Actor#update_state_frames: repair state hashes restored from JSON.
# JSON converts numeric Hash keys to strings; the engine reads them as
# integers, which otherwise produces nil > 0 and crashes the update loop.
#--------------------------------------------------------------------------
if defined?(Game_Actor)
class Game_Actor
if instance_methods.include?(:update_state_frames) && !method_defined?(:mp__update_state_frames__orig)
alias mp__update_state_frames__orig update_state_frames
def mp__normalize_state_counter_hash(value)
fixed = {}
if value.is_a?(Hash)
value.each do |key, counter|
sid = key.is_a?(String) ? key.to_i : key
fixed[sid] = counter.to_i if sid.to_i > 0
end
end
fixed
rescue
{}
end
def update_state_frames
@state_steps = mp__normalize_state_counter_hash(@state_steps)
@state_frames = mp__normalize_state_counter_hash(@state_frames)
begin
states.each do |state|
next if state.nil?
sid = state.id.to_i
next if sid <= 0
@state_steps[sid] = 0 unless @state_steps[sid].is_a?(Numeric)
@state_frames[sid] = 0 unless @state_frames[sid].is_a?(Numeric)
end
rescue
end
mp__update_state_frames__orig
end
end
end
end