#!/usr/bin/env ruby require 'RGSS' require 'optparse' $logging = false opts = {} OptionParser.new do |options| options.banner = "This tool allows to parse RPG Maker project to .txt files and back.\n\nUsage: rvpacker-txt COMMAND [options]\n\nCOMMANDS:\n read - Parses RPG Maker game files to .txt\n write - Writes parsed files back to their initial form\nOPTIONS:\n" options.on('-d', '--input-dir DIRECTORY', 'Input directory of RPG Maker project.', 'Must contain "Data" or "original" folder to read,', 'and additionally "translation" with "maps" and "other" subdirectories to write.') do |dir| opts[:input_dir] = dir end options.on('-l', '--log', 'Log information while processing.') do $logging = true end options.on('-s', '--shuffle NUMBER', 'At value 1: Shuffles all lines in strings, at value 2: shuffles all lines and words in strings.') do |number| opts[:shuffle] = number end options.on_tail('-h', '--help', 'Show help message.') do puts options exit end end.parse!(ARGV) if ARGV.empty? puts 'COMMAND argument is required.' exit end opts[:action] = ARGV.shift unless %w[read write].include?(opts[:action]) puts 'Invalid command. Allowed commands are: read, write.' exit end project_types = { 'vx' => :vx, 'ace' => :ace, 'xp' => :xp } directory = opts[:input_dir] raise "#{directory} not found" unless File.exist?(directory) data_folder = Dir.foreach(directory).find { |filename| filename.downcase == 'original' } data_folder = Dir.foreach(directory).find { |filename| filename.downcase == 'data' } if data_folder.nil? raise '"Data" or "original" directory not found within input directory.' if data_folder.nil? types = %w[vx ace xp] type = types.find do |_type| case _type when 'xp' break project_types[_type] if File.exist?(File.join(directory, data_folder, 'System.rxdata')) when 'vx' break project_types[_type] if File.exist?(File.join(directory, data_folder, 'System.rvdata')) when 'ace' break project_types[_type] if File.exist?(File.join(directory, data_folder, 'System.rvdata2')) else break nil end end raise 'Couldn\'t determine project engine.' if type.nil? RGSS.serialize(type, opts[:action], directory)