148 lines
4.5 KiB
Ruby
148 lines
4.5 KiB
Ruby
# encoding: UTF-8
|
|
# AutoEnableMods for LonaRPG (MKXP-Z / Ruby 3.1)
|
|
# Install into: ModScripts/499_AutoEnableMods.rb
|
|
# Runs before ModScripts/500_Mod_loader.rb.
|
|
# Enables every folder mod; no GameMods.ini is needed (with included core patches).
|
|
# Does not modify or bypass the game's own security checks.
|
|
|
|
module AutoEnableAllMods
|
|
MOD_ROOT = "ModScripts/_Mods".freeze
|
|
# Log output disabled: no AutoEnableMods.log file or console messages.
|
|
def self.log(_message)
|
|
nil
|
|
end
|
|
|
|
# Check disk only. The game's stock manager has already been
|
|
# created; register any folders that it may not have picked up.
|
|
def self.scan(manager)
|
|
unless Dir.exist?(MOD_ROOT)
|
|
log("Folder not found: #{MOD_ROOT}")
|
|
return
|
|
end
|
|
|
|
Dir.glob(File.join(MOD_ROOT, "*")).sort.each do |path|
|
|
next unless File.directory?(path)
|
|
next if manager.mods.values.any? { |mod| File.expand_path(mod.path) == File.expand_path(path) }
|
|
begin
|
|
existing_ids = manager.mods.keys.dup
|
|
mod = manager.load_mod_info(path)
|
|
if existing_ids.include?(mod.id) && File.expand_path(mod.path) != File.expand_path(path)
|
|
log("SKIPPED #{path}: duplicate mod ID #{mod.id}")
|
|
else
|
|
log("DISCOVERED #{mod.id} (#{path})")
|
|
end
|
|
rescue StandardError => error
|
|
log("SCAN ERROR #{path}: #{error.class}: #{error.message}")
|
|
end
|
|
end
|
|
end
|
|
|
|
# Deterministic dependency-aware order, independent of INI LoadOrder.
|
|
# Dependencies are ordered when present, but missing ones do not disable a mod.
|
|
def self.order(mods)
|
|
by_id = {}
|
|
mods.each { |mod| by_id[mod.id.to_s] = mod }
|
|
incoming = {}
|
|
next_mods = {}
|
|
mods.each do |mod|
|
|
incoming[mod] = 0
|
|
next_mods[mod] = []
|
|
end
|
|
|
|
add_edge = lambda do |first, second|
|
|
next if first.nil? || second.nil? || first.equal?(second)
|
|
next if next_mods[first].include?(second)
|
|
next_mods[first] << second
|
|
incoming[second] += 1
|
|
end
|
|
|
|
mods.each do |mod|
|
|
if mod.requires.is_a?(Hash)
|
|
mod.requires.each_key do |required_id|
|
|
next if required_id.to_s == "LonaRPG"
|
|
prerequisite = by_id[required_id.to_s]
|
|
if prerequisite
|
|
add_edge.call(prerequisite, mod)
|
|
else
|
|
log("MISSING DEPENDENCY #{mod.id}: #{required_id}; loading will still be attempted")
|
|
end
|
|
end
|
|
end
|
|
Array(mod.before).each do |target_id|
|
|
add_edge.call(mod, by_id[target_id.to_s])
|
|
end
|
|
end
|
|
|
|
sort_key = lambda { |mod| [mod.id.to_s.downcase, mod.path.to_s.downcase] }
|
|
ready = mods.select { |mod| incoming[mod] == 0 }.sort_by(&sort_key)
|
|
result = []
|
|
until ready.empty?
|
|
current = ready.shift
|
|
result << current
|
|
next_mods[current].each do |following|
|
|
incoming[following] -= 1
|
|
ready << following if incoming[following] == 0
|
|
end
|
|
ready.sort_by!(&sort_key)
|
|
end
|
|
|
|
if result.size < mods.size
|
|
remaining = (mods - result).sort_by(&sort_key)
|
|
log("CYCLIC DEPENDENCIES: #{remaining.map(&:id).join(', ')}; attempting to load anyway")
|
|
result.concat(remaining)
|
|
end
|
|
result
|
|
end
|
|
|
|
def self.prepare(manager)
|
|
scan(manager)
|
|
sorted = order(manager.mods.values)
|
|
manager.instance_variable_set(:@load_order, sorted)
|
|
sorted.each do |mod|
|
|
# An error from the stock dependency/INI pass must not disable it.
|
|
mod.clear_error if mod.respond_to?(:clear_error) && !mod.loaded
|
|
mod.enabled = true
|
|
end
|
|
log("AUTO ENABLED #{sorted.length} mod(s): #{sorted.map(&:id).join(', ')}")
|
|
sorted
|
|
end
|
|
|
|
def self.report(manager)
|
|
manager.load_order.each do |mod|
|
|
if mod.loaded
|
|
log("LOADED #{mod.id}")
|
|
else
|
|
details = mod.respond_to?(:error) ? mod.error.to_s.gsub(/\s+/, " ")[0, 300] : ""
|
|
log("LOAD FAILED #{mod.id}: #{details}")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
module AutoEnableModsManagerHook
|
|
def link_texts
|
|
AutoEnableAllMods.prepare(self)
|
|
super
|
|
end
|
|
|
|
def preload_mods
|
|
AutoEnableAllMods.prepare(self)
|
|
super
|
|
end
|
|
|
|
def load_mods(bnd)
|
|
AutoEnableAllMods.prepare(self)
|
|
super
|
|
ensure
|
|
AutoEnableAllMods.report(self)
|
|
end
|
|
end
|
|
|
|
if defined?(ModManager) && defined?($mod_manager) && $mod_manager
|
|
ModManager.prepend(AutoEnableModsManagerHook) unless ModManager.ancestors.include?(AutoEnableModsManagerHook)
|
|
AutoEnableAllMods.log("START (INI-less auto-loading of all folder mods)")
|
|
AutoEnableAllMods.prepare($mod_manager)
|
|
else
|
|
AutoEnableAllMods.log("ERROR: ModManager is not available. Check script load order.")
|
|
end
|