global

def self.read_scripts(scripts_file_path, output_path, romanize, logging, processing_mode)

Parameters:
  • processing_mode (Symbol) -- Whether to read in default mode, force rewrite or append new text to existing files
  • logging (Boolean) -- Whether to log
  • romanize (Boolean) -- Whether to romanize text
  • output_path (String) --
  • scripts_file_path (String) --
def self.read_scripts(scripts_file_path, output_path, romanize, logging, processing_mode)
    scripts_filename = File.basename(scripts_file_path)
    scripts_basename = File.basename(scripts_file_path, '.*').downcase

    scripts_plain_output_path = File.join(output_path, "#{scripts_basename}_plain.txt")
    scripts_output_path = File.join(output_path, "#{scripts_basename}.txt")
    scripts_trans_output_path = File.join(output_path, "#{scripts_basename}_trans.txt")

    if processing_mode == :default && File.exist?(scripts_trans_output_path)
        puts 'scripts_trans.txt file already exists. If you want to forcefully re-read all files, use --force flag, ' \
                      'or --append if you want append new text to already existing files.'
        return
    end

    script_entries = Marshal.load(File.binread(scripts_file_path))

    # @type [Set<String>]
    scripts_lines = Set.new
    # @type [Hash{String => String}]
    scripts_translation_map = {}

    if processing_mode == :append
        if File.exist?(scripts_trans_output_path)
            scripts_translation_map =
                Hash[
                    File.readlines(scripts_output_path, encoding: 'utf-8', chomp: true).zip(
                        File.readlines(scripts_trans_output_path, encoding: 'utf-8', chomp: true),
                    )
                ]
        else
            puts APPEND_FLAG_OMIT_MSG
            processing_mode = :default
        end
    end

    # @type [Array<String>]
    codes_content = []

    # This code was fun before `that` game used Windows-1252 degree symbol
    script_entries.each do |script|
        # @type [String]
        code = Zlib::Inflate.inflate(script[2])
        code = convert_to_utf8(code)
        codes_content.push(code)
    end

    extracted = extract_strings(codes_content.join)

    extracted.each do |string|
        # Removes the U+3000 Japanese typographical space to check if string, when stripped, is truly empty
        string = string.gsub(' ', ' ').strip

        next if string.empty?

        if string.match?(%r{(Graphics|Data|Audio|Movies|System)/.*/?}) || string.match?(/r[xv]data2?$/) ||
                  string.match?(STRING_IS_ONLY_SYMBOLS_RE) || string.match?(/@window/) || string.match?(/\$game/) ||
                  string.match?(/_/) || string.match?(/^\\e/) || string.match?(/.*\(/) ||
                  string.match?(/^([d\d\p{P}+-]*|[d\p{P}+-]*)$/) || string.match?(/ALPHAC/) ||
                  string.match?(
                      /^(Actor<id>|ExtraDropItem|EquipLearnSkill|GameOver|Iconset|Window|true|false|MActor%d|[wr]b|\\f|\\n|\[[A-Z]*\])$/,
                  )
            next
        end

        string = romanize_string(string) if romanize

        if processing_mode == :append && !scripts_translation_map.include?(string)
            scripts_translation_map.insert_at_index(scripts_lines.length, string, '')
        end

        scripts_lines.add(string)
    end

    puts "Parsed #{scripts_filename}" if logging

    File.binwrite(scripts_plain_output_path, codes_content.join("\n"))

    original_content, translated_content =
        if processing_mode == :append
            [scripts_translation_map.keys.join("\n"), scripts_translation_map.values.join("\n")]
        else
            [scripts_lines.join("\n"), "\n" * (scripts_lines.empty? ? 0 : scripts_lines.length - 1)]
        end

    File.binwrite(scripts_output_path, original_content)
    File.binwrite(scripts_trans_output_path, translated_content)
end