Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Script Tool - RSC Require

Status
Not open for further replies.
RSC Require
Version: 1.5.0

Introduction

This little bit of code lets you require Ruby Script Collection (RSC) files into your game. But why .rsc files? RSC files are compressed files that contain many scripts. This is indeed very similar to the Scripts.rxdata file. They can be used for including those large libraries like the SDK and the MACL.

Script

Code:
#==============================================================================
#  ** RSC Require
#------------------------------------------------------------------------------
#  Version 1.5.0
#  ? Yeyinde, 2007
#------------------------------------------------------------------------------
#  Usage: require('My_RSCFile') or rsc_require('My_RSCFile')
#==============================================================================

#-----------------------------------------------------------------------------
#  * Aliasing
#-----------------------------------------------------------------------------
if @rsc_original_require.nil?
  alias rsc_original_require require
  @rsc_original_require = true
end
#-----------------------------------------------------------------------------
#  * require
#       file : the file to load, rsc files will be handled in it's own method
#-----------------------------------------------------------------------------
def require(file)
  begin
    rsc_original_require file
  rescue LoadError
    rsc_require file
  end
end
#-----------------------------------------------------------------------------
#  * rsc_require
#           file : the RSC file which will be loaded. The .rsc extension may
#                   be ommited
#-----------------------------------------------------------------------------
def rsc_require(file)
  # Create error object
  load_error = LoadError.new("No such file to load -- #{file}")
  # Tack on the extension if the argument does not already include it.
  file += '.rsc' unless file.split('.')[-1] == 'rsc'
  # Iterate over all require directories
  $:.each do |dir|
    # Make the rout-to-file
    filename = dir + '/' + file
    # If load was successful
    if load_rsc(filename)
      # Load was successful
      return true
    end
  end
  # Raise the error if there was no file to load
  raise load_error
end
#-----------------------------------------------------------------------------
#  * load_rsc
#    file_name : the file to be evaluated
#-----------------------------------------------------------------------------
def load_rsc(file_name)
  file_name = file_name[2..file_name.length] if file_name[0..1] == './'
  # If the file exists and so does the encypted archive
  if File.exists?(file_name) && ENCRYPTED
    raise RSCSecurityError.new("Potential security breach:
#{File.basename(file_name)} is a real file")
  end
  # Start a begin-rescue block
  begin
    # Load the data from the file
    script_data = load_data(file_name)
    # Iterate over each data entry in the array
    script_data.each_with_index do |data|
      # Store the script name
      script_name = data[0][0]
      # Store the script data after inflating it
      script = Zlib::Inflate.inflate(data[0][1])
      # Start a begin-rescue block
      begin
        # Evaluate the script data
        eval(script)
      # If there is an error with the script
      rescue Exception => error
        # Remove the (eval): from the start of the message
        error.message.gsub!('(eval):', '')
        # Get the line number
        line = error.message.sub(/:[\w \W]+/) {$1}
        # Get the error details
        message = error.message.sub(/\d+:in `load_rsc'/) {''}
        # Print the error and details
        print "File '#{file_name}' script '#{script_name}' line #{line}: #{error.type} occured.

#{message}"
        # Exit with value 1 (standard error)
        exit 1
      end
    end
    # Load was a success
    return true
  # No file to load
  rescue Errno::ENOENT
    # Load was a failure
    return false
  end
end

#==============================================================================
#  ** RSCSecurityError
#==============================================================================
class RSCSecurityError < StandardError; end

#---------------------------------------------------------------------------
#  ** Encrypted archive detection
#---------------------------------------------------------------------------
list = Dir.entries('.')
list.reject!{|file| File.extname(file) != '.rgssad' || 
                    File.basename(file) != '.rgssad'}
ENCRYPTED = !list.empty?

# Insert Game.exe's base folder into require array
$: << '.' unless $:.include?('.')
# Remove all nil objects
$:.compact!

Instructions

Place above every script in your script list. Then make a new section below Scene_Debug and above Main and call it REQUIRE. Add all your required libraries here through this code:
Code:
require 'LIBRARYNAME'
or
Code:
rsc_require 'LIBRARYNAME'
All the scripts in the .rsc file (Or other source files) will be added into your game.

If a RSC file exists when the game is encrypted, the game will shut down. This is a security measure to prevent hacked methods.

FAQ

No questions asked

Compatibility

Should be compatible with everying.

Credits and Thanks
Thanks to the wx_ruby team and Trickster. A big hand to Trickster!

Author's Notes

If you require a file twice, an error will be raised. I'm currently looking into that.

Do you know of a set of scripts that you think should be made into a RSC file? Please tell! The RSC Library can never be too large!
Want to make an RSC file yourself? Download the ScriptEditor program here! (Ruby Program)
You can also download a stand-alone Win32 version here!
 
Yeah Great Work

I will use this to make my scripts into one simple file, since everyone know that I don't put everything in one script because I like to be organized about this :D
 

Anonymous

Guest

Yes. Everyone knows that. They teach us that in elementary.

Good work, though I fail to see any major advantage except for organization. Will this speed the interpreting of the script? Or the other way round, considering you have to decompress and re interpret?
 
Well think about it this way you have alot of scripts/files to require (like MACL/SDK) just make all of them a .rsc file and then you can just require the rsc file and they all get required :)
 

Anonymous

Guest

That's exactly what I pointed out. Apart from organization benefits, do we get anything else?
 

Anonymous

Guest

I don't doubt it. I'm simply asking. I'm not a RMXP wizard.
 
In a word... 'nice!'

But I have a question.

I understand that there 'is' a limit to the number of scripts that can be employed/added into a single RMXP project... possibly due to the number of insertable pages. Something like 200. I could be wrong about the quantity... but I did hear about SOME script quaitity limit.

Do you think it likely that this system could negate this limitation?
 
I highly doubt the fact that there is a limit, if it is, it probably is something like 999 "sections" in the editor. It's not like you can't place 500 scripts into 1 section.

Zeriab did some research on the number of lines of code before it took a while to interpret and said that there was no noticable change until nearly 500,000 lines.

I believe this just takes a slight more time to interpreter, but probably not something noticable.
 
THIS is what will make MACL easy to use and available to all : it is just a library and doesn't overwrite anything (I guess), so an archive is really nice.

Just one thing. When you write :
File.open(file_name, 'rb') do |file|
script_data = Marshal.load(file)
end

You should use load_data. Essentially the same thing, but can read from encrypted archives.
Actually, when an rgssad exist, it should ONLY load from the archive : evaluating unknown scripts is waaay too risky.
 
Actually, I wrote that instead of load_data so that the RSC require will work in Ruby, not just RGSS. I was thinking universal when I started to make the script and data ^_^
 
^^ you should offer the script to compress the thing:p or even better a embeded ruby application.


Edit:

arf thanks :p for the pm .. xD
 
^Ghost_Hunter^, if you know of a set of scripts that could be made into an RSC file, then tell. I'll hunt them down and RSC-ize them if they prove useful.
 
Guess what? I'm releasing ScriptEditor early! See the first post for the download link. The program is simple to use. Just tell me if you encounter any problems using it.
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top