486 lines
16 KiB
Ruby
486 lines
16 KiB
Ruby
# encoding: UTF-8
|
|
return if defined?($mp_disconnect_guard_loaded) && $mp_disconnect_guard_loaded
|
|
$mp_disconnect_guard_loaded = true
|
|
|
|
begin
|
|
if defined?(MPStateInfra)
|
|
if MPStateInfra.const_defined?(:LOG_ENABLED)
|
|
MPStateInfra.send(:remove_const, :LOG_ENABLED)
|
|
end
|
|
MPStateInfra.const_set(:LOG_ENABLED, false)
|
|
end
|
|
rescue Exception
|
|
end
|
|
|
|
if defined?(WebSocket)
|
|
module WebSocket
|
|
class << self
|
|
def mp_disconnect_guard_log(tag, message)
|
|
# Disconnect-guard tracing is intentionally disabled.
|
|
false
|
|
end
|
|
|
|
def disconnecting?
|
|
!!@mp_disconnect_guard_active
|
|
rescue
|
|
false
|
|
end
|
|
|
|
unless method_defined?(:mp_disconnect_guard_disconnect_without_guard)
|
|
alias mp_disconnect_guard_disconnect_without_guard disconnect
|
|
end
|
|
|
|
def disconnect(*args, &block)
|
|
if @mp_disconnect_guard_active
|
|
begin
|
|
transport_close if respond_to?(:transport_close)
|
|
rescue Exception
|
|
ensure
|
|
@connected = false
|
|
end
|
|
return false
|
|
end
|
|
|
|
@mp_disconnect_guard_active = true
|
|
begin
|
|
mp_disconnect_guard_disconnect_without_guard(*args, &block)
|
|
ensure
|
|
@mp_disconnect_guard_active = false
|
|
end
|
|
end
|
|
|
|
unless method_defined?(:mp_disconnect_guard_send_line_without_trace)
|
|
alias mp_disconnect_guard_send_line_without_trace send_line
|
|
end
|
|
|
|
def send_line(s)
|
|
begin
|
|
line = s.to_s
|
|
cmd = line.split(":", 2)[0].to_s
|
|
case cmd
|
|
when "auth"
|
|
parts = line.split(":", 3)
|
|
nick = parts[1].to_s
|
|
mp_disconnect_guard_log("send", "auth nick=#{nick.inspect} password=<redacted>")
|
|
when "version", "bootstrap_loaded", "bank_req"
|
|
mp_disconnect_guard_log("send", line[0, 300])
|
|
end
|
|
rescue Exception
|
|
end
|
|
mp_disconnect_guard_send_line_without_trace(s)
|
|
end
|
|
|
|
unless method_defined?(:mp_disconnect_guard_parse_without_trace)
|
|
alias mp_disconnect_guard_parse_without_trace parse
|
|
end
|
|
|
|
def parse(msg)
|
|
begin
|
|
line = msg.to_s
|
|
cmd = line.split(":", 2)[0].to_s
|
|
case cmd
|
|
when "version_need", "version_ok", "version_bad", "auth_ok", "auth_error",
|
|
"bootstrap_mode", "bootstrap_map", "bootstrap_pos", "bootstrap_state",
|
|
"bank_state", "sync_unlocked", "server_kick"
|
|
preview = line[0, 500]
|
|
preview = "bootstrap_state:<#{line.length} chars>" if cmd == "bootstrap_state"
|
|
mp_disconnect_guard_log("recv", preview)
|
|
end
|
|
rescue Exception
|
|
end
|
|
mp_disconnect_guard_parse_without_trace(msg)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if defined?(MPConnectStatus)
|
|
module MPConnectStatus
|
|
class << self
|
|
unless method_defined?(:mp_disconnect_guard_socket_error_without_guard)
|
|
alias mp_disconnect_guard_socket_error_without_guard socket_error!
|
|
end
|
|
|
|
def socket_error!
|
|
return if @mp_socket_error_guard_active
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:disconnecting?) && WebSocket.disconnecting?
|
|
return
|
|
end
|
|
|
|
@mp_socket_error_guard_active = true
|
|
begin
|
|
mp_disconnect_guard_socket_error_without_guard
|
|
ensure
|
|
@mp_socket_error_guard_active = false
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
module MPStatShapeGuard
|
|
MAX_DEPTH = 8 unless const_defined?(:MAX_DEPTH)
|
|
STAT_CHANGED_INDEX = 7 unless const_defined?(:STAT_CHANGED_INDEX)
|
|
|
|
class << self
|
|
def log(tag, message)
|
|
text = message.to_s
|
|
puts "[STATGUARD] #{tag} #{text}" rescue nil
|
|
if defined?(MPStateInfra) && MPStateInfra.respond_to?(:log_line)
|
|
MPStateInfra.log_line("stat_guard_#{tag}", text)
|
|
end
|
|
rescue Exception
|
|
end
|
|
|
|
def scalar(value)
|
|
depth = 0
|
|
while value.is_a?(Array) && depth < MAX_DEPTH
|
|
value = value[0]
|
|
depth += 1
|
|
end
|
|
value
|
|
rescue Exception
|
|
value
|
|
end
|
|
|
|
def number(value, fallback = 0)
|
|
value = scalar(value)
|
|
return value if value.is_a?(Numeric)
|
|
if value.is_a?(String)
|
|
text = value.strip
|
|
return text.include?(".") ? text.to_f : text.to_i if text =~ /\A-?\d+(?:\.\d+)?\z/
|
|
end
|
|
fallback
|
|
rescue Exception
|
|
fallback
|
|
end
|
|
|
|
def current_value(value, fallback = 0)
|
|
number(value, fallback)
|
|
end
|
|
|
|
def stat_row?(value)
|
|
value.is_a?(Array) && (value.length >= 5 || value[0].is_a?(Array))
|
|
rescue Exception
|
|
false
|
|
end
|
|
|
|
def clone_stat_row(row, fallback_row = nil)
|
|
return nil unless row.is_a?(Array)
|
|
base = fallback_row.is_a?(Array) ? fallback_row : []
|
|
len = [row.length, base.length, 8].max
|
|
out = []
|
|
0.upto(len - 1) do |i|
|
|
raw = i < row.length ? row[i] : base[i]
|
|
if i <= 6
|
|
fallback = base[i].is_a?(Numeric) ? base[i] : 0
|
|
out[i] = number(raw, fallback)
|
|
elsif i == STAT_CHANGED_INDEX
|
|
out[i] = raw.nil? ? true : !!raw
|
|
else
|
|
out[i] = raw.is_a?(Array) ? raw.map { |v| v } : raw
|
|
end
|
|
end
|
|
out[0] = current_value(row[0], base[0].is_a?(Numeric) ? base[0] : 0)
|
|
out
|
|
rescue Exception
|
|
nil
|
|
end
|
|
|
|
def stat_table(actor)
|
|
return nil if actor.nil?
|
|
act_stat = actor.instance_variable_get(:@actStat) rescue nil
|
|
return nil unless act_stat && act_stat.respond_to?(:stat)
|
|
act_stat.stat
|
|
rescue Exception
|
|
nil
|
|
end
|
|
|
|
def repair_table(table, reason = nil)
|
|
return 0 unless table.is_a?(Hash)
|
|
repaired = 0
|
|
table.keys.each do |key|
|
|
row = table[key]
|
|
next unless row.is_a?(Array)
|
|
needs = row[0].is_a?(Array)
|
|
unless needs
|
|
1.upto([row.length - 1, 6].min) do |i|
|
|
if row[i].is_a?(Array)
|
|
needs = true
|
|
break
|
|
end
|
|
end
|
|
end
|
|
next unless needs
|
|
fixed = clone_stat_row(row, row)
|
|
next unless fixed.is_a?(Array)
|
|
table[key] = fixed
|
|
repaired += 1
|
|
end
|
|
log("repair", "#{reason || 'table'} rows=#{repaired}") if repaired > 0
|
|
repaired
|
|
rescue Exception
|
|
0
|
|
end
|
|
|
|
def repair_actor(actor, reason = nil)
|
|
table = stat_table(actor)
|
|
count = repair_table(table, reason)
|
|
if count > 0
|
|
act_stat = actor.instance_variable_get(:@actStat) rescue nil
|
|
begin
|
|
act_stat.check_stat if act_stat && act_stat.respond_to?(:check_stat)
|
|
rescue Exception
|
|
end
|
|
begin
|
|
actor.recalculate_stats if actor && actor.respond_to?(:recalculate_stats)
|
|
rescue Exception
|
|
end
|
|
begin
|
|
actor.instance_variable_set(:@stat_changed, true) if actor
|
|
rescue Exception
|
|
end
|
|
end
|
|
count
|
|
rescue Exception
|
|
0
|
|
end
|
|
|
|
def current_actor
|
|
begin
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:_live_sync_actor)
|
|
actor = WebSocket._live_sync_actor
|
|
return actor if actor
|
|
end
|
|
rescue Exception
|
|
end
|
|
begin
|
|
return $game_player.actor if defined?($game_player) && $game_player && $game_player.respond_to?(:actor)
|
|
rescue Exception
|
|
end
|
|
nil
|
|
end
|
|
|
|
def repair_current_actor(reason = nil)
|
|
repair_actor(current_actor, reason)
|
|
rescue Exception
|
|
0
|
|
end
|
|
|
|
def sanitize_stats_json(stats_json, actor = nil)
|
|
return stats_json unless stats_json.is_a?(Hash)
|
|
safe = stats_json.dup
|
|
table = stat_table(actor)
|
|
full = safe["act_stat_full"]
|
|
if full.is_a?(Hash)
|
|
new_full = {}
|
|
full.each do |raw_key, raw_value|
|
|
key = raw_key.to_s
|
|
fallback = table.is_a?(Hash) ? table[key] : nil
|
|
fixed = raw_value.is_a?(Array) ? clone_stat_row(raw_value, fallback) : nil
|
|
new_full[key] = fixed.is_a?(Array) ? fixed : raw_value
|
|
end
|
|
safe["act_stat_full"] = new_full
|
|
end
|
|
special = {
|
|
"act_stat_full" => true,
|
|
"trait_point" => true,
|
|
"actor_class_id" => true,
|
|
"actor_exp" => true,
|
|
"actor_skills" => true
|
|
}
|
|
fixed_scalars = 0
|
|
safe.keys.each do |raw_key|
|
|
key = raw_key.to_s
|
|
next if special[key]
|
|
value = safe[raw_key]
|
|
next unless value.is_a?(Array)
|
|
next unless (table.is_a?(Hash) && table.has_key?(key)) || (actor && actor.respond_to?(key + "="))
|
|
fallback_row = table.is_a?(Hash) ? table[key] : nil
|
|
fallback = fallback_row.is_a?(Array) ? current_value(fallback_row[0], 0) : 0
|
|
safe[raw_key] = current_value(value, fallback)
|
|
fixed_scalars += 1
|
|
end
|
|
log("sanitize_stats", "scalar_arrays=#{fixed_scalars}") if fixed_scalars > 0
|
|
safe
|
|
rescue Exception
|
|
stats_json
|
|
end
|
|
|
|
def sanitize_body_json(body_json, actor = nil)
|
|
return body_json unless body_json.is_a?(Hash)
|
|
safe = body_json.dup
|
|
stats = safe["body_stats"]
|
|
return safe unless stats.is_a?(Hash)
|
|
table = stat_table(actor)
|
|
new_stats = {}
|
|
fixed = 0
|
|
stats.each do |raw_key, raw_value|
|
|
key = raw_key.to_s
|
|
if raw_value.is_a?(Array)
|
|
fallback_row = table.is_a?(Hash) ? table[key] : nil
|
|
fallback = fallback_row.is_a?(Array) ? current_value(fallback_row[0], 0) : 0
|
|
new_stats[raw_key] = current_value(raw_value, fallback)
|
|
fixed += 1
|
|
else
|
|
new_stats[raw_key] = raw_value
|
|
end
|
|
end
|
|
safe["body_stats"] = new_stats
|
|
log("sanitize_body", "body_stat_arrays=#{fixed}") if fixed > 0
|
|
safe
|
|
rescue Exception
|
|
body_json
|
|
end
|
|
end
|
|
end
|
|
|
|
if defined?(ActorStat)
|
|
class ActorStat
|
|
unless method_defined?(:mp_stat_shape_guard_set_stat_without_guard)
|
|
alias mp_stat_shape_guard_set_stat_without_guard set_stat
|
|
def set_stat(stat_name, value, type = CURRENT_STAT)
|
|
value = MPStatShapeGuard.current_value(value, 0) if defined?(MPStatShapeGuard) && value.is_a?(Array)
|
|
mp_stat_shape_guard_set_stat_without_guard(stat_name, value, type)
|
|
end
|
|
end
|
|
|
|
if method_defined?(:check_stat) && !method_defined?(:mp_stat_shape_guard_check_stat_without_guard)
|
|
alias mp_stat_shape_guard_check_stat_without_guard check_stat
|
|
def check_stat(*args, &block)
|
|
MPStatShapeGuard.repair_table(@stat, "actor_stat_check") if defined?(MPStatShapeGuard)
|
|
mp_stat_shape_guard_check_stat_without_guard(*args, &block)
|
|
end
|
|
end
|
|
|
|
if method_defined?(:reset_definition) && !method_defined?(:mp_stat_shape_guard_reset_definition_without_guard)
|
|
alias mp_stat_shape_guard_reset_definition_without_guard reset_definition
|
|
def reset_definition(*args, &block)
|
|
MPStatShapeGuard.repair_table(@stat, "actor_stat_reset") if defined?(MPStatShapeGuard)
|
|
mp_stat_shape_guard_reset_definition_without_guard(*args, &block)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if defined?(LonaActorStat)
|
|
class LonaActorStat
|
|
unless method_defined?(:mp_stat_shape_guard_lona_set_stat_without_guard)
|
|
alias mp_stat_shape_guard_lona_set_stat_without_guard set_stat
|
|
def set_stat(stat_name, value, type = CURRENT_STAT)
|
|
value = MPStatShapeGuard.current_value(value, 0) if defined?(MPStatShapeGuard) && value.is_a?(Array)
|
|
mp_stat_shape_guard_lona_set_stat_without_guard(stat_name, value, type)
|
|
end
|
|
end
|
|
|
|
if method_defined?(:check_single_stat) && !method_defined?(:mp_stat_shape_guard_check_single_stat_without_guard)
|
|
alias mp_stat_shape_guard_check_single_stat_without_guard check_single_stat
|
|
def check_single_stat(key)
|
|
if defined?(MPStatShapeGuard) && @stat.is_a?(Hash)
|
|
row = @stat[key]
|
|
MPStatShapeGuard.repair_table(@stat, "lona_check_single") if row.is_a?(Array) && row[0].is_a?(Array)
|
|
end
|
|
mp_stat_shape_guard_check_single_stat_without_guard(key)
|
|
end
|
|
end
|
|
|
|
if method_defined?(:reset_definition) && !method_defined?(:mp_stat_shape_guard_lona_reset_definition_without_guard)
|
|
alias mp_stat_shape_guard_lona_reset_definition_without_guard reset_definition
|
|
def reset_definition(*args, &block)
|
|
MPStatShapeGuard.repair_table(@stat, "lona_reset") if defined?(MPStatShapeGuard)
|
|
mp_stat_shape_guard_lona_reset_definition_without_guard(*args, &block)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if defined?(WebSocket)
|
|
module WebSocket
|
|
class << self
|
|
if method_defined?(:_bootstrap_apply_stats_json) && !method_defined?(:mp_stat_shape_guard_apply_stats_json_without_guard)
|
|
alias mp_stat_shape_guard_apply_stats_json_without_guard _bootstrap_apply_stats_json
|
|
def _bootstrap_apply_stats_json(actor, stats_json)
|
|
safe = defined?(MPStatShapeGuard) ? MPStatShapeGuard.sanitize_stats_json(stats_json, actor) : stats_json
|
|
result = mp_stat_shape_guard_apply_stats_json_without_guard(actor, safe)
|
|
MPStatShapeGuard.repair_actor(actor, "after_stats_json") if defined?(MPStatShapeGuard)
|
|
result
|
|
end
|
|
end
|
|
|
|
if method_defined?(:_bootstrap_apply_body_json) && !method_defined?(:mp_stat_shape_guard_apply_body_json_without_guard)
|
|
alias mp_stat_shape_guard_apply_body_json_without_guard _bootstrap_apply_body_json
|
|
def _bootstrap_apply_body_json(actor, body_json)
|
|
safe = defined?(MPStatShapeGuard) ? MPStatShapeGuard.sanitize_body_json(body_json, actor) : body_json
|
|
result = mp_stat_shape_guard_apply_body_json_without_guard(actor, safe)
|
|
MPStatShapeGuard.repair_actor(actor, "after_body_json") if defined?(MPStatShapeGuard)
|
|
result
|
|
end
|
|
end
|
|
|
|
if method_defined?(:apply_bootstrap_state) && !method_defined?(:mp_stat_shape_guard_apply_bootstrap_state_without_guard)
|
|
alias mp_stat_shape_guard_apply_bootstrap_state_without_guard apply_bootstrap_state
|
|
def apply_bootstrap_state(state)
|
|
result = mp_stat_shape_guard_apply_bootstrap_state_without_guard(state)
|
|
MPStatShapeGuard.repair_current_actor("after_bootstrap") if defined?(MPStatShapeGuard)
|
|
result
|
|
end
|
|
end
|
|
|
|
if method_defined?(:_live_sync_build_packet) && !method_defined?(:mp_stat_shape_guard_build_packet_without_guard)
|
|
alias mp_stat_shape_guard_build_packet_without_guard _live_sync_build_packet
|
|
def _live_sync_build_packet
|
|
MPStatShapeGuard.repair_current_actor("before_cloud_packet") if defined?(MPStatShapeGuard)
|
|
mp_stat_shape_guard_build_packet_without_guard
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if defined?(Scene_Map)
|
|
class Scene_Map
|
|
unless method_defined?(:mp_stat_shape_guard_start_without_guard)
|
|
alias mp_stat_shape_guard_start_without_guard start
|
|
def start(*args, &block)
|
|
MPStatShapeGuard.repair_current_actor("scene_start") if defined?(MPStatShapeGuard)
|
|
mp_stat_shape_guard_start_without_guard(*args, &block)
|
|
end
|
|
end
|
|
|
|
if method_defined?(:update) && !method_defined?(:mp_stat_shape_guard_update_without_guard)
|
|
alias mp_stat_shape_guard_update_without_guard update
|
|
def update(*args, &block)
|
|
MPStatShapeGuard.repair_current_actor("scene_update") if defined?(MPStatShapeGuard)
|
|
mp_stat_shape_guard_update_without_guard(*args, &block)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if defined?(Game_Actor)
|
|
class Game_Actor
|
|
if method_defined?(:change_equip) && !method_defined?(:mp_stat_shape_guard_change_equip_without_guard)
|
|
alias mp_stat_shape_guard_change_equip_without_guard change_equip
|
|
def change_equip(*args, &block)
|
|
MPStatShapeGuard.repair_actor(self, "before_change_equip") if defined?(MPStatShapeGuard)
|
|
result = mp_stat_shape_guard_change_equip_without_guard(*args, &block)
|
|
MPStatShapeGuard.repair_actor(self, "after_change_equip") if defined?(MPStatShapeGuard)
|
|
result
|
|
end
|
|
end
|
|
|
|
if method_defined?(:force_change_equip) && !method_defined?(:mp_stat_shape_guard_force_change_equip_without_guard)
|
|
alias mp_stat_shape_guard_force_change_equip_without_guard force_change_equip
|
|
def force_change_equip(*args, &block)
|
|
MPStatShapeGuard.repair_actor(self, "before_force_change_equip") if defined?(MPStatShapeGuard)
|
|
result = mp_stat_shape_guard_force_change_equip_without_guard(*args, &block)
|
|
MPStatShapeGuard.repair_actor(self, "after_force_change_equip") if defined?(MPStatShapeGuard)
|
|
result
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
puts "[NET] disconnect guard enabled" rescue nil
|
|
puts "[STATGUARD] stat shape guard enabled" rescue nil
|