Script Tool - RSC Require

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

Script Spoiler
#==============================================================================
#  ** 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!
Expand to see the code.


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:
require 'LIBRARYNAME'
Expand to see the code.
or
rsc_require 'LIBRARYNAME'
Expand to see the code.

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!
Great work Yeyinde!!!

I will be sure to be using this soon (and making a SDK RSC file)
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
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 :)
That's exactly what I pointed out. Apart from organization benefits, do we get anything else?
Awesome idea :)

CTM, organization benefits are enough for this to be a great addition if you're working on a large project
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
I'll release the editor into the public once it reaches version 1.0. But it has a while to go before it reaches that ^^
how can i make RSC ?
You'll need Yeyinde's script editor and by his last post

Quote:
I'll release the editor into the public once it reaches version 1.0. But it has a while to go before it reaches that ^^
^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.
YEAH!!! I can't wait to try this out.

(NOW WORK ON THAT PHP SCRIPT, BEFORE I PAY SOMEONE ELSE TO DO IT!)
I've packaged this: (contained 6 script)
Aleworks Input Module, 2.0
Aleworks Input Module, 2.20? It's on version 2.00...
But thanks, trebor777. Good script yeyinde. Great tool.
xD .. I got confused with the SDK .... sorry...
Let's push this topic up a notch. Some screenshot too to show that ScriptEditor works on almost any OS :D

Windows XP Spoiler

Mac OSX Spoiler

Thanks to the Slacked Admin, jamie, for taking this screenshot.
Awesome. Here's a question. I know that every game one makes with rmxp requires the rgss102e.dll which is located in C:\Windows\System32. This helps so that one does not have to have a copy of the dll file in every game directory, thus saving some harddrive space. That said, is there a way to put the rsc files in a 'global' directory, like the rgss.dll, and use this script to require them so that one does not have to include them with every game? If so, how would one go about doing this?

I also know that if one does include the rgss.dll in the game's directory, the game will use the local dll rather than the global dll. Can one also do this with the rsc files, in case a game requires an edited version?
DraycosGoldaryn's idea is excellent. Get the RTP directory (using those 3 RGSS lines I don't have here right now), and check if the rsc are here in case they weren't in the local directories.

I don't know if you should examine every RTP folder or set up for the root... I guess it's up to you.
Ok, Yeyinde answered my question in the IRC chat. He said if one puts
$: << ENV['SystemRoot'] + '\\System32'
Expand to see the code.
anywhere before one requires, it will search for a local rsc and if it doesn't find one, it will search within System32.

I suppose one could change ENV['SystemRoot'] + '\\System32' to whichever folder one wants the global rsc files to be located.

If you do this multiple times with different directories, it will search each one consecutively until it finds the proper rsc file.

Thank you Yeyinde for this great script, and for answering my questions.
Can't these RSC files be edited, so you can basically add any code you want to the game? (Meaning you could easily cheat) Or is there some sort of checksum to make sure the file isn't edited?
RSC files are meant to house script libraries' not actual game script. There should be now way to cheat while using RSC files.
Perhaps say, if you added code to level up your characters in the database in Main.
You can't actually do that because the all the $game_ and $data_ objects do not exist when the RSC files are loaded. They are created during Scene_Title and Scene_Load.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=21163