127 lines
4.1 KiB
Ruby
127 lines
4.1 KiB
Ruby
|
|
# [124] 74646050: Script_import
|
|
=begin
|
|
#===============================================================================
|
|
Title: External Script Loader
|
|
Author: Tsukihime
|
|
Date: Dec 2, 2013
|
|
URL: http://himeworks.wordpress.com/2013/12/02/external-script-loader/
|
|
--------------------------------------------------------------------------------
|
|
** Change log
|
|
Dec 2, 2013
|
|
- Initial release
|
|
--------------------------------------------------------------------------------
|
|
** Terms of Use
|
|
* Free to use in commercial/non-commercial projects
|
|
* No real support. The script is provided as-is
|
|
* Will do bug fixes, but no compatibility patches
|
|
* Features may be requested but no guarantees, especially if it is non-trivial
|
|
* Credits to Tsukihime in your project
|
|
* Preserve this header
|
|
--------------------------------------------------------------------------------
|
|
** Description
|
|
|
|
This script allows you to load external scripts into the game. It supports
|
|
loading from encrypted archives as well.
|
|
|
|
--------------------------------------------------------------------------------
|
|
** Installation
|
|
|
|
Place this script below Materials and above Main
|
|
|
|
--------------------------------------------------------------------------------
|
|
** Usage
|
|
|
|
To load and evaluate script, use the following function call:
|
|
|
|
load_script(script_path)
|
|
|
|
--------------------------------------------------------------------------------
|
|
** Example
|
|
|
|
I have a folder called "Scripts" and a script called "test.rb" in that folder.
|
|
If I want to load the script, I would just write
|
|
|
|
load_script("Scripts/test.rb")
|
|
|
|
This will evaluate the test script.
|
|
|
|
#===============================================================================
|
|
=end
|
|
|
|
#===============================================================================
|
|
# * Rest of Script
|
|
#===============================================================================
|
|
#-------------------------------------------------------------------------------
|
|
# Convenience function. Equivalent to
|
|
# script = load_data(path)
|
|
# eval(script)
|
|
# It supports loading from encrypted archives
|
|
#-------------------------------------------------------------------------------
|
|
def load_script(path)
|
|
if defined?(LonaIntegrity) && LonaIntegrity.respond_to?(:script_allowed?) && !LonaIntegrity.script_allowed?(path.to_s)
|
|
puts "[INTEGRITY] blocked Ruby script not present in Secure/List.json: #{path}" rescue nil
|
|
return false
|
|
end
|
|
if !defined?(Ruby310Compat)
|
|
begin
|
|
compat_candidates = []
|
|
begin
|
|
cwd = Dir.pwd.to_s
|
|
unless cwd.empty?
|
|
compat_candidates << File.expand_path('Data/Scripts/000_Ruby310_Compat.rb', cwd)
|
|
compat_candidates << File.expand_path('000_Ruby310_Compat.rb', cwd)
|
|
end
|
|
rescue StandardError
|
|
end
|
|
compat_candidates.compact.uniq.each do |compat_path|
|
|
next unless File.file?(compat_path)
|
|
load compat_path
|
|
break if defined?(Ruby310Compat)
|
|
end
|
|
rescue StandardError
|
|
end
|
|
end
|
|
use_alternative = FileGetter::COMPRESSED && File.file?(path)
|
|
path = $mod_load_script.fetch(path, path)
|
|
prp "#{use_alternative ? "alt: " : ""}load_script path=#{path}", 6
|
|
begin
|
|
script = if defined?(Ruby310Compat)
|
|
Ruby310Compat.read_text_file(path)
|
|
elsif use_alternative
|
|
File.read(path)
|
|
else
|
|
File.open(path, 'rb', &:read)
|
|
end
|
|
prev_verbose = $VERBOSE
|
|
$VERBOSE = nil
|
|
if path.start_with?("Data/HCGframes/") || path.start_with?("Data/Batch/")
|
|
self.instance_eval(script, path)
|
|
else
|
|
eval(script, binding, path)
|
|
end
|
|
$VERBOSE = prev_verbose
|
|
rescue => ex
|
|
msgbox ex.message + "\n" + ex.backtrace.join("\n")
|
|
ensure
|
|
$VERBOSE = prev_verbose if defined?(prev_verbose)
|
|
end
|
|
end
|
|
|
|
#-------------------------------------------------------------------------------
|
|
# Load files from non-RM files
|
|
#-------------------------------------------------------------------------------
|
|
class << Marshal
|
|
alias_method(:th_core_load, :load)
|
|
def load(port, proc = nil)
|
|
th_core_load(port, proc)
|
|
rescue TypeError
|
|
if port.kind_of?(File)
|
|
port.rewind
|
|
port.read
|
|
else
|
|
port
|
|
end
|
|
end
|
|
end unless Marshal.respond_to?(:th_core_load)
|