rvpacker-txt 1.11.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
 
95
- maps_lines = IndexSet.new
96
- names_lines = IndexSet.new
198
+ # @type [Set<String>]
199
+ maps_lines = Set.new
200
+ # @type [Set<String>]
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,110 +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
- end
165
-
166
- line.clear
167
- in_sequence = false
168
- end
169
-
170
- next unless allowed_codes.include?(code)
171
-
172
- parameters = item.parameters
173
-
174
- if code == 401
175
- next unless parameters[0].is_a?(String) && !parameters[0].empty?
176
-
177
- in_sequence = true
178
- line.push(parameters[0].gsub(' ', ' ').strip)
179
- elsif parameters[0].is_a?(Array)
180
- parameters[0].each do |subparameter|
181
- next unless subparameter.is_a?(String)
182
-
183
- subparameter = subparameter.strip
184
- next if subparameter.empty?
185
-
186
- parsed = parse_parameter(code, subparameter, game_type)
187
- next if parsed.nil?
188
-
189
- parsed = romanize_string(parsed) if romanize
190
-
191
- maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_mode == :append &&
192
- !maps_translation_map.include?(parsed)
193
-
194
- maps_lines.add(parsed)
195
- end
196
- elsif parameters[0].is_a?(String)
197
- parameter = parameters[0].strip
198
- next if parameter.empty?
261
+ next unless list
199
262
 
200
- parsed = parse_parameter(code, parameter, game_type)
201
- next if parsed.nil?
202
-
203
- parsed = romanize_string(parsed) if romanize
204
-
205
- maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_mode == :append &&
206
- !maps_translation_map.include?(parsed)
207
-
208
- maps_lines.add(parsed)
209
- end
210
- end
263
+ parse_list(list, allowed_codes, romanize, game_type, processing_mode, maps_lines, maps_translation_map)
211
264
  end
212
265
  end
213
266
 
214
267
  puts "Parsed #{filename}" if logging
215
268
  end
216
269
 
217
- maps_original_content,
218
- maps_translated_content,
219
- names_original_content,
220
- names_translated_content = if processing_mode == :append
221
- [maps_translation_map.keys.join("\n"),
222
- maps_translation_map.values.join("\n"),
223
- names_translation_map.keys.join("\n"),
224
- names_translation_map.values.join("\n")]
225
- else
226
- [maps_lines.join("\n"),
227
- "\n" * (maps_lines.empty? ? 0 : maps_lines.length - 1),
228
- names_lines.join("\n"),
229
- "\n" * (names_lines.empty? ? 0 : names_lines.length - 1)]
230
- 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
231
286
 
232
287
  File.binwrite(maps_output_path, maps_original_content)
233
288
  File.binwrite(maps_trans_output_path, maps_translated_content)
@@ -240,16 +295,18 @@ end
240
295
  # @param [Boolean] romanize Whether to romanize text
241
296
  # @param [Boolean] logging Whether to log
242
297
  # @param [String] game_type Game type for custom processing
243
- # @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
244
299
  def self.read_other(other_files_paths, output_path, romanize, logging, game_type, processing_mode)
245
300
  other_object_array_map = Hash[other_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]
246
301
 
247
- inner_processing_type = processing_mode
302
+ inner_processing_mode = processing_mode
303
+
304
+ # @type [Array<Integer>]
248
305
  # 401 - dialogue lines
249
306
  # 405 - credits lines
250
307
  # 102 - dialogue choices array
251
308
  # 356 - system lines/special texts (do they even exist before mv?)
252
- allowed_codes = [401, 405, 102, 356].freeze
309
+ allowed_codes = [102, 320, 324, 356, 401, 405].freeze
253
310
 
254
311
  other_object_array_map.each do |filename, other_object_array|
255
312
  processed_filename = File.basename(filename, '.*').downcase
@@ -258,160 +315,103 @@ def self.read_other(other_files_paths, output_path, romanize, logging, game_type
258
315
  other_trans_output_path = File.join(output_path, "#{processed_filename}_trans.txt")
259
316
 
260
317
  if processing_mode == :default && File.exist?(other_trans_output_path)
261
- 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."
262
320
  next
263
321
  end
264
322
 
265
- other_lines = IndexSet.new
266
- other_translation_map = nil
323
+ # @type [Set<String>]
324
+ other_lines = Set.new
325
+ # @type [Hash{String => String}]
326
+ other_translation_map = {}
267
327
 
268
328
  if processing_mode == :append
269
329
  if File.exist?(other_trans_output_path)
270
- inner_processing_type == :append
271
- other_translation_map = Hash[File.readlines(other_output_path, chomp: true)
272
- .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
+ ]
273
337
  else
274
338
  puts APPEND_FLAG_OMIT_MSG
275
- inner_processing_type = :default
339
+ inner_processing_mode = :default
276
340
  end
277
341
  end
278
342
 
279
- if !filename.start_with?(/Common|Troops/)
343
+ if !filename.match?(/^(Common|Troops)/)
280
344
  other_object_array.each do |object|
281
- next if object.nil?
282
-
283
- name = object.name
284
- nickname = object.nickname if object.is_a?(RPG::Actor)
285
- description = object.description
286
- note = object.note
287
-
288
- catch :next_object do
289
- [name, nickname, description, note].each_with_index do |variable, type|
290
- next unless variable.is_a?(String)
291
-
292
- variable = variable.strip
293
- next if variable.empty?
294
-
295
- parsed = parse_variable(variable, type, game_type)
296
-
297
- if !parsed.nil?
298
- parsed = romanize_string(parsed) if romanize
299
-
300
- 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
301
372
 
302
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
303
- !other_translation_map.include?(parsed)
373
+ parsed = romanize_string(parsed) if romanize
374
+ parsed = parsed.split("\n").map(&:strip).join('\#')
304
375
 
305
- other_lines.add(parsed)
306
- elsif type.zero?
307
- throw :next_object
308
- end
376
+ if inner_processing_mode == :append && !other_translation_map.include?(parsed)
377
+ other_translation_map.insert_at_index(other_lines.length, parsed, '')
309
378
  end
379
+
380
+ other_lines.add(parsed)
310
381
  end
311
382
  end
312
383
  else
313
384
  other_object_array.each do |object|
314
- next if object.nil?
385
+ next unless object
315
386
 
316
387
  pages = object.pages
317
- pages_length = pages.nil? ? 1 : pages.length
388
+ pages_length = !pages ? 1 : pages.length
318
389
 
319
390
  (0..pages_length).each do |i|
320
- list = pages.nil? ? object.list : pages[i].instance_variable_get(:@list)
321
- next if list.nil?
322
-
323
- in_sequence = false
324
- line = []
325
-
326
- list.each do |item|
327
- code = item.code
328
-
329
- if in_sequence && ![401, 405].include?(code)
330
- unless line.empty?
331
- joined = line.join("\n").strip.gsub("\n", '\#')
332
- parsed = parse_parameter(401, joined, game_type)
333
-
334
- unless parsed.nil?
335
- parsed = romanize_string(parsed) if romanize
336
-
337
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
338
- !other_translation_map.include?(parsed)
339
-
340
- other_lines.add(parsed)
341
- end
342
- end
343
-
344
- line.clear
345
- in_sequence = false
346
- end
347
-
348
- next unless allowed_codes.include?(code)
349
-
350
- parameters = item.parameters
351
-
352
- if [401, 405].include?(code)
353
- next unless parameters[0].is_a?(String) && !parameters[0].empty?
354
-
355
- in_sequence = true
356
- line.push(parameters[0].gsub(' ', ' ').strip)
357
- elsif parameters[0].is_a?(Array)
358
- parameters[0].each do |subparameter|
359
- next unless subparameter.is_a?(String)
360
-
361
- subparameter = subparameter.strip
362
- next if subparameter.empty?
363
-
364
- parsed = parse_parameter(code, subparameter, game_type)
365
- next if parsed.nil?
366
-
367
- parsed = romanize_string(parsed) if romanize
368
-
369
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
370
- !other_translation_map.include?(parsed)
371
-
372
- other_lines.add(parsed)
373
- end
374
- elsif parameters[0].is_a?(String)
375
- parameter = parameters[0].strip
376
- next if parameter.empty?
377
-
378
- parsed = parse_parameter(code, parameter, game_type)
379
- next if parsed.nil?
380
-
381
- parsed = romanize_string(parsed) if romanize
382
-
383
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
384
- !other_translation_map.include?(parsed)
385
-
386
- other_lines.add(parsed)
387
- elsif parameters[1].is_a?(String)
388
- parameter = parameters[1].strip
389
- next if parameter.empty?
390
-
391
- parsed = parse_parameter(code, parameter, game_type)
392
- next if parsed.nil?
393
-
394
- parsed = romanize_string(parsed) if romanize
395
-
396
- other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == :append &&
397
- !other_translation_map.include?(parsed)
398
-
399
- other_lines.add(parsed)
400
- end
401
- 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
+ )
402
403
  end
403
404
  end
404
405
  end
405
406
 
406
407
  puts "Parsed #{filename}" if logging
407
408
 
408
- original_content, translated_content = if processing_mode == :append
409
- [other_translation_map.keys.join("\n"),
410
- other_translation_map.values.join("\n")]
411
- else
412
- [other_lines.join("\n"),
413
- "\n" * (other_lines.empty? ? 0 : other_lines.length - 1)]
414
- 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
415
415
 
416
416
  File.binwrite(other_output_path, original_content)
417
417
  File.binwrite(other_trans_output_path, translated_content)
@@ -434,7 +434,7 @@ end
434
434
  # @param [String] output_path
435
435
  # @param [Boolean] romanize Whether to romanize text
436
436
  # @param [Boolean] logging Whether to log
437
- # @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
438
438
  def self.read_system(system_file_path, ini_file_path, output_path, romanize, logging, processing_mode)
439
439
  system_filename = File.basename(system_file_path)
440
440
  system_basename = File.basename(system_file_path, '.*').downcase
@@ -443,19 +443,26 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
443
443
  system_trans_output_path = File.join(output_path, "#{system_basename}_trans.txt")
444
444
 
445
445
  if processing_mode == :default && File.exist?(system_trans_output_path)
446
- 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.'
447
448
  return
448
449
  end
449
450
 
450
451
  system_object = Marshal.load(File.binread(system_file_path))
451
452
 
452
- system_lines = IndexSet.new
453
- system_translation_map = nil
453
+ # @type [Set<String>]
454
+ system_lines = Set.new
455
+ # @type [Hash{String => String}]
456
+ system_translation_map = {}
454
457
 
455
458
  if processing_mode == :append
456
459
  if File.exist?(system_trans_output_path)
457
- system_translation_map = Hash[File.readlines(system_output_path, chomp: true)
458
- .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
+ ]
459
466
  else
460
467
  puts APPEND_FLAG_OMIT_MSG
461
468
  processing_mode = :default
@@ -470,7 +477,7 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
470
477
  terms = system_object.terms || system_object.words
471
478
 
472
479
  [elements, skill_types, weapon_types, armor_types].each do |array|
473
- next if array.nil?
480
+ next unless array
474
481
 
475
482
  array.each do |string|
476
483
  next unless string.is_a?(String)
@@ -478,10 +485,12 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
478
485
  string = string.strip
479
486
  next if string.empty?
480
487
 
488
+ string = convert_to_utf8(string)
481
489
  string = romanize_string(string) if romanize
482
490
 
483
- system_translation_map.insert_at_index(system_lines.length, string, '') if processing_mode == :append &&
484
- !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
485
494
 
486
495
  system_lines.add(string)
487
496
  end
@@ -491,10 +500,12 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
491
500
  currency_unit = currency_unit.strip
492
501
 
493
502
  unless currency_unit.empty?
503
+ currency_unit = convert_to_utf8(currency_unit)
494
504
  currency_unit = romanize_string(currency_unit) if romanize
495
505
 
496
- system_translation_map.insert_at_index(system_lines.length, currency_unit, '') if processing_mode == :append &&
497
- !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
498
509
 
499
510
  system_lines.add(currency_unit)
500
511
  end
@@ -507,10 +518,12 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
507
518
  value = value.strip
508
519
 
509
520
  unless value.empty?
510
- value = romanize_string(string) if romanize
521
+ value = convert_to_utf8(value)
522
+ value = romanize_string(value) if romanize
511
523
 
512
- system_translation_map.insert_at_index(system_lines.length, value, '') if processing_mode == :append &&
513
- !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
514
527
 
515
528
  system_lines.add(value)
516
529
  end
@@ -521,35 +534,38 @@ def self.read_system(system_file_path, ini_file_path, output_path, romanize, log
521
534
  string = string.strip
522
535
  next if string.empty?
523
536
 
537
+ string = convert_to_utf8(string)
524
538
  string = romanize_string(string) if romanize
525
539
 
526
- system_translation_map.insert_at_index(system_lines.length, string, '') if processing_mode == :append &&
527
- !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
528
543
 
529
544
  system_lines.add(string)
530
545
  end
531
546
  end
532
547
  end
533
548
 
534
- # 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
535
551
  # So just throw that ini ass and continue
536
552
  ini_game_title = read_ini_title(ini_file_path).strip
537
553
  ini_game_title = romanize_string(ini_game_title) if romanize
538
554
 
539
- system_translation_map.insert_at_index(system_lines.length, ini_game_title, '') if processing_mode == :append &&
540
- !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
541
558
 
542
559
  system_lines.add(ini_game_title)
543
560
 
544
561
  puts "Parsed #{system_filename}" if logging
545
562
 
546
- original_content, translated_content = if processing_mode == :append
547
- [system_translation_map.keys.join("\n"),
548
- system_translation_map.values.join("\n")]
549
- else
550
- [system_lines.join("\n"),
551
- "\n" * (system_lines.empty? ? 0 : system_lines.length - 1)]
552
- 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
553
569
 
554
570
  File.binwrite(system_output_path, original_content)
555
571
  File.binwrite(system_trans_output_path, translated_content)
@@ -559,7 +575,7 @@ end
559
575
  # @param [String] output_path
560
576
  # @param [Boolean] romanize Whether to romanize text
561
577
  # @param [Boolean] logging Whether to log
562
- # @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
563
579
  def self.read_scripts(scripts_file_path, output_path, romanize, logging, processing_mode)
564
580
  scripts_filename = File.basename(scripts_file_path)
565
581
  scripts_basename = File.basename(scripts_file_path, '.*').downcase
@@ -569,116 +585,80 @@ def self.read_scripts(scripts_file_path, output_path, romanize, logging, process
569
585
  scripts_trans_output_path = File.join(output_path, "#{scripts_basename}_trans.txt")
570
586
 
571
587
  if processing_mode == :default && File.exist?(scripts_trans_output_path)
572
- 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.'
573
590
  return
574
591
  end
575
592
 
576
593
  script_entries = Marshal.load(File.binread(scripts_file_path))
577
594
 
578
- scripts_lines = IndexSet.new
579
- scripts_translation_map = nil
595
+ # @type [Set<String>]
596
+ scripts_lines = Set.new
597
+ # @type [Hash{String => String}]
598
+ scripts_translation_map = {}
580
599
 
581
600
  if processing_mode == :append
582
601
  if File.exist?(scripts_trans_output_path)
583
- scripts_translation_map = Hash[File.readlines(scripts_output_path, chomp: true)
584
- .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
+ ]
585
608
  else
586
609
  puts APPEND_FLAG_OMIT_MSG
587
610
  processing_mode = :default
588
611
  end
589
612
  end
590
613
 
614
+ # @type [Array<String>]
591
615
  codes_content = []
592
616
 
593
617
  # This code was fun before `that` game used Windows-1252 degree symbol
594
618
  script_entries.each do |script|
595
- code = Zlib::Inflate.inflate(script[2]).force_encoding('UTF-8')
596
- # we're fucking cloning because of encoding issue
597
- codes_content.push(code.clone)
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
619
+ # @type [String]
620
+ code = Zlib::Inflate.inflate(script[2])
621
+ code = convert_to_utf8(code)
622
+ codes_content.push(code)
623
+ end
612
624
 
613
- extract_quoted_strings(code, :read).each do |string|
614
- # Removes the U+3000 Japanese typographical space to check if string, when stripped, is truly empty
615
- string = string.strip.delete(' ')
625
+ extracted = extract_strings(codes_content.join)
616
626
 
617
- next if string.empty?
627
+ extracted.each do |string|
628
+ # Removes the U+3000 Japanese typographical space to check if string, when stripped, is truly empty
629
+ string = string.gsub(' ', ' ').strip
618
630
 
619
- # Maybe this mess will remove something that mustn't be removed, but it needs to be tested
620
- next if string.start_with?(/([#!?$@]|(\.\/)?(Graphics|Data|Audio|CG|Movies|Save)\/)/) ||
621
- string.match?(/^[^\p{L}]+$/) ||
622
- string.match?(/^\d+$/) ||
623
- string.match?(/%.*(\d|\+|\*)d\]?:?$/) ||
624
- string.match?(/^\[((ON|OFF)|P[EMRT])\]$/) ||
625
- string.match?(/^\[\]$/) ||
626
- string.match?(/^(.)\1{2,}$/) ||
627
- string.match?(/^(false|true)$/) ||
628
- string.match?(/^[wr]b$/) ||
629
- string.match?(/^(?=.*\d)[A-Za-z0-9-]+$/) ||
630
- string.match?(/^[a-z\-()\/ +'&]*$/) ||
631
- string.match?(/^[A-Za-z]+[+-]$/) ||
632
- string.match?(STRING_IS_ONLY_SYMBOLS_RE) ||
633
- string.match?(/^Tile.*[A-Z]$/) ||
634
- string.match?(/^[a-zA-Z][a-z]+([A-Z][a-z]*)+$/) ||
635
- string.match?(/^Cancel Action$|^Invert$|^End$|^Individual$|^Missed File$|^Bitmap$|^Audio$/) ||
636
- string.match?(/^(?=.*%d)(?=.*%m)(?=.*%Y).*$/) ||
637
- string.match?(/^\\\\ALPHAC/) ||
638
- string.match?(/^[A-Z]{,3}-[A-Z][A-Za-z]+/) ||
639
- string.match?(/\.(mp3|ogg|jpg|png|ini|txt)$/i) ||
640
- string.match?(/\/(\d.*)?$/) ||
641
- string.match?(/FILE$/) ||
642
- string.match?(/#\{/) ||
643
- string.match?(/(?<!\\)\\(?![\\G#])/) ||
644
- string.match?(/\+?=?=/) ||
645
- string.match?(/[}{_<>]/) ||
646
- string.match?(/r[vx]data/) ||
647
- string.match?(/No such file or directory/) ||
648
- string.match?(/level \*\*/) ||
649
- string.match?(/Courier New|Comic Sans|Lucida|Verdana|Tahoma|Arial|Times New Roman/) ||
650
- string.match?(/Player start location/) ||
651
- string.match?(/Common event call has exceeded/) ||
652
- string.match?(/se-/) ||
653
- string.match?(/Start Pos/) ||
654
- string.match?(/An error has occurred/) ||
655
- string.match?(/Define it first/) ||
656
- string.match?(/Process Skill/) ||
657
- string.match?(/Wpn Only/) ||
658
- string.match?(/Don't Wait/) ||
659
- string.match?(/Clear image/) ||
660
- string.match?(/Can Collapse/)
631
+ next if string.empty?
661
632
 
662
- string = romanize_string(string) if romanize
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
663
642
 
664
- scripts_translation_map.insert_at_index(scripts_lines.length, string, '') if processing_mode == :append &&
665
- !scripts_translation_map.include?(string)
643
+ string = romanize_string(string) if romanize
666
644
 
667
- scripts_lines.add(string)
645
+ if processing_mode == :append && !scripts_translation_map.include?(string)
646
+ scripts_translation_map.insert_at_index(scripts_lines.length, string, '')
668
647
  end
648
+
649
+ scripts_lines.add(string)
669
650
  end
670
651
 
671
652
  puts "Parsed #{scripts_filename}" if logging
672
653
 
673
654
  File.binwrite(scripts_plain_output_path, codes_content.join("\n"))
674
655
 
675
- original_content, translated_content = if processing_mode == :append
676
- [scripts_translation_map.keys.join("\n"),
677
- scripts_translation_map.values.join("\n")]
678
- else
679
- [scripts_lines.join("\n"),
680
- "\n" * (scripts_lines.empty? ? 0 : scripts_lines.length - 1)]
681
- 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
682
662
 
683
663
  File.binwrite(scripts_output_path, original_content)
684
664
  File.binwrite(scripts_trans_output_path, translated_content)