# RubyEncoder v3 loader (patched for SketchUp)
# SketchUp Windows 的 RUBY_PLATFORM 返回 'x64-mswin64_140'，
# 但 rgloader 文件用的是 'mingw' 命名，需要硬编码适配。

_v = RUBY_VERSION.split('.')[0..1].join   # "3.2.2" -> "32", "2.7.6" -> "27"
_d = File.expand_path(File.dirname(__FILE__))

if RUBY_PLATFORM =~ /darwin/i
  _plat = 'darwin'
  _arch = ''
else
  # Windows: SketchUp 报 mswin64_140 / mingw32 等，统一用 mingw
  _plat = 'mingw'
  _arch = '.x64'   # SketchUp 2023+ 全是 64 位
end

_candidates = [
  "rgloader#{_v}.#{_plat}#{_arch}",    # rgloader32.mingw.x64
  "rgloader#{_v}.#{_plat}",             # rgloader32.mingw
]

_loaded = false
_candidates.each do |base|
  f = File.join(_d, base)
  $LOADED_FEATURES.reject! { |p| p.start_with?(f) }
  begin
    require f
    _loaded = true
    break
  rescue LoadError
  end
end

unless _loaded
  raise LoadError,
    "The RubyEncoder loader is not installed. " \
    "Please visit the http://www.rubyencoder.com/loaders/ RubyEncoder site " \
    "to download the required loader for '#{RUBY_PLATFORM}' " \
    "and unpack it into '#{_d}' directory to run this protected script."
end
