Skip to content

Instantly share code, notes, and snippets.

@masked-rpgmaker
Last active March 16, 2020 02:57
Show Gist options
  • Save masked-rpgmaker/235581d6f5ad1ffa6e764697224367f8 to your computer and use it in GitHub Desktop.
Save masked-rpgmaker/235581d6f5ad1ffa6e764697224367f8 to your computer and use it in GitHub Desktop.
Script para extrair e montar Scripts.rvdata2 e executar o Game.exe do RPG Maker VX Ace por linha de comando.
# frozen_string_literal: true
require 'fileutils'
require 'zlib'
SESSION_SYMBOL = "\xE2\x96\xBC"
$commands = {}
#
# Registers a CLI command
#
# @param sym Command symbol
# @param klass Handler class
#
def register_command(sym, klass)
$commands[sym] = klass.new
end
#
# Extractor
#
# Extracts scripts from Data/Scripts.rvdata2.
#
class Extractor
def initialize
@section = ''
@title = ''
@content = ''
end
def load_scripts
File.open('Data/Scripts.rvdata2', 'rb') do |file|
return Marshal.load(file)
end
end
def append_to_list(filename, title)
File.open(filename, 'a') do |file|
file.write(title)
file.write("\n")
end
end
def extract_script(section, title, content)
content = Zlib.inflate(content)
path = "Scripts/#{section}"
FileUtils.mkdir_p(path)
append_to_list("#{path}/.list", title)
return if content.empty? && title.empty?
puts "Extracting #{section} > #{title}..."
filename = "#{path}/#{title}.rb"
File.open(filename, 'wb') do |file|
file.write(content)
end
end
def section?
i = @title.index(SESSION_SYMBOL)
return false if i.nil?
sec = @title[i + 1..-1].strip
@section = sec
end
def section_name
title[title.index("\xE2\x96\xBC") + 1..-1].strip
end
def run
File.rename 'Scripts', 'Scripts.tmp' rescue nil
FileUtils.mkdir_p 'Scripts'
puts "Loading scripts from Data/Scripts.rvdata2..."
scripts = load_scripts
puts "Found #{scripts.size} scripts."
puts
scripts.each do |item|
_, @title, @content = item
if section?
append_to_list('Scripts/.list', @section)
next
end
extract_script(@section, @title, @content)
end
puts
puts "Done."
FileUtils.rm_rf 'Scripts.tmp'
end
end
register_command :extract, Extractor
#
# Builder
#
# Builds Data/Scripts.rvdata2 from scripts on Scripts folder.
#
class Builder
def deflate(string)
d = Zlib::Deflate.new(Zlib::BEST_COMPRESSION)
data = d.deflate(string, Zlib::FINISH)
d.close
data
end
def read_list(filename)
File.open(filename) do |file|
file.each_line do |line|
yield line.chomp.force_encoding('UTF-8')
end
end
end
def read_folder(folder)
scripts = []
puts
puts "Entering #{folder}"
read_list("#{folder}/.list") do |title|
next scripts << [0, '', deflate('')] if title.strip.empty?
if FileTest.directory?("#{folder}/#{title}")
scripts << [0, "#{SESSION_SYMBOL} #{title}", deflate('')]
scripts += read_folder("#{folder}/#{title}")
end
if FileTest.file?("#{folder}/#{title}.rb")
scripts << build_script(folder, title)
end
end
scripts
end
def build_script(folder, title)
puts "Building #{folder.split('/')[1..-1].join(' > ')} > #{title}..."
content = File.read(folder + '/' + title + '.rb')
content = content.force_encoding('utf-8')
[0, title, deflate(content)]
end
def run
scripts = read_folder('Scripts')
puts
puts "Built #{scripts.size} scripts."
File.open('Data/Scripts.rvdata2', 'wb') do |file|
Marshal.dump(scripts, file)
end
puts "Done."
end
end
register_command :build, Builder
#
# Runner
#
# Executes Game.exe with appropriate flags
#
class Runner
def run
system 'Game.exe console test'
end
end
register_command :start, Runner
# Main process
command = ARGV[0].to_sym
unless $commands.key?(command)
STDERR.puts "Unrecognized command `#{command}'"
exit 1
end
$commands[command].run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment