global

def self.write_scripts(scripts_file_path, other_path, output_path, romanize, logging)

Parameters:
  • logging (Boolean) -- Whether to log
  • romanize (Boolean) -- If files were read with romanize, this option will romanize original game text to compare
  • output_path (String) -- Path to the output directory
  • other_path (String) -- Path to translation/other directory containing .txt files
  • scripts_file_path (String) -- Path to Scripts.*data file
def self.write_scripts(scripts_file_path, other_path, output_path, romanize, logging)
    scripts_basename = File.basename(scripts_file_path)
    script_entries = Marshal.load(File.binread(scripts_file_path))

    # @type [Array<String>]
    scripts_original_text =
        File
            .readlines(File.join(other_path, 'scripts.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\r\n") }
    # @type [Array<String>]
    scripts_translated_text =
        File
            .readlines(File.join(other_path, 'scripts_trans.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\r\n") }

    # @type [Hash{String => String}]
    scripts_translation_map = Hash[scripts_original_text.zip(scripts_translated_text)]

    script_entries.each do |script|
        # @type [String]
        code = Zlib::Inflate.inflate(script[2])
        code = convert_to_utf8(code)

        string_array, index_array = extract_strings(code, mode: true)

        string_array
            .zip(index_array)
            .reverse_each do |string, index|
                string = string.gsub(' ', '').strip
                next if string.empty? || !scripts_translation_map.include?(string)

                string = romanize_string(string) if romanize

                translated = scripts_translation_map[string]
                code[index, string.length] = translated if translated && !translated.empty?
            end

        script[2] = Zlib::Deflate.deflate(code, Zlib::BEST_COMPRESSION)
    end

    File.binwrite(File.join(output_path, scripts_basename), Marshal.dump(script_entries))
    puts "Written #{scripts_basename}" if logging
end