global

def self.read_other(other_files_paths, output_path, logging, game_type, processing_type)

Parameters:
  • processing_type (String) --
  • game_type (String) --
  • logging (Boolean) --
  • output_path (String) --
  • other_files_paths (Array) --
def self.read_other(other_files_paths, output_path, logging, game_type, processing_type)
    other_object_array_map = Hash[other_files_paths.map do |filename|
        [File.basename(filename), Marshal.load(File.binread(filename))]
    end]

    inner_processing_type = processing_type
    # 401 - dialogue lines
    # 405 - credits lines
    # 102 - dialogue choices array
    # 402 - one of the dialogue choices from the array
    # 356 - system lines/special texts (do they even exist before mv?)
    allowed_codes = [401, 405, 102, 402, 356].freeze

    other_object_array_map.each do |filename, other_object_array|
        processed_filename = File.basename(filename, '.*').downcase

        other_output_path = File.join(output_path, "#{processed_filename}.txt")
        other_trans_output_path = File.join(output_path, "#{processed_filename}_trans.txt")

        if processing_type == :default && File.exist?(other_trans_output_path)
            puts "#{processed_filename}_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."
            next
        end

        other_lines = IndexSet.new
        other_translation_map = nil

        if processing_type == :append
            if File.exist?(other_trans_output_path)
                inner_processing_type == :append
                other_translation_map = Hash[File.readlines(other_output_path, chomp: true)
                                                 .zip(File.readlines(other_trans_output_path, chomp: true))]
            else
                puts "Files aren't already parsed. Continuing as if --append flag was omitted."
                inner_processing_type = :default
            end
        end

        if !filename.start_with?(/Common|Troops/)
            other_object_array.each do |object|
                next if object.nil?

                name = object.name
                nickname = object.nickname
                description = object.description
                note = object.note

                [name, nickname, description, note].each do |variable|
                    next unless variable.is_a?(String)

                    variable = variable.strip
                    next if variable.empty?

                    parsed = parse_variable(variable, game_type)
                    next if parsed.nil?

                    parsed = parsed.gsub(/\r?\n/, '\#')

                    other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
                        !other_translation_map.include?(parsed)

                    other_lines.add(parsed)
                end
            end
        else
            other_object_array.each do |object|
                next if object.nil?

                pages = object.pages
                pages_length = pages.nil? ? 1 : pages.length

                (0..pages_length).each do |i|
                    list = pages.nil? ? object.list : pages[i].instance_variable_get(:@list)
                    next if list.nil?

                    in_sequence = false
                    line = []

                    list.each do |item|
                        code = item.code

                        unless allowed_codes.include?(code)
                            if in_sequence
                                joined = line.join('\#').strip

                                other_translation_map.insert_at_index(other_lines.length, joined, '') if inner_processing_type == :append &&
                                    !other_translation_map.include?(joined)

                                other_lines.add(joined)

                                line.clear
                                in_sequence = false
                            end
                            next
                        end

                        parameters = item.parameters

                        if [401, 405].include?(code)
                            next unless parameters[0].is_a?(String) && !parameters[0].empty?

                            in_sequence = true
                            line.push(parameters[0].gsub(/\r?\n/, '\#'))
                        elsif parameters[0].is_a?(Array)
                            parameters[0].each do |subparameter|
                                next unless subparameter.is_a?(String)

                                subparameter = subparameter.strip
                                next if subparameter.empty?

                                other_translation_map.insert_at_index(other_lines.length, subparameter, '') if inner_processing_type == :append &&
                                    !other_translation_map.include?(subparameter)

                                other_lines.add(subparameter)
                            end
                        elsif parameters[0].is_a?(String)
                            parameter = parameters[0].strip
                            next if parameter.empty?

                            parameter = parameter.gsub(/\r?\n/, '\#')

                            other_translation_map.insert_at_index(other_lines.length, parameter, '') if inner_processing_type == :append &&
                                !other_translation_map.include?(parameter)

                            other_lines.add(parameter)
                        elsif parameters[1].is_a?(String)
                            parameter = parameters[1].strip
                            next if parameter.empty?

                            parameter = parameter.gsub(/\r?\n/, '\#')

                            other_translation_map.insert_at_index(other_lines.length, parameter, '') if inner_processing_type == :append &&
                                !other_translation_map.include?(parameter)

                            other_lines.add(parameter)
                        end
                    end
                end
            end
        end

        puts "Parsed #{filename}" if logging

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

        File.binwrite(other_output_path, original_content)
        File.binwrite(other_trans_output_path, translated_content)
    end
end