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/write.rb CHANGED
@@ -8,7 +8,11 @@ require_relative 'extensions'
8
8
  # @param [Hash{String => String}] hashmap Translation hashmap (as everything in Ruby passed by reference, this pass is free!)
9
9
  # @param [String] game_type
10
10
  def self.get_parameter_translated(code, parameter, hashmap, game_type)
11
+ # @type [Array<String>]
11
12
  remaining_strings = []
13
+ # @type [Array<Boolean>]
14
+ # true - insert at end
15
+ # false - insert at start
12
16
  insert_positions = []
13
17
 
14
18
  ends_with_if = parameter[/ if\(.*\)$/]
@@ -16,43 +20,40 @@ def self.get_parameter_translated(code, parameter, hashmap, game_type)
16
20
  if ends_with_if
17
21
  parameter = parameter.chomp(ends_with_if)
18
22
  remaining_strings.push(ends_with_if)
19
- insert_positions.push(1)
23
+ insert_positions.push(true)
20
24
  end
21
25
 
22
- unless game_type.nil?
26
+ if game_type
23
27
  case game_type
24
- when 'lisa'
25
- case code
26
- when 401, 405
27
- prefix = parameter[/^(\\et\[[0-9]+\]|\\nbt)/]
28
- parameter = parameter.sub(prefix, '') if prefix
29
-
30
- remaining_strings.push(prefix)
31
- insert_positions.push(0)
32
- when 102, 402
33
- # Implement some custom parsing
34
- when 356
35
- # Implement some custom parsing
36
- else
37
- nil
38
- end
39
- # Implement cases for other games
28
+ when 'lisa'
29
+ case code
30
+ when 401, 405
31
+ prefix = parameter[/^(\\et\[[0-9]+\]|\\nbt)/]
32
+ parameter = parameter.sub(prefix, '') if prefix
33
+
34
+ remaining_strings.push(prefix)
35
+ insert_positions.push(false)
40
36
  else
41
37
  nil
38
+ end
39
+ # Implement cases for other games
40
+ else
41
+ nil
42
42
  end
43
43
  end
44
44
 
45
45
  translated = hashmap[parameter]
46
- return nil if translated.nil? || translated.empty?
46
+ return nil if !translated || translated.empty?
47
47
 
48
- remaining_strings.zip(insert_positions).each do |string, position|
49
- case position
50
- when 0
48
+ remaining_strings
49
+ .zip(insert_positions)
50
+ .each do |string, position|
51
+ if !position
51
52
  translated = string + translated
52
- when 1
53
+ else
53
54
  translated += string
55
+ end
54
56
  end
55
- end
56
57
 
57
58
  translated
58
59
  end
@@ -60,19 +61,20 @@ end
60
61
  # @param [String] variable
61
62
  # @param [Integer] type
62
63
  # @param [String] filename
63
- # @param [Hash{String => String}] hashmap Translation hashmap (as everything in Ruby passed by reference, this pass is free!)
64
+ # @param [Hash{String => String}] hashmap Translation hashmap (as everything in Ruby passed by reference, this pass is
65
+ # free!)
64
66
  # @param [String] _game_type
65
67
  # @return [String]
66
68
  def self.get_variable_translated(variable, type, _filename, hashmap, _game_type)
67
69
  variable = variable.gsub(/\r?\n/, "\n")
68
70
 
69
71
  case type
70
- when 0 # name
71
- when 1 # nickname
72
- when 2 # description
73
- when 3 # note
74
- else
75
- nil
72
+ when 0 # name
73
+ when 1 # nickname
74
+ when 2 # description
75
+ when 3 # note
76
+ else
77
+ nil
76
78
  end
77
79
 
78
80
  hashmap[variable]
@@ -88,21 +90,31 @@ end
88
90
  def self.write_map(original_files_paths, maps_path, output_path, shuffle_level, romanize, logging, game_type)
89
91
  maps_object_map = Hash[original_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]
90
92
 
91
- maps_original_text = File.readlines(File.join(maps_path, 'maps.txt'), encoding: 'UTF-8', chomp: true).map do |line|
92
- line.gsub('\#', "\n").strip
93
- end.freeze
94
-
95
- names_original_text = File.readlines(File.join(maps_path, 'names.txt'), encoding: 'UTF-8', chomp: true).map do |line|
96
- line.gsub('\#', "\n").strip
97
- end.freeze
98
-
99
- maps_translated_text = File.readlines(File.join(maps_path, 'maps_trans.txt'), encoding: 'UTF-8', chomp: true).map do |line|
100
- line.gsub('\#', "\n").strip
101
- end
102
-
103
- names_translated_text = File.readlines(File.join(maps_path, 'names_trans.txt'), encoding: 'UTF-8', chomp: true).map do |line|
104
- line.gsub('\#', "\n").strip
105
- end
93
+ # @type [Array<String>]
94
+ maps_original_text =
95
+ File
96
+ .readlines(File.join(maps_path, 'maps.txt'), encoding: 'UTF-8', chomp: true)
97
+ .map { |line| line.gsub('\#', "\n").strip }
98
+ .freeze
99
+
100
+ # @type [Array<String>]
101
+ names_original_text =
102
+ File
103
+ .readlines(File.join(maps_path, 'names.txt'), encoding: 'UTF-8', chomp: true)
104
+ .map { |line| line.gsub('\#', "\n").strip }
105
+ .freeze
106
+
107
+ # @type [Array<String>]
108
+ maps_translated_text =
109
+ File
110
+ .readlines(File.join(maps_path, 'maps_trans.txt'), encoding: 'UTF-8', chomp: true)
111
+ .map { |line| line.gsub('\#', "\n").strip }
112
+
113
+ # @type [Array<String>]
114
+ names_translated_text =
115
+ File
116
+ .readlines(File.join(maps_path, 'names_trans.txt'), encoding: 'UTF-8', chomp: true)
117
+ .map { |line| line.gsub('\#', "\n").strip }
106
118
 
107
119
  if shuffle_level.positive?
108
120
  maps_translated_text.shuffle!
@@ -114,36 +126,43 @@ def self.write_map(original_files_paths, maps_path, output_path, shuffle_level,
114
126
  end
115
127
  end
116
128
 
129
+ # @type [Hash{String => String}]
117
130
  maps_translation_map = Hash[maps_original_text.zip(maps_translated_text)].freeze
131
+ # @type [Hash{String => String}]
118
132
  names_translation_map = Hash[names_original_text.zip(names_translated_text)].freeze
119
133
 
134
+ # @type [Array<Integer>]
120
135
  # 401 - dialogue lines
121
136
  # 102 - dialogue choices array
122
137
  # 402 - one of the dialogue choices from the array
123
138
  # 356 - system lines/special texts (do they even exist before mv?)
124
- allowed_codes = [401, 102, 402, 356].freeze
139
+ allowed_codes = [102, 320, 324, 356, 401, 402].freeze
125
140
 
126
141
  maps_object_map.each do |filename, object|
142
+ # @type [String]
127
143
  display_name = object.display_name
128
- display_name_gotten = names_translation_map[display_name]
129
- object.display_name = display_name_gotten unless display_name_gotten.nil?
144
+ display_name_translated = names_translation_map[display_name]
145
+ object.display_name = display_name_translated if display_name_translated
130
146
 
131
147
  events = object.events
132
- next if events.nil?
148
+ next unless events
133
149
 
134
150
  events.each do |ev, event|
135
151
  pages = event.pages
136
- next if pages.nil?
152
+ next unless pages
137
153
 
138
154
  pages.each_with_index do |page, pg|
139
155
  list = page.list
140
- next if list.nil?
156
+ next unless list
141
157
 
142
158
  in_sequence = false
159
+ # @type [Array<String>]
143
160
  line = []
161
+ # @type [Array<Integer>]
144
162
  item_indices = []
145
163
 
146
164
  list.each_with_index do |item, it|
165
+ # @type [Integer]
147
166
  code = item.code
148
167
 
149
168
  if in_sequence && code != 401
@@ -153,64 +172,80 @@ def self.write_map(original_files_paths, maps_path, output_path, shuffle_level,
153
172
 
154
173
  translated = get_parameter_translated(401, joined, maps_translation_map, game_type)
155
174
 
156
- if !translated.nil? && !translated.empty?
175
+ if translated && !translated.empty?
157
176
  split = translated.split('\#')
158
177
 
159
178
  split_length = split.length
160
179
  line_length = line.length
161
180
 
162
- item_indices.each_with_index do |index, i|
163
- list[index].parameters[0] = i < split_length ? split[i] : ''
164
- end
181
+ item_indices.each_with_index { |index, i| list[index].parameters[0] = i < split_length ? split[i] : '' }
165
182
 
166
183
  if split_length > line_length
167
184
  list[item_indices.last].parameters[0] = split[line_length - 1..].join("\n")
168
185
  end
169
186
  end
187
+
188
+ line.clear
170
189
  end
171
190
 
172
- line.clear
173
191
  in_sequence = false
174
192
  end
175
193
 
176
194
  next unless allowed_codes.include?(code)
177
195
 
196
+ # @type [Array<String>]
178
197
  parameters = item.parameters
179
198
 
180
- if code == 401
181
- next unless parameters[0].is_a?(String) && !parameters[0].empty?
199
+ case code
200
+ when 401
201
+ # @type [String]
202
+ parameter = parameters[0]
203
+ next unless parameter.is_a?(String)
204
+
205
+ parameter = convert_to_utf8(parameter)
182
206
 
183
207
  in_sequence = true
184
- line.push(parameters[0].gsub(' ', ' ').strip)
208
+ line.push(parameter.gsub(' ', ' ').strip)
185
209
  item_indices.push(it)
186
- elsif parameters[0].is_a?(Array)
210
+ when 102
187
211
  parameters[0].each_with_index do |subparameter, sp|
188
212
  next unless subparameter.is_a?(String)
189
213
 
190
214
  subparameter = subparameter.strip
191
215
  next if subparameter.empty?
192
216
 
193
- subparameter = romanize_string(subparameter) if romanize
217
+ subparameter = convert_to_utf8(subparameter)
218
+ subparameter = romanize_string(subparameter.strip) if romanize
194
219
 
195
220
  translated = get_parameter_translated(code, subparameter, maps_translation_map, game_type)
196
- parameters[0][sp] = translated if !translated.nil? && !translated.empty?
221
+ parameters[0][sp] = translated if translated && !translated.empty?
197
222
  end
198
- elsif parameters[0].is_a?(String)
199
- parameter = parameters[0].strip
223
+ when 356
224
+ # @type [String]
225
+ parameter = parameters[0]
226
+ next unless parameter.is_a?(String)
227
+
228
+ parameter = parameter.strip
200
229
  next if parameter.empty?
201
230
 
231
+ parameter = convert_to_utf8(parameter)
202
232
  parameter = romanize_string(parameter) if romanize
203
233
 
204
234
  translated = get_parameter_translated(code, parameter, maps_translation_map, game_type)
205
- parameters[0] = translated if !translated.nil? && !translated.empty?
206
- elsif parameters[1].is_a?(String)
207
- parameter = parameters[1].strip
235
+ parameters[0] = translated if translated && !translated.empty?
236
+ when 320, 324, 402
237
+ # @type [String]
238
+ parameter = parameters[1]
239
+ next unless parameter.is_a?(String)
240
+
241
+ parameter = parameter.strip
208
242
  next if parameter.empty?
209
243
 
210
- parameter = romanize_string(parameter) if romanize
244
+ parameter = convert_to_utf8(parameter)
245
+ parameter = romanize_string(parameters[1].strip) if romanize
211
246
 
212
247
  translated = get_parameter_translated(code, parameter, maps_translation_map, game_type)
213
- parameters[1] = translated if !translated.nil? && !translated.empty?
248
+ parameters[1] = translated if translated && !translated.empty?
214
249
  end
215
250
 
216
251
  item.parameters = parameters
@@ -242,79 +277,88 @@ end
242
277
  def self.write_other(original_files_paths, other_path, output_path, shuffle_level, romanize, logging, game_type)
243
278
  other_object_array_map = Hash[original_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]
244
279
 
280
+ # @type [Array<String>]
245
281
  # 401 - dialogue lines
246
282
  # 405 - credits lines
247
283
  # 102 - dialogue choices array
248
284
  # 402 - one of the dialogue choices from the array
249
285
  # 356 - system lines/special texts (do they even exist before mv?)
250
- allowed_codes = [401, 405, 102, 402, 356].freeze
286
+ allowed_codes = [102, 320, 324, 356, 401, 402, 405].freeze
251
287
 
252
288
  other_object_array_map.each do |filename, other_object_array|
253
289
  other_filename = File.basename(filename, '.*').downcase
254
290
 
255
- other_original_text = File.readlines(File.join(other_path, "#{other_filename}.txt"), encoding: 'UTF-8', chomp: true)
256
- .map { |line| line.gsub('\#', "\n").strip }
291
+ # @type [Array<String>]
292
+ other_original_text =
293
+ File
294
+ .readlines(File.join(other_path, "#{other_filename}.txt"), encoding: 'UTF-8', chomp: true)
295
+ .map { |line| line.gsub('\#', "\n").strip }
257
296
 
258
- other_translated_text = File.readlines(File.join(other_path, "#{other_filename}_trans.txt"), encoding: 'UTF-8', chomp: true)
259
- .map { |line| line.gsub('\#', "\n").strip }
297
+ # @type [Array<String>]
298
+ other_translated_text =
299
+ File
300
+ .readlines(File.join(other_path, "#{other_filename}_trans.txt"), encoding: 'UTF-8', chomp: true)
301
+ .map { |line| line.gsub('\#', "\n").strip }
260
302
 
261
303
  if shuffle_level.positive?
262
304
  other_translated_text.shuffle!
263
305
  other_translated_text = shuffle_words(other_translated_text) if shuffle_level == 2
264
306
  end
265
307
 
308
+ # @type [Hash{String => String}]
266
309
  other_translation_map = Hash[other_original_text.zip(other_translated_text)].freeze
267
310
 
268
311
  if !filename.start_with?(/Common|Troops/)
269
312
  other_object_array.each do |object|
270
- next if object.nil?
313
+ next unless object
271
314
 
272
- name = object.name
273
- nickname = object.nickname if object.is_a?(RPG::Actor)
274
- description = object.description
275
- note = object.note
315
+ variables = [
316
+ object.name,
317
+ object.is_a?(RPG::Actor) ? object.nickname : nil,
318
+ object.description,
319
+ object.note,
320
+ object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message1 : nil,
321
+ object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message2 : nil,
322
+ object.is_a?(RPG::State) ? object.message3 : nil,
323
+ object.is_a?(RPG::State) ? object.message4 : nil,
324
+ ]
276
325
 
277
- [name, nickname, description, note].each_with_index do |variable, type|
278
- next unless variable.is_a?(String)
326
+ attributes = %i[name nickname description note message1 message2 message3 message4]
279
327
 
280
- variable = variable.strip
281
- next if variable.empty?
328
+ variables.each_with_index do |var, type|
329
+ next unless var.is_a?(String)
282
330
 
283
- variable = romanize_string(variable) if romanize
331
+ var = var.strip
332
+ next if var.empty?
284
333
 
285
- variable = variable.split("\n").map(&:strip).join("\n")
334
+ var = convert_to_utf8(var)
335
+ var = romanize_string(var) if romanize
336
+ var = var.split("\n").map(&:strip).join("\n")
286
337
 
287
- translated = get_variable_translated(variable, type, filename, other_translation_map, game_type)
338
+ translated = get_variable_translated(var, type, filename, other_translation_map, game_type)
288
339
 
289
- if !translated.nil? && !translated.empty?
290
- if type.zero?
291
- object.name = translated
292
- elsif type == 1
293
- object.nickname = translated if object.is_a?(RPG::Actor)
294
- elsif type == 2
295
- object.description = translated
296
- else
297
- object.note = translated
298
- end
299
- end
340
+ object.send("#{attributes[type]}=", translated) if translated && !translated.empty?
300
341
  end
301
342
  end
302
343
  else
303
344
  other_object_array.each_with_index do |object, obj|
304
- next if object.nil?
345
+ next unless object
305
346
 
306
347
  pages = object.pages
307
- pages_length = pages.nil? ? 1 : pages.length
348
+ pages_length = !pages ? 1 : pages.length
308
349
 
309
350
  (0..pages_length).each do |pg|
310
- list = pages.nil? ? object.list : pages[pg].instance_variable_get(:@list) # for some reason .list access doesn't work (wtf?)
311
- next if list.nil?
351
+ list = !pages ? object.list : pages[pg].instance_variable_get(:@list) # for some reason .list access doesn't work (wtf?)
352
+ next unless list
312
353
 
313
354
  in_sequence = false
355
+ # @type [Array<String>]
314
356
  line = []
357
+ # @type [Array<Integer>]
315
358
  item_indices = []
316
359
 
317
360
  list.each_with_index do |item, it|
361
+ # @type [Integer]
318
362
  code = item.code
319
363
 
320
364
  if in_sequence && ![401, 405].include?(code)
@@ -324,7 +368,7 @@ def self.write_other(original_files_paths, other_path, output_path, shuffle_leve
324
368
 
325
369
  translated = get_parameter_translated(401, joined, other_translation_map, game_type)
326
370
 
327
- if !translated.nil? && !translated.empty?
371
+ if translated && !translated.empty?
328
372
  split = translated.split('\#')
329
373
 
330
374
  split_length = split.length
@@ -338,57 +382,75 @@ def self.write_other(original_files_paths, other_path, output_path, shuffle_leve
338
382
  list[item_indices.last].parameters[0] = split[line_length - 1..].join("\n")
339
383
  end
340
384
  end
385
+
386
+ line.clear
341
387
  end
342
388
 
343
- line.clear
344
389
  in_sequence = false
345
390
  end
346
391
 
347
392
  next unless allowed_codes.include?(code)
348
393
 
394
+ # @type [Array<String>]
349
395
  parameters = item.parameters
350
396
 
351
- if [401, 405].include?(code)
352
- next unless parameters[0].is_a?(String) && !parameters[0].empty?
397
+ case code
398
+ when 401, 405
399
+ # @type [String]
400
+ parameter = parameters[0]
401
+ next unless parameter.is_a?(String)
402
+
403
+ parameter = convert_to_utf8(parameter)
353
404
 
354
405
  in_sequence = true
355
- line.push(parameters[0].gsub(' ', ' ').strip)
406
+ line.push(parameter.gsub(' ', ' ').strip)
356
407
  item_indices.push(it)
357
- elsif parameters[0].is_a?(Array)
408
+ when 102
358
409
  parameters[0].each_with_index do |subparameter, sp|
359
410
  next unless subparameter.is_a?(String)
360
411
 
361
412
  subparameter = subparameter.strip
362
413
  next if subparameter.empty?
363
414
 
415
+ subparameter = convert_to_utf8(subparameter)
364
416
  subparameter = romanize_string(subparameter) if romanize
365
417
 
366
418
  translated = get_parameter_translated(code, subparameter, other_translation_map, game_type)
367
- parameters[0][sp] = translated if !translated.nil? && !translated.empty?
419
+ parameters[0][sp] = translated if translated && !translated.empty?
368
420
  end
369
- elsif parameters[0].is_a?(String)
370
- parameter = parameters[0].strip
421
+ when 356
422
+ # @type [String]
423
+ parameter = parameters[0]
424
+ next unless parameter.is_a?(String)
425
+
426
+ parameter = parameter.strip
371
427
  next if parameter.empty?
372
428
 
429
+ parameter = convert_to_utf8(parameter)
373
430
  parameter = romanize_string(parameter) if romanize
374
431
 
375
432
  translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
376
- parameters[0] = translated if !translated.nil? && !translated.empty?
377
- elsif parameters[1].is_a?(String)
378
- parameter = parameters[1].strip
433
+ parameters[0] = translated if translated && !translated.empty?
434
+ when 320, 324, 402
435
+ # @type [String]
436
+ parameter = parameters[1]
437
+ next unless parameter.is_a?(String)
438
+
439
+ parameter = parameter.strip
379
440
  next if parameter.empty?
380
441
 
442
+ parameter = convert_to_utf8(parameter)
381
443
  parameter = romanize_string(parameter) if romanize
382
444
 
383
445
  translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
384
- parameters[1] = translated if !translated.nil? && !translated.empty?
446
+ parameters[1] = translated if translated && !translated.empty?
385
447
  end
386
448
 
387
449
  item.parameters = parameters
388
450
  list[it] = item
389
451
  end
390
452
 
391
- if pages.nil?
453
+ if !pages
392
454
  object.list = list
393
455
  else
394
456
  pages[pg].instance_variable_set(:@list, list)
@@ -421,24 +483,29 @@ end
421
483
  # @param [String] other_path
422
484
  # @param [String] output_path
423
485
  # @param [Integer] shuffle_level Level of shuffle
424
- # @param [Boolean] romanize If files were read with romanize, this option will romanize original game text to compare with parsed
486
+ # @param [Boolean] romanize If files were read with romanize, this option will romanize original game text to compare
487
+ # with parsed
425
488
  # @param [Boolean] logging Whether to log
426
489
  def self.write_system(system_file_path, ini_file_path, other_path, output_path, shuffle_level, romanize, logging)
427
490
  system_basename = File.basename(system_file_path)
491
+
492
+ # @type [System]
428
493
  system_object = Marshal.load(File.binread(system_file_path))
429
494
 
430
- system_original_text = File.readlines(File.join(other_path, 'system.txt'), encoding: 'UTF-8', chomp: true)
431
- .map(&:strip)
432
- .freeze
495
+ # @type [Array<String>]
496
+ system_original_text =
497
+ File.readlines(File.join(other_path, 'system.txt'), encoding: 'UTF-8', chomp: true).map(&:strip).freeze
433
498
 
434
- system_translated_text = File.readlines(File.join(other_path, 'system_trans.txt'), encoding: 'UTF-8', chomp: true)
435
- .map(&:strip)
499
+ # @type [Array<String>]
500
+ system_translated_text =
501
+ File.readlines(File.join(other_path, 'system_trans.txt'), encoding: 'UTF-8', chomp: true).map(&:strip)
436
502
 
437
503
  if shuffle_level.positive?
438
504
  system_translated_text.shuffle!
439
505
  system_translated_text = shuffle_words(system_translated_text) if shuffle_level == 2
440
506
  end
441
507
 
508
+ # @type [Hash{String => String}]
442
509
  system_translation_map = Hash[system_original_text.zip(system_translated_text)].freeze
443
510
 
444
511
  elements = system_object.elements
@@ -448,70 +515,73 @@ def self.write_system(system_file_path, ini_file_path, other_path, output_path,
448
515
  currency_unit = system_object.currency_unit
449
516
  terms_vocabulary = system_object.terms || system_object.words
450
517
 
451
- [elements, skill_types, weapon_types, armor_types].each_with_index do |array, i|
452
- next unless array.is_a?(Array)
518
+ arrays = [elements, skill_types, weapon_types, armor_types]
519
+ attributes = %i[elements skill_types weapon_types armor_types]
453
520
 
454
- array.each_with_index do |string, i|
455
- string = string.strip
456
- next if string.empty?
521
+ arrays
522
+ .zip(attributes)
523
+ .each do |array, attr|
524
+ next unless array.is_a?(Array)
457
525
 
458
- string = romanize_string(string) if romanize
526
+ array.each_with_index do |string, i|
527
+ string = string.strip
528
+ next if string.empty?
459
529
 
460
- translated = system_translation_map[string]
461
- array[i] = translated if !translated.nil? && !translated.empty?
462
- end
530
+ string = convert_to_utf8(string)
531
+ string = romanize_string(string) if romanize
463
532
 
464
- if i.zero?
465
- system_object.elements = array
466
- elsif i == 1
467
- system_object.skill_types = array
468
- elsif i == 2
469
- system_object.weapon_types = array
470
- else
471
- system_object.armor_types = array
533
+ translated = system_translation_map[string]
534
+ array[i] = translated if translated && !translated.empty?
535
+ end
536
+
537
+ system_object.send("#{attr}=", array)
472
538
  end
473
- end
474
539
 
475
- currency_unit = romanize_string(currency_unit) if romanize
476
- currency_unit_translated = system_translation_map[currency_unit]
477
- system_object.currency_unit = currency_unit_translated if currency_unit.is_a?(String) &&
478
- (!currency_unit_translated.nil? && !currency_unit_translated.empty?)
540
+ if currency_unit
541
+ currency_unit = romanize_string(currency_unit) if romanize
542
+ currency_unit_translated = system_translation_map[currency_unit]
543
+ system_object.currency_unit = currency_unit_translated if currency_unit.is_a?(String) && currency_unit_translated &&
544
+ !currency_unit_translated.empty?
545
+ end
479
546
 
480
547
  terms_vocabulary.instance_variables.each do |variable|
548
+ # @type [String | Array<String>]
481
549
  value = terms_vocabulary.instance_variable_get(variable)
482
550
 
483
551
  if value.is_a?(String)
484
552
  value = value.strip
485
553
  next if value.empty?
486
554
 
555
+ value = convert_to_utf8(value)
487
556
  value = romanize_string(value) if romanize
488
557
 
489
558
  translated = system_translation_map[value]
490
- value = translated if !translated.nil? && !translated.empty?
559
+ value = translated if translated && !translated.empty?
491
560
  elsif value.is_a?(Array)
492
561
  value.each_with_index do |string, i|
493
562
  string = string.strip
494
563
  next if string.empty?
495
564
 
565
+ string = convert_to_utf8(string)
496
566
  string = romanize_string(string) if romanize
497
567
 
498
568
  translated = system_translation_map[string]
499
- value[i] = translated if !translated.nil? && !translated.empty?
569
+ value[i] = translated if translated && !translated.empty?
500
570
  end
501
571
  end
502
572
 
503
573
  terms_vocabulary.instance_variable_set(variable, value)
504
574
  end
505
575
 
506
- if system_object.terms.nil?
576
+ if !system_object.terms
507
577
  system_object.words = terms_vocabulary
508
578
  else
509
579
  system_object.terms = terms_vocabulary
510
580
  end
511
581
 
512
- game_title_translated = system_translated_text[-1]
582
+ game_title_translated = system_translated_text.last
513
583
 
514
- unless game_title_translated.nil? || game_title_translated.empty?
584
+ if game_title_translated && !game_title_translated.empty?
515
585
  system_object.game_title = game_title_translated
516
586
  write_ini_title(ini_file_path, game_title_translated)
517
587
  end
@@ -523,48 +593,45 @@ end
523
593
  # @param [String] scripts_file_path Path to Scripts.*data file
524
594
  # @param [String] other_path Path to translation/other directory containing .txt files
525
595
  # @param [String] output_path Path to the output directory
526
- # @param [Boolean] romanize If files were read with romanize, this option will romanize original game text to compare with parsed
596
+ # @param [Boolean] romanize If files were read with romanize, this option will romanize original game text to compare
597
+ # with parsed
527
598
  # @param [Boolean] logging Whether to log
528
599
  def self.write_scripts(scripts_file_path, other_path, output_path, romanize, logging)
529
600
  scripts_basename = File.basename(scripts_file_path)
530
601
  script_entries = Marshal.load(File.binread(scripts_file_path))
531
602
 
532
- scripts_original_text = File.readlines(File.join(other_path, 'scripts.txt'), encoding: 'UTF-8', chomp: true)
533
- .map { |line| line.gsub('\#', "\r\n") }
534
- scripts_translated_text = File.readlines(File.join(other_path, 'scripts_trans.txt'), encoding: 'UTF-8', chomp: true)
535
- .map { |line| line.gsub('\#', "\r\n") }
536
-
603
+ # @type [Array<String>]
604
+ scripts_original_text =
605
+ File
606
+ .readlines(File.join(other_path, 'scripts.txt'), encoding: 'UTF-8', chomp: true)
607
+ .map { |line| line.gsub('\#', "\r\n") }
608
+ # @type [Array<String>]
609
+ scripts_translated_text =
610
+ File
611
+ .readlines(File.join(other_path, 'scripts_trans.txt'), encoding: 'UTF-8', chomp: true)
612
+ .map { |line| line.gsub('\#', "\r\n") }
613
+
614
+ # @type [Hash{String => String}]
537
615
  scripts_translation_map = Hash[scripts_original_text.zip(scripts_translated_text)]
538
616
 
539
617
  script_entries.each do |script|
618
+ # @type [String]
540
619
  code = Zlib::Inflate.inflate(script[2])
541
- code.force_encoding('UTF-8')
620
+ code = convert_to_utf8(code)
542
621
 
543
- unless code.valid_encoding?
544
- [Encoding::UTF_8, Encoding::WINDOWS_1252, Encoding::SHIFT_JIS].each do |encoding|
545
- encoded = code.encode(code.encoding, encoding)
622
+ string_array, index_array = extract_strings(code, mode: true)
546
623
 
547
- if encoded.valid_encoding?
548
- code.force_encoding(encoding)
549
- break
550
- end
551
- rescue Encoding::InvalidByteSequenceError
552
- next
553
- end
554
- end
555
-
556
- # this shit finally works and requires NO further changes
557
- string_array, index_array = extract_strings(code, true)
558
-
559
- string_array.zip(index_array).reverse_each do |string, index|
560
- string = string.gsub(' ', '').strip
561
- next if string.empty? || !scripts_translation_map.include?(string)
624
+ string_array
625
+ .zip(index_array)
626
+ .reverse_each do |string, index|
627
+ string = string.gsub(' ', '').strip
628
+ next if string.empty? || !scripts_translation_map.include?(string)
562
629
 
563
- string = romanize_string(string) if romanize
630
+ string = romanize_string(string) if romanize
564
631
 
565
- gotten = scripts_translation_map[string]
566
- code[index, string.length] = gotten unless gotten.nil? || gotten.empty?
567
- end
632
+ translated = scripts_translation_map[string]
633
+ code[index, string.length] = translated if translated && !translated.empty?
634
+ end
568
635
 
569
636
  script[2] = Zlib::Deflate.deflate(code, Zlib::BEST_COMPRESSION)
570
637
  end