Release 0.0.1.8.8.7
This commit is contained in:
1 parent
3c8a17aa78
commit
c7beab414c
16283 files changed
+768761
-2
No files matched your search
@@ -0,0 +1,434 @@
|
||||
# encoding: UTF-8
|
||||
#==============================================================================
|
||||
# Ruby 3.x / mkxp-z compatibility helpers for Data scripts.
|
||||
# - UTF-8 safe text/json helpers
|
||||
# - robust warning logging fallback
|
||||
# - Win32API-compatible wrapper backed by Fiddle
|
||||
#==============================================================================
|
||||
|
||||
module Ruby310Compat
|
||||
@json_ready = false
|
||||
@fiddle_ready = false
|
||||
@fiddle_error = nil
|
||||
@library_cache = {}
|
||||
|
||||
class << self
|
||||
attr_reader :fiddle_error
|
||||
end
|
||||
|
||||
def self.json_ready?
|
||||
return true if @json_ready
|
||||
|
||||
begin
|
||||
require 'json'
|
||||
if defined?(JSON)
|
||||
JSON.singleton_class.class_eval do
|
||||
alias decode parse unless respond_to?(:decode)
|
||||
alias encode generate unless respond_to?(:encode)
|
||||
end
|
||||
end
|
||||
@json_ready = true
|
||||
true
|
||||
rescue LoadError, StandardError
|
||||
@json_ready = false
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def self.windows?
|
||||
!!(RUBY_PLATFORM =~ /mswin|mingw|cygwin/i)
|
||||
rescue
|
||||
false
|
||||
end
|
||||
|
||||
def self.fiddle_ready?
|
||||
return true if @fiddle_ready
|
||||
|
||||
begin
|
||||
require 'fiddle'
|
||||
require 'fiddle/import'
|
||||
@fiddle_ready = true
|
||||
@fiddle_error = nil
|
||||
true
|
||||
rescue LoadError, StandardError => e
|
||||
@fiddle_ready = false
|
||||
@fiddle_error = e
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def self.normalize_library_name(name)
|
||||
name.to_s.strip.gsub('\\', '/')
|
||||
end
|
||||
|
||||
def self.library_candidates(name)
|
||||
raw = normalize_library_name(name)
|
||||
return [] if raw == ''
|
||||
|
||||
candidates = []
|
||||
candidates << raw
|
||||
candidates << "#{raw}.dll" unless raw =~ /\.[A-Za-z0-9]+\z/
|
||||
|
||||
begin
|
||||
base = Dir.pwd.to_s
|
||||
unless base.nil? || base == ''
|
||||
candidates << File.expand_path(raw, base)
|
||||
candidates << File.expand_path("#{raw}.dll", base) unless raw =~ /\.[A-Za-z0-9]+\z/
|
||||
end
|
||||
rescue
|
||||
end
|
||||
|
||||
uniq = {}
|
||||
candidates.compact.map { |v| v.to_s }.reject { |v| v == '' }.select { |v| uniq[v] ? false : (uniq[v] = true) }
|
||||
end
|
||||
|
||||
def self.open_library(name)
|
||||
raise LoadError, 'Fiddle is unavailable' unless fiddle_ready?
|
||||
|
||||
cache_key = normalize_library_name(name)
|
||||
handle = @library_cache[cache_key]
|
||||
return handle if handle
|
||||
|
||||
last_error = nil
|
||||
library_candidates(name).each do |candidate|
|
||||
begin
|
||||
handle = Fiddle.dlopen(candidate)
|
||||
@library_cache[cache_key] = handle
|
||||
return handle
|
||||
rescue Fiddle::DLError, StandardError => e
|
||||
last_error = e
|
||||
end
|
||||
end
|
||||
|
||||
raise(last_error || LoadError.new("Unable to load library: #{name}"))
|
||||
end
|
||||
|
||||
def self.stdcall_abi
|
||||
return @stdcall_abi if defined?(@stdcall_abi)
|
||||
|
||||
@stdcall_abi = begin
|
||||
if defined?(Fiddle::Importer::CALL_TYPE_TO_ABI)
|
||||
Fiddle::Importer::CALL_TYPE_TO_ABI[:stdcall] || Fiddle::Function::DEFAULT
|
||||
else
|
||||
Fiddle::Function::DEFAULT
|
||||
end
|
||||
rescue
|
||||
Fiddle::Function::DEFAULT
|
||||
end
|
||||
end
|
||||
|
||||
def self.int_value(value)
|
||||
return 1 if value == true
|
||||
return 0 if value == false || value.nil?
|
||||
value.to_i
|
||||
rescue
|
||||
0
|
||||
end
|
||||
|
||||
def self.bootstrap_candidates
|
||||
candidates = []
|
||||
begin
|
||||
cwd = Dir.pwd.to_s
|
||||
unless cwd.nil? || cwd == ''
|
||||
candidates << File.expand_path('Data/Scripts/000_Ruby310_Compat.rb', cwd)
|
||||
candidates << File.expand_path('000_Ruby310_Compat.rb', cwd)
|
||||
end
|
||||
rescue StandardError
|
||||
end
|
||||
begin
|
||||
script_dir = File.dirname(__FILE__)
|
||||
unless script_dir.nil? || script_dir == ''
|
||||
candidates << File.expand_path('000_Ruby310_Compat.rb', script_dir)
|
||||
end
|
||||
rescue StandardError
|
||||
end
|
||||
uniq = {}
|
||||
candidates.compact.map { |entry| entry.to_s }.reject { |entry| entry.empty? }.select { |entry| uniq[entry] ? false : (uniq[entry] = true) }
|
||||
end
|
||||
|
||||
|
||||
|
||||
def self.strip_utf8_bom(content)
|
||||
return content if content.nil?
|
||||
string = content.is_a?(String) ? content.dup : content.to_s
|
||||
bytes = string.b
|
||||
return string unless bytes.start_with?("\xEF\xBB\xBF")
|
||||
stripped = bytes.byteslice(3, bytes.bytesize - 3) || ''.b
|
||||
if string.encoding == Encoding::ASCII_8BIT
|
||||
stripped
|
||||
else
|
||||
stripped.force_encoding(string.encoding)
|
||||
end
|
||||
rescue StandardError
|
||||
content
|
||||
end
|
||||
|
||||
def self.decode_text_content(content)
|
||||
string = strip_utf8_bom(content)
|
||||
return string unless string.respond_to?(:force_encoding)
|
||||
|
||||
return string unless string.encoding == Encoding::ASCII_8BIT
|
||||
|
||||
utf8 = string.dup
|
||||
utf8.force_encoding(Encoding::UTF_8)
|
||||
return utf8 if utf8.valid_encoding?
|
||||
|
||||
cp1251 = string.dup
|
||||
cp1251.force_encoding(Encoding::Windows_1251)
|
||||
return cp1251.encode(Encoding::UTF_8) if cp1251.valid_encoding?
|
||||
|
||||
string.force_encoding(Encoding::UTF_8)
|
||||
rescue StandardError
|
||||
content
|
||||
end
|
||||
|
||||
def self.read_text_file(path)
|
||||
decode_text_content(File.binread(path))
|
||||
rescue StandardError
|
||||
decode_text_content(File.read(path))
|
||||
end
|
||||
|
||||
def self.normalize_json_source(content)
|
||||
text = decode_text_content(content)
|
||||
text = strip_utf8_bom(text)
|
||||
return text unless text.respond_to?(:gsub)
|
||||
text.gsub("\r\n", "\n").gsub("\r", "\n")
|
||||
rescue StandardError
|
||||
content
|
||||
end
|
||||
|
||||
def self.decode_json_text(content)
|
||||
json_ready?
|
||||
JSON.decode(normalize_json_source(content))
|
||||
end
|
||||
|
||||
def self.parse_json_file(path)
|
||||
decode_json_text(read_text_file(path))
|
||||
end
|
||||
def self.ensure_bootstrapped
|
||||
return true if defined?(Ruby310Compat)
|
||||
bootstrap_candidates.each do |candidate|
|
||||
next unless File.file?(candidate)
|
||||
load candidate
|
||||
return true if defined?(Ruby310Compat)
|
||||
end
|
||||
false
|
||||
rescue StandardError
|
||||
false
|
||||
end
|
||||
|
||||
def self.ensure_user_data_dir
|
||||
begin
|
||||
Dir.mkdir('UserData') unless Dir.exist?('UserData')
|
||||
rescue StandardError
|
||||
end
|
||||
end
|
||||
|
||||
def self.warning_log_path
|
||||
ensure_user_data_dir
|
||||
'UserData/ruby_warning_fallback.log'
|
||||
end
|
||||
|
||||
def self.safe_warn_write(message)
|
||||
text = begin
|
||||
msg = message.nil? ? '' : message.to_s
|
||||
msg.end_with?("\n") ? msg : (msg + "\n")
|
||||
rescue StandardError
|
||||
"warning: <unprintable>\n"
|
||||
end
|
||||
begin
|
||||
File.open(warning_log_path, 'ab') { |f| f.write(text.b) }
|
||||
true
|
||||
rescue StandardError
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Ruby310Compat.json_ready?
|
||||
|
||||
|
||||
module Warning
|
||||
class << self
|
||||
unless method_defined?(:_ruby310compat_original_warn)
|
||||
alias _ruby310compat_original_warn warn
|
||||
end
|
||||
|
||||
def warn(message)
|
||||
_ruby310compat_original_warn(message)
|
||||
rescue StandardError
|
||||
Ruby310Compat.safe_warn_write(message)
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Kernel
|
||||
unless method_defined?(:_ruby310compat_kernel_warn)
|
||||
alias _ruby310compat_kernel_warn warn
|
||||
end
|
||||
|
||||
def warn(*messages, **kwargs)
|
||||
_ruby310compat_kernel_warn(*messages, **kwargs)
|
||||
rescue StandardError
|
||||
messages.each { |message| Ruby310Compat.safe_warn_write(message) }
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
module Ruby310Compat
|
||||
class NativeFunction
|
||||
def initialize(dll_name, function_name, import = '', export = 'L')
|
||||
@impl = if defined?(Win32API)
|
||||
Win32API.new(dll_name, function_name, import, export)
|
||||
else
|
||||
raise LoadError, 'Win32API/Fiddle bridge is unavailable'
|
||||
end
|
||||
end
|
||||
|
||||
def call(*args)
|
||||
@impl.call(*args)
|
||||
end
|
||||
alias Call call
|
||||
end
|
||||
|
||||
def self.native_function(dll_name, function_name, import = '', export = 'L')
|
||||
NativeFunction.new(dll_name, function_name, import, export)
|
||||
end
|
||||
|
||||
class << self
|
||||
alias native_api native_function
|
||||
end
|
||||
end
|
||||
|
||||
unless defined?(Win32API)
|
||||
class Win32API
|
||||
ARG_TYPE_MAP = {
|
||||
'I' => proc { Fiddle::TYPE_INT },
|
||||
'L' => proc { Fiddle::TYPE_LONG },
|
||||
'P' => proc { Fiddle::TYPE_VOIDP },
|
||||
'V' => proc { Fiddle::TYPE_VOID }
|
||||
}
|
||||
|
||||
RET_TYPE_MAP = {
|
||||
'I' => proc { Fiddle::TYPE_INT },
|
||||
'L' => proc { Fiddle::TYPE_LONG },
|
||||
'P' => proc { Fiddle::TYPE_VOIDP },
|
||||
'V' => proc { Fiddle::TYPE_VOID }
|
||||
}
|
||||
|
||||
def initialize(dll_name, function_name, import = '', export = 'L')
|
||||
raise LoadError, 'Fiddle is unavailable' unless Ruby310Compat.fiddle_ready?
|
||||
|
||||
@dll_name = dll_name.to_s
|
||||
@function_name = function_name.to_s
|
||||
@import_signature = normalize_signature(import)
|
||||
@export_signature = normalize_return_signature(export)
|
||||
@handle = Ruby310Compat.open_library(@dll_name)
|
||||
@function = Fiddle::Function.new(
|
||||
@handle[@function_name],
|
||||
@import_signature.map { |entry| map_arg_type(entry) },
|
||||
map_return_type(@export_signature),
|
||||
Ruby310Compat.stdcall_abi
|
||||
)
|
||||
end
|
||||
|
||||
def call(*args)
|
||||
keepalive = []
|
||||
converted = []
|
||||
|
||||
@import_signature.each_with_index do |sig, index|
|
||||
converted << convert_arg(sig, args[index], keepalive)
|
||||
end
|
||||
|
||||
result = @function.call(*converted)
|
||||
convert_result(result)
|
||||
end
|
||||
alias Call call
|
||||
|
||||
private
|
||||
|
||||
def normalize_signature(signature)
|
||||
case signature
|
||||
when Array
|
||||
signature.map { |entry| entry.to_s[0, 1].upcase }
|
||||
else
|
||||
signature.to_s.each_char.map { |entry| entry.to_s[0, 1].upcase }
|
||||
end
|
||||
rescue
|
||||
[]
|
||||
end
|
||||
|
||||
def normalize_return_signature(signature)
|
||||
sig = signature.to_s
|
||||
sig = 'V' if sig == ''
|
||||
sig[0, 1].upcase
|
||||
rescue
|
||||
'V'
|
||||
end
|
||||
|
||||
def map_arg_type(sig)
|
||||
resolver = ARG_TYPE_MAP[sig]
|
||||
raise ArgumentError, "Unsupported Win32API arg type: #{sig.inspect}" unless resolver
|
||||
resolver.call
|
||||
end
|
||||
|
||||
def map_return_type(sig)
|
||||
resolver = RET_TYPE_MAP[sig]
|
||||
raise ArgumentError, "Unsupported Win32API return type: #{sig.inspect}" unless resolver
|
||||
resolver.call
|
||||
end
|
||||
|
||||
def convert_arg(sig, value, keepalive)
|
||||
case sig
|
||||
when 'P'
|
||||
convert_pointer_arg(value, keepalive)
|
||||
when 'I', 'L'
|
||||
Ruby310Compat.int_value(value)
|
||||
when 'V'
|
||||
nil
|
||||
else
|
||||
raise ArgumentError, "Unsupported Win32API arg type: #{sig.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
def convert_pointer_arg(value, keepalive)
|
||||
return 0 if value.nil?
|
||||
return value if defined?(Fiddle::Pointer) && value.is_a?(Fiddle::Pointer)
|
||||
|
||||
if value.is_a?(String)
|
||||
keepalive << value
|
||||
return Fiddle::Pointer[value]
|
||||
end
|
||||
|
||||
if value.respond_to?(:to_str)
|
||||
string_value = value.to_str
|
||||
keepalive << string_value
|
||||
return Fiddle::Pointer[string_value]
|
||||
end
|
||||
|
||||
Ruby310Compat.int_value(value)
|
||||
rescue
|
||||
0
|
||||
end
|
||||
|
||||
def convert_result(value)
|
||||
case @export_signature
|
||||
when 'V'
|
||||
nil
|
||||
when 'P'
|
||||
ptr = Ruby310Compat.int_value(value)
|
||||
return nil if ptr == 0
|
||||
begin
|
||||
Fiddle::Pointer.new(ptr).to_s
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
else
|
||||
Ruby310Compat.int_value(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user