Module: Studio::Text

Defined in:
scripts/00800 Studio/00400 Text.rb

Overview

Module that helps the game to get text in various langages

Author:

  • Nuri Yuri

Constant Summary collapse

Available_Langs =

List of lang id available in the game

%w[en fr it de es ko kana]
CSV_BASE =

Base index of pokemon text in csv files

100_000
VD_TEXT_FILENAME =

Name of the file containing all the dialogs

'Data/2.dat'

Class Method Summary collapse

Class Method Details

.build_dialog_from_csv_rows(rows, lang_index) ⇒ Array<String>

Build the text array from the csv rows

Parameters:

  • rows (Array)
  • lang_index (Integer)

Returns:



136
137
138
139
140
# File 'scripts/00800 Studio/00400 Text.rb', line 136

def build_dialog_from_csv_rows(rows, lang_index)
  return Array.new(rows.size - 1) do |i|
    rows[i + 1][lang_index].to_s.gsub('\nl', "\n")
  end
end

.compile

Marshalize the dialogs



143
144
145
146
147
# File 'scripts/00800 Studio/00400 Text.rb', line 143

def compile
  Dir.chdir('Data/Text/Dialogs') do
    Dir['*.csv'].grep(/^[0-9]+\.csv$/).each { |filename| compile_csv(filename) }
  end
end

.compile_csv(filename)

Compile a single csv file

Parameters:

  • filename (String)

    name of the csv file



151
152
153
154
155
156
157
158
159
160
# File 'scripts/00800 Studio/00400 Text.rb', line 151

def compile_csv(filename)
  file_id = filename.to_i
  rows = CSV.read(filename)
  rows.first.each_with_index do |lang, lang_index|
    next unless Available_Langs.include?(lang = lang.strip.downcase)
    arr = build_dialog_from_csv_rows(rows, lang_index)
    output_filename = format('%<id>d.%<lang>s.dat', id: file_id, lang: lang)
    save_data(arr, output_filename)
  end
end

.default_langString

Return the default game lang

Returns:



38
39
40
# File 'scripts/00800 Studio/00400 Text.rb', line 38

def default_lang
  Configs.language.default_language_code
end

.get(file_id, text_id) ⇒ String

Get a text front the text database

Parameters:

  • file_id (Integer)

    ID of the text file

  • text_id (Integer)

    ID of the text in the file

Returns:



46
47
48
# File 'scripts/00800 Studio/00400 Text.rb', line 46

def get(file_id, text_id)
  get_dialog_message(CSV_BASE + file_id, text_id)
end

.get_dialog_message(file_id, text_id) ⇒ String Also known as: get_external

Get a dialog message

Parameters:

  • file_id (Integer)

    id of the dialog file

  • text_id (Integer)

    id of the dialog message in the file (0 = 2nd line of csv, 1 = 3rd line of csv)

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'scripts/00800 Studio/00400 Text.rb', line 66

def get_dialog_message(file_id, text_id)
  # Try to find the text from the cache
  if (file = @dialogs[file_id])
    if (text = file[text_id])
      return text
    end
    return log_error("Unable to find text #{text_id} in dialog file #{file_id}.")
  end
  # Try to load the texts
  unless try2get_marshalized_dialog(file_id) || try2get_csv_dialog(file_id)
    return log_error("Dialog file #{file_id} doesn't exist.")
  end
  # Return the result after the text was loaded
  return get_dialog_message(file_id, text_id)
end

.get_file(file_id) ⇒ Array<String>

Get a list of text from the text database

Parameters:

  • file_id (Integer)

    ID of the text file

Returns:

  • (Array<String>)

    the list of text contained in the file.



53
54
55
56
57
58
59
60
# File 'scripts/00800 Studio/00400 Text.rb', line 53

def get_file(file_id)
  file_id += CSV_BASE
  return @dialogs[file_id] if @dialogs.key?(file_id)
  unless try2get_marshalized_dialog(file_id) || try2get_csv_dialog(file_id)
    return log_error("Text file #{file_id - CSV_BASE} doesn't exist.")
  end
  return @dialogs[file_id]
end

.load

load text in the correct lang ($options.language or LANG in game.ini)



24
25
26
27
28
29
30
31
32
33
34
# File 'scripts/00800 Studio/00400 Text.rb', line 24

def load
  reload_rh_texts unless PSDK_CONFIG.release?
  lang = (PFM.game_state ? PFM.game_state.options.language : default_lang)
  unless lang && Available_Langs.include?(lang)
    log_error "Unsupported language code (#{lang}).\nSupported language code are : #{Available_Langs.join(', ')}"
    lang = Available_Langs.first
    log_info "Fallback language code : #{lang}"
  end
  @lang = lang
  @dialogs.clear
end

.marshalized_text_file_exist?(filename) ⇒ Boolean

Test if a marshalized text file exist

Parameters:

  • filename (String)

    name of the file in Data/text/Dialogs

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'scripts/00800 Studio/00400 Text.rb', line 101

def marshalized_text_file_exist?(filename)
  if PSDK_CONFIG.release?
    vdfilename = VD_TEXT_FILENAME
    ::Kernel::Loaded[vdfilename] = Yuki::VD.new(vdfilename, :read) unless ::Kernel::Loaded.key?(vdfilename)
    return ::Kernel::Loaded[vdfilename].exists?(File.basename(filename))
  end
  return File.exist?(filename)
end

.reload_rh_texts

Reload texts from Ruby Host



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'scripts/00800 Studio/00400 Text.rb', line 163

def reload_rh_texts
  langs = Dir["Data/Text/Dialogs/#{CSV_BASE}.*.dat"].collect { |i| i.match(/[0-9]+\.([a-z]+)\.dat$/).captures[0] }
  if langs.empty? || (!File.exist?('project.studio') &&
    File.mtime("Data/Text/Dialogs/#{CSV_BASE}.#{langs.first}.dat") < File.mtime("Data/Text/#{langs.first}.dat"))
    langs.concat(Configs.language.choosable_language_code) if langs.empty? # Must add all project supported languages
    langs << Configs.language.default_language_code if langs.empty?
    unless File.exist?('project.studio') # No RH back compilation for studio project!
      log_debug('Updating Text files')
      ScriptLoader.load_tool('Text2CSV')
    end
    Available_Langs.clear
    Available_Langs.concat(langs)
    log_debug('Compiling Text files')
    compile
  else
    denom = "#{langs.first}.dat"
    must_update = Dir["Data/Text/Dialogs/*.#{denom}"].any? do |dat_filename|
      csv_filename = dat_filename.sub(denom, 'csv')
      next false unless File.exist?(csv_filename)

      next File.mtime(dat_filename) < File.mtime(csv_filename)
    end
    if must_update
      log_debug('Recompiling texts from CSV')
      compile
    end
  end
end

.try2get_csv_dialog(file_id) ⇒ Boolean

Try to load a csv dialog file

Parameters:

  • file_id (Integer)

    id of the dialog file

Returns:

  • (Boolean)

    if the operation was a success



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'scripts/00800 Studio/00400 Text.rb', line 113

def try2get_csv_dialog(file_id)
  if File.exist?(filename = format('Data/Text/Dialogs/%<file_id>d.csv', file_id: file_id))
    rows = CSV.read(filename)
    lang_index = rows.first.index { |el| el.strip.downcase == @lang }
    unless lang_index
      lang_index = rows.first.index { |el| Available_Langs.include?(el.strip.downcase) }
      unless lang_index
        log_error("Failed to find any lang in #{filename}")
        @dialogs[file_id] = []
        return true
      end
    end
    @dialogs[file_id] = build_dialog_from_csv_rows(rows, lang_index)
    log_info("CSV text #{filename} was loaded") if debug?
    return true
  end
  return false
end

.try2get_marshalized_dialog(file_id) ⇒ Boolean

Try to load a preprocessed dialog file (Marshal)

Parameters:

  • file_id (Integer)

    id of the dialog file

Returns:

  • (Boolean)

    if the operation was a success



88
89
90
91
92
93
94
95
96
# File 'scripts/00800 Studio/00400 Text.rb', line 88

def try2get_marshalized_dialog(file_id)
  filename = format('Data/Text/Dialogs/%<id>d.%<lang>s.dat', id: file_id, lang: @lang)
  if marshalized_text_file_exist?(filename)
    @dialogs[file_id] = load_data(filename)
    log_info("Marshal text #{filename} was loaded") if debug?
    return true
  end
  return false
end