Skip to content

Instantly share code, notes, and snippets.

@pulsejet
Created October 18, 2020 20:09
Show Gist options
  • Save pulsejet/6cb547db7222a65b32f34499c7266c36 to your computer and use it in GitHub Desktop.
Save pulsejet/6cb547db7222a65b32f34499c7266c36 to your computer and use it in GitHub Desktop.
Dump RPG XP Scripts.rxdata to ruby files
# Adapted from https://github.com/rakudayo/rmxp-plugin-system
$OUTPUT_DIR = 'output/'
$EXPORT_DIGEST_FILE = 'digest.rb'
$INFILE_S = 'Scripts.rxdata'
$COLUMN1_WIDTH = 12
$COLUMN2_WIDTH = 45
require 'fileutils'
require 'zlib'
FileUtils.mkdir_p $OUTPUT_DIR
# An array of invalid Windows filename strings and their substitutions. This
# array is used to modify the script title in RMXP's script editor to construct
# a filename for saving the script out to the filesystem.
$INVALID_CHARS_FOR_FILENAME= [
[" - ", "_"],
[" ", "_"],
["-", "_"],
[":", "_"],
["/", "_"],
["\\", "_"],
["*", "_"],
["|", "_"],
["<", "_"],
[">", "_"],
["?", "_"] ]
# Length of a filename (for output formatting purposes)
$FILENAME_WIDTH = 35
# Length of a line separator for output
$LINE_LENGTH = 80
#----------------------------------------------------------------------------
# generate_filename: Generates a filename given an RGSS script entry.
# script: An entry for a script in the loaded Scripts file. This
# is a three element array with the 0th element as the unique id,
# the 1st element is the script's title in RM, and the 3rd
# element is the script's compressed text
#----------------------------------------------------------------------------
def generate_filename(script)
(Zlib::Inflate.inflate(script[2]) != '' ? "#{fix_name(script[1])}.rb" : 'EMPTY')
end
#----------------------------------------------------------------------------
# generate_filename: Generates a filename given an RGSS script's title.
# title: The title of the script in RM's script editor
#----------------------------------------------------------------------------
def fix_name(title)
result = String.new( title )
# Replace all invalid characters
for substitution in $INVALID_CHARS_FOR_FILENAME
result.gsub!(substitution[0], substitution[1])
end
result
end
# Get scripts
scripts = []
File.open($INFILE_S, File::RDONLY|File::BINARY) do |infile|
scripts = Marshal.load(infile)
end
# Create the export digest
digest = []
File.open($OUTPUT_DIR + $EXPORT_DIGEST_FILE, File::WRONLY|File::CREAT|File::TRUNC) do |digestfile|
scripts.each_index do |i|
digest[i] = []
digest[i] << scripts[i][0]
digest[i] << scripts[i][1]
digest[i] << generate_filename(scripts[i])
line = "#{digest[i][0].to_s.ljust($COLUMN1_WIDTH)}#{digest[i][1].ljust($COLUMN2_WIDTH)}#{digest[i][2]}\n"
#puts line
digestfile << line
end
end
# Find out how many non-empty scripts we have
num_scripts = digest.select { |e| e[2].upcase != "EMPTY" }.size
num_exported = 0
# Save each script to a separate file
scripts.each_index do |i|
if digest[i][2].upcase != "EMPTY"
inflate_start_time = Time.now
File.open($OUTPUT_DIR + digest[i][2], File::WRONLY|File::CREAT|File::TRUNC|File::BINARY) do |outfile|
outfile << Zlib::Inflate.inflate(scripts[i][2])
end
num_exported += 1
inflate_elapsed_time = Time.now - inflate_start_time
str = "Exported #{digest[i][2].ljust($FILENAME_WIDTH)}(#{num_exported.to_s.rjust(3, '0')}/#{num_scripts.to_s.rjust(3, '0')})"
str += " #{inflate_elapsed_time} seconds" if inflate_elapsed_time > 0.0
puts str
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment