global

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

Parameters:
  • logging (Boolean) -- Whether to log
  • 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, logging)
    scripts_basename = File.basename(scripts_file_path)
    script_entries = Marshal.load(File.binread(scripts_file_path))

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

    scripts_translation_map = Hash[scripts_original_text.zip(scripts_translated_text)]

    # Shuffle can possibly break the game in scripts, so no shuffling
    codes = []

    # This code was fun before `that` game used Windows-1252 degree symbol
    script_entries.each do |script|
        code = Zlib::Inflate.inflate(script[2])
        code.force_encoding('UTF-8')

        # I figured how String#encode works - now everything is good
        unless code.valid_encoding?
            [Encoding::UTF_8, Encoding::WINDOWS_1252, Encoding::SHIFT_JIS].each do |encoding|
                encoded = code.encode(code.encoding, encoding)

                if encoded.valid_encoding?
                    code.force_encoding(encoding)
                    break
                end
            rescue Encoding::InvalidByteSequenceError
                next
            end
        end

        # this shit finally works and requires NO further changes
        string_array, index_array = extract_quoted_strings(code)

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

            gotten = scripts_translation_map[string]
            code[index - string.length, string.length] = gotten unless gotten.nil? || gotten.empty?
        end

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

    puts "Written #{scripts_basename}" if logging

    # File.binwrite(File.join(output_path, 'scripts_plain.txt'), codes.join("\n")) - debug line
    File.binwrite(File.join(output_path, scripts_basename), Marshal.dump(script_entries))
end