rvpacker-txt 1.5.1 → 1.6.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
@@ -2,32 +2,30 @@
2
2
 
3
3
  require 'zlib'
4
4
 
5
- def self.insert_at_index(hash, index, key, value)
6
- return hash[key] = value if index >= hash.size
5
+ class Hash
6
+ def insert_at_index(index, key, value)
7
+ return self[key] = value if index >= size
7
8
 
8
- temp_hash = hash.to_a
9
- temp_hash.insert(index, [key, value])
10
- hash.clear
11
- hash.merge!(temp_hash.to_h)
9
+ temp_array = to_a
10
+ temp_array.insert(index, [key, value])
11
+ replace(temp_array.to_h)
12
+ end
12
13
  end
13
14
 
15
+ # @param [String] string A parsed scripts code string, containing raw Ruby code
16
+ # @return [IndexSet<String>] Hash of parsed from code strings and their start indices
14
17
  def self.extract_quoted_strings(string)
15
- # Hash of string-index key-value pairs
16
- result = {}
18
+ result = IndexSet.new
17
19
 
18
20
  skip_block = false
19
21
  in_quotes = false
20
22
  quote_type = nil
21
23
  buffer = []
22
24
 
23
- # I hope this calculates index correctly
24
- current_string_index = 0
25
25
  string.each_line do |line|
26
26
  stripped = line.strip
27
27
 
28
- if stripped[0] == '#' || stripped.start_with?(/(Win|Lose)|_Fanfare/)
29
- next
30
- end
28
+ next if stripped[0] == '#' || stripped.start_with?(/(Win|Lose)|_Fanfare/)
31
29
 
32
30
  skip_block = true if stripped.start_with?('=begin')
33
31
  skip_block = false if stripped.start_with?('=end')
@@ -45,61 +43,73 @@ def self.extract_quoted_strings(string)
45
43
 
46
44
  quote_type = char
47
45
  in_quotes = !in_quotes
48
- result[buffer.join] = current_string_index + index
46
+ result.add(buffer.join)
49
47
  buffer.clear
50
48
  next
51
49
  end
52
50
 
53
- if in_quotes
54
- buffer.push(char)
55
- end
51
+ buffer.push(char) if in_quotes
56
52
  end
57
-
58
- current_string_index += line.length
59
53
  end
60
54
 
61
55
  result
62
56
  end
63
57
 
58
+ # @param [Integer] code
59
+ # @param [String] parameter
60
+ # @param [String] game_type
61
+ # @return [String]
64
62
  def self.parse_parameter(code, parameter, game_type)
65
- case code
66
- when 401, 405
67
- case game_type
68
- when 'lisa'
69
- match = parameter.scan(/^(\\et\[[0-9]+\]|\\nbt)/)
70
- parameter = parameter.slice((match[0].length)..) if match
71
- else
72
- nil
73
- end
74
- when 102
75
- # Implement some custom parsing
76
- when 356
77
- # Implement some custom parsing
78
- else
79
- return nil
63
+ unless game_type.nil?
64
+ case code
65
+ when 401, 405
66
+ case game_type
67
+ when 'lisa'
68
+ match = parameter.scan(/^(\\et\[[0-9]+\]|\\nbt)/)
69
+ parameter = parameter.slice((match[0].length)..) unless match.empty?
70
+ else
71
+ nil
72
+ end
73
+ when 102
74
+ # Implement some custom parsing
75
+ when 356
76
+ # Implement some custom parsing
77
+ else
78
+ return nil
79
+ end
80
80
  end
81
81
 
82
82
  parameter
83
83
  end
84
84
 
85
+ # @param [String] variable
86
+ # @param [String] game_type
87
+ # @return [String]
85
88
  def self.parse_variable(variable, game_type)
86
- lines_count = variable.count("\n")
89
+ unless game_type.nil?
90
+ lines_count = variable.count("\n")
87
91
 
88
- if lines_count.positive?
89
- variable = variable.gsub(/\r?\n/, '\#')
92
+ if lines_count.positive?
93
+ variable = variable.gsub(/\r?\n/, '\#')
90
94
 
91
- case game_type
92
- when 'lisa'
93
- return nil unless variable.split('\#').all? { |line| line.match?(/^<.*>\.?$/) || line.empty? }
94
- else
95
- nil
95
+ case game_type
96
+ when 'lisa'
97
+ return nil unless variable.split('\#').all? { |line| line.match?(/^<.*>\.?$/) || line.empty? }
98
+ else
99
+ nil
100
+ end
96
101
  end
97
102
  end
98
103
 
99
104
  variable
100
105
  end
101
106
 
102
- def self.read_map(maps_files, output_path, logging, game_type, processing_type)
107
+ # @param [Array<String>] maps_files_paths
108
+ # @param [String] output_path
109
+ # @param [Boolean] logging
110
+ # @param [String] game_type
111
+ # @param [String] processing_type
112
+ def self.read_map(maps_files_paths, output_path, logging, game_type, processing_type)
103
113
  maps_output_path = File.join(output_path, 'maps.txt')
104
114
  names_output_path = File.join(output_path, 'names.txt')
105
115
  maps_trans_output_path = File.join(output_path, 'maps_trans.txt')
@@ -110,7 +120,7 @@ def self.read_map(maps_files, output_path, logging, game_type, processing_type)
110
120
  return
111
121
  end
112
122
 
113
- maps_object_map = Hash[maps_files.map do |filename|
123
+ maps_object_map = Hash[maps_files_paths.map do |filename|
114
124
  [File.basename(filename), Marshal.load(File.binread(filename))]
115
125
  end]
116
126
 
@@ -122,8 +132,10 @@ def self.read_map(maps_files, output_path, logging, game_type, processing_type)
122
132
 
123
133
  if processing_type == 'append'
124
134
  if File.exist?(maps_trans_output_path)
125
- maps_translation_map = Hash[File.readlines(maps_output_path, chomp: true).zip(File.readlines(maps_trans_output_path, chomp: true))]
126
- names_translation_map = Hash[File.readlines(names_output_path, chomp: true).zip(File.readlines(names_trans_output_path, chomp: true))]
135
+ maps_translation_map = Hash[File.readlines(maps_output_path, chomp: true)
136
+ .zip(File.readlines(maps_trans_output_path, chomp: true))]
137
+ names_translation_map = Hash[File.readlines(names_output_path, chomp: true)
138
+ .zip(File.readlines(names_trans_output_path, chomp: true))]
127
139
  else
128
140
  puts "Files aren't already parsed. Continuing as if --append flag was omitted."
129
141
  processing_type = 'default'
@@ -133,12 +145,15 @@ def self.read_map(maps_files, output_path, logging, game_type, processing_type)
133
145
  maps_object_map.each do |filename, object|
134
146
  display_name = object.instance_variable_get(:@display_name)
135
147
 
136
- if display_name.is_a?(String) && !display_name.empty?
137
- if processing_type == 'append' && !names_translation_map.include?(display_name)
138
- insert_at_index(names_translation_map, names_lines.length, display_name, '')
139
- end
148
+ if display_name.is_a?(String)
149
+ display_name = display_name.strip
140
150
 
141
- names_lines.add(display_name)
151
+ unless display_name.empty?
152
+ names_translation_map.insert_at_index(names_lines.length, display_name, '') if processing_type == 'append' &&
153
+ !names_translation_map.include?(display_name)
154
+
155
+ names_lines.add(display_name)
156
+ end
142
157
  end
143
158
 
144
159
  events = object.instance_variable_get(:@events)
@@ -161,21 +176,19 @@ def self.read_map(maps_files, output_path, logging, game_type, processing_type)
161
176
 
162
177
  parameters.each do |parameter|
163
178
  if code == 401
164
- if parameter.is_a?(String) && !parameter.empty?
165
- in_sequence = true
179
+ next unless parameter.is_a?(String) && !parameter.empty?
166
180
 
167
- parsed = parse_parameter(code, parameter, game_type)
168
- line.push(parsed) unless parsed.nil?
169
- end
181
+ in_sequence = true
182
+ line.push(parameter)
170
183
  else
171
184
  if in_sequence
172
- joined = line.join('\#')
185
+ joined = line.join('\#').strip
186
+ parsed = parse_parameter(401, joined, game_type)
173
187
 
174
- if processing_type == 'append' && !maps_translation_map.include?(joined)
175
- insert_at_index(maps_translation_map, maps_lines.length, joined, '')
176
- end
188
+ maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_type == 'append' &&
189
+ !maps_translation_map.include?(parsed)
177
190
 
178
- maps_lines.add(joined)
191
+ maps_lines.add(parsed)
179
192
 
180
193
  line.clear
181
194
  in_sequence = false
@@ -183,30 +196,36 @@ def self.read_map(maps_files, output_path, logging, game_type, processing_type)
183
196
 
184
197
  if code == 102 && parameter.is_a?(Array)
185
198
  parameter.each do |subparameter|
186
- if subparameter.is_a?(String) && !subparameter.empty?
187
- parsed = parse_parameter(code, subparameter, game_type)
199
+ next unless subparameter.is_a?(String)
188
200
 
189
- unless parsed.nil?
190
- if processing_type == 'append' && !maps_translation_map.include?(parsed)
191
- insert_at_index(maps_translation_map, maps_lines.length, parsed, '')
192
- end
201
+ subparameter = subparameter.strip
193
202
 
194
- maps_lines.add(parsed)
195
- end
196
- end
203
+ next if subparameter.empty?
204
+
205
+ parsed = parse_parameter(code, subparameter, game_type)
206
+
207
+ next if parsed.nil?
208
+
209
+ maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_type == 'append' &&
210
+ !maps_translation_map.include?(parsed)
211
+
212
+ maps_lines.add(parsed)
197
213
  end
198
- elsif code == 356 && parameter.is_a?(String) && !parameter.empty?
214
+ elsif code == 356 && parameter.is_a?(String)
215
+ parameter = parameter.strip
216
+
217
+ next if parameter.empty?
218
+
199
219
  parsed = parse_parameter(code, parameter, game_type)
200
220
 
201
- unless parsed.nil?
202
- subbed = parsed.gsub(/\r?\n/, '\#')
221
+ next if parsed.nil?
203
222
 
204
- if processing_type == 'append' && !maps_translation_map.include?(parsed)
205
- insert_at_index(maps_translation_map, maps_lines.length, parsed, '')
206
- end
223
+ parsed = parsed.gsub(/\r?\n/, '\#')
207
224
 
208
- maps_lines.add(subbed)
209
- end
225
+ maps_translation_map.insert_at_index(maps_lines.length, parsed, '') if processing_type == 'append' &&
226
+ !maps_translation_map.include?(parsed)
227
+
228
+ maps_lines.add(parsed)
210
229
  end
211
230
  end
212
231
  end
@@ -238,16 +257,17 @@ def self.read_map(maps_files, output_path, logging, game_type, processing_type)
238
257
  File.binwrite(names_trans_output_path, names_translated_content)
239
258
  end
240
259
 
241
- def self.read_other(other_files, output_path, logging, game_type, processing_type)
242
- other_object_array_map = Hash[other_files.map do |filename|
243
- basename = File.basename(filename)
244
- object = Marshal.load(File.binread(filename))
245
- object = merge_other(object).slice(1..) if basename.start_with?(/Common|Troops/)
246
-
247
- [basename, object]
260
+ # @param [Array<String>] other_files_paths
261
+ # @param [String] output_path
262
+ # @param [Boolean] logging
263
+ # @param [String] game_type
264
+ # @param [String] processing_type
265
+ def self.read_other(other_files_paths, output_path, logging, game_type, processing_type)
266
+ other_object_array_map = Hash[other_files_paths.map do |filename|
267
+ [File.basename(filename), Marshal.load(File.binread(filename))]
248
268
  end]
249
269
 
250
- internal_processing_type = processing_type
270
+ inner_processing_type = processing_type
251
271
 
252
272
  other_object_array_map.each do |filename, other_object_array|
253
273
  processed_filename = File.basename(filename, '.*').downcase
@@ -265,11 +285,12 @@ def self.read_other(other_files, output_path, logging, game_type, processing_typ
265
285
 
266
286
  if processing_type == 'append'
267
287
  if File.exist?(other_trans_output_path)
268
- internal_processing_type == 'append'
269
- other_translation_map = Hash[File.readlines(other_output_path, chomp: true).zip(File.readlines(other_trans_output_path, chomp: true))]
288
+ inner_processing_type == 'append'
289
+ other_translation_map = Hash[File.readlines(other_output_path, chomp: true)
290
+ .zip(File.readlines(other_trans_output_path, chomp: true))]
270
291
  else
271
292
  puts "Files aren't already parsed. Continuing as if --append flag was omitted."
272
- internal_processing_type = 'default'
293
+ inner_processing_type = 'default'
273
294
  end
274
295
  end
275
296
 
@@ -281,17 +302,22 @@ def self.read_other(other_files, output_path, logging, game_type, processing_typ
281
302
  note = object.instance_variable_get(:@note)
282
303
 
283
304
  [name, nickname, description, note].each do |variable|
284
- if variable.is_a?(String) && !variable.empty?
285
- parsed = parse_variable(variable, game_type)
305
+ next unless variable.is_a?(String)
286
306
 
287
- unless parsed.nil?
288
- if internal_processing_type == 'append' && !other_translation_map.include?(parsed)
289
- insert_at_index(other_translation_map, other_lines.length, parsed, '')
290
- end
307
+ variable = variable.strip
291
308
 
292
- other_lines.add(parsed)
293
- end
294
- end
309
+ next if variable.empty?
310
+
311
+ parsed = parse_variable(variable, game_type)
312
+
313
+ next if parsed.nil?
314
+
315
+ parsed = parsed.gsub(/\r?\n/, '\#')
316
+
317
+ other_translation_map.insert_at_index(other_lines.length, parsed, '') if inner_processing_type == 'append' &&
318
+ !other_translation_map.include?(parsed)
319
+
320
+ other_lines.add(parsed)
295
321
  end
296
322
  end
297
323
  else
@@ -312,15 +338,16 @@ def self.read_other(other_files, output_path, logging, game_type, processing_typ
312
338
 
313
339
  parameters.each do |parameter|
314
340
  if [401, 405].include?(code)
341
+ next unless parameter.is_a?(String) && !parameter.empty?
342
+
315
343
  in_sequence = true
316
- line.push(parameter.gsub(/\r?\n/, '\#')) if parameter.is_a?(String) && !parameter.empty?
344
+ line.push(parameter.gsub(/\r?\n/, '\#'))
317
345
  else
318
346
  if in_sequence
319
- joined = line.join('\#')
347
+ joined = line.join('\#').strip
320
348
 
321
- if internal_processing_type == 'append' && !other_translation_map.include?(joined)
322
- insert_at_index(other_translation_map, other_lines.length, joined, '')
323
- end
349
+ other_translation_map.insert_at_index(other_lines.length, joined, '') if inner_processing_type == 'append' &&
350
+ !other_translation_map.include?(joined)
324
351
 
325
352
  other_lines.add(joined)
326
353
 
@@ -332,25 +359,31 @@ def self.read_other(other_files, output_path, logging, game_type, processing_typ
332
359
  when 102
333
360
  if parameter.is_a?(Array)
334
361
  parameter.each do |subparameter|
335
- if subparameter.is_a?(String) && !subparameter.empty?
336
- if internal_processing_type == 'append' && !other_translation_map.include?(subparameter)
337
- insert_at_index(other_translation_map, other_lines.length, subparameter, '')
338
- end
362
+ next unless subparameter.is_a?(String)
363
+
364
+ subparameter = subparameter.strip
339
365
 
340
- other_lines.add(subparameter)
341
- end
366
+ next if subparameter.empty?
367
+
368
+ other_translation_map.insert_at_index(other_lines.length, subparameter, '') if inner_processing_type == 'append' &&
369
+ !other_translation_map.include?(subparameter)
370
+
371
+ other_lines.add(subparameter)
342
372
  end
343
373
  end
344
374
  when 356
345
- if parameter.is_a?(String) && !parameter.empty?
346
- subbed = parameter.gsub(/\r?\n/, '\#')
375
+ next unless parameter.is_a?(String)
347
376
 
348
- if internal_processing_type == 'append'
349
- insert_at_index(other_translation_map, other_lines.length, subbed, '')
350
- end
377
+ parameter = parameter.strip
351
378
 
352
- other_lines.add(subbed)
353
- end
379
+ next if parameter.empty?
380
+
381
+ parameter = parameter.gsub(/\r?\n/, '\#')
382
+
383
+ other_translation_map.insert_at_index(other_lines.length, parameter, '') if inner_processing_type == 'append' &&
384
+ !other_translation_map.include?(parameter)
385
+
386
+ other_lines.add(parameter)
354
387
  else
355
388
  nil
356
389
  end
@@ -364,9 +397,11 @@ def self.read_other(other_files, output_path, logging, game_type, processing_typ
364
397
  puts "Parsed #{filename}" if logging
365
398
 
366
399
  original_content, translated_content = if processing_type == 'append'
367
- [other_translation_map.keys.join("\n"), other_translation_map.values.join("\n")]
400
+ [other_translation_map.keys.join("\n"),
401
+ other_translation_map.values.join("\n")]
368
402
  else
369
- [other_lines.join("\n"), "\n" * (other_lines.empty? ? 0 : other_lines.length - 1)]
403
+ [other_lines.join("\n"),
404
+ "\n" * (other_lines.empty? ? 0 : other_lines.length - 1)]
370
405
  end
371
406
 
372
407
  File.binwrite(other_output_path, original_content)
@@ -374,17 +409,23 @@ def self.read_other(other_files, output_path, logging, game_type, processing_typ
374
409
  end
375
410
  end
376
411
 
377
- def self.read_system(system_file_path, ini_file_path, output_path, logging, processing_type)
378
- def self.read_ini_title(ini_file_path)
379
- file_lines = File.readlines(ini_file_path, chomp: true)
380
- file_lines.each do |line|
381
- if line.start_with?('title')
382
- parts = line.partition('=')
383
- break parts[2].strip
384
- end
412
+ # @param [String] ini_file_path
413
+ def self.read_ini_title(ini_file_path)
414
+ file_lines = File.readlines(ini_file_path, chomp: true)
415
+ file_lines.each do |line|
416
+ if line.start_with?('title')
417
+ parts = line.partition('=')
418
+ break parts[2].strip
385
419
  end
386
420
  end
421
+ end
387
422
 
423
+ # @param [String] system_file_path
424
+ # @param [String] ini_file_path
425
+ # @param [String] output_path
426
+ # @param [Boolean] logging
427
+ # @param [String] processing_type
428
+ def self.read_system(system_file_path, ini_file_path, output_path, logging, processing_type)
388
429
  system_filename = File.basename(system_file_path)
389
430
  system_basename = File.basename(system_file_path, '.*').downcase
390
431
 
@@ -392,7 +433,7 @@ def self.read_system(system_file_path, ini_file_path, output_path, logging, proc
392
433
  system_trans_output_path = File.join(output_path, "#{system_basename}_trans.txt")
393
434
 
394
435
  if processing_type == 'default' && File.exist?(system_trans_output_path)
395
- 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."
436
+ 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.'
396
437
  return
397
438
  end
398
439
 
@@ -403,7 +444,8 @@ def self.read_system(system_file_path, ini_file_path, output_path, logging, proc
403
444
 
404
445
  if processing_type == 'append'
405
446
  if File.exist?(system_trans_output_path)
406
- system_translation_map = Hash[File.readlines(system_output_path, chomp: true).zip(File.readlines(system_trans_output_path, chomp: true))]
447
+ system_translation_map = Hash[File.readlines(system_output_path, chomp: true)
448
+ .zip(File.readlines(system_trans_output_path, chomp: true))]
407
449
  else
408
450
  puts "Files aren't already parsed. Continuing as if --append flag was omitted."
409
451
  processing_type = 'default'
@@ -416,38 +458,44 @@ def self.read_system(system_file_path, ini_file_path, output_path, logging, proc
416
458
  armor_types = system_object.instance_variable_get(:@armor_types)
417
459
  currency_unit = system_object.instance_variable_get(:@currency_unit)
418
460
  terms = system_object.instance_variable_get(:@terms) || system_object.instance_variable_get(:@words)
419
- game_title = system_object.instance_variable_get(:@game_title)
420
461
 
421
462
  [elements, skill_types, weapon_types, armor_types].each do |array|
422
463
  next if array.nil?
423
464
 
424
465
  array.each do |string|
425
- if string.is_a?(String) && !string.empty?
426
- if processing_type == 'append' && !system_translation_map.include?(string)
427
- insert_at_index(system_translation_map, system_lines.length, string, '')
428
- end
466
+ next unless string.is_a?(String)
429
467
 
430
- system_lines.add(string)
431
- end
468
+ string = string.strip
469
+
470
+ next if string.empty?
471
+
472
+ system_translation_map.insert_at_index(system_lines.length, string, '') if processing_type == 'append' &&
473
+ !system_translation_map.include?(string)
474
+
475
+ system_lines.add(string)
432
476
  end
433
477
  end
434
478
 
435
- if currency_unit.is_a?(String) && !currency_unit.empty?
436
- if processing_type == 'append' && !system_translation_map.include?(currency_unit)
437
- insert_at_index(system_translation_map, system_lines.length, currency_unit, '')
438
- end
479
+ if currency_unit.is_a?(String)
480
+ currency_unit = currency_unit.strip
481
+
482
+ unless currency_unit.empty?
483
+ system_translation_map.insert_at_index(system_lines.length, currency_unit, '') if processing_type == 'append' &&
484
+ !system_translation_map.include?(currency_unit)
439
485
 
440
- system_lines.add(currency_unit)
486
+ system_lines.add(currency_unit)
487
+ end
441
488
  end
442
489
 
443
490
  terms.instance_variables.each do |variable|
444
491
  value = terms.instance_variable_get(variable)
445
492
 
446
493
  if value.is_a?(String)
494
+ value = value.strip
495
+
447
496
  unless value.empty?
448
- if processing_type == 'append' && !system_translation_map.include?(value)
449
- insert_at_index(system_translation_map, system_lines.length, value, '')
450
- end
497
+ system_translation_map.insert_at_index(system_lines.length, value, '') if processing_type == 'append' &&
498
+ !system_translation_map.include?(value)
451
499
 
452
500
  system_lines.add(value)
453
501
  end
@@ -456,63 +504,47 @@ def self.read_system(system_file_path, ini_file_path, output_path, logging, proc
456
504
  end
457
505
 
458
506
  value.each do |string|
459
- if string.is_a?(String) && !string.empty?
460
- if processing_type == 'append' && !system_translation_map.include?(string)
461
- insert_at_index(system_translation_map, system_lines.length, string, '')
462
- end
463
-
464
- system_lines.add(string)
465
- end
466
- end
467
- end
507
+ next unless string.is_a?(String)
468
508
 
469
- ini_game_title = read_ini_title(ini_file_path)
509
+ string = string.strip
470
510
 
471
- $wait_time = 0
511
+ next if string.empty?
472
512
 
473
- if ini_game_title != game_title
474
- if game_title.is_a?(String) && !game_title.empty?
475
- wait_time_start = Time.now
513
+ system_translation_map.insert_at_index(system_lines.length, string, '') if processing_type == 'append' &&
514
+ !system_translation_map.include?(string)
476
515
 
477
- puts "Game title from the Game.ini file and game title from the System file are different.\nWhich game title would you like to parse?\n(That doesn't affect anything major, just when you'll write the game back, translated game title will be applied both to the .ini and System file.)\n0, System title - #{game_title}\n1, Game.ini title - #{ini_game_title}"
478
- choice = gets.chomp.to_i
479
-
480
- $wait_time = Time.now - wait_time_start
481
-
482
- if choice == 0
483
- if processing_type == 'append' && !system_translation_map.include?(game_title)
484
- insert_at_index(system_translation_map, system_lines.length, game_title, '')
485
- end
516
+ system_lines.add(string)
517
+ end
518
+ end
486
519
 
487
- system_lines.add(game_title)
488
- else
489
- if processing_type == 'append' && !system_translation_map.include?(ini_game_title)
490
- insert_at_index(system_translation_map, system_lines.length, ini_game_title, '')
491
- end
520
+ # Game title from System file and ini file may differ, but requesting user request to determine which line do they want is LAME
521
+ # So just throw that ini ass and continue
522
+ _game_title = system_object.instance_variable_get(:@game_title)
523
+ ini_game_title = read_ini_title(ini_file_path).strip
492
524
 
493
- system_lines.add(ini_game_title)
494
- end
495
- else
496
- if processing_type == 'append' && !system_translation_map.include?(ini_game_title)
497
- insert_at_index(system_translation_map, system_lines.length, ini_game_title, '')
498
- end
525
+ system_translation_map.insert_at_index(system_lines.length, ini_game_title, '') if processing_type == 'append' &&
526
+ !system_translation_map.include?(ini_game_title)
499
527
 
500
- system_lines.add(ini_game_title)
501
- end
502
- end
528
+ system_lines.add(ini_game_title)
503
529
 
504
530
  puts "Parsed #{system_filename}" if logging
505
531
 
506
532
  original_content, translated_content = if processing_type == 'append'
507
- [system_translation_map.keys.join("\n"), system_translation_map.values.join("\n")]
533
+ [system_translation_map.keys.join("\n"),
534
+ system_translation_map.values.join("\n")]
508
535
  else
509
- [system_lines.join("\n"), "\n" * (system_lines.empty? ? 0 : system_lines.length - 1)]
536
+ [system_lines.join("\n"),
537
+ "\n" * (system_lines.empty? ? 0 : system_lines.length - 1)]
510
538
  end
511
539
 
512
540
  File.binwrite(system_output_path, original_content)
513
541
  File.binwrite(system_trans_output_path, translated_content)
514
542
  end
515
543
 
544
+ # @param [String] scripts_file_path
545
+ # @param [String] output_path
546
+ # @param [Boolean] logging
547
+ # @param [String] processing_type
516
548
  def self.read_scripts(scripts_file_path, output_path, logging, processing_type)
517
549
  scripts_filename = File.basename(scripts_file_path)
518
550
  scripts_basename = File.basename(scripts_file_path, '.*').downcase
@@ -522,7 +554,7 @@ def self.read_scripts(scripts_file_path, output_path, logging, processing_type)
522
554
  scripts_trans_output_path = File.join(output_path, "#{scripts_basename}_trans.txt")
523
555
 
524
556
  if processing_type == 'default' && File.exist?(scripts_trans_output_path)
525
- 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."
557
+ 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.'
526
558
  return
527
559
  end
528
560
 
@@ -533,7 +565,8 @@ def self.read_scripts(scripts_file_path, output_path, logging, processing_type)
533
565
 
534
566
  if processing_type == 'append'
535
567
  if File.exist?(scripts_trans_output_path)
536
- scripts_translation_map = Hash[File.readlines(scripts_output_path, chomp: true).zip(File.readlines(scripts_trans_output_path, chomp: true))]
568
+ scripts_translation_map = Hash[File.readlines(scripts_output_path, chomp: true)
569
+ .zip(File.readlines(scripts_trans_output_path, chomp: true))]
537
570
  else
538
571
  puts "Files aren't already parsed. Continuing as if --append flag was omitted."
539
572
  processing_type = 'default'
@@ -546,32 +579,31 @@ def self.read_scripts(scripts_file_path, output_path, logging, processing_type)
546
579
  code = Zlib::Inflate.inflate(script[2]).force_encoding('UTF-8')
547
580
  codes_content.push(code)
548
581
 
549
- extract_quoted_strings(code).keys.each do |string|
550
- string.strip!
551
-
582
+ extract_quoted_strings(code).each do |string|
552
583
  # Removes the U+3000 Japanese typographical space to check if string, when stripped, is truly empty
553
- next if string.empty? || string.gsub(' ', '').empty?
584
+ string = string.strip.delete(' ')
585
+
586
+ next if string.empty?
554
587
 
555
588
  # Maybe this mess will remove something that mustn't be removed, but it needs to be tested
589
+
556
590
  next if string.start_with?(/([#!?$@]|(\.\/)?(Graphics|Data|Audio|CG|Movies|Save)\/)/) ||
557
591
  string.match?(/^\d+$/) ||
558
592
  string.match?(/^(.)\1{2,}$/) ||
559
593
  string.match?(/^(false|true)$/) ||
560
594
  string.match?(/^[wr]b$/) ||
561
595
  string.match?(/^(?=.*\d)[A-Za-z0-9\-]+$/) ||
562
- string.match?(/^[A-Z\-()\/ +'&]*$/) ||
563
596
  string.match?(/^[a-z\-()\/ +'&]*$/) ||
564
597
  string.match?(/^[A-Za-z]+[+-]$/) ||
565
598
  string.match?(/^[.()+-:;\[\]^~%&!*\/→×??x%▼|]$/) ||
566
599
  string.match?(/^Tile.*[A-Z]$/) ||
567
- string.match?(/^:?%.*[ds][:%]*?$/) ||
568
- string.match?(/^[a-zA-Z]+([A-Z][a-z]*)+$/) ||
600
+ string.match?(/^[a-zA-Z][a-z]+([A-Z][a-z]*)+$/) ||
569
601
  string.match?(/^Cancel Action$|^Invert$|^End$|^Individual$|^Missed File$|^Bitmap$|^Audio$/) ||
570
602
  string.match?(/\.(mp3|ogg|jpg|png|ini)$/) ||
571
603
  string.match?(/\/(\d.*)?$/) ||
572
604
  string.match?(/FILE$/) ||
573
605
  string.match?(/#\{/) ||
574
- string.match?(/\\(?!#)/) ||
606
+ string.match?(/(?<!\\)\\(?!\\|G|#)/) ||
575
607
  string.match?(/\+?=?=/) ||
576
608
  string.match?(/[}{_<>]/) ||
577
609
  string.match?(/r[vx]data/) ||
@@ -595,9 +627,8 @@ def self.read_scripts(scripts_file_path, output_path, logging, processing_type)
595
627
  string.match?(/Clear image/) ||
596
628
  string.match?(/Can Collapse/)
597
629
 
598
- if processing_type == 'append' && !scripts_translation_map.include?(string)
599
- insert_at_index(scripts_translation_map, scripts_lines.length, string, '')
600
- end
630
+ scripts_translation_map.insert_at_index(scripts_lines.length, string, '') if processing_type == 'append' &&
631
+ !scripts_translation_map.include?(string)
601
632
 
602
633
  scripts_lines.add(string)
603
634
  end
@@ -608,9 +639,11 @@ def self.read_scripts(scripts_file_path, output_path, logging, processing_type)
608
639
  File.binwrite(scripts_plain_output_path, codes_content.join("\n"))
609
640
 
610
641
  original_content, translated_content = if processing_type == 'append'
611
- [scripts_translation_map.keys.join("\n"), scripts_translation_map.values.join("\n")]
642
+ [scripts_translation_map.keys.join("\n"),
643
+ scripts_translation_map.values.join("\n")]
612
644
  else
613
- [scripts_lines.join("\n"), "\n" * (scripts_lines.empty? ? 0 : scripts_lines.length - 1)]
645
+ [scripts_lines.join("\n"),
646
+ "\n" * (scripts_lines.empty? ? 0 : scripts_lines.length - 1)]
614
647
  end
615
648
 
616
649
  File.binwrite(scripts_output_path, original_content)