Files
LonaRPG-Online-Games/ModScripts/999999999999999_ClanEstateTransientChestFix.rb
T
2026-10-29 21:59:06 +04:00

205 lines
8.4 KiB
Ruby

# encoding: UTF-8
# ClanEstate transient chest hotfix 1.3.3d
#
# Box 65532 is a CLIENT-SIDE MIRROR of the authoritative clan chest and must
# never become persistent player storage. Box 65533 (personal NoerHome/bank)
# is intentionally untouched.
#
# 1.3.3d fixes a recursion bug from 1.3.3c: singleton_class.prepend + a later
# alias of DataManager.setup_new_game / WebSocket.disconnect could make `super`
# re-enter the wrapper forever (SystemStackError: stack level too deep).
# This revision uses explicit alias chains and does NOT wrap setup_new_game at
# all; create_game_objects already runs for New Game and is sufficient.
return if defined?($mp_clan_chest_transient_fix_v133d_loaded) && $mp_clan_chest_transient_fix_v133d_loaded
$mp_clan_chest_transient_fix_v133d_loaded = true
module MPClanChestTransientFix
VERSION = '1.3.3d' unless const_defined?(:VERSION)
BOX_ID = 65_532 unless const_defined?(:BOX_ID)
def self.clear_transient_box(reason=nil)
begin
if defined?($game_boxes) && $game_boxes
# Avoid write_box(65532,nil) on a pristine game because some box
# implementations use a sparse Array and would allocate up to this id.
boxes = $game_boxes.instance_variable_get(:@boxes)
if boxes.is_a?(Array)
boxes[BOX_ID] = nil if BOX_ID < boxes.length
elsif $game_boxes.respond_to?(:write_box)
# Non-array implementations are assumed not to have the sparse-array
# allocation problem.
$game_boxes.write_box(BOX_ID, nil)
end
end
rescue Exception => e
puts "[ClanEstate transient chest clear] #{e.class}: #{e.message}" rescue nil
end
begin
if defined?(MPClanEstate)
MPClanEstate.instance_variable_set(:@chest_box_fingerprint, nil)
end
rescue Exception => e
puts "[ClanEstate transient chest fingerprint] #{e.class}: #{e.message}" rescue nil
end
true
end
def self.save_copy_without_clan_chest(source)
return source unless source
copy = source.dup
boxes = source.instance_variable_get(:@boxes)
if boxes.is_a?(Array)
save_boxes = boxes.dup
save_boxes[BOX_ID] = nil if BOX_ID < save_boxes.length
copy.instance_variable_set(:@boxes, save_boxes)
end
copy
rescue Exception => e
puts "[ClanEstate transient chest save filter] #{e.class}: #{e.message}" rescue nil
source
end
end
# -----------------------------------------------------------------------------
# DataManager
# -----------------------------------------------------------------------------
# IMPORTANT: no prepend here. A later script (CloudSaveDurabilityFix) aliases
# setup_new_game. In 1.3.3c the prepended setup_new_game wrapper was copied by
# that alias and its `super` then re-entered the later method, causing infinite
# recursion. New Game already calls create_game_objects, so wrapping
# create_game_objects is enough to clear 65532 safely.
if defined?(DataManager)
module DataManager
class << self
if method_defined?(:make_save_contents) && !method_defined?(:mp_clan_chest_transient_make_save_contents_without_filter)
alias mp_clan_chest_transient_make_save_contents_without_filter make_save_contents
def make_save_contents(*args, &block)
contents = mp_clan_chest_transient_make_save_contents_without_filter(*args, &block)
begin
if contents.is_a?(Hash) && contents[:imp_boxes]
contents[:imp_boxes] = MPClanChestTransientFix.save_copy_without_clan_chest(contents[:imp_boxes])
end
rescue Exception => e
puts "[ClanEstate transient chest save contents] #{e.class}: #{e.message}" rescue nil
end
contents
end
end
if method_defined?(:extract_save_contents) && !method_defined?(:mp_clan_chest_transient_extract_save_contents_without_clear)
alias mp_clan_chest_transient_extract_save_contents_without_clear extract_save_contents
def extract_save_contents(contents, *args, &block)
result = mp_clan_chest_transient_extract_save_contents_without_clear(contents, *args, &block)
MPClanChestTransientFix.clear_transient_box('load')
result
end
end
if method_defined?(:create_game_objects) && !method_defined?(:mp_clan_chest_transient_create_game_objects_without_clear)
alias mp_clan_chest_transient_create_game_objects_without_clear create_game_objects
def create_game_objects(*args, &block)
result = mp_clan_chest_transient_create_game_objects_without_clear(*args, &block)
MPClanChestTransientFix.clear_transient_box('create_game_objects')
result
end
end
end
end
end
# -----------------------------------------------------------------------------
# ClanEstate state/session hooks
# -----------------------------------------------------------------------------
if defined?(MPClanEstate)
class << MPClanEstate
if method_defined?(:ensure_game_session) && !method_defined?(:mp_clan_chest_transient_ensure_game_session_without_clear)
alias mp_clan_chest_transient_ensure_game_session_without_clear ensure_game_session
def ensure_game_session(*args, &block)
changed = mp_clan_chest_transient_ensure_game_session_without_clear(*args, &block)
MPClanChestTransientFix.clear_transient_box('game_session') if changed
changed
end
end
if method_defined?(:apply_state) && !method_defined?(:mp_clan_chest_transient_apply_state_without_clear)
alias mp_clan_chest_transient_apply_state_without_clear apply_state
def apply_state(value, *args, &block)
begin
if value.is_a?(Hash)
previous = respond_to?(:state) ? state : nil
if previous.is_a?(Hash)
old_profile = previous['profile'].to_i
new_profile = value['profile'].to_i
old_clan = previous['clan'].to_i
new_clan = value['clan'].to_i
profile_changed = old_profile > 0 && new_profile > 0 && old_profile != new_profile
clan_changed = old_clan > 0 && new_clan > 0 && old_clan != new_clan
MPClanChestTransientFix.clear_transient_box('estate_identity_change') if profile_changed || clan_changed
end
end
rescue Exception => e
puts "[ClanEstate transient chest identity] #{e.class}: #{e.message}" rescue nil
end
mp_clan_chest_transient_apply_state_without_clear(value, *args, &block)
end
end
if method_defined?(:home_kind=) && !method_defined?(:mp_clan_chest_transient_home_kind_without_clear=)
alias mp_clan_chest_transient_home_kind_without_clear= home_kind=
def home_kind=(value)
previous = begin
home_kind
rescue Exception
nil
end
result = self.mp_clan_chest_transient_home_kind_without_clear = value
if !previous.nil? && value.nil?
MPClanChestTransientFix.clear_transient_box('leave_estate')
end
result
end
end
end
end
# -----------------------------------------------------------------------------
# WebSocket disconnect
# -----------------------------------------------------------------------------
# Explicit alias chain is intentional. A later CloudSave patch also aliases
# disconnect; this form remains safe because our wrapper calls the captured old
# implementation directly instead of using `super`.
if defined?(WebSocket)
class << WebSocket
if method_defined?(:disconnect) && !method_defined?(:mp_clan_chest_transient_disconnect_without_clear)
alias mp_clan_chest_transient_disconnect_without_clear disconnect
def disconnect(*args, &block)
begin
mp_clan_chest_transient_disconnect_without_clear(*args, &block)
ensure
MPClanChestTransientFix.clear_transient_box('disconnect')
end
end
end
end
end
# -----------------------------------------------------------------------------
# Clan chest scene close
# -----------------------------------------------------------------------------
if defined?(Scene_ClanEstateStorage)
class Scene_ClanEstateStorage
if method_defined?(:terminate) && !method_defined?(:mp_clan_chest_transient_terminate_without_clear)
alias mp_clan_chest_transient_terminate_without_clear terminate
def terminate(*args, &block)
begin
mp_clan_chest_transient_terminate_without_clear(*args, &block)
ensure
MPClanChestTransientFix.clear_transient_box('chest_close')
end
end
end
end
end