global

def self.read_system(system_file_path, ini_file_path, output_path, logging, processing_type)

Parameters:
  • processing_type (String) --
  • logging (Boolean) --
  • output_path (String) --
  • ini_file_path (String) --
  • system_file_path (String) --
def self.read_system(system_file_path, ini_file_path, output_path, logging, processing_type)
    system_filename = File.basename(system_file_path)
    system_basename = File.basename(system_file_path, '.*').downcase

    system_output_path = File.join(output_path, "#{system_basename}.txt")
    system_trans_output_path = File.join(output_path, "#{system_basename}_trans.txt")

    if processing_type == :default && File.exist?(system_trans_output_path)
        puts 'system_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

    system_object = Marshal.load(File.binread(system_file_path))

    system_lines = IndexSet.new
    system_translation_map = nil

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

    elements = system_object.elements
    skill_types = system_object.skill_types
    weapon_types = system_object.weapon_types
    armor_types = system_object.armor_types
    currency_unit = system_object.currency_unit
    terms = system_object.terms || system_object.words

    [elements, skill_types, weapon_types, armor_types].each do |array|
        next if array.nil?

        array.each do |string|
            next unless string.is_a?(String)

            string = string.strip
            next if string.empty?

            system_translation_map.insert_at_index(system_lines.length, string, '') if processing_type == :append &&
                !system_translation_map.include?(string)

            system_lines.add(string)
        end
    end

    if currency_unit.is_a?(String)
        currency_unit = currency_unit.strip

        unless currency_unit.empty?
            system_translation_map.insert_at_index(system_lines.length, currency_unit, '') if processing_type == :append &&
                !system_translation_map.include?(currency_unit)

            system_lines.add(currency_unit)
        end
    end

    terms.instance_variables.each do |variable|
        value = terms.instance_variable_get(variable)

        if value.is_a?(String)
            value = value.strip

            unless value.empty?
                system_translation_map.insert_at_index(system_lines.length, value, '') if processing_type == :append &&
                    !system_translation_map.include?(value)

                system_lines.add(value)
            end

            next
        end

        value.each do |string|
            next unless string.is_a?(String)

            string = string.strip
            next if string.empty?

            system_translation_map.insert_at_index(system_lines.length, string, '') if processing_type == :append &&
                !system_translation_map.include?(string)

            system_lines.add(string)
        end
    end

    # Game title from System file and ini file may differ, but requesting user request to determine which line do they want is LAME
    # So just throw that ini ass and continue
    ini_game_title = read_ini_title(ini_file_path).strip

    system_translation_map.insert_at_index(system_lines.length, ini_game_title, '') if processing_type == :append &&
        !system_translation_map.include?(ini_game_title)

    system_lines.add(ini_game_title)

    puts "Parsed #{system_filename}" if logging

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

    File.binwrite(system_output_path, original_content)
    File.binwrite(system_trans_output_path, translated_content)
end