global

def self.write_other(original_files_paths, other_path, output_path, shuffle_level, logging, game_type)

Parameters:
  • game_type (String) --
  • logging (Boolean) --
  • shuffle_level (Integer) --
  • output_path (String) --
  • other_path (String) --
  • original_files (Array) --
def self.write_other(original_files_paths, other_path, output_path, shuffle_level, logging, game_type)
    other_object_array_map = Hash[original_files_paths.map do |filename|
        [File.basename(filename), Marshal.load(File.binread(filename))]
    end]

    # 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|
        other_filename = File.basename(filename, '.*').downcase

        other_original_text = File.readlines(File.join(other_path, "#{other_filename}.txt"), encoding: 'UTF-8', chomp: true)
                                  .map { |line| line.gsub('\#', "\n").strip }

        other_translated_text = File.readlines(File.join(other_path, "#{other_filename}_trans.txt"), encoding: 'UTF-8', chomp: true)
                                    .map { |line| line.gsub('\#', "\n").strip }

        if shuffle_level.positive?
            other_translated_text.shuffle!
            other_translated_text = shuffle_words(other_translated_text) if shuffle_level == 2
        end

        other_translation_map = Hash[other_original_text.zip(other_translated_text)].freeze

        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_with_index do |variable, i|
                    next unless variable.is_a?(String)

                    variable = variable.strip
                    next if variable.empty?

                    variable = variable.gsub(/\r\n/, "\n")
                    translated = get_variable_translated(variable, other_translation_map, game_type)

                    unless translated.nil? || translated.empty?
                        if i.zero?
                            object.name = translated
                        elsif i == 1
                            object.nickname = translated
                        elsif i == 2
                            object.description = translated
                        else
                            object.note = translated
                        end
                    end
                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 |pg|
                    list = pages.nil? ? object.list : pages[pg].instance_variable_get(:@list) # for some reason .list access doesn't work (wtf?)
                    next if list.nil?

                    in_sequence = false
                    line = []
                    item_indices = []

                    list.each_with_index do |item, it|
                        code = item.code

                        unless allowed_codes.include?(code)
                            if in_sequence
                                joined = line.join('\#').strip
                                translated = get_parameter_translated(401, joined, other_translation_map, game_type)

                                unless translated.nil? || translated.empty?
                                    split = translated.split('\#')

                                    split_length = split.length
                                    line_length = line.length

                                    item_indices.each_with_index do |index, i|
                                        list[index].parameters[0] = i < split_length ? split[i] : ''
                                    end

                                    list[item_indices.last].parameters[0] = split[line_length..].join("\n") if split_length > line_length
                                end
                            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])
                            item_indices.push(it)
                        elsif parameters[0].is_a?(Array)
                            parameters[0].each_with_index do |subparameter, sp|
                                next unless subparameter.is_a?(String)

                                subparameter = subparameter.strip
                                next if subparameter.empty?

                                translated = get_parameter_translated(code, subparameter, other_translation_map, game_type)
                                parameters[0][sp] = translated unless translated.nil? || translated.empty?
                            end
                        elsif parameters[0].is_a?(String)
                            parameter = parameters[0].strip
                            next if parameter.empty?

                            translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
                            parameters[0] = translated unless translated.nil? || translated.empty?
                        elsif parameters[1].is_a?(String)
                            parameter = parameters[1].strip
                            next if parameter.empty?

                            translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
                            parameters[1] = translated unless translated.nil? || translated.empty?
                        end

                        item.parameters = parameters
                    end
                end
            end
        end

        puts "Written #{filename}" if logging

        File.binwrite(File.join(output_path, filename), Marshal.dump(other_object_array))
    end
end