Save-Point
Find out what's your file's architecture using Ruby! - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Material Development (https://www.save-point.org/forum-8.html)
+--- Forum: Tools (https://www.save-point.org/forum-42.html)
+--- Thread: Find out what's your file's architecture using Ruby! (/thread-7516.html)



Find out what's your file's architecture using Ruby! - kyonides - 08-03-2019

After searching for an easy way to find out if an executable or library were meant to be run on x86 alias i386 (or i486 or i586) or x64 alias amd64 OS, I stumbled upon perl and python scripts. Then I thought there got to be a way to do it with Ruby and I was right! Shocked


Code:
def arch_of(fn)
  f = File.open(fn,"rb")
  dos = f.read(64)
  magic, padding, offset = dos.unpack("a2a58l")
  if magic != "MZ"
    puts "This is neither an executable or a library!"
    return
  end
  f.pos = offset
  hdr = f.read(6)
  sig, padding, machine = hdr.unpack("a2a2v")
  f.close
  if sig != "PE"
    return puts "File #{fn} has an unknown architecture..."
  end
  if machine == 0x014c
    return puts fn + " architecture detected: i386"
  elsif machine == 0x0200
    return puts fn + " architecture detected: IA64"
  elsif machine == 0x8664
    return puts fn + " architecture detected: x64"
  else
    return puts "File #{fn} has an unknown architecture..."
  end
end

filenames = Dir['*.exe'].sort + Dir['*.dll'].sort
filenames.each {|fn| arch_of(fn) }

I felt great for finding it out myself, when I opened another website and read how another github user had posted a similar script a few years ago... Sad

Anyway, now you can use it to get an extensive list of what file has what kind of architecture! Shocked 

Just replace "puts" with "print" or in case you're an experienced scripter, you might already know how to store the results in a TXT file you can read at anytime. Laughing + Tongue sticking out

I think it can even run if you copy and paste it on the maker's script editor. Happy with a sweat


RE: Find out what's your file's architecture using Ruby! - DerVVulfman - 09-01-2019

This works fine to perform a test upon Game.Exe and the accompanying RGSS.dll, these being in the root directory of the project. So quite often, anyone running this will receive the same response as any other.

Would there be a script available to test the architecture of the computer running said program, testing if it is in a Windows, Mac or Linux environment?


RE: Find out what's your file's architecture using Ruby! - kyonides - 09-02-2019

Actually, that would be feasible indeed. It's not complicated at all.


RE: Find out what's your file's architecture using Ruby! - DerVVulfman - 09-08-2019

Well... guess I'd have to provide an answer. Tongue sticking out

For those thinking that RUBY_PLATFORM would do the trick, nope. It falters when Java is in use, returning 'Java' even if the PC itself was Windows, Mac or Linux. So I had to dig further.

The main consensus is the use of RbConfig. Unfortunately, RPGMaker XP (or any) do not come supplied with it, and there are various versions as there are various versions of Ruby. But after examining various versions of RbConfig from versions 1.8.5 through to more modern 2., I determined that I had need of only a handful of lines.

Below is a scriptette that can determine what Operating System your PC is using, returning something like :windows, :linux, :unix or :macosx.

Code:
#==============================================================================
# ** OS Detection via Ruby
#------------------------------------------------------------------------------
#    by DerVVulfman (09/08/2019) MM/DD/YYYY
#    based on the work of Thomas Enebo (11/27/2012) MM/DD/YYYY
#==============================================================================



#==============================================================================
# ** RbConfig
#------------------------------------------------------------------------------
#  This module holds Configuration settings for Ruby Operations.
#  THIS IS NOT COMPLETE, BUT A BAREBONES EXERPT.  VARIOUS RBCONFIGS EXIST
#  BASED ON VERSION NUMBERS
#==============================================================================

module RbConfig
  CONFIG = {}  
  CONFIG["host_os"] = "mingw32msvc"
end



#==============================================================================
# ** Operating System Detection
#------------------------------------------------------------------------------
#  This module actively uses the RbConfig system to detect what operating
#  system is in use.  The below code is sound, the RbConfig used above is
#  merely a snippet of the whole.
#
#  Refer to OS_Detect::execute to perform the operation
#==============================================================================

module OS_Detect
  #--------------------------------------------------------------------------
  # * Methods/Functions Executable From Outside Call
  #--------------------------------------------------------------------------
  module_function  
  #--------------------------------------------------------------------------
  # * Object Execution
  #--------------------------------------------------------------------------
  def execute
    @os ||= (
      host_os = RbConfig::CONFIG['host_os']
      case host_os
      when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
        :windows
      when /darwin|mac os/
        :macosx
      when /linux/
        :linux
      when /solaris|bsd/
        :unix
      else
        raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
      end
    )
  end
end

  
# THIS prints the version
p OS_Detect::execute

You can replace the returned values (like :windows) with numeric values if it helps Winking

This will prove useful if one wishes to permit Win32DLL applications to run in their game for Windows Users, and perhaps have a workaround for those without.


RE: Find out what's your file's architecture using Ruby! - kyonides - 09-09-2019

Use the more conventional Module.method notation instead, even for rubyists it's weird to use double colon for calling such module method.