rvpacker-txt 1.12.0 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/read.rb CHANGED
@@ -3,38 +3,39 @@
3
3
  require 'zlib'
4
4
  require_relative 'extensions'
5
5
 
6
- STRING_IS_ONLY_SYMBOLS_RE = /^[.()+\-:;\[\]^~%&!№$@`*\/→×??x%▼|♥♪!:〜『』「」〽。…‥=゠、,【】[]{}()〔〕⦅⦆〘〙〈〉《》・\\#'"<>=_ー※▶ⅠⅰⅡⅱⅢⅲⅣⅳⅤⅴⅥⅵⅦⅶⅧⅷⅨⅸⅩⅹⅪⅺⅫⅻⅬⅼⅭⅽⅮⅾⅯⅿ\s0-9]+$/
6
+ STRING_IS_ONLY_SYMBOLS_RE =
7
+ %r{^[.()+\-:;\[\]^~%&!№$@`*/→×??x%▼|♥♪!:〜『』「」〽。…‥=゠、,【】[]{}()〔〕⦅⦆〘〙〈〉《》・\\#'"<>=_ー※▶ⅠⅰⅡⅱⅢⅲⅣⅳⅤⅴⅥⅵⅦⅶⅧⅷⅨⅸⅩⅹⅪⅺⅫⅻⅬⅼⅭⅽⅮⅾⅯⅿ\s0-9]+$}
7
8
  APPEND_FLAG_OMIT_MSG = "Files aren't already parsed. Continuing as if --append flag was omitted."
8
9
 
9
10
  # @param [Integer] code
10
11
  # @param [String] parameter
11
12
  # @param [String] game_type
12
- # @return [String]
13
+ # @return [String | nil]
13
14
  def self.parse_parameter(code, parameter, game_type)
14
15
  return nil if parameter.match?(STRING_IS_ONLY_SYMBOLS_RE)
15
16
 
16
17
  ends_with_if = parameter[/ if\(.*\)$/]
18
+
17
19
  parameter = parameter.chomp(ends_with_if) if ends_with_if
18
20
 
19
- unless game_type.nil?
21
+ if game_type
20
22
  case game_type
21
- when 'lisa'
22
- case code
23
- when 401, 405
24
- prefix = parameter[/^(\\et\[[0-9]+\]|\\nbt)/]
25
- parameter = parameter.sub(prefix, '') if prefix
26
- when 102
27
- # Implement some custom parsing
28
- when 356
29
- # Implement some custom parsing
30
- else
31
- return nil
32
- end
33
- # Implement cases for other games
23
+ when 'lisa'
24
+ case code
25
+ when 401, 405
26
+ prefix = parameter[/^(\\et\[[0-9]+\]|\\nbt)/]
27
+ parameter = parameter.sub(prefix, '') if prefix
28
+ when 102
29
+ # Implement some custom parsing
30
+ when 356
31
+ # Implement some custom parsing
34
32
  else
35
- nil
33
+ return nil
34
+ end
35
+ # Implement cases for other games
36
+ else
37
+ nil
36
38
  end
37
-
38
39
  end
39
40
 
40
41
  parameter
@@ -49,36 +50,137 @@ def self.parse_variable(variable, type, _game_type)
49
50
  # for some reason it returns true if multi-line string contains carriage returns (wtf?)
50
51
  return nil if variable.match?(STRING_IS_ONLY_SYMBOLS_RE)
51
52
 
52
- if variable.split("\n").all? do |line|
53
- line.empty? ||
54
- line.match?(/^#? ?<.*>.?$/) ||
55
- line.match?(/^[a-z][0-9]$/)
56
- end
53
+ if variable.split("\n").all? { |line| line.empty? || line.match?(/^#? ?<.*>.?$/) || line.match?(/^[a-z][0-9]$/) }
57
54
  return nil
58
55
  end
59
56
 
60
- return nil if variable.match?(/^[+-]?[0-9]+$/) ||
61
- variable.match?(/---/) ||
62
- variable.match?(/restrict eval/)
57
+ return nil if variable.match?(/^[+-]?[0-9]+$/) || variable.match?(/---/) || variable.match?(/restrict eval/)
63
58
 
64
59
  case type
65
- when 0 # name
66
- when 1 # nickname
67
- when 2 # description
68
- when 3 # note
69
- else
70
- nil
60
+ when 0 # name
61
+ when 1 # nickname
62
+ when 2 # description
63
+ when 3 # note
64
+ else
65
+ nil
71
66
  end
72
67
 
73
68
  variable
74
69
  end
75
70
 
71
+ # @param [Array<Object>] list
72
+ # @param [Array<Integer>] allowed_codes
73
+ # @param [Boolean] romanize
74
+ # @param [String] game_type
75
+ # @param [Symbol] processing_mode
76
+ # @param [Set<String>] set
77
+ # @param [Hash{String => String}] map
78
+ def self.parse_list(list, allowed_codes, romanize, game_type, processing_mode, set, map)
79
+ in_sequence = false
80
+ # @type [Array<String>]
81
+ line = []
82
+
83
+ list.each do |item|
84
+ # @type [Integer]
85
+ code = item.code
86
+
87
+ if in_sequence && ![401, 405].include?(code)
88
+ unless line.empty?
89
+ joined = line.join("\n").strip.gsub("\n", '\#')
90
+ parsed = parse_parameter(401, joined, game_type)
91
+
92
+ if parsed
93
+ parsed = romanize_string(parsed) if romanize
94
+
95
+ map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)
96
+
97
+ set.add(parsed)
98
+ end
99
+
100
+ line.clear
101
+ end
102
+
103
+ in_sequence = false
104
+ end
105
+
106
+ next unless allowed_codes.include?(code)
107
+
108
+ # @type [Array<String>]
109
+ parameters = item.parameters
110
+
111
+ case code
112
+ when 401, 405
113
+ # @type [String]
114
+ parameter = parameters[0]
115
+ next unless parameter.is_a?(String)
116
+
117
+ parameter = convert_to_utf8(parameter)
118
+
119
+ in_sequence = true
120
+ line.push(parameter.gsub(' ', ' ').strip)
121
+ when 102
122
+ if parameters[0].is_a?(Array)
123
+ parameters[0].each do |subparameter|
124
+ next unless subparameter.is_a?(String)
125
+
126
+ subparameter = subparameter.strip
127
+ next if subparameter.empty?
128
+
129
+ subparameter = convert_to_utf8(subparameter)
130
+ parsed = parse_parameter(code, subparameter, game_type)
131
+ next unless parsed
132
+
133
+ parsed = romanize_string(parsed) if romanize
134
+
135
+ map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)
136
+
137
+ set.add(parsed)
138
+ end
139
+ end
140
+ when 356
141
+ # @type [String]
142
+ parameter = parameters[0]
143
+ next unless parameter.is_a?(String)
144
+
145
+ parameter = parameter.strip
146
+ next if parameter.empty?
147
+
148
+ parameter = convert_to_utf8(parameter)
149
+ parsed = parse_parameter(code, parameter, game_type)
150
+ next unless parsed
151
+
152
+ parsed = romanize_string(parsed) if romanize
153
+
154
+ map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)
155
+
156
+ set.add(parsed)
157
+ when 320, 324
158
+ # @type [String]
159
+ parameter = parameters[1]
160
+ next unless parameter.is_a?(String)
161
+
162
+ parameter = parameter.strip
163
+ next if parameter.empty?
164
+
165
+ parameter = convert_to_utf8(parameter)
166
+ parsed = parse_parameter(code, parameter, game_type)
167
+ next unless parsed
168
+
169
+ parsed = romanize_string(parsed) if romanize
170
+
171
+ map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)
172
+
173
+ set.add(parsed)
174
+ end
175
+ end
176
+ end
177
+
76
178
  # @param [Array<String>] maps_files_paths Array of paths to original maps files
77
179
  # @param [String] output_path Path to output directory
78
180
  # @param [Boolean] romanize Whether to romanize text
79
181
  # @param [Boolean] logging Whether to log
80
182
  # @param [String] game_type Game type for custom processing
81
- # @param [String] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
183
+ # @param [Symbol] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
82
184
  def self.read_map(maps_files_paths, output_path, romanize, logging, game_type, processing_mode)
83
185
  maps_output_path = File.join(output_path, 'maps.txt')
84
186
  names_output_path = File.join(output_path, 'names.txt')
@@ -86,36 +188,51 @@ def self.read_map(maps_files_paths, output_path, romanize, logging, game_type, p
86
188
  names_trans_output_path = File.join(output_path, 'names_trans.txt')
87
189
 
88
190
  if processing_mode == :default && (File.exist?(maps_trans_output_path) || File.exist?(names_trans_output_path))
89
- puts 'maps_trans.txt or names_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.'
191
+ puts 'maps_trans.txt or names_trans.txt file already exists. If you want to forcefully re-read all files, ' \
192
+ 'use --force flag, or --append if you want append new text to already existing files.'
90
193
  return
91
194
  end
92
195
 
93
196
  maps_object_map = Hash[maps_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]
94
197
 
198
+ # @type [Set<String>]
95
199
  maps_lines = Set.new
200
+ # @type [Set<String>]
96
201
  names_lines = Set.new
97
202
 
98
- maps_translation_map = nil
99
- names_translation_map = nil
203
+ # @type [Hash{String => String}]
204
+ maps_translation_map = {}
205
+ # @type [Hash{String => String}]
206
+ names_translation_map = {}
100
207
 
101
208
  if processing_mode == :append
102
209
  if File.exist?(maps_trans_output_path)
103
- maps_translation_map = Hash[File.readlines(maps_output_path, chomp: true)
104
- .zip(File.readlines(maps_trans_output_path, chomp: true))]
105
- names_translation_map = Hash[File.readlines(names_output_path, chomp: true)
106
- .zip(File.readlines(names_trans_output_path, chomp: true))]
210
+ maps_translation_map =
211
+ Hash[
212
+ File.readlines(maps_output_path, encoding: 'utf-8', chomp: true).zip(
213
+ File.readlines(maps_trans_output_path, encoding: 'utf-8', chomp: true),
214
+ )
215
+ ]
216
+ names_translation_map =
217
+ Hash[
218
+ File.readlines(names_output_path, encoding: 'utf-8', chomp: true).zip(
219
+ File.readlines(names_trans_output_path, encoding: 'utf-8', chomp: true),
220
+ )
221
+ ]
107
222
  else
108
223
  puts APPEND_FLAG_OMIT_MSG
109
224
  processing_mode = :default
110
225
  end
111
226
  end
112
227
 
228
+ # @type [Array<Integer>]
113
229
  # 401 - dialogue lines
114
230
  # 102 - dialogue choices array
115
231
  # 356 - system lines/special texts (do they even exist before mv?)
116
- allowed_codes = [401, 102, 356].freeze
232
+ allowed_codes = [102, 320, 324, 356, 401].freeze
117
233
 
118
234
  maps_object_map.each do |filename, object|
235
+ # @type [String]
119
236
  display_name = object.display_name
120
237
 
121
238
  if display_name.is_a?(String)
@@ -124,111 +241,48 @@ def self.read_map(maps_files_paths, output_path, romanize, logging, game_type, p
124
241
  unless display_name.empty?
125
242
  display_name = romanize_string(display_name) if romanize
126
243
 
127
- names_translation_map.insert_at_index(names_lines.length, display_name, '') if processing_mode == :append &&
128
- !names_translation_map.include?(display_name)
244
+ if processing_mode == :append && !names_translation_map.include?(display_name)
245
+ names_translation_map.insert_at_index(names_lines.length, display_name, '')
246
+ end
129
247
 
130
248
  names_lines.add(display_name)
131
249
  end
132
250
  end
133
251
 
134
252
  events = object.events
135
- next if events.nil?
253
+ next unless events
136
254
 
137
255
  events.each_value do |event|
138
256
  pages = event.pages
139
- next if pages.nil?
257
+ next unless pages
140
258
 
141
259
  pages.each do |page|
142
260
  list = page.list
143
- next if list.nil?
144
-
145
- in_sequence = false
146
- line = []
147
-
148
- list.each do |item|
149
- code = item.code
150
-
151
- if in_sequence && code != 401
152
- unless line.empty?
153
- joined = line.join("\n").strip.gsub("\n", '\#')
154
- parsed = parse_parameter(401, joined, game_type)
155
-
156
- unless parsed.nil?
157
- parsed = romanize_string(parsed) if romanize
158
-
159
- maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_mode == :append &&
160
- !maps_translation_map.include?(parsed)
161
-
162
- maps_lines.add(parsed)
163
- end
164
-
165
- line.clear
166
- end
167
-
168
- in_sequence = false
169
- end
170
-
171
- next unless allowed_codes.include?(code)
172
-
173
- parameters = item.parameters
174
-
175
- if code == 401
176
- next unless parameters[0].is_a?(String) && !parameters[0].empty?
177
-
178
- in_sequence = true
179
- line.push(parameters[0].gsub(' ', ' ').strip)
180
- elsif parameters[0].is_a?(Array)
181
- parameters[0].each do |subparameter|
182
- next unless subparameter.is_a?(String)
183
-
184
- subparameter = subparameter.strip
185
- next if subparameter.empty?
186
-
187
- parsed = parse_parameter(code, subparameter, game_type)
188
- next if parsed.nil?
189
-
190
- parsed = romanize_string(parsed) if romanize
191
-
192
- maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_mode == :append &&
193
- !maps_translation_map.include?(parsed)
194
-
195
- maps_lines.add(parsed)
196
- end
197
- elsif parameters[0].is_a?(String)
198
- parameter = parameters[0].strip
199
- next if parameter.empty?
261
+ next unless list
200
262
 
201
- parsed = parse_parameter(code, parameter, game_type)
202
- next if parsed.nil?
203
-
204
- parsed = romanize_string(parsed) if romanize
205
-
206
- maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_mode == :append &&
207
- !maps_translation_map.include?(parsed)
208
-
209
- maps_lines.add(parsed)
210
- end
211
- end
263
+ parse_list(list, allowed_codes, romanize, game_type, processing_mode, maps_lines, maps_translation_map)
212
264
  end
213
265
  end
214
266
 
215
267
  puts "Parsed #{filename}" if logging
216
268
  end
217
269
 
218
- maps_original_content,
219
- maps_translated_content,
220
- names_original_content,
221
- names_translated_content = if processing_mode == :append
222
- [maps_translation_map.keys.join("\n"),
223
- maps_translation_map.values.join("\n"),
224
- names_translation_map.keys.join("\n"),
225
- names_translation_map.values.join("\n")]
226
- else
227
- [maps_lines.join("\n"),
228
- "\n" * (maps_lines.empty? ? 0 : maps_lines.length - 1),
229
- names_lines.join("\n"),
230
- "\n" * (names_lines.empty? ? 0 : names_lines.length - 1)]
231
- end
270
+ maps_original_content, maps_translated_content, names_original_content, names_translated_content =
271
+ if processing_mode == :append
272
+ [
273
+ maps_translation_map.keys.join("\n"),
274
+ maps_translation_map.values.join("\n"),
275
+ names_translation_map.keys.join("\n"),
276
+ names_translation_map.values.join("\n"),
277
+ ]
278
+ else
279
+ [
280
+ maps_lines.join("\n"),
281
+ "\n" * (maps_lines.empty? ? 0 : maps_lines.length - 1),
282
+ names_lines.join("\n"),
283
+ "\n" * (names_lines.empty? ? 0 : names_lines.length - 1),
284
+ ]
285
+ end
232
286
 
233
287
  File.binwrite(maps_output_path, maps_original_content)
234
288
  File.binwrite(maps_trans_output_path, maps_translated_content)
@@ -241,16 +295,18 @@ end
241
295
  # @param [Boolean] romanize Whether to romanize text
242
296
  # @param [Boolean] logging Whether to log
243
297
  # @param [String] game_type Game type for custom processing
244
- # @param [String] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
298
+ # @param [Symbol] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
245
299
  def self.read_other(other_files_paths, output_path, romanize, logging, game_type, processing_mode)
246
300
  other_object_array_map = Hash[other_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]
247
301
 
248
- inner_processing_type = processing_mode
302
+ inner_processing_mode = processing_mode
303
+
304
+ # @type [Array<Integer>]
249
305
  # 401 - dialogue lines
250
306
  # 405 - credits lines
251
307
  # 102 - dialogue choices array
252
308
  # 356 - system lines/special texts (do they even exist before mv?)
253
- allowed_codes = [401, 405, 102, 356].freeze
309
+ allowed_codes = [102, 320, 324, 356, 401, 405].freeze
254
310
 
255
311
  other_object_array_map.each do |filename, other_object_array|
256
312
  processed_filename = File.basename(filename, '.*').downcase
@@ -259,161 +315,103 @@ def self.read_other(other_files_paths, output_path, romanize, logging, game_type
259
315
  other_trans_output_path = File.join(output_path, "#{processed_filename}_trans.txt")
260
316
 
261
317
  if processing_mode == :default && File.exist?(other_trans_output_path)
262
- 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."
318
+ puts "#{processed_filename}_trans.txt file already exists. If you want to forcefully re-read all files, ' \
319
+ 'use --force flag, or --append if you want append new text to already existing files."
263
320
  next
264
321
  end
265
322
 
323
+ # @type [Set<String>]
266
324
  other_lines = Set.new
267
- other_translation_map = nil
325
+ # @type [Hash{String => String}]
326
+ other_translation_map = {}
268
327
 
269
328
  if processing_mode == :append
270
329
  if File.exist?(other_trans_output_path)
271
- inner_processing_type == :append
272
- other_translation_map = Hash[File.readlines(other_output_path, chomp: true)
273
- .zip(File.readlines(other_trans_output_path, chomp: true))]
330
+ inner_processing_mode = :append
331
+ other_translation_map =
332
+ Hash[
333
+ File.readlines(other_output_path, encoding: 'utf-8', chomp: true).zip(
334
+ File.readlines(other_trans_output_path, encoding: 'utf-8', chomp: true),
335
+ )
336
+ ]
274
337
  else
275
338
  puts APPEND_FLAG_OMIT_MSG
276
- inner_processing_type = :default
339
+ inner_processing_mode = :default
277
340
  end
278
341
  end
279
342
 
280
- if !filename.start_with?(/Common|Troops/)
343
+ if !filename.match?(/^(Common|Troops)/)
281
344
  other_object_array.each do |object|
282
- next if object.nil?
283
-
284
- name = object.name
285
- nickname = object.nickname if object.is_a?(RPG::Actor)
286
- description = object.description
287
- note = object.note
288
-
289
- catch :next_object do
290
- [name, nickname, description, note].each_with_index do |variable, type|
291
- next unless variable.is_a?(String)
292
-
293
- variable = variable.strip
294
- next if variable.empty?
295
-
296
- parsed = parse_variable(variable, type, game_type)
297
-
298
- if !parsed.nil?
299
- parsed = romanize_string(parsed) if romanize
300
-
301
- parsed = parsed.split("\n").map(&:strip).join('\#')
345
+ next unless object
346
+
347
+ # @type [Array<String | nil>]
348
+ attributes = [
349
+ object.name,
350
+ object.is_a?(RPG::Actor) ? object.nickname : nil,
351
+ object.description,
352
+ object.note,
353
+ object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message1 : nil,
354
+ object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message2 : nil,
355
+ object.is_a?(RPG::State) ? object.message3 : nil,
356
+ object.is_a?(RPG::State) ? object.message4 : nil,
357
+ ]
358
+
359
+ attributes.each_with_index do |var, type|
360
+ next unless var.is_a?(String)
361
+
362
+ var = var.strip
363
+ next if var.empty?
364
+
365
+ var = convert_to_utf8(var)
366
+ parsed = parse_variable(var, type, game_type)
367
+
368
+ unless parsed
369
+ break if type.zero?
370
+ next
371
+ end
302
372
 
303
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
304
- !other_translation_map.include?(parsed)
373
+ parsed = romanize_string(parsed) if romanize
374
+ parsed = parsed.split("\n").map(&:strip).join('\#')
305
375
 
306
- other_lines.add(parsed)
307
- elsif type.zero?
308
- throw :next_object
309
- end
376
+ if inner_processing_mode == :append && !other_translation_map.include?(parsed)
377
+ other_translation_map.insert_at_index(other_lines.length, parsed, '')
310
378
  end
379
+
380
+ other_lines.add(parsed)
311
381
  end
312
382
  end
313
383
  else
314
384
  other_object_array.each do |object|
315
- next if object.nil?
385
+ next unless object
316
386
 
317
387
  pages = object.pages
318
- pages_length = pages.nil? ? 1 : pages.length
388
+ pages_length = !pages ? 1 : pages.length
319
389
 
320
390
  (0..pages_length).each do |i|
321
- list = pages.nil? ? object.list : pages[i].instance_variable_get(:@list)
322
- next if list.nil?
323
-
324
- in_sequence = false
325
- line = []
326
-
327
- list.each do |item|
328
- code = item.code
329
-
330
- if in_sequence && ![401, 405].include?(code)
331
- unless line.empty?
332
- joined = line.join("\n").strip.gsub("\n", '\#')
333
- parsed = parse_parameter(401, joined, game_type)
334
-
335
- unless parsed.nil?
336
- parsed = romanize_string(parsed) if romanize
337
-
338
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
339
- !other_translation_map.include?(parsed)
340
-
341
- other_lines.add(parsed)
342
- end
343
-
344
- line.clear
345
- end
346
-
347
- in_sequence = false
348
- end
349
-
350
- next unless allowed_codes.include?(code)
351
-
352
- parameters = item.parameters
353
-
354
- if [401, 405].include?(code)
355
- next unless parameters[0].is_a?(String) && !parameters[0].empty?
356
-
357
- in_sequence = true
358
- line.push(parameters[0].gsub(' ', ' ').strip)
359
- elsif parameters[0].is_a?(Array)
360
- parameters[0].each do |subparameter|
361
- next unless subparameter.is_a?(String)
362
-
363
- subparameter = subparameter.strip
364
- next if subparameter.empty?
365
-
366
- parsed = parse_parameter(code, subparameter, game_type)
367
- next if parsed.nil?
368
-
369
- parsed = romanize_string(parsed) if romanize
370
-
371
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
372
- !other_translation_map.include?(parsed)
373
-
374
- other_lines.add(parsed)
375
- end
376
- elsif parameters[0].is_a?(String)
377
- parameter = parameters[0].strip
378
- next if parameter.empty?
379
-
380
- parsed = parse_parameter(code, parameter, game_type)
381
- next if parsed.nil?
382
-
383
- parsed = romanize_string(parsed) if romanize
384
-
385
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
386
- !other_translation_map.include?(parsed)
387
-
388
- other_lines.add(parsed)
389
- elsif parameters[1].is_a?(String)
390
- parameter = parameters[1].strip
391
- next if parameter.empty?
392
-
393
- parsed = parse_parameter(code, parameter, game_type)
394
- next if parsed.nil?
395
-
396
- parsed = romanize_string(parsed) if romanize
397
-
398
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
399
- !other_translation_map.include?(parsed)
400
-
401
- other_lines.add(parsed)
402
- end
403
- end
391
+ list = !pages ? object.list : pages[i].instance_variable_get(:@list)
392
+ next unless list
393
+
394
+ parse_list(
395
+ list,
396
+ allowed_codes,
397
+ romanize,
398
+ game_type,
399
+ inner_processing_mode,
400
+ other_lines,
401
+ other_translation_map,
402
+ )
404
403
  end
405
404
  end
406
405
  end
407
406
 
408
407
  puts "Parsed #{filename}" if logging
409
408
 
410
- original_content, translated_content = if processing_mode == :append
411
- [other_translation_map.keys.join("\n"),
412
- other_translation_map.values.join("\n")]
413
- else
414
- [other_lines.join("\n"),
415
- "\n" * (other_lines.empty? ? 0 : other_lines.length - 1)]
416
- end
409
+ original_content, translated_content =
410
+ if processing_mode == :append
411
+ [other_translation_map.keys.join("\n"), other_translation_map.values.join("\n")]
412
+ else
413
+ [other_lines.join("\n"), "\n" * (other_lines.empty? ? 0 : other_lines.length - 1)]
414
+ end
417
415
 
418
416
  File.binwrite(other_output_path, original_content)
419
417
  File.binwrite(other_trans_output_path, translated_content)
@@ -436,7 +434,7 @@ end
436
434
  # @param [String] output_path
437
435
  # @param [Boolean] romanize Whether to romanize text
438
436
  # @param [Boolean] logging Whether to log
439
- # @param [String] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
437
+ # @param [Symbol] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
440
438
  def self.read_system(system_file_path, ini_file_path, output_path, romanize, logging, processing_mode)
441
439
  system_filename = File.basename(system_file_path)
442
440
  system_basename = File.basename(system_file_path, '.*').downcase
@@ -445,19 +443,26 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
445
443
  system_trans_output_path = File.join(output_path, "#{system_basename}_trans.txt")
446
444
 
447
445
  if processing_mode == :default && File.exist?(system_trans_output_path)
448
- 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.'
446
+ puts 'system_trans.txt file already exists. If you want to forcefully re-read all files, use --force flag, ' \
447
+ 'or --append if you want append new text to already existing files.'
449
448
  return
450
449
  end
451
450
 
452
451
  system_object = Marshal.load(File.binread(system_file_path))
453
452
 
453
+ # @type [Set<String>]
454
454
  system_lines = Set.new
455
- system_translation_map = nil
455
+ # @type [Hash{String => String}]
456
+ system_translation_map = {}
456
457
 
457
458
  if processing_mode == :append
458
459
  if File.exist?(system_trans_output_path)
459
- system_translation_map = Hash[File.readlines(system_output_path, chomp: true)
460
- .zip(File.readlines(system_trans_output_path, chomp: true))]
460
+ system_translation_map =
461
+ Hash[
462
+ File.readlines(system_output_path, encoding: 'utf-8', chomp: true).zip(
463
+ File.readlines(system_trans_output_path, encoding: 'utf-8', chomp: true),
464
+ )
465
+ ]
461
466
  else
462
467
  puts APPEND_FLAG_OMIT_MSG
463
468
  processing_mode = :default
@@ -472,7 +477,7 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
472
477
  terms = system_object.terms || system_object.words
473
478
 
474
479
  [elements, skill_types, weapon_types, armor_types].each do |array|
475
- next if array.nil?
480
+ next unless array
476
481
 
477
482
  array.each do |string|
478
483
  next unless string.is_a?(String)
@@ -480,10 +485,12 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
480
485
  string = string.strip
481
486
  next if string.empty?
482
487
 
488
+ string = convert_to_utf8(string)
483
489
  string = romanize_string(string) if romanize
484
490
 
485
- system_translation_map.insert_at_index(system_lines.length, string, '') if processing_mode == :append &&
486
- !system_translation_map.include?(string)
491
+ if processing_mode == :append && !system_translation_map.include?(string)
492
+ system_translation_map.insert_at_index(system_lines.length, string, '')
493
+ end
487
494
 
488
495
  system_lines.add(string)
489
496
  end
@@ -493,10 +500,12 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
493
500
  currency_unit = currency_unit.strip
494
501
 
495
502
  unless currency_unit.empty?
503
+ currency_unit = convert_to_utf8(currency_unit)
496
504
  currency_unit = romanize_string(currency_unit) if romanize
497
505
 
498
- system_translation_map.insert_at_index(system_lines.length, currency_unit, '') if processing_mode == :append &&
499
- !system_translation_map.include?(currency_unit)
506
+ if processing_mode == :append && !system_translation_map.include?(currency_unit)
507
+ system_translation_map.insert_at_index(system_lines.length, currency_unit, '')
508
+ end
500
509
 
501
510
  system_lines.add(currency_unit)
502
511
  end
@@ -509,10 +518,12 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
509
518
  value = value.strip
510
519
 
511
520
  unless value.empty?
512
- value = romanize_string(string) if romanize
521
+ value = convert_to_utf8(value)
522
+ value = romanize_string(value) if romanize
513
523
 
514
- system_translation_map.insert_at_index(system_lines.length, value, '') if processing_mode == :append &&
515
- !system_translation_map.include?(value)
524
+ if processing_mode == :append && !system_translation_map.include?(value)
525
+ system_translation_map.insert_at_index(system_lines.length, value, '')
526
+ end
516
527
 
517
528
  system_lines.add(value)
518
529
  end
@@ -523,35 +534,38 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
523
534
  string = string.strip
524
535
  next if string.empty?
525
536
 
537
+ string = convert_to_utf8(string)
526
538
  string = romanize_string(string) if romanize
527
539
 
528
- system_translation_map.insert_at_index(system_lines.length, string, '') if processing_mode == :append &&
529
- !system_translation_map.include?(string)
540
+ if processing_mode == :append && !system_translation_map.include?(string)
541
+ system_translation_map.insert_at_index(system_lines.length, string, '')
542
+ end
530
543
 
531
544
  system_lines.add(string)
532
545
  end
533
546
  end
534
547
  end
535
548
 
536
- # Game title from System file and ini file may differ, but requesting user request to determine which line do they want is LAME
549
+ # Game title from System file and ini file may differ, but requesting user request to determine which line do they
550
+ # want is LAME
537
551
  # So just throw that ini ass and continue
538
552
  ini_game_title = read_ini_title(ini_file_path).strip
539
553
  ini_game_title = romanize_string(ini_game_title) if romanize
540
554
 
541
- system_translation_map.insert_at_index(system_lines.length, ini_game_title, '') if processing_mode == :append &&
542
- !system_translation_map.include?(ini_game_title)
555
+ if processing_mode == :append && !system_translation_map.include?(ini_game_title)
556
+ system_translation_map.insert_at_index(system_lines.length, ini_game_title, '')
557
+ end
543
558
 
544
559
  system_lines.add(ini_game_title)
545
560
 
546
561
  puts "Parsed #{system_filename}" if logging
547
562
 
548
- original_content, translated_content = if processing_mode == :append
549
- [system_translation_map.keys.join("\n"),
550
- system_translation_map.values.join("\n")]
551
- else
552
- [system_lines.join("\n"),
553
- "\n" * (system_lines.empty? ? 0 : system_lines.length - 1)]
554
- end
563
+ original_content, translated_content =
564
+ if processing_mode == :append
565
+ [system_translation_map.keys.join("\n"), system_translation_map.values.join("\n")]
566
+ else
567
+ [system_lines.join("\n"), "\n" * (system_lines.empty? ? 0 : system_lines.length - 1)]
568
+ end
555
569
 
556
570
  File.binwrite(system_output_path, original_content)
557
571
  File.binwrite(system_trans_output_path, translated_content)
@@ -561,7 +575,7 @@ end
561
575
  # @param [String] output_path
562
576
  # @param [Boolean] romanize Whether to romanize text
563
577
  # @param [Boolean] logging Whether to log
564
- # @param [String] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
578
+ # @param [Symbol] processing_mode Whether to read in default mode, force rewrite or append new text to existing files
565
579
  def self.read_scripts(scripts_file_path, output_path, romanize, logging, processing_mode)
566
580
  scripts_filename = File.basename(scripts_file_path)
567
581
  scripts_basename = File.basename(scripts_file_path, '.*').downcase
@@ -571,45 +585,40 @@ def self.read_scripts(scripts_file_path, output_path, romanize, logging, process
571
585
  scripts_trans_output_path = File.join(output_path, "#{scripts_basename}_trans.txt")
572
586
 
573
587
  if processing_mode == :default && File.exist?(scripts_trans_output_path)
574
- 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.'
588
+ puts 'scripts_trans.txt file already exists. If you want to forcefully re-read all files, use --force flag, ' \
589
+ 'or --append if you want append new text to already existing files.'
575
590
  return
576
591
  end
577
592
 
578
593
  script_entries = Marshal.load(File.binread(scripts_file_path))
579
594
 
595
+ # @type [Set<String>]
580
596
  scripts_lines = Set.new
581
- scripts_translation_map = nil
597
+ # @type [Hash{String => String}]
598
+ scripts_translation_map = {}
582
599
 
583
600
  if processing_mode == :append
584
601
  if File.exist?(scripts_trans_output_path)
585
- scripts_translation_map = Hash[File.readlines(scripts_output_path, chomp: true)
586
- .zip(File.readlines(scripts_trans_output_path, chomp: true))]
602
+ scripts_translation_map =
603
+ Hash[
604
+ File.readlines(scripts_output_path, encoding: 'utf-8', chomp: true).zip(
605
+ File.readlines(scripts_trans_output_path, encoding: 'utf-8', chomp: true),
606
+ )
607
+ ]
587
608
  else
588
609
  puts APPEND_FLAG_OMIT_MSG
589
610
  processing_mode = :default
590
611
  end
591
612
  end
592
613
 
614
+ # @type [Array<String>]
593
615
  codes_content = []
594
616
 
595
617
  # This code was fun before `that` game used Windows-1252 degree symbol
596
618
  script_entries.each do |script|
597
- code = Zlib::Inflate.inflate(script[2]).force_encoding('UTF-8')
598
-
599
- # I figured how String#encode works - now everything is good
600
- unless code.valid_encoding?
601
- [Encoding::UTF_8, Encoding::WINDOWS_1252, Encoding::SHIFT_JIS].each do |encoding|
602
- encoded = code.encode(code.encoding, encoding)
603
-
604
- if encoded.valid_encoding?
605
- code.force_encoding(encoding)
606
- break
607
- end
608
- rescue Encoding::InvalidByteSequenceError
609
- next
610
- end
611
- end
612
-
619
+ # @type [String]
620
+ code = Zlib::Inflate.inflate(script[2])
621
+ code = convert_to_utf8(code)
613
622
  codes_content.push(code)
614
623
  end
615
624
 
@@ -621,22 +630,21 @@ def self.read_scripts(scripts_file_path, output_path, romanize, logging, process
621
630
 
622
631
  next if string.empty?
623
632
 
624
- next if string.match?(/(Graphics|Data|Audio|Movies|System)\/.*\/?/) ||
625
- string.match?(/r[xv]data2?$/) ||
626
- string.match?(STRING_IS_ONLY_SYMBOLS_RE) ||
627
- string.match?(/@window/) ||
628
- string.match?(/\$game/) ||
629
- string.match?(/_/) ||
630
- string.match?(/^\\e/) ||
631
- string.match?(/.*\(/) ||
632
- string.match?(/^([d\d\p{P}+-]*|[d\p{P}+-]*)$/) ||
633
- string.match?(/ALPHAC/) ||
634
- string.match?(/^(Actor<id>|ExtraDropItem|EquipLearnSkill|GameOver|Iconset|Window|true|false|MActor%d|w[rb]|\\f|\\n|\[[A-Z]*\])$/)
633
+ if string.match?(%r{(Graphics|Data|Audio|Movies|System)/.*/?}) || string.match?(/r[xv]data2?$/) ||
634
+ string.match?(STRING_IS_ONLY_SYMBOLS_RE) || string.match?(/@window/) || string.match?(/\$game/) ||
635
+ string.match?(/_/) || string.match?(/^\\e/) || string.match?(/.*\(/) ||
636
+ string.match?(/^([d\d\p{P}+-]*|[d\p{P}+-]*)$/) || string.match?(/ALPHAC/) ||
637
+ string.match?(
638
+ /^(Actor<id>|ExtraDropItem|EquipLearnSkill|GameOver|Iconset|Window|true|false|MActor%d|[wr]b|\\f|\\n|\[[A-Z]*\])$/,
639
+ )
640
+ next
641
+ end
635
642
 
636
643
  string = romanize_string(string) if romanize
637
644
 
638
- scripts_translation_map.insert_at_index(scripts_lines.length, string, '') if processing_mode == :append &&
639
- !scripts_translation_map.include?(string)
645
+ if processing_mode == :append && !scripts_translation_map.include?(string)
646
+ scripts_translation_map.insert_at_index(scripts_lines.length, string, '')
647
+ end
640
648
 
641
649
  scripts_lines.add(string)
642
650
  end
@@ -645,13 +653,12 @@ def self.read_scripts(scripts_file_path, output_path, romanize, logging, process
645
653
 
646
654
  File.binwrite(scripts_plain_output_path, codes_content.join("\n"))
647
655
 
648
- original_content, translated_content = if processing_mode == :append
649
- [scripts_translation_map.keys.join("\n"),
650
- scripts_translation_map.values.join("\n")]
651
- else
652
- [scripts_lines.join("\n"),
653
- "\n" * (scripts_lines.empty? ? 0 : scripts_lines.length - 1)]
654
- end
656
+ original_content, translated_content =
657
+ if processing_mode == :append
658
+ [scripts_translation_map.keys.join("\n"), scripts_translation_map.values.join("\n")]
659
+ else
660
+ [scripts_lines.join("\n"), "\n" * (scripts_lines.empty? ? 0 : scripts_lines.length - 1)]
661
+ end
655
662
 
656
663
  File.binwrite(scripts_output_path, original_content)
657
664
  File.binwrite(scripts_trans_output_path, translated_content)