1976 lines
53 KiB
Ruby
1976 lines
53 KiB
Ruby
# encoding: UTF-8
|
|||
|
|
|
||
|
|
return unless defined?(WebSocket)
|
||
|
|
|
||
|
|
module MPCompanionSync
|
||
|
|
FILE_NAME = "ModScripts/multiplayer/nickname.json"
|
||
|
|
MAX_NAME_LEN = 16
|
||
|
|
SYNC_EVERY_FRAMES = 6
|
||
|
|
PERSONAL_DYNAMIC_EVENT_ID_BASE = 80000
|
||
|
|
PERSONAL_DYNAMIC_EVENT_ID_MAX = 89999
|
||
|
|
NETWORK_REPLICATION_ENABLED = false
|
||
|
|
|
||
|
|
SLOT_BY_NUM = {
|
||
|
|
0 => "back",
|
||
|
|
1 => "front",
|
||
|
|
-1 => "ext",
|
||
|
|
}
|
||
|
|
|
||
|
|
SLOT_INDEX = {
|
||
|
|
"back" => 0,
|
||
|
|
"front" => 1,
|
||
|
|
"ext" => 2,
|
||
|
|
}
|
||
|
|
|
||
|
|
COCONA_NAMES = %w[
|
||
|
|
UniqueCocona
|
||
|
|
UniqueCoconaMaid
|
||
|
|
Cocona
|
||
|
|
CoconaMaid
|
||
|
|
]
|
||
|
|
|
||
|
|
class NetCompanionActor
|
||
|
|
attr_accessor :stat
|
||
|
|
attr_accessor :record_HairColor
|
||
|
|
|
||
|
|
def initialize
|
||
|
|
@stat = {}
|
||
|
|
@record_HairColor = 0
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.primary_path
|
||
|
|
begin
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:primary_path)
|
||
|
|
return NicknameInput.primary_path.to_s
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
FILE_NAME
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.fallback_dir
|
||
|
|
begin
|
||
|
|
return System_Settings::USER_DATA_PATH if defined?(System_Settings) && System_Settings.const_defined?(:USER_DATA_PATH)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
"UserData/"
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.fallback_path
|
||
|
|
begin
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:fallback_path)
|
||
|
|
return NicknameInput.fallback_path.to_s
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
primary_path
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.ensure_fallback_dir
|
||
|
|
ensure_parent_dir(primary_path)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.ensure_parent_dir(path)
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:ensure_parent_dir)
|
||
|
|
return NicknameInput.ensure_parent_dir(path)
|
||
|
|
end
|
||
|
|
|
||
|
|
dir = File.dirname(path.to_s)
|
||
|
|
return if dir.to_s.strip == "" || Dir.exist?(dir)
|
||
|
|
|
||
|
|
parts = dir.gsub("\\", "/").split("/")
|
||
|
|
cur = parts.shift.to_s
|
||
|
|
|
||
|
|
if cur =~ /\A[A-Za-z]:\z/
|
||
|
|
cur += "/"
|
||
|
|
elsif cur == ""
|
||
|
|
cur = "/"
|
||
|
|
end
|
||
|
|
|
||
|
|
parts.each do |part|
|
||
|
|
next if part.to_s == ""
|
||
|
|
cur = cur == "/" ? "/#{part}" : File.join(cur, part)
|
||
|
|
Dir.mkdir(cur) unless Dir.exist?(cur)
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.sanitize_label(text)
|
||
|
|
s = text.to_s
|
||
|
|
s = s.gsub(/[\r\n\t]/, " ")
|
||
|
|
s = s.gsub("\0", "")
|
||
|
|
s = s.strip
|
||
|
|
s = s[0, MAX_NAME_LEN]
|
||
|
|
s
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.read_json_label(path)
|
||
|
|
return nil unless File.exist?(path)
|
||
|
|
raw = Ruby310Compat.read_text_file(path)
|
||
|
|
data = nil
|
||
|
|
|
||
|
|
if defined?(JSON)
|
||
|
|
begin
|
||
|
|
data = JSON.parse(raw)
|
||
|
|
rescue
|
||
|
|
data = nil
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if data.is_a?(Hash)
|
||
|
|
value = data["cocona_nickname"] || data[:cocona_nickname]
|
||
|
|
value = sanitize_label(value)
|
||
|
|
return value unless value == ""
|
||
|
|
end
|
||
|
|
|
||
|
|
if raw =~ /"cocona_nickname"\s*:\s*"([^"]*)"/
|
||
|
|
value = sanitize_label($1.to_s)
|
||
|
|
return value unless value == ""
|
||
|
|
end
|
||
|
|
|
||
|
|
nil
|
||
|
|
rescue
|
||
|
|
nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.read_json_hash(path)
|
||
|
|
return {} unless File.exist?(path)
|
||
|
|
raw = Ruby310Compat.read_text_file(path)
|
||
|
|
|
||
|
|
if defined?(JSON)
|
||
|
|
begin
|
||
|
|
data = JSON.parse(raw)
|
||
|
|
return data if data.is_a?(Hash)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
data = JSON.parse(raw)
|
||
|
|
return data if data.is_a?(Hash)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:parse_simple_json_hash)
|
||
|
|
return NicknameInput.parse_simple_json_hash(raw)
|
||
|
|
end
|
||
|
|
|
||
|
|
data = {}
|
||
|
|
raw.to_s.scan(/"((?:\\.|[^"\\])*)"\s*:\s*"((?:\\.|[^"\\])*)"/m) do |key, value|
|
||
|
|
data[key.to_s.gsub("\\\"", "\"").gsub("\\\\", "\\")] = value.to_s.gsub("\\\"", "\"").gsub("\\\\", "\\")
|
||
|
|
end
|
||
|
|
data
|
||
|
|
rescue
|
||
|
|
{}
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.read_combined_json_hash
|
||
|
|
data = {}
|
||
|
|
part = read_json_hash(primary_path)
|
||
|
|
data.merge!(part) if part.is_a?(Hash)
|
||
|
|
data
|
||
|
|
rescue
|
||
|
|
{}
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.escape_json_string(text)
|
||
|
|
return NicknameInput.escape_json_string(text) if defined?(NicknameInput) && NicknameInput.respond_to?(:escape_json_string)
|
||
|
|
s = text.to_s
|
||
|
|
s = s.gsub(/["\\]/) { |match| match == "\"" ? "\\\"" : "\\\\" }
|
||
|
|
s = s.gsub("\r", "\\r")
|
||
|
|
s = s.gsub("\n", "\\n")
|
||
|
|
s = s.gsub("\t", "\\t")
|
||
|
|
s
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.encode_hash(data)
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:encode_hash)
|
||
|
|
return NicknameInput.encode_hash(data)
|
||
|
|
end
|
||
|
|
|
||
|
|
if defined?(JSON)
|
||
|
|
begin
|
||
|
|
return JSON.generate(data)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
return JSON.generate(data)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
pairs = []
|
||
|
|
data.each do |key, value|
|
||
|
|
pairs << "\"#{escape_json_string(key)}\":\"#{escape_json_string(value)}\""
|
||
|
|
end
|
||
|
|
"{#{pairs.join(',')}}"
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.load_cocona_label
|
||
|
|
return @cocona_label if instance_variable_defined?(:@cocona_label)
|
||
|
|
@cocona_label = read_json_label(primary_path) || ""
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.save_cocona_label(text)
|
||
|
|
label = sanitize_label(text)
|
||
|
|
@cocona_label = label
|
||
|
|
data = read_combined_json_hash
|
||
|
|
data = {} unless data.is_a?(Hash)
|
||
|
|
data["cocona_nickname"] = label
|
||
|
|
payload = encode_hash(data)
|
||
|
|
saved = false
|
||
|
|
|
||
|
|
begin
|
||
|
|
ensure_parent_dir(primary_path)
|
||
|
|
File.open(primary_path, "wb") { |f| f.write(payload) }
|
||
|
|
saved = true
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
saved
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.ui_font_name
|
||
|
|
begin
|
||
|
|
return MPSafeFont.name if defined?(MPSafeFont)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
return NicknameInput.ui_font_name if defined?(NicknameInput) && NicknameInput.respond_to?(:ui_font_name)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
"VL Gothic"
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.current_frame
|
||
|
|
return NicknameInput.current_frame if defined?(NicknameInput) && NicknameInput.respond_to?(:current_frame)
|
||
|
|
Graphics.frame_count
|
||
|
|
rescue
|
||
|
|
nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.input_trigger_any(*symbols)
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:input_trigger_any)
|
||
|
|
return NicknameInput.input_trigger_any(*symbols)
|
||
|
|
end
|
||
|
|
symbols.any? { |sym| Input.trigger?(sym) }
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.confirm_pressed?
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:confirm_pressed?)
|
||
|
|
return NicknameInput.confirm_pressed?
|
||
|
|
end
|
||
|
|
input_trigger_any(:C, :RETURN, :ENTER)
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.cancel_pressed?
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:cancel_pressed?)
|
||
|
|
return NicknameInput.cancel_pressed?
|
||
|
|
end
|
||
|
|
input_trigger_any(:B, :ESCAPE)
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.typed_text
|
||
|
|
frame = current_frame
|
||
|
|
if !frame.nil?
|
||
|
|
return "" if @typed_text_frame == frame
|
||
|
|
@typed_text_frame = frame
|
||
|
|
end
|
||
|
|
|
||
|
|
text = ""
|
||
|
|
|
||
|
|
begin
|
||
|
|
if defined?(MPChat) && MPChat.respond_to?(:typed_text)
|
||
|
|
text = MPChat.typed_text.to_s
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
text = ""
|
||
|
|
end
|
||
|
|
|
||
|
|
if text.to_s == ""
|
||
|
|
begin
|
||
|
|
if defined?(NicknameInput) && NicknameInput.respond_to?(:typed_chars)
|
||
|
|
text = NicknameInput.typed_chars.to_s
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
text = ""
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if text.to_s == ""
|
||
|
|
begin
|
||
|
|
text = Input.UTF8String.to_s if Input.respond_to?(:UTF8String)
|
||
|
|
rescue
|
||
|
|
text = ""
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
text = text.to_s
|
||
|
|
text = text.gsub(/[\r\n\t]/, "")
|
||
|
|
text = text.gsub("\0", "")
|
||
|
|
text.gsub(/[\u0000-\u001F\u007F]/, "")
|
||
|
|
rescue
|
||
|
|
""
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.current_map_id
|
||
|
|
return 0 unless defined?($game_map) && $game_map
|
||
|
|
$game_map.map_id.to_i
|
||
|
|
rescue
|
||
|
|
0
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.pack_kv(hash)
|
||
|
|
return WebSocket._ps_pack_kv(hash) if WebSocket.respond_to?(:_ps_pack_kv)
|
||
|
|
return "" unless hash.is_a?(Hash)
|
||
|
|
|
||
|
|
out = []
|
||
|
|
hash.each do |k, v|
|
||
|
|
key = k.to_s.gsub(":", ";").gsub(/\r|\n|\t/, " ").gsub("\0", "").strip
|
||
|
|
next if key == ""
|
||
|
|
value = v.nil? ? "nil" : v.to_s
|
||
|
|
value = value.gsub(":", ";").gsub(/\r|\n|\t/, " ").gsub("\0", "")
|
||
|
|
value = value.gsub("|", "/")
|
||
|
|
out << "#{key}=#{value}"
|
||
|
|
end
|
||
|
|
out.join("|")
|
||
|
|
rescue
|
||
|
|
""
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.unpack_kv(text)
|
||
|
|
return WebSocket._ps_unpack_kv(text.to_s) if WebSocket.respond_to?(:_ps_unpack_kv)
|
||
|
|
out = {}
|
||
|
|
text.to_s.split("|").each do |pair|
|
||
|
|
next if pair.to_s == ""
|
||
|
|
key, value = pair.split("=", 2)
|
||
|
|
next if key.to_s == ""
|
||
|
|
out[key.to_s] = value.to_s
|
||
|
|
end
|
||
|
|
out
|
||
|
|
rescue
|
||
|
|
{}
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.slot_key_from_numeric(num)
|
||
|
|
SLOT_BY_NUM[num.to_i]
|
||
|
|
rescue
|
||
|
|
nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.slot_key_from_event(ev)
|
||
|
|
return nil unless ev
|
||
|
|
|
||
|
|
follower = nil
|
||
|
|
begin
|
||
|
|
follower = ev.follower if ev.respond_to?(:follower)
|
||
|
|
rescue
|
||
|
|
follower = nil
|
||
|
|
end
|
||
|
|
if follower.nil?
|
||
|
|
begin
|
||
|
|
follower = ev.instance_variable_get(:@follower)
|
||
|
|
rescue
|
||
|
|
follower = nil
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if follower.is_a?(Array) && follower.size >= 3 && follower[0].to_i == 1
|
||
|
|
slot = slot_key_from_numeric(follower[2])
|
||
|
|
return slot unless slot.nil?
|
||
|
|
end
|
||
|
|
|
||
|
|
summon = nil
|
||
|
|
begin
|
||
|
|
summon = ev.summon_data if ev.respond_to?(:summon_data)
|
||
|
|
rescue
|
||
|
|
summon = nil
|
||
|
|
end
|
||
|
|
if summon.nil?
|
||
|
|
begin
|
||
|
|
summon = ev.instance_variable_get(:@summon_data)
|
||
|
|
rescue
|
||
|
|
summon = nil
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return "front" if summon.is_a?(Hash) && summon[:followerFRONT]
|
||
|
|
return "back" if summon.is_a?(Hash) && summon[:followerBACK]
|
||
|
|
return "ext" if summon.is_a?(Hash) && summon[:followerEXT]
|
||
|
|
nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.local_companion?(ev)
|
||
|
|
return false if ev.nil?
|
||
|
|
|
||
|
|
slot = slot_key_from_event(ev)
|
||
|
|
return false if slot.nil?
|
||
|
|
|
||
|
|
follower = nil
|
||
|
|
begin
|
||
|
|
follower = ev.follower if ev.respond_to?(:follower)
|
||
|
|
rescue
|
||
|
|
follower = nil
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
follower = ev.instance_variable_get(:@follower) if follower.nil?
|
||
|
|
rescue
|
||
|
|
follower = nil
|
||
|
|
end
|
||
|
|
return true if follower.is_a?(Array) && follower[0].to_i == 1
|
||
|
|
|
||
|
|
begin
|
||
|
|
list = $game_player.follower_list if $game_player && $game_player.respond_to?(:follower_list)
|
||
|
|
return true if list.respond_to?(:include?) && ev.respond_to?(:id) && list.include?(ev.id)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
if ev.respond_to?(:npc?) && ev.npc? && ev.npc && ev.npc.master == $game_player
|
||
|
|
return true
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
act = ev.actor if ev.respond_to?(:actor)
|
||
|
|
return true if act && act.respond_to?(:master) && act.master == $game_player
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
recorded = ""
|
||
|
|
begin
|
||
|
|
case slot.to_s
|
||
|
|
when "front"
|
||
|
|
recorded = $game_player.record_companion_name_front.to_s
|
||
|
|
when "back"
|
||
|
|
recorded = $game_player.record_companion_name_back.to_s
|
||
|
|
when "ext"
|
||
|
|
recorded = $game_player.record_companion_name_ext.to_s
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
recorded = ""
|
||
|
|
end
|
||
|
|
|
||
|
|
if recorded != ""
|
||
|
|
names = []
|
||
|
|
begin
|
||
|
|
names << companion_internal_name(ev).to_s
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
names << ev.name.to_s if ev.respond_to?(:name)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
return true if names.include?(recorded)
|
||
|
|
end
|
||
|
|
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.local_companion_event_id?(event_id)
|
||
|
|
return false unless defined?($game_map) && $game_map && $game_map.respond_to?(:events)
|
||
|
|
ev = $game_map.events[event_id.to_i] rescue nil
|
||
|
|
local_companion?(ev)
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.personal_world_event_id?(event_id)
|
||
|
|
eid = event_id.to_i
|
||
|
|
dynamic_base = 50000
|
||
|
|
begin
|
||
|
|
dynamic_base = WebSocket::SERVER_DYNAMIC_EVENT_ID_BASE
|
||
|
|
rescue
|
||
|
|
dynamic_base = 50000
|
||
|
|
end
|
||
|
|
return false if eid >= dynamic_base
|
||
|
|
local_companion_event_id?(eid)
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.personal_dynamic_event_id?(event_id)
|
||
|
|
eid = event_id.to_i
|
||
|
|
eid >= PERSONAL_DYNAMIC_EVENT_ID_BASE && eid <= PERSONAL_DYNAMIC_EVENT_ID_MAX
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.ensure_remote_companion_registry
|
||
|
|
map_id = current_map_id
|
||
|
|
if @remote_companion_registry_map_id.nil? || @remote_companion_registry_map_id.to_i != map_id.to_i
|
||
|
|
@remote_companion_meta_by_server_id = {}
|
||
|
|
@remote_story_companion_event_ids = {}
|
||
|
|
@personal_story_event_ids = {}
|
||
|
|
@remote_companion_registry_map_id = map_id
|
||
|
|
end
|
||
|
|
@remote_companion_meta_by_server_id ||= {}
|
||
|
|
@remote_story_companion_event_ids ||= {}
|
||
|
|
@personal_story_event_ids ||= {}
|
||
|
|
rescue
|
||
|
|
@remote_companion_meta_by_server_id ||= {}
|
||
|
|
@remote_story_companion_event_ids ||= {}
|
||
|
|
@personal_story_event_ids ||= {}
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.companion_meta?(meta)
|
||
|
|
return false unless meta.is_a?(Hash)
|
||
|
|
kind = meta["kind"].to_s.downcase
|
||
|
|
kind == "companion" || kind == "cocona"
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.personal_story_npc_type?(npc_type)
|
||
|
|
token = npc_type.to_s.strip.downcase
|
||
|
|
return false if token == ""
|
||
|
|
return true if token.start_with?("unique")
|
||
|
|
return true if token.include?("compextunique")
|
||
|
|
false
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.story_companion_meta?(meta)
|
||
|
|
return false unless companion_meta?(meta)
|
||
|
|
base_name = meta["base_name"].to_s
|
||
|
|
return true if personal_story_npc_type?(base_name)
|
||
|
|
COCONA_NAMES.any? { |name| name.to_s.downcase == base_name.downcase }
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.remember_remote_companion_meta(server_event_id, meta)
|
||
|
|
ensure_remote_companion_registry
|
||
|
|
return unless companion_meta?(meta)
|
||
|
|
|
||
|
|
eid = server_event_id.to_i
|
||
|
|
stored = meta.dup
|
||
|
|
stored["kind"] = "companion"
|
||
|
|
@remote_companion_meta_by_server_id[eid] = stored
|
||
|
|
|
||
|
|
if story_companion_meta?(stored) && !remote_companion_visible_for_me?(stored["owner_id"])
|
||
|
|
template_id = (stored["template_id"] || stored["template"]).to_i
|
||
|
|
if template_id > 0
|
||
|
|
dynamic_base = 50000
|
||
|
|
begin
|
||
|
|
dynamic_base = WebSocket::SERVER_DYNAMIC_EVENT_ID_BASE
|
||
|
|
rescue
|
||
|
|
dynamic_base = 50000
|
||
|
|
end
|
||
|
|
@remote_story_companion_event_ids[template_id] = true if template_id < dynamic_base
|
||
|
|
end
|
||
|
|
end
|
||
|
|
stored
|
||
|
|
rescue
|
||
|
|
nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.forget_remote_companion_meta(server_event_id)
|
||
|
|
ensure_remote_companion_registry
|
||
|
|
@remote_companion_meta_by_server_id.delete(server_event_id.to_i)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.remote_companion_meta_for(server_event_id)
|
||
|
|
ensure_remote_companion_registry
|
||
|
|
@remote_companion_meta_by_server_id[server_event_id.to_i]
|
||
|
|
rescue
|
||
|
|
nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.mark_personal_story_event(server_event_id, npc_type)
|
||
|
|
ensure_remote_companion_registry
|
||
|
|
eid = server_event_id.to_i
|
||
|
|
if personal_story_npc_type?(npc_type)
|
||
|
|
@personal_story_event_ids[eid] = true
|
||
|
|
else
|
||
|
|
@personal_story_event_ids.delete(eid)
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.personal_story_world_event_id?(event_id)
|
||
|
|
ensure_remote_companion_registry
|
||
|
|
eid = event_id.to_i
|
||
|
|
return false if eid <= 0
|
||
|
|
|
||
|
|
dynamic_base = 50000
|
||
|
|
begin
|
||
|
|
dynamic_base = WebSocket::SERVER_DYNAMIC_EVENT_ID_BASE
|
||
|
|
rescue
|
||
|
|
dynamic_base = 50000
|
||
|
|
end
|
||
|
|
return false if eid >= dynamic_base
|
||
|
|
return true if @remote_story_companion_event_ids[eid]
|
||
|
|
return true if @personal_story_event_ids[eid]
|
||
|
|
|
||
|
|
ev = nil
|
||
|
|
begin
|
||
|
|
ev = $game_map.events[eid] if $game_map && $game_map.respond_to?(:events)
|
||
|
|
rescue
|
||
|
|
ev = nil
|
||
|
|
end
|
||
|
|
npc_type = ""
|
||
|
|
begin
|
||
|
|
if ev && ev.respond_to?(:npc?) && ev.npc? && ev.npc
|
||
|
|
npc_type = ev.npc.npc_name.to_s if ev.npc.respond_to?(:npc_name)
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
npc_type = ""
|
||
|
|
end
|
||
|
|
personal_story_npc_type?(npc_type)
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
# Unknown ids in the server's reserved companion range are blocked until an
|
||
|
|
# owner-bearing npc_type packet arrives. Foreign companions remain blocked
|
||
|
|
# by server id even after their temporary Game_Event object is removed.
|
||
|
|
def self.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
ensure_remote_companion_registry
|
||
|
|
eid = server_event_id.to_i
|
||
|
|
return true if personal_story_world_event_id?(eid)
|
||
|
|
|
||
|
|
meta = @remote_companion_meta_by_server_id[eid]
|
||
|
|
if personal_dynamic_event_id?(eid)
|
||
|
|
return true unless companion_meta?(meta)
|
||
|
|
return !remote_companion_visible_for_me?(meta["owner_id"])
|
||
|
|
end
|
||
|
|
return false unless companion_meta?(meta)
|
||
|
|
|
||
|
|
dynamic_base = 50000
|
||
|
|
begin
|
||
|
|
dynamic_base = WebSocket::SERVER_DYNAMIC_EVENT_ID_BASE
|
||
|
|
rescue
|
||
|
|
dynamic_base = 50000
|
||
|
|
end
|
||
|
|
return true if eid < dynamic_base
|
||
|
|
!remote_companion_visible_for_me?(meta["owner_id"])
|
||
|
|
rescue
|
||
|
|
eid = server_event_id.to_i rescue 0
|
||
|
|
eid >= PERSONAL_DYNAMIC_EVENT_ID_BASE && eid <= PERSONAL_DYNAMIC_EVENT_ID_MAX
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.remote_companion_creation_blocked?(server_event_id)
|
||
|
|
eid = server_event_id.to_i
|
||
|
|
return false unless personal_dynamic_event_id?(eid) || companion_meta?(remote_companion_meta_for(eid))
|
||
|
|
incoming_companion_packet_blocked?(eid)
|
||
|
|
rescue
|
||
|
|
eid = server_event_id.to_i rescue 0
|
||
|
|
eid >= PERSONAL_DYNAMIC_EVENT_ID_BASE && eid <= PERSONAL_DYNAMIC_EVENT_ID_MAX
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.local_companions
|
||
|
|
out = {}
|
||
|
|
return out unless defined?($game_map) && $game_map && $game_map.respond_to?(:npcs)
|
||
|
|
|
||
|
|
begin
|
||
|
|
$game_map.npcs.each do |ev|
|
||
|
|
next unless local_companion?(ev)
|
||
|
|
slot = slot_key_from_event(ev)
|
||
|
|
next if slot.nil?
|
||
|
|
out[slot] = ev unless out[slot]
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
out
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.companion_internal_name(ev)
|
||
|
|
return "" if ev.nil?
|
||
|
|
|
||
|
|
begin
|
||
|
|
if ev.respond_to?(:npc?) && ev.npc? && ev.npc && ev.npc.respond_to?(:npc_name)
|
||
|
|
name = ev.npc.npc_name.to_s
|
||
|
|
return name unless name == ""
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
name = ev.name.to_s if ev.respond_to?(:name)
|
||
|
|
return name unless name.to_s == ""
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
return ev.event.name.to_s if ev.respond_to?(:event) && ev.event
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
""
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.pretty_name(name)
|
||
|
|
raw = name.to_s
|
||
|
|
raw = "Cocona" if raw == ""
|
||
|
|
raw = raw.sub(/\AUnique/, "")
|
||
|
|
raw = raw.gsub(/_+/, " ")
|
||
|
|
raw = raw.gsub(/([a-z])([A-Z])/, "\\1 \\2")
|
||
|
|
raw.strip
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.cocona_name?(name)
|
||
|
|
token = name.to_s.downcase
|
||
|
|
COCONA_NAMES.any? { |entry| entry.downcase == token }
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.cocona_event?(ev)
|
||
|
|
cocona_name?(companion_internal_name(ev))
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.label_for_companion(ev)
|
||
|
|
if cocona_event?(ev)
|
||
|
|
label = load_cocona_label.to_s
|
||
|
|
label = "Cocona" if label.strip == ""
|
||
|
|
return label
|
||
|
|
end
|
||
|
|
pretty_name(companion_internal_name(ev))
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.visual_payload_for(ev)
|
||
|
|
char_name = ""
|
||
|
|
char_index = 0
|
||
|
|
chs_type = ""
|
||
|
|
charset_index = 0
|
||
|
|
cfg_pack = ""
|
||
|
|
|
||
|
|
begin
|
||
|
|
char_name = ev.character_name.to_s if ev.respond_to?(:character_name)
|
||
|
|
rescue
|
||
|
|
char_name = ""
|
||
|
|
end
|
||
|
|
if char_name == ""
|
||
|
|
begin
|
||
|
|
char_name = ev.instance_variable_get(:@character_name).to_s
|
||
|
|
rescue
|
||
|
|
char_name = ""
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
char_index = ev.character_index.to_i if ev.respond_to?(:character_index)
|
||
|
|
rescue
|
||
|
|
char_index = 0
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
chs_type = ev.instance_variable_get(:@chs_type).to_s
|
||
|
|
rescue
|
||
|
|
chs_type = ""
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
charset_index = ev.instance_variable_get(:@charset_index).to_i
|
||
|
|
rescue
|
||
|
|
charset_index = 0
|
||
|
|
end
|
||
|
|
|
||
|
|
cfg = nil
|
||
|
|
begin
|
||
|
|
cfg = ev.instance_variable_get(:@chs_configuration)
|
||
|
|
rescue
|
||
|
|
cfg = nil
|
||
|
|
end
|
||
|
|
cfg = {} unless cfg.is_a?(Hash)
|
||
|
|
cfg_pack = pack_kv(cfg) if !cfg.empty?
|
||
|
|
|
||
|
|
if char_name.to_s == ""
|
||
|
|
begin
|
||
|
|
page = nil
|
||
|
|
if ev.respond_to?(:page) && ev.page
|
||
|
|
page = ev.page
|
||
|
|
elsif ev.respond_to?(:event) && ev.event && ev.event.pages && !ev.event.pages.empty?
|
||
|
|
idx = (ev.instance_variable_get(:@page_index) rescue 0).to_i
|
||
|
|
idx = 0 if idx < 0
|
||
|
|
idx = ev.event.pages.size - 1 if idx >= ev.event.pages.size
|
||
|
|
page = ev.event.pages[idx]
|
||
|
|
end
|
||
|
|
if page && page.respond_to?(:graphic) && page.graphic
|
||
|
|
char_name = page.graphic.character_name.to_s if char_name.to_s == ""
|
||
|
|
char_index = page.graphic.character_index.to_i if char_index.to_i == 0
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
[char_name, char_index, chs_type, charset_index, cfg_pack]
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.build_sync_packet(slot_key, ev)
|
||
|
|
return nil if slot_key.to_s == "" || ev.nil?
|
||
|
|
|
||
|
|
label = sanitize_label(label_for_companion(ev))
|
||
|
|
base_name = companion_internal_name(ev).to_s
|
||
|
|
template_id = 0
|
||
|
|
begin
|
||
|
|
template_id = ev.id.to_i if ev.respond_to?(:id)
|
||
|
|
rescue
|
||
|
|
template_id = 0
|
||
|
|
end
|
||
|
|
|
||
|
|
char_name, char_index, chs_type, charset_index, cfg_pack = visual_payload_for(ev)
|
||
|
|
pattern = 0
|
||
|
|
begin
|
||
|
|
pattern = ev.instance_variable_get(:@pattern).to_i
|
||
|
|
rescue
|
||
|
|
pattern = 0
|
||
|
|
end
|
||
|
|
|
||
|
|
fields = [
|
||
|
|
slot_key.to_s,
|
||
|
|
WebSocket.esc(label),
|
||
|
|
WebSocket.esc(base_name),
|
||
|
|
template_id.to_i,
|
||
|
|
ev.x.to_i,
|
||
|
|
ev.y.to_i,
|
||
|
|
ev.direction.to_i,
|
||
|
|
pattern.to_i,
|
||
|
|
WebSocket.esc(char_name.to_s),
|
||
|
|
char_index.to_i,
|
||
|
|
WebSocket.esc(chs_type.to_s),
|
||
|
|
charset_index.to_i,
|
||
|
|
WebSocket.esc(cfg_pack.to_s),
|
||
|
|
]
|
||
|
|
|
||
|
|
"companion_sync2:" + fields.join(":")
|
||
|
|
rescue
|
||
|
|
nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.send_hide(slot_key)
|
||
|
|
slot = slot_key.to_s
|
||
|
|
@last_packets ||= {}
|
||
|
|
@last_packets.delete(slot)
|
||
|
|
return unless WebSocket.connected?
|
||
|
|
WebSocket.send_line("companion_hide:#{slot}")
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.send_legacy_cocona_hide
|
||
|
|
return unless WebSocket.connected?
|
||
|
|
WebSocket.send_line("cocona_hide")
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.send_all_hides
|
||
|
|
slots = []
|
||
|
|
@last_packets ||= {}
|
||
|
|
slots.concat(@last_packets.keys)
|
||
|
|
slots.concat(local_companions.keys)
|
||
|
|
slots.compact.map { |slot| slot.to_s }.uniq.each do |slot|
|
||
|
|
send_hide(slot)
|
||
|
|
end
|
||
|
|
send_legacy_cocona_hide
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.reset_sync_state
|
||
|
|
@last_packets = {}
|
||
|
|
@frame = 0
|
||
|
|
@last_map_id = current_map_id
|
||
|
|
@server_hide_map_id = nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.network_ready?
|
||
|
|
return false unless WebSocket.connected?
|
||
|
|
return false unless WebSocket.worldsync?
|
||
|
|
return false unless defined?(SceneManager) && defined?(Scene_Map) && SceneManager.scene_is?(Scene_Map)
|
||
|
|
true
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.before_network_update
|
||
|
|
cur_map = current_map_id
|
||
|
|
@last_map_id = cur_map if @last_map_id.nil?
|
||
|
|
if WebSocket.connected? && @last_map_id.to_i != cur_map.to_i
|
||
|
|
send_all_hides
|
||
|
|
end
|
||
|
|
@last_map_id = cur_map
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.network_update
|
||
|
|
return unless network_ready?
|
||
|
|
@frame ||= 0
|
||
|
|
@frame += 1
|
||
|
|
return if (@frame % SYNC_EVERY_FRAMES) != 0
|
||
|
|
|
||
|
|
unless NETWORK_REPLICATION_ENABLED
|
||
|
|
cur_map = current_map_id
|
||
|
|
if @server_hide_map_id.nil? || @server_hide_map_id.to_i != cur_map.to_i
|
||
|
|
%w[front back ext].each { |slot| send_hide(slot) }
|
||
|
|
send_legacy_cocona_hide
|
||
|
|
@server_hide_map_id = cur_map
|
||
|
|
end
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
current = local_companions
|
||
|
|
sent_slots = []
|
||
|
|
@last_packets ||= {}
|
||
|
|
|
||
|
|
current.each do |slot, ev|
|
||
|
|
packet = build_sync_packet(slot, ev)
|
||
|
|
next if packet.nil? || packet == ""
|
||
|
|
sent_slots << slot.to_s
|
||
|
|
next if @last_packets[slot.to_s] == packet
|
||
|
|
WebSocket.send_line(packet)
|
||
|
|
@last_packets[slot.to_s] = packet
|
||
|
|
end
|
||
|
|
|
||
|
|
(@last_packets.keys - sent_slots).each do |slot|
|
||
|
|
send_hide(slot)
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.recorded_companion_names
|
||
|
|
out = {}
|
||
|
|
return out unless defined?($game_player) && $game_player
|
||
|
|
|
||
|
|
begin
|
||
|
|
out["front"] = $game_player.record_companion_name_front.to_s
|
||
|
|
rescue
|
||
|
|
out["front"] = ""
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
out["back"] = $game_player.record_companion_name_back.to_s
|
||
|
|
rescue
|
||
|
|
out["back"] = ""
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
out["ext"] = $game_player.record_companion_name_ext.to_s
|
||
|
|
rescue
|
||
|
|
out["ext"] = ""
|
||
|
|
end
|
||
|
|
out
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.queue_cocona_prompt(slot_key)
|
||
|
|
slot = slot_key.to_s
|
||
|
|
return if slot == ""
|
||
|
|
return unless cocona_prompt_needed?
|
||
|
|
@queued_prompt_slot = slot
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.cocona_prompt_needed?
|
||
|
|
load_cocona_label.to_s.strip == ""
|
||
|
|
rescue
|
||
|
|
true
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.queue_cocona_prompt_for_event(ev)
|
||
|
|
return unless cocona_event?(ev)
|
||
|
|
return unless cocona_prompt_needed?
|
||
|
|
queue_cocona_prompt(slot_key_from_event(ev) || "back")
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.watch_for_cocona_hire
|
||
|
|
current = recorded_companion_names
|
||
|
|
|
||
|
|
if @record_snapshot.nil?
|
||
|
|
@record_snapshot = current
|
||
|
|
current.each do |slot, name|
|
||
|
|
if cocona_name?(name) && load_cocona_label.to_s.strip == ""
|
||
|
|
queue_cocona_prompt(slot)
|
||
|
|
break
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
current.each do |slot, name|
|
||
|
|
prev = @record_snapshot[slot].to_s
|
||
|
|
next unless cocona_name?(name)
|
||
|
|
next unless cocona_prompt_needed?
|
||
|
|
queue_cocona_prompt(slot) unless cocona_name?(prev)
|
||
|
|
end
|
||
|
|
|
||
|
|
@record_snapshot = current
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.prompt_queued?
|
||
|
|
@queued_prompt_slot.to_s != ""
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.input_passthrough?
|
||
|
|
!!@input_passthrough
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.with_input_passthrough
|
||
|
|
@input_passthrough = true
|
||
|
|
yield
|
||
|
|
ensure
|
||
|
|
@input_passthrough = false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.recently_closed?
|
||
|
|
frame = current_frame
|
||
|
|
!frame.nil? && @last_close_frame == frame
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.scene_update
|
||
|
|
watch_for_cocona_hire
|
||
|
|
ensure_prompt_window
|
||
|
|
with_input_passthrough do
|
||
|
|
@prompt_window.update if @prompt_window
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.scene_terminate
|
||
|
|
dispose_prompt_window
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.ui_active?
|
||
|
|
!@prompt_window.nil? || prompt_queued?
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.ensure_prompt_window
|
||
|
|
return if @prompt_window
|
||
|
|
slot = @queued_prompt_slot.to_s
|
||
|
|
return if slot == ""
|
||
|
|
return unless defined?(SceneManager) && defined?(Scene_Map) && SceneManager.scene_is?(Scene_Map)
|
||
|
|
|
||
|
|
@prompt_window = Window_MPCoconaNickname.new(load_cocona_label.to_s)
|
||
|
|
@prompt_window.set_handler(:ok, method(:on_prompt_ok))
|
||
|
|
@prompt_window.set_handler(:cancel, method(:on_prompt_cancel))
|
||
|
|
rescue
|
||
|
|
@prompt_window = nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.dispose_prompt_window
|
||
|
|
return unless @prompt_window
|
||
|
|
begin
|
||
|
|
@prompt_window.dispose
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
@prompt_window = nil
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.finish_prompt
|
||
|
|
@queued_prompt_slot = nil
|
||
|
|
@record_snapshot = recorded_companion_names
|
||
|
|
@last_close_frame = current_frame
|
||
|
|
@last_packets.delete("back") if @last_packets
|
||
|
|
@last_packets.delete("front") if @last_packets
|
||
|
|
@last_packets.delete("ext") if @last_packets
|
||
|
|
dispose_prompt_window
|
||
|
|
rescue
|
||
|
|
dispose_prompt_window
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.on_prompt_ok(text)
|
||
|
|
value = sanitize_label(text)
|
||
|
|
value = "Cocona" if value == ""
|
||
|
|
save_cocona_label(value)
|
||
|
|
finish_prompt
|
||
|
|
rescue
|
||
|
|
finish_prompt
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.on_prompt_cancel
|
||
|
|
if load_cocona_label.to_s.strip == ""
|
||
|
|
save_cocona_label("Cocona")
|
||
|
|
end
|
||
|
|
finish_prompt
|
||
|
|
rescue
|
||
|
|
finish_prompt
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.meta_from_npc_type(text)
|
||
|
|
out = {}
|
||
|
|
text.to_s.split("|").each do |pair|
|
||
|
|
next if pair.to_s == ""
|
||
|
|
key, value = pair.split("=", 2)
|
||
|
|
next if key.to_s == ""
|
||
|
|
out[key.to_s] = WebSocket.unesc(value.to_s)
|
||
|
|
end
|
||
|
|
out
|
||
|
|
rescue
|
||
|
|
{}
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.apply_remote_regular_visual(ev, char_name, char_index)
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@net_actor, nil)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@chs_configuration, nil)
|
||
|
|
ev.instance_variable_set(:@chs_type, "")
|
||
|
|
ev.instance_variable_set(:@charset_index, 0)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@character_name, char_name.to_s)
|
||
|
|
ev.instance_variable_set(:@manual_character_name, char_name.to_s)
|
||
|
|
ev.instance_variable_set(:@character_index, char_index.to_i)
|
||
|
|
ev.instance_variable_set(:@manual_character_index, char_index.to_i)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.apply_remote_chs_visual(ev, owner_id, slot_key, chs_type, charset_index, cfg_pack)
|
||
|
|
cfg = unpack_kv(cfg_pack.to_s)
|
||
|
|
actor = nil
|
||
|
|
|
||
|
|
begin
|
||
|
|
actor = ev.instance_variable_get(:@net_actor)
|
||
|
|
rescue
|
||
|
|
actor = nil
|
||
|
|
end
|
||
|
|
actor = NetCompanionActor.new unless actor && actor.respond_to?(:stat)
|
||
|
|
|
||
|
|
begin
|
||
|
|
actor.stat = cfg
|
||
|
|
rescue
|
||
|
|
actor.instance_variable_set(:@stat, cfg) rescue nil
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
hair_color = cfg["HairColor"] || cfg[:HairColor] || 0
|
||
|
|
actor.record_HairColor = hair_color.to_i if actor.respond_to?(:record_HairColor=)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@net_actor, actor)
|
||
|
|
ev.instance_variable_set(:@chs_configuration, cfg.dup)
|
||
|
|
ev.instance_variable_set(:@chs_type, chs_type.to_s)
|
||
|
|
ev.instance_variable_set(:@charset_index, charset_index.to_i)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
real_char_index = 0
|
||
|
|
chs_def = ($chs_data && $chs_data[chs_type.to_s]) ? $chs_data[chs_type.to_s] : nil
|
||
|
|
if chs_def && chs_def.respond_to?(:chs_default_index)
|
||
|
|
arr = chs_def.chs_default_index
|
||
|
|
if arr && !arr[charset_index.to_i].nil?
|
||
|
|
real_char_index = arr[charset_index.to_i].to_i
|
||
|
|
end
|
||
|
|
end
|
||
|
|
ev.instance_variable_set(:@character_index, real_char_index)
|
||
|
|
ev.instance_variable_set(:@ps_sync_character_index, real_char_index)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
cache_key = "NetComp_#{owner_id}_#{slot_key}"
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@chs_name, cache_key)
|
||
|
|
ev.instance_variable_set(:@chs_name_h, cache_key + "_H")
|
||
|
|
ev.instance_variable_set(:@character_name, "")
|
||
|
|
ev.instance_variable_set(:@manual_character_name, "")
|
||
|
|
ev.instance_variable_set(:@chs_need_update, true)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.remote_companion_visible_for_me?(owner_id)
|
||
|
|
oid = owner_id.to_s.to_i
|
||
|
|
return false if oid <= 0
|
||
|
|
|
||
|
|
my_id = 0
|
||
|
|
begin
|
||
|
|
my_id = WebSocket.my_id.to_i if WebSocket.respond_to?(:my_id)
|
||
|
|
rescue
|
||
|
|
my_id = 0
|
||
|
|
end
|
||
|
|
return false if my_id <= 0
|
||
|
|
|
||
|
|
oid == my_id
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.hide_remote_companion_for_local_client(ev)
|
||
|
|
return if ev.nil?
|
||
|
|
|
||
|
|
server_event_id = 0
|
||
|
|
begin
|
||
|
|
server_event_id = ev.instance_variable_get(:@net_server_event_id).to_i
|
||
|
|
rescue
|
||
|
|
server_event_id = 0
|
||
|
|
end
|
||
|
|
|
||
|
|
dynamic_base = 50000
|
||
|
|
begin
|
||
|
|
dynamic_base = WebSocket::SERVER_DYNAMIC_EVENT_ID_BASE
|
||
|
|
rescue
|
||
|
|
dynamic_base = 50000
|
||
|
|
end
|
||
|
|
|
||
|
|
# A static RPG Maker event belongs to this player's own quest state. A
|
||
|
|
# foreign packet must never make that local event transparent or inert.
|
||
|
|
return if server_event_id <= 0 || server_event_id < dynamic_base
|
||
|
|
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@net_companion_hidden_for_local, true)
|
||
|
|
ev.instance_variable_set(:@net_companion_label, nil)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
if server_event_id >= dynamic_base && WebSocket.respond_to?(:remove_remote_event)
|
||
|
|
WebSocket.remove_remote_event(server_event_id)
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
ev.transparent = true if ev.respond_to?(:transparent=)
|
||
|
|
ev.instance_variable_set(:@through, true)
|
||
|
|
ev.instance_variable_set(:@move_type, 0)
|
||
|
|
ev.instance_variable_set(:@stop_count, 0)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.remote_companion_interaction_blocked?(ev)
|
||
|
|
return false if ev.nil?
|
||
|
|
|
||
|
|
begin
|
||
|
|
return true if ev.instance_variable_get(:@net_companion_hidden_for_local)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
kind = ""
|
||
|
|
begin
|
||
|
|
kind = ev.instance_variable_get(:@net_companion_kind).to_s
|
||
|
|
rescue
|
||
|
|
kind = ""
|
||
|
|
end
|
||
|
|
return false unless kind == "companion"
|
||
|
|
|
||
|
|
owner_id = 0
|
||
|
|
begin
|
||
|
|
owner_id = ev.instance_variable_get(:@net_companion_owner_id).to_i
|
||
|
|
rescue
|
||
|
|
owner_id = 0
|
||
|
|
end
|
||
|
|
|
||
|
|
!remote_companion_visible_for_me?(owner_id)
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.clear_remote_companion_meta(ev)
|
||
|
|
return if ev.nil?
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@net_companion_kind, nil)
|
||
|
|
ev.instance_variable_set(:@net_companion_slot, nil)
|
||
|
|
ev.instance_variable_set(:@net_companion_owner_id, nil)
|
||
|
|
ev.instance_variable_set(:@net_companion_label, nil)
|
||
|
|
ev.instance_variable_set(:@net_companion_hidden_for_local, false)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def self.apply_remote_companion_meta(ev, meta)
|
||
|
|
return if ev.nil? || !meta.is_a?(Hash)
|
||
|
|
return unless companion_meta?(meta)
|
||
|
|
|
||
|
|
slot_key = meta["slot"].to_s
|
||
|
|
owner_id = meta["owner_id"].to_s.to_i
|
||
|
|
|
||
|
|
label = meta["label"].to_s
|
||
|
|
label = meta["base_name"].to_s if label.strip == ""
|
||
|
|
label = pretty_name(label)
|
||
|
|
|
||
|
|
begin
|
||
|
|
ev.instance_variable_set(:@net_companion_kind, "companion")
|
||
|
|
ev.instance_variable_set(:@net_companion_slot, slot_key)
|
||
|
|
ev.instance_variable_set(:@net_companion_owner_id, owner_id)
|
||
|
|
ev.instance_variable_set(:@net_companion_label, label)
|
||
|
|
ev.instance_variable_set(:@net_companion_hidden_for_local, !remote_companion_visible_for_me?(owner_id))
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
unless remote_companion_visible_for_me?(owner_id)
|
||
|
|
hide_remote_companion_for_local_client(ev)
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
chs_type = meta["chs_type"].to_s
|
||
|
|
charset_index = meta["charset_index"].to_i
|
||
|
|
cfg_pack = meta["cfg_pack"].to_s
|
||
|
|
char_name = meta["char_name"].to_s
|
||
|
|
char_index = meta["char_index"].to_i
|
||
|
|
|
||
|
|
if chs_type != "" && cfg_pack != ""
|
||
|
|
apply_remote_chs_visual(ev, owner_id, slot_key, chs_type, charset_index, cfg_pack)
|
||
|
|
else
|
||
|
|
apply_remote_regular_visual(ev, char_name, char_index)
|
||
|
|
end
|
||
|
|
|
||
|
|
begin
|
||
|
|
ev.transparent = false
|
||
|
|
ev.opacity = 255 if ev.respond_to?(:opacity=)
|
||
|
|
ev.instance_variable_set(:@move_type, 0)
|
||
|
|
ev.instance_variable_set(:@stop_count, 0)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
class Window_MPCoconaNickname < Window_Base
|
||
|
|
def initialize(start_text = "")
|
||
|
|
w = [360, (Graphics.width * 0.72).to_i].max
|
||
|
|
h = fitting_height(3)
|
||
|
|
x = (Graphics.width - w) / 2
|
||
|
|
y = (Graphics.height - h) / 2
|
||
|
|
super(x, y, w, h)
|
||
|
|
self.z = 9999
|
||
|
|
@text = MPCompanionSync.sanitize_label(start_text)
|
||
|
|
@ok_handler = nil
|
||
|
|
@cancel_handler = nil
|
||
|
|
@last_input_frame = nil
|
||
|
|
activate
|
||
|
|
refresh
|
||
|
|
end
|
||
|
|
|
||
|
|
def set_handler(sym, handler)
|
||
|
|
@ok_handler = handler if sym == :ok
|
||
|
|
@cancel_handler = handler if sym == :cancel
|
||
|
|
end
|
||
|
|
|
||
|
|
def update
|
||
|
|
super
|
||
|
|
return unless active
|
||
|
|
|
||
|
|
frame = MPCompanionSync.current_frame
|
||
|
|
if !frame.nil?
|
||
|
|
return if @last_input_frame == frame
|
||
|
|
@last_input_frame = frame
|
||
|
|
end
|
||
|
|
|
||
|
|
typed = MPCompanionSync.typed_text
|
||
|
|
if typed && typed != ""
|
||
|
|
typed.each_char do |ch|
|
||
|
|
break if @text.length >= MPCompanionSync::MAX_NAME_LEN
|
||
|
|
@text << ch
|
||
|
|
end
|
||
|
|
refresh
|
||
|
|
end
|
||
|
|
|
||
|
|
if Input.repeat?(:BACK) || Input.repeat?(:DELETE)
|
||
|
|
if @text.length > 0
|
||
|
|
@text.chop!
|
||
|
|
refresh
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if MPCompanionSync.confirm_pressed?
|
||
|
|
value = MPCompanionSync.sanitize_label(@text)
|
||
|
|
value = "Cocona" if value == ""
|
||
|
|
deactivate
|
||
|
|
@ok_handler.call(value) if @ok_handler
|
||
|
|
elsif MPCompanionSync.cancel_pressed?
|
||
|
|
deactivate
|
||
|
|
@cancel_handler.call if @cancel_handler
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def refresh
|
||
|
|
contents.clear
|
||
|
|
contents.font.name = MPCompanionSync.ui_font_name
|
||
|
|
contents.font.size = 18
|
||
|
|
contents.font.bold = false
|
||
|
|
contents.font.outline = false
|
||
|
|
contents.font.shadow = true
|
||
|
|
|
||
|
|
draw_text(0, 0, contents_width, line_height, "Name for Cocona", 1)
|
||
|
|
|
||
|
|
cursor = ((Graphics.frame_count / 20) % 2 == 0) ? "|" : " "
|
||
|
|
draw_text(0, line_height, contents_width, line_height, @text.to_s + cursor, 1)
|
||
|
|
|
||
|
|
change_color(system_color)
|
||
|
|
draw_text(0, line_height * 2, contents_width, line_height, "Enter - OK, Esc - Cancel", 1)
|
||
|
|
change_color(normal_color)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
class Scene_Map < Scene_Base
|
||
|
|
unless method_defined?(:mp_companion_prompt_start)
|
||
|
|
alias mp_companion_prompt_start start
|
||
|
|
end
|
||
|
|
|
||
|
|
def start(*args, &block)
|
||
|
|
mp_companion_prompt_start(*args, &block)
|
||
|
|
MPCompanionSync.reset_sync_state if defined?(MPCompanionSync)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_prompt_update)
|
||
|
|
alias mp_companion_prompt_update update
|
||
|
|
end
|
||
|
|
|
||
|
|
def update(*args, &block)
|
||
|
|
mp_companion_prompt_update(*args, &block)
|
||
|
|
MPCompanionSync.scene_update if defined?(MPCompanionSync)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_prompt_terminate)
|
||
|
|
alias mp_companion_prompt_terminate terminate
|
||
|
|
end
|
||
|
|
|
||
|
|
def terminate(*args, &block)
|
||
|
|
MPCompanionSync.scene_terminate if defined?(MPCompanionSync)
|
||
|
|
mp_companion_prompt_terminate(*args, &block)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
class Game_Player
|
||
|
|
if method_defined?(:movable?) && !method_defined?(:mp_companion_prompt_movable)
|
||
|
|
alias mp_companion_prompt_movable movable?
|
||
|
|
|
||
|
|
def movable?
|
||
|
|
return false if defined?(MPCompanionSync) && MPCompanionSync.ui_active?
|
||
|
|
mp_companion_prompt_movable
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
module Input
|
||
|
|
class << self
|
||
|
|
unless method_defined?(:mp_companion_input_orig_trigger)
|
||
|
|
alias mp_companion_input_orig_trigger trigger?
|
||
|
|
alias mp_companion_input_orig_press press?
|
||
|
|
alias mp_companion_input_orig_repeat repeat?
|
||
|
|
alias mp_companion_input_orig_dir4 dir4
|
||
|
|
alias mp_companion_input_orig_dir8 dir8
|
||
|
|
end
|
||
|
|
|
||
|
|
def mp_companion_input_masked?
|
||
|
|
return false unless defined?(MPCompanionSync)
|
||
|
|
return false unless defined?(SceneManager) && defined?(Scene_Map) && SceneManager.scene_is?(Scene_Map)
|
||
|
|
return false if MPCompanionSync.respond_to?(:input_passthrough?) && MPCompanionSync.input_passthrough?
|
||
|
|
MPCompanionSync.ui_active? || MPCompanionSync.recently_closed?
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
def trigger?(sym)
|
||
|
|
return false if mp_companion_input_masked?
|
||
|
|
mp_companion_input_orig_trigger(sym)
|
||
|
|
end
|
||
|
|
|
||
|
|
def press?(sym)
|
||
|
|
return false if mp_companion_input_masked?
|
||
|
|
mp_companion_input_orig_press(sym)
|
||
|
|
end
|
||
|
|
|
||
|
|
def repeat?(sym)
|
||
|
|
return false if mp_companion_input_masked?
|
||
|
|
mp_companion_input_orig_repeat(sym)
|
||
|
|
end
|
||
|
|
|
||
|
|
def dir4
|
||
|
|
return 0 if mp_companion_input_masked?
|
||
|
|
mp_companion_input_orig_dir4
|
||
|
|
end
|
||
|
|
|
||
|
|
def dir8
|
||
|
|
return 0 if mp_companion_input_masked?
|
||
|
|
mp_companion_input_orig_dir8
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
class Game_Event < Game_Character
|
||
|
|
# Remote PWO-копия чужого спутника не должна становиться локальным follower'ом.
|
||
|
|
def mp_companion_remote_foreign_replica?
|
||
|
|
begin
|
||
|
|
return true if instance_variable_get(:@mp_remote_player_object_id).to_i > 0
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
# Во время materialize id ещё не проставлен, но building-флаг уже true.
|
||
|
|
begin
|
||
|
|
return true if defined?(WebSocket) && WebSocket.respond_to?(:player_object_building_remote?) &&
|
||
|
|
WebSocket.player_object_building_remote?
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
return true if instance_variable_defined?(:@net_companion_kind) &&
|
||
|
|
!instance_variable_get(:@net_companion_kind).to_s.empty?
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
false
|
||
|
|
rescue
|
||
|
|
false
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:set_this_event_companion_front) && !method_defined?(:mp_companion_orig_set_front)
|
||
|
|
alias mp_companion_orig_set_front set_this_event_companion_front
|
||
|
|
|
||
|
|
def set_this_event_companion_front(*args, &block)
|
||
|
|
return if mp_companion_remote_foreign_replica?
|
||
|
|
result = mp_companion_orig_set_front(*args, &block)
|
||
|
|
MPCompanionSync.queue_cocona_prompt_for_event(self) if defined?(MPCompanionSync)
|
||
|
|
result
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:set_this_event_companion_back) && !method_defined?(:mp_companion_orig_set_back)
|
||
|
|
alias mp_companion_orig_set_back set_this_event_companion_back
|
||
|
|
|
||
|
|
def set_this_event_companion_back(*args, &block)
|
||
|
|
return if mp_companion_remote_foreign_replica?
|
||
|
|
result = mp_companion_orig_set_back(*args, &block)
|
||
|
|
MPCompanionSync.queue_cocona_prompt_for_event(self) if defined?(MPCompanionSync)
|
||
|
|
result
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:set_this_event_companion_ext) && !method_defined?(:mp_companion_orig_set_ext)
|
||
|
|
alias mp_companion_orig_set_ext set_this_event_companion_ext
|
||
|
|
|
||
|
|
def set_this_event_companion_ext(*args, &block)
|
||
|
|
return if mp_companion_remote_foreign_replica?
|
||
|
|
result = mp_companion_orig_set_ext(*args, &block)
|
||
|
|
MPCompanionSync.queue_cocona_prompt_for_event(self) if defined?(MPCompanionSync)
|
||
|
|
result
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:set_this_event_companion_back_lite) && !method_defined?(:mp_companion_orig_set_back_lite)
|
||
|
|
alias mp_companion_orig_set_back_lite set_this_event_companion_back_lite
|
||
|
|
|
||
|
|
def set_this_event_companion_back_lite(*args, &block)
|
||
|
|
return if mp_companion_remote_foreign_replica?
|
||
|
|
result = mp_companion_orig_set_back_lite(*args, &block)
|
||
|
|
MPCompanionSync.queue_cocona_prompt_for_event(self) if defined?(MPCompanionSync)
|
||
|
|
result
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:set_this_event_companion_front_lite) && !method_defined?(:mp_companion_orig_set_front_lite)
|
||
|
|
alias mp_companion_orig_set_front_lite set_this_event_companion_front_lite
|
||
|
|
|
||
|
|
def set_this_event_companion_front_lite(*args, &block)
|
||
|
|
return if mp_companion_remote_foreign_replica?
|
||
|
|
result = mp_companion_orig_set_front_lite(*args, &block)
|
||
|
|
MPCompanionSync.queue_cocona_prompt_for_event(self) if defined?(MPCompanionSync)
|
||
|
|
result
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:update_self_movement) && !method_defined?(:mp_companion_orig_update_self_movement)
|
||
|
|
alias mp_companion_orig_update_self_movement update_self_movement
|
||
|
|
|
||
|
|
def update_self_movement
|
||
|
|
begin
|
||
|
|
sid = instance_variable_get(:@net_server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.personal_dynamic_event_id?(sid)
|
||
|
|
return if instance_variable_defined?(:@net_companion_kind)
|
||
|
|
# Чужой PWO-спутник не должен chase'ить локального игрока между pwo_move.
|
||
|
|
if instance_variable_get(:@mp_remote_player_object_id).to_i > 0
|
||
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:player_object_companion_like?) &&
|
||
|
|
WebSocket.player_object_companion_like?(self)
|
||
|
|
return
|
||
|
|
end
|
||
|
|
summon = nil
|
||
|
|
begin
|
||
|
|
summon = @summon_data
|
||
|
|
rescue
|
||
|
|
summon = nil
|
||
|
|
end
|
||
|
|
if summon.is_a?(Hash) && (summon[:followerFRONT] || summon[:followerBACK] || summon[:followerEXT])
|
||
|
|
return
|
||
|
|
end
|
||
|
|
follower = nil
|
||
|
|
begin
|
||
|
|
follower = @follower
|
||
|
|
rescue
|
||
|
|
follower = nil
|
||
|
|
end
|
||
|
|
return if follower.is_a?(Array) && follower[0].to_i == 1
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
mp_companion_orig_update_self_movement
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:reach_anomally?) && !method_defined?(:mp_companion_orig_reach_anomally)
|
||
|
|
alias mp_companion_orig_reach_anomally reach_anomally?
|
||
|
|
|
||
|
|
def reach_anomally?(*args, &block)
|
||
|
|
npc_obj = nil
|
||
|
|
begin
|
||
|
|
npc_obj = @npc
|
||
|
|
rescue
|
||
|
|
npc_obj = nil
|
||
|
|
end
|
||
|
|
|
||
|
|
return true if npc_obj.nil?
|
||
|
|
|
||
|
|
begin
|
||
|
|
return true if !npc_obj.respond_to?(:anomally)
|
||
|
|
rescue
|
||
|
|
return true
|
||
|
|
end
|
||
|
|
|
||
|
|
mp_companion_orig_reach_anomally(*args, &block)
|
||
|
|
rescue
|
||
|
|
true
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:move_type_xy) && !method_defined?(:mp_companion_orig_move_type_xy)
|
||
|
|
alias mp_companion_orig_move_type_xy move_type_xy
|
||
|
|
|
||
|
|
def move_type_xy(*args, &block)
|
||
|
|
npc_obj = nil
|
||
|
|
begin
|
||
|
|
npc_obj = @npc
|
||
|
|
rescue
|
||
|
|
npc_obj = nil
|
||
|
|
end
|
||
|
|
|
||
|
|
if !self.is_follower? && (npc_obj.nil? || !npc_obj.respond_to?(:anomally))
|
||
|
|
begin
|
||
|
|
@move_type = :move_type_return
|
||
|
|
@investigate_count = nil
|
||
|
|
@traffic_jam_abandom = 0
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
mp_companion_orig_move_type_xy(*args, &block)
|
||
|
|
rescue
|
||
|
|
begin
|
||
|
|
@move_type = :move_type_return
|
||
|
|
@investigate_count = nil
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if method_defined?(:start) && !method_defined?(:mp_companion_orig_start)
|
||
|
|
alias mp_companion_orig_start start
|
||
|
|
|
||
|
|
def start(*args, &block)
|
||
|
|
begin
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.remote_companion_interaction_blocked?(self)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
mp_companion_orig_start(*args, &block)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
module WebSocket
|
||
|
|
class << self
|
||
|
|
unless method_defined?(:mp_companion_old_npc_event)
|
||
|
|
alias_method :mp_companion_old_npc_event, :npc_event?
|
||
|
|
end
|
||
|
|
|
||
|
|
def npc_event?(ev)
|
||
|
|
return false if defined?(MPCompanionSync) && MPCompanionSync.local_companion?(ev)
|
||
|
|
begin
|
||
|
|
if defined?(MPCompanionSync) && ev && ev.respond_to?(:id) && MPCompanionSync.personal_story_world_event_id?(ev.id)
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
if ev && ev.instance_variable_get(:@net_companion_kind).to_s == "companion"
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
mp_companion_old_npc_event(ev)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_create_remote_event)
|
||
|
|
alias_method :mp_companion_old_create_remote_event, :create_remote_event
|
||
|
|
end
|
||
|
|
|
||
|
|
def create_remote_event(server_event_id, template_id, x, y, name="")
|
||
|
|
if defined?(MPCompanionSync) && MPCompanionSync.remote_companion_creation_blocked?(server_event_id)
|
||
|
|
return
|
||
|
|
end
|
||
|
|
mp_companion_old_create_remote_event(server_event_id, template_id, x, y, name)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_incoming_event_move_allowed)
|
||
|
|
alias_method :mp_companion_old_incoming_event_move_allowed, :incoming_event_move_allowed?
|
||
|
|
end
|
||
|
|
|
||
|
|
def incoming_event_move_allowed?(server_event_id)
|
||
|
|
if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
mp_companion_old_incoming_event_move_allowed(server_event_id)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_incoming_event_state_allowed)
|
||
|
|
alias_method :mp_companion_old_incoming_event_state_allowed, :incoming_event_state_allowed?
|
||
|
|
end
|
||
|
|
|
||
|
|
def incoming_event_state_allowed?(server_event_id, state_type)
|
||
|
|
if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
mp_companion_old_incoming_event_state_allowed(server_event_id, state_type)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_incoming_event_lifecycle_allowed)
|
||
|
|
alias_method :mp_companion_old_incoming_event_lifecycle_allowed, :incoming_event_lifecycle_allowed?
|
||
|
|
end
|
||
|
|
|
||
|
|
def incoming_event_lifecycle_allowed?(server_event_id)
|
||
|
|
if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
mp_companion_old_incoming_event_lifecycle_allowed(server_event_id)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_move_remote_event)
|
||
|
|
alias_method :mp_companion_old_move_remote_event, :move_remote_event
|
||
|
|
end
|
||
|
|
|
||
|
|
def move_remote_event(server_event_id, x, y, dir, pattern)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.personal_world_event_id?(server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
mp_companion_old_move_remote_event(server_event_id, x, y, dir, pattern)
|
||
|
|
begin
|
||
|
|
ev = get_map_event(server_event_id)
|
||
|
|
if ev && defined?(MPCompanionSync) && MPCompanionSync.remote_companion_interaction_blocked?(ev)
|
||
|
|
MPCompanionSync.hide_remote_companion_for_local_client(ev)
|
||
|
|
return
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_apply_event_state)
|
||
|
|
alias_method :mp_companion_old_apply_event_state, :apply_event_state
|
||
|
|
end
|
||
|
|
|
||
|
|
def apply_event_state(server_event_id, stype, value)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.personal_world_event_id?(server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
mp_companion_old_apply_event_state(server_event_id, stype, value)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_apply_event_ai)
|
||
|
|
alias_method :mp_companion_old_apply_event_ai, :apply_event_ai
|
||
|
|
end
|
||
|
|
|
||
|
|
def apply_event_ai(server_event_id, ai_parts)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.personal_world_event_id?(server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
mp_companion_old_apply_event_ai(server_event_id, ai_parts)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_play_event_animation)
|
||
|
|
alias_method :mp_companion_old_play_event_animation, :play_event_animation
|
||
|
|
end
|
||
|
|
|
||
|
|
def play_event_animation(server_event_id, anim_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.personal_world_event_id?(server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
mp_companion_old_play_event_animation(server_event_id, anim_id)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_erase_event)
|
||
|
|
alias_method :mp_companion_old_erase_event, :erase_event
|
||
|
|
end
|
||
|
|
|
||
|
|
def erase_event(server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.personal_world_event_id?(server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
mp_companion_old_erase_event(server_event_id)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_respawn_event)
|
||
|
|
alias_method :mp_companion_old_respawn_event, :respawn_event
|
||
|
|
end
|
||
|
|
|
||
|
|
def respawn_event(server_event_id, x, y)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.personal_world_event_id?(server_event_id)
|
||
|
|
return if defined?(MPCompanionSync) && MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
mp_companion_old_respawn_event(server_event_id, x, y)
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_apply_npc_type)
|
||
|
|
alias_method :mp_companion_old_apply_npc_type, :apply_npc_type
|
||
|
|
end
|
||
|
|
|
||
|
|
def apply_npc_type(server_event_id, npc_type)
|
||
|
|
meta = MPCompanionSync.meta_from_npc_type(npc_type)
|
||
|
|
if MPCompanionSync.companion_meta?(meta)
|
||
|
|
MPCompanionSync.remember_remote_companion_meta(server_event_id, meta)
|
||
|
|
if respond_to?(:static_map_event_id?) && static_map_event_id?(server_event_id)
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
if MPCompanionSync.incoming_companion_packet_blocked?(server_event_id)
|
||
|
|
ev = get_map_event(server_event_id) rescue nil
|
||
|
|
MPCompanionSync.apply_remote_companion_meta(ev, meta) if ev
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
mp_companion_old_apply_npc_type(server_event_id, npc_type)
|
||
|
|
ev = get_map_event(server_event_id) rescue nil
|
||
|
|
return if ev.nil?
|
||
|
|
MPCompanionSync.apply_remote_companion_meta(ev, meta)
|
||
|
|
else
|
||
|
|
MPCompanionSync.forget_remote_companion_meta(server_event_id)
|
||
|
|
if respond_to?(:static_map_event_id?) && static_map_event_id?(server_event_id)
|
||
|
|
MPCompanionSync.mark_personal_story_event(server_event_id, npc_type)
|
||
|
|
return if MPCompanionSync.personal_story_npc_type?(npc_type)
|
||
|
|
elsif MPCompanionSync.personal_dynamic_event_id?(server_event_id)
|
||
|
|
ev = get_map_event(server_event_id) rescue nil
|
||
|
|
remove_remote_event(server_event_id) if ev && respond_to?(:remove_remote_event)
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
mp_companion_old_apply_npc_type(server_event_id, npc_type)
|
||
|
|
ev = get_map_event(server_event_id) rescue nil
|
||
|
|
return if ev.nil?
|
||
|
|
MPCompanionSync.clear_remote_companion_meta(ev)
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_connect)
|
||
|
|
alias_method :mp_companion_old_connect, :connect
|
||
|
|
end
|
||
|
|
|
||
|
|
def connect(*args)
|
||
|
|
result = mp_companion_old_connect(*args)
|
||
|
|
MPCompanionSync.reset_sync_state if defined?(MPCompanionSync)
|
||
|
|
result
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_disconnect)
|
||
|
|
alias_method :mp_companion_old_disconnect, :disconnect
|
||
|
|
end
|
||
|
|
|
||
|
|
def disconnect
|
||
|
|
MPCompanionSync.send_all_hides if defined?(MPCompanionSync)
|
||
|
|
MPCompanionSync.reset_sync_state if defined?(MPCompanionSync)
|
||
|
|
mp_companion_old_disconnect
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_old_update)
|
||
|
|
alias_method :mp_companion_old_update, :update
|
||
|
|
end
|
||
|
|
|
||
|
|
def update(*args)
|
||
|
|
MPCompanionSync.before_network_update if defined?(MPCompanionSync)
|
||
|
|
result = mp_companion_old_update(*args)
|
||
|
|
MPCompanionSync.network_update if defined?(MPCompanionSync)
|
||
|
|
result
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
class Sprite_Character < Sprite_Base
|
||
|
|
unless method_defined?(:mp_companion_label_update)
|
||
|
|
alias mp_companion_label_update update
|
||
|
|
end
|
||
|
|
|
||
|
|
def update
|
||
|
|
mp_companion_label_update
|
||
|
|
mp_update_companion_label
|
||
|
|
end
|
||
|
|
|
||
|
|
def mp_update_companion_label
|
||
|
|
label = nil
|
||
|
|
begin
|
||
|
|
if @character
|
||
|
|
label = @character.instance_variable_get(:@net_companion_label)
|
||
|
|
end
|
||
|
|
rescue
|
||
|
|
label = nil
|
||
|
|
end
|
||
|
|
|
||
|
|
if label.nil? || label.to_s.strip == ""
|
||
|
|
mp_dispose_companion_label
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
label = label.to_s
|
||
|
|
if @mp_companion_label_text != label
|
||
|
|
mp_dispose_companion_label
|
||
|
|
mp_create_companion_label(label)
|
||
|
|
@mp_companion_label_text = label
|
||
|
|
end
|
||
|
|
|
||
|
|
return if @mp_companion_label_sprite.nil?
|
||
|
|
|
||
|
|
@mp_companion_label_sprite.x = self.x
|
||
|
|
@mp_companion_label_sprite.y = self.y - height - 1
|
||
|
|
@mp_companion_label_sprite.z = self.z + 1
|
||
|
|
@mp_companion_label_sprite.visible = self.visible
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def mp_create_companion_label(text)
|
||
|
|
temp = Bitmap.new(1, 1)
|
||
|
|
temp.font.name = MPCompanionSync.ui_font_name
|
||
|
|
temp.font.size = 16
|
||
|
|
temp.font.bold = false
|
||
|
|
temp.font.outline = false
|
||
|
|
temp.font.shadow = true
|
||
|
|
w = temp.text_size(text).width + 8
|
||
|
|
h = temp.text_size(text).height
|
||
|
|
temp.dispose
|
||
|
|
|
||
|
|
@mp_companion_label_sprite = Sprite.new(self.viewport)
|
||
|
|
@mp_companion_label_sprite.bitmap = Bitmap.new(w, h)
|
||
|
|
@mp_companion_label_sprite.bitmap.font.name = MPCompanionSync.ui_font_name
|
||
|
|
@mp_companion_label_sprite.bitmap.font.size = 16
|
||
|
|
@mp_companion_label_sprite.bitmap.font.bold = false
|
||
|
|
@mp_companion_label_sprite.bitmap.font.outline = false
|
||
|
|
@mp_companion_label_sprite.bitmap.font.shadow = true
|
||
|
|
@mp_companion_label_sprite.ox = w / 2
|
||
|
|
@mp_companion_label_sprite.oy = h
|
||
|
|
@mp_companion_label_sprite.bitmap.draw_text(0, 0, w, h, text, 1)
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
|
||
|
|
def mp_dispose_companion_label
|
||
|
|
return unless @mp_companion_label_sprite
|
||
|
|
begin
|
||
|
|
@mp_companion_label_sprite.bitmap.dispose if @mp_companion_label_sprite.bitmap
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
begin
|
||
|
|
@mp_companion_label_sprite.dispose
|
||
|
|
rescue
|
||
|
|
end
|
||
|
|
@mp_companion_label_sprite = nil
|
||
|
|
@mp_companion_label_text = nil
|
||
|
|
end
|
||
|
|
|
||
|
|
unless method_defined?(:mp_companion_label_dispose)
|
||
|
|
alias mp_companion_label_dispose dispose
|
||
|
|
end
|
||
|
|
|
||
|
|
def dispose
|
||
|
|
mp_dispose_companion_label
|
||
|
|
mp_companion_label_dispose
|
||
|
|
end
|
||
|
|
end
|