rvpacker-txt 1.5.2 → 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/write.rb CHANGED
@@ -2,28 +2,34 @@
2
2
 
3
3
  require 'zlib'
4
4
 
5
+ # @param [String] string A parsed scripts code string, containing raw Ruby code
6
+ # @return [[Array<String>, Array<Integer>]] Hash of parsed from code strings and their start indices
5
7
  def self.extract_quoted_strings(string)
6
8
  # Hash of string-index key-value pairs
7
- result = {}
9
+ strings_array = []
10
+ indices_array = []
8
11
 
9
12
  skip_block = false
10
13
  in_quotes = false
11
14
  quote_type = nil
12
15
  buffer = []
13
16
 
14
- # I hope this calculates index correctly
15
17
  current_string_index = 0
16
18
  string.each_line do |line|
17
19
  stripped = line.strip
18
20
 
19
21
  if stripped[0] == '#' || stripped.start_with?(/(Win|Lose)|_Fanfare/)
22
+ current_string_index += line.length
20
23
  next
21
24
  end
22
25
 
23
26
  skip_block = true if stripped.start_with?('=begin')
24
27
  skip_block = false if stripped.start_with?('=end')
25
28
 
26
- next if skip_block
29
+ if skip_block
30
+ current_string_index += line.length
31
+ next
32
+ end
27
33
 
28
34
  buffer.push('\#') if in_quotes
29
35
 
@@ -36,169 +42,110 @@ def self.extract_quoted_strings(string)
36
42
 
37
43
  quote_type = char
38
44
  in_quotes = !in_quotes
39
- result[buffer.join] = current_string_index + index
45
+
46
+ strings_array.push(buffer.join)
47
+ indices_array.push(current_string_index + index)
48
+
40
49
  buffer.clear
41
50
  next
42
51
  end
43
52
 
44
- if in_quotes
45
- buffer.push(char)
46
- end
53
+ buffer.push(char) if in_quotes
47
54
  end
48
55
 
49
56
  current_string_index += line.length
50
57
  end
51
58
 
52
- result
59
+ [strings_array, indices_array]
53
60
  end
54
61
 
55
- def shuffle_words(array)
62
+ # @param [Array<String>] array
63
+ # @return [Array<String>]
64
+ def self.shuffle_words(array)
56
65
  array.each do |string|
57
- words = string.scan(/\S+/)
58
- shuffled_words = words.shuffle
59
- string.gsub(/\S+/) { shuffled_words.pop || "" }
66
+ select_words_re = /\S+/
67
+ words = string.scan(select_words_re).shuffle
68
+ string.gsub(select_words_re) { words.pop || '' }
60
69
  end
61
70
  end
62
71
 
63
- def self.merge_seq(object_array)
64
- first = nil
65
- number = -1
66
- in_sequence = false
67
- string_array = []
68
-
69
- i = 0
70
-
71
- while i > object_array.length
72
- object = object_array[i]
73
- code = object.instance_variable_get(:@code)
74
-
75
- if [401, 405].include?(code)
76
- first = i if first.nil?
77
-
78
- number += 1
79
- string_array.push(object.instance_variable_get(:@parameters)[0])
80
- in_sequence = true
81
- elsif i.positive? && in_sequence && !first.nil? && !number.negative?
82
- parameters = object_array[first].instance_variable_get(:@parameters)
83
- parameters[0] = string_array.join("\n")
84
- object_array[first].instance_variable_set(:@parameters, parameters)
85
-
86
- start_index = first + 1
87
- items_to_delete = start_index + number
88
- object_array.slice(start_index, items_to_delete)
89
-
90
- string_array.clear
91
- i -= number
92
- number = -1
93
- first = nil
94
- in_sequence = false
95
- end
96
-
97
- i += 1
98
- end
99
-
100
- object_array
101
- end
102
-
103
- def self.merge_map(object)
104
- events = object.instance_variable_get(:@events)
105
- return object if events.nil?
106
-
107
- events.each_value do |event|
108
- pages = event.instance_variable_get(:@pages)
109
- next if pages.nil?
110
-
111
- pages.each do |page|
112
- list = page.instance_variable_get(:@list)
113
- page.instance_variable_set(:@list, merge_seq(list))
72
+ # @param [Integer] code
73
+ # @param [String] parameter
74
+ # @param [Hash{String => String}] hashmap
75
+ # @param [String] game_type
76
+ def self.get_parameter_translated(code, parameter, hashmap, game_type)
77
+ unless game_type.nil?
78
+ lisa_start = nil
79
+
80
+ case code
81
+ when 401, 405
82
+ case game_type
83
+ when 'lisa'
84
+ match = parameter.scan(/^(\\et\[[0-9]+\]|\\nbt)/)
85
+
86
+ unless match.empty?
87
+ lisa_start = match[0]
88
+ parameter = parameter.slice((match[0].length)..)
89
+ end
90
+ else
91
+ nil
92
+ end
93
+ when 102, 402
94
+ # Implement some custom parsing
95
+ when 356
96
+ # Implement some custom parsing
97
+ else
98
+ nil
114
99
  end
115
- end
116
-
117
- object
118
- end
119
-
120
- def self.merge_other(object_array)
121
- object_array.each do |object|
122
- next if object.nil?
123
-
124
- pages = object.instance_variable_get(:@pages)
125
-
126
- if pages.is_a?(Array)
127
- pages.each do |page|
128
- list = page.instance_variable_get(:@list)
129
- next unless list.is_a?(Array)
130
-
131
- page.instance_variable_set(:@list, merge_seq(list))
132
- end
133
100
 
134
- object.instance_variable_set(:@pages, pages)
135
- else
136
- list = object.instance_variable_get(:@list)
137
- next unless list.is_a?(Array)
101
+ gotten = hashmap[parameter]
138
102
 
139
- object.instance_variable_set(:@list, merge_seq(list))
103
+ case game_type
104
+ when 'lisa'
105
+ gotten = lisa_start + gotten unless lisa_start.nil?
106
+ else
107
+ nil
140
108
  end
141
- end
142
-
143
- object_array
144
- end
145
-
146
- def self.get_parameter_translated(code, parameter, hashmap, game_type)
147
- lisa_start = nil
148
-
149
- case code
150
- when 401, 405
151
- case game_type
152
- when 'lisa'
153
- match = parameter.scan(/^(\\et\[[0-9]+\]|\\nbt)/)
154
- lisa_start = match[0]
155
- parameter = parameter.slice((match[0].length)..) unless match.nil?
156
- else
157
- nil
158
- end
159
- when 102, 402
160
- # Implement some custom parsing
161
- when 356
162
- # Implement some custom parsing
163
- else
164
- nil
165
- end
166
-
167
- gotten = hashmap[parameter]
168
109
 
169
- case game_type
170
- when 'lisa'
171
- gotten = lisa_start + gotten unless lisa_start.nil?
172
- else
173
- nil
110
+ return gotten
174
111
  end
175
112
 
176
- gotten
113
+ hashmap[parameter]
177
114
  end
178
115
 
116
+ # @param [String] variable
117
+ # @param [Hash{String => String}] hashmap
118
+ # @param [String] _game_type
119
+ # @return [String]
179
120
  def self.get_variable_translated(variable, hashmap, _game_type)
180
121
  hashmap[variable]
181
122
  end
182
123
 
183
- def self.write_map(original_files, maps_path, output_path, shuffle_level, logging, game_type)
184
- maps_object_map = Hash[original_files.map do |filename|
185
- [File.basename(filename), merge_map(Marshal.load(File.binread(filename)))]
124
+ # @param [Array<String>] original_files_paths
125
+ # @param [String] maps_path
126
+ # @param [String] output_path
127
+ # @param [Integer] shuffle_level
128
+ # @param [Boolean] logging
129
+ # @param [String] game_type
130
+ def self.write_map(original_files_paths, maps_path, output_path, shuffle_level, logging, game_type)
131
+ maps_object_map = Hash[original_files_paths.map do |filename|
132
+ [File.basename(filename), Marshal.load(File.binread(filename))]
186
133
  end]
187
134
 
188
- maps_original_text = (File.readlines("#{maps_path}/maps.txt", encoding: 'UTF-8', chomp: true).map do |line|
189
- line.gsub('\#', "\n")
190
- end).freeze
135
+ maps_original_text = File.readlines(File.join(maps_path, 'maps.txt'), encoding: 'UTF-8', chomp: true).map do |line|
136
+ line.gsub('\#', "\n").strip
137
+ end.freeze
191
138
 
192
- names_original_text = (File.readlines("#{maps_path}/names.txt", encoding: 'UTF-8', chomp: true).map do |line|
193
- line.gsub('\#', "\n")
194
- end).freeze
139
+ names_original_text = File.readlines(File.join(maps_path, 'names.txt'), encoding: 'UTF-8', chomp: true).map do |line|
140
+ line.gsub('\#', "\n").strip
141
+ end.freeze
195
142
 
196
- maps_translated_text = File.readlines("#{maps_path}/maps_trans.txt", encoding: 'UTF-8', chomp: true).map do |line|
197
- line.gsub('\#', "\n")
143
+ maps_translated_text = File.readlines(File.join(maps_path, 'maps.txt'), encoding: 'UTF-8', chomp: true).map do |line|
144
+ line.gsub('\#', "\n").strip
198
145
  end
199
146
 
200
- names_translated_text = File.readlines("#{maps_path}/names_trans.txt", encoding: 'UTF-8', chomp: true).map do |line|
201
- line.gsub('\#', "\n")
147
+ names_translated_text = File.readlines(File.join(maps_path, 'names_trans.txt'), encoding: 'UTF-8', chomp: true).map do |line|
148
+ line.gsub('\#', "\n").strip
202
149
  end
203
150
 
204
151
  if shuffle_level.positive?
@@ -232,24 +179,75 @@ def self.write_map(original_files, maps_path, output_path, shuffle_level, loggin
232
179
  list = page.instance_variable_get(:@list)
233
180
  next if list.nil?
234
181
 
235
- list.each do |item|
182
+ in_sequence = false
183
+ line = []
184
+ item_indices = []
185
+ parameter_indices = []
186
+
187
+ list.each_with_index do |item, it|
236
188
  code = item.instance_variable_get(:@code)
237
189
  next unless allowed_codes.include?(code)
238
190
 
239
191
  parameters = item.instance_variable_get(:@parameters)
240
192
 
241
- parameters.each_with_index do |parameter, i|
242
- if [401, 402, 356].include?(code)
243
- if parameter.is_a?(String) && !parameter.empty?
244
- translated = get_parameter_translated(code, parameter, maps_translation_map, game_type)
245
- parameters[i] = translated unless translated.nil?
246
- end
247
- elsif parameter.is_a?(Array)
248
- parameter.each_with_index do |subparameter, j|
249
- if subparameter.is_a?(String) && !subparameter.empty?
250
- translated = get_parameter_translated(code, subparameter, maps_translation_map, game_type)
251
- parameters[i][j] = translated unless translated.nil?
193
+ parameters.each_with_index do |parameter, pr|
194
+ if code == 401
195
+ next unless parameter.is_a?(String) && !parameter.empty?
196
+
197
+ in_sequence = true
198
+ line.push(parameter)
199
+ item_indices.push(it)
200
+ parameter_indices.push(pr)
201
+ else
202
+ if in_sequence
203
+ joined = line.join('\#').strip
204
+ translated = get_parameter_translated(401, joined, maps_translation_map, game_type)
205
+
206
+ unless translated.nil? || translated.empty?
207
+ split = translated.split('\#')
208
+
209
+ (0..line.length - 1).each do |i|
210
+ list[item_indices[i]].instance_variable_get(:@parameters)[parameter_indices[i]] = split[i]
211
+ end
252
212
  end
213
+
214
+ line.clear
215
+ item_indices.clear
216
+ parameter_indices.clear
217
+ in_sequence = false
218
+ end
219
+
220
+ case code
221
+ when 402
222
+ next unless parameter.is_a?(String)
223
+
224
+ parameter = parameter.strip
225
+ next if parameter.empty?
226
+
227
+ translated = get_parameter_translated(code, parameter, maps_translation_map, game_type)
228
+ parameters[pr] = translated unless translated.nil? || translated.empty?
229
+ when 102
230
+ next unless parameter.is_a?(Array)
231
+
232
+ parameter.each_with_index do |subparameter, sp|
233
+ next unless subparameter.is_a?(String)
234
+
235
+ subparameter = subparameter.strip
236
+ next if subparameter.empty?
237
+
238
+ translated = get_parameter_translated(code, subparameter, maps_translation_map, game_type)
239
+ parameters[pr][sp] = translated unless translated.nil? || translated.empty?
240
+ end
241
+ when 356
242
+ next unless parameter.is_a?(String)
243
+
244
+ parameter = parameter.strip
245
+ next if parameter.empty?
246
+
247
+ translated = get_parameter_translated(code, parameter, maps_translation_map, game_type)
248
+ parameters[pr] = translated unless translated.nil? || translated.empty?
249
+ else
250
+ nil
253
251
  end
254
252
  end
255
253
  end
@@ -265,11 +263,16 @@ def self.write_map(original_files, maps_path, output_path, shuffle_level, loggin
265
263
  end
266
264
  end
267
265
 
268
- def self.write_other(original_files, other_path, output_path, shuffle_level, logging, game_type)
269
- other_object_array_map = Hash[original_files.map do |filename|
266
+ # @param [Array<String>] original_files
267
+ # @param [String] other_path
268
+ # @param [String] output_path
269
+ # @param [Integer] shuffle_level
270
+ # @param [Boolean] logging
271
+ # @param [String] game_type
272
+ def self.write_other(original_files_paths, other_path, output_path, shuffle_level, logging, game_type)
273
+ other_object_array_map = Hash[original_files_paths.map do |filename|
270
274
  basename = File.basename(filename)
271
275
  object = Marshal.load(File.binread(filename))
272
- object = merge_other(object).slice(1..) if basename.start_with?(/Common|Troops/)
273
276
 
274
277
  [basename, object]
275
278
  end]
@@ -279,11 +282,11 @@ def self.write_other(original_files, other_path, output_path, shuffle_level, log
279
282
  other_object_array_map.each do |filename, other_object_array|
280
283
  other_filename = File.basename(filename, '.*').downcase
281
284
 
282
- other_original_text = File.readlines("#{File.join(other_path, other_filename)}.txt", encoding: 'UTF-8', chomp: true)
283
- .map { |line| line.gsub('\#', "\n") }
285
+ other_original_text = File.readlines(File.join(other_path, "#{other_filename}.txt"), encoding: 'UTF-8', chomp: true)
286
+ .map { |line| line.gsub('\#', "\n").strip }
284
287
 
285
- other_translated_text = File.readlines("#{File.join(other_path, other_filename)}_trans.txt", encoding: 'UTF-8', chomp: true)
286
- .map { |line| line.gsub('\#', "\n") }
288
+ other_translated_text = File.readlines(File.join(other_path, "#{other_filename}_trans.txt"), encoding: 'UTF-8', chomp: true)
289
+ .map { |line| line.gsub('\#', "\n").strip }
287
290
 
288
291
  if shuffle_level.positive?
289
292
  other_translated_text.shuffle!
@@ -307,10 +310,15 @@ def self.write_other(original_files, other_path, output_path, shuffle_level, log
307
310
  [variables_symbols[1], nickname],
308
311
  [variables_symbols[2], description],
309
312
  [variables_symbols[3], note]].each do |symbol, variable|
310
- if variable.is_a?(String) && !variable.empty?
311
- translated = get_variable_translated(variable, other_translation_map, game_type)
312
- object.instance_variable_set(symbol, variable) unless translated.nil?
313
- end
313
+ next unless variable.is_a?(String)
314
+
315
+ variable = variable.strip
316
+ next if variable.empty?
317
+
318
+ variable = variable.gsub(/\r\n/, "\n")
319
+
320
+ translated = get_variable_translated(variable, other_translation_map, game_type)
321
+ object.instance_variable_set(symbol, translated) unless translated.nil? || translated.empty?
314
322
  end
315
323
  end
316
324
  else
@@ -318,27 +326,79 @@ def self.write_other(original_files, other_path, output_path, shuffle_level, log
318
326
  pages = object.instance_variable_get(:@pages)
319
327
  pages_length = pages.nil? ? 1 : pages.length
320
328
 
321
- (0..pages_length).each do |i|
322
- list = pages.nil? ? object.instance_variable_get(:@list) : pages[i].instance_variable_get(:@list)
329
+ (0..pages_length).each do |pg|
330
+ list = pages.nil? ? object.instance_variable_get(:@list) : pages[pg].instance_variable_get(:@list)
323
331
  next if list.nil?
324
332
 
325
- list.each do |item|
333
+ in_sequence = false
334
+ line = []
335
+ item_indices = []
336
+ parameter_indices = []
337
+
338
+ list.each_with_index do |item, it|
326
339
  code = item.instance_variable_get(:@code)
327
340
  next unless allowed_codes.include?(code)
328
341
 
329
342
  parameters = item.instance_variable_get(:@parameters)
330
- parameters.each do |parameter|
331
- if [401, 402, 356, 405].include?(code)
332
- if parameter.is_a?(String) && !parameter.empty?
333
- translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
334
- parameters[i] = translated unless translated.nil?
335
- end
336
- elsif parameter.is_a?(Array)
337
- parameter.each_with_index do |subparameter, j|
338
- if subparameter.is_a?(String) && !subparameter.empty?
339
- translated = get_parameter_translated(code, subparameter, other_translation_map, game_type)
340
- parameters[i][j] = translated unless translated.nil?
343
+
344
+ parameters.each_with_index do |parameter, pr|
345
+ if [401, 405].include?(code)
346
+ next unless parameter.is_a?(String) && !parameter.empty?
347
+
348
+ in_sequence = true
349
+ line.push(parameter)
350
+ item_indices.push(it)
351
+ parameter_indices.push(pr)
352
+ else
353
+ if in_sequence
354
+ joined = line.join('\#').strip
355
+ translated = get_parameter_translated(401, joined, other_translation_map, game_type)
356
+
357
+ unless translated.nil? || translated.empty?
358
+ split = translated.split('\#')
359
+
360
+ (0..line.length - 1).each do |i|
361
+ list[item_indices[i]].instance_variable_get(:@parameters)[parameter_indices[i]] = split[i]
362
+ end
341
363
  end
364
+
365
+ line.clear
366
+ item_indices.clear
367
+ parameter_indices.clear
368
+ in_sequence = false
369
+ end
370
+
371
+ case code
372
+ when 402
373
+ next unless parameter.is_a?(String)
374
+
375
+ parameter = parameter.strip
376
+ next if parameter.empty?
377
+
378
+ translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
379
+ parameters[pr] = translated unless translated.nil? || translated.empty?
380
+ when 102
381
+ next unless parameter.is_a?(Array)
382
+
383
+ parameter.each_with_index do |subparameter, sp|
384
+ next unless subparameter.is_a?(String)
385
+
386
+ subparameter = subparameter.strip
387
+ next if subparameter.empty?
388
+
389
+ translated = get_parameter_translated(code, subparameter, other_translation_map, game_type)
390
+ parameters[pr][sp] = translated unless translated.nil? || translated.empty?
391
+ end
392
+ when 356
393
+ next unless parameter.is_a?(String)
394
+
395
+ parameter = parameter.strip
396
+ next if parameter.empty?
397
+
398
+ translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
399
+ parameters[pr] = translated unless translated.nil? || translated.empty?
400
+ else
401
+ nil
342
402
  end
343
403
  end
344
404
  end
@@ -355,24 +415,34 @@ def self.write_other(original_files, other_path, output_path, shuffle_level, log
355
415
  end
356
416
  end
357
417
 
358
- def self.write_system(system_file_path, ini_file_path, other_path, output_path, shuffle_level, logging)
359
- def self.write_ini_title(ini_file_path, translated)
360
- file_lines = File.readlines(ini_file_path, chomp: true)
361
- title_line_index = file_lines.each_with_index do |line, i|
362
- break i if line.start_with?('title')
363
- end
364
-
365
- file_lines[title_line_index] = translated
366
- File.binwrite(ini_file_path, file_lines.join)
418
+ # @param [String] ini_file_path
419
+ # @param [String] translated
420
+ def self.write_ini_title(ini_file_path, translated)
421
+ file_lines = File.readlines(ini_file_path, chomp: true)
422
+ title_line_index = file_lines.each_with_index do |line, i|
423
+ break i if line.start_with?('title')
367
424
  end
368
425
 
426
+ file_lines[title_line_index] = translated
427
+ File.binwrite(ini_file_path, file_lines.join("\n"))
428
+ end
429
+
430
+ # @param [String] system_file_path
431
+ # @param [String] ini_file_path
432
+ # @param [String] other_path
433
+ # @param [String] output_path
434
+ # @param [Integer] shuffle_level
435
+ # @param [Boolean] logging
436
+ def self.write_system(system_file_path, ini_file_path, other_path, output_path, shuffle_level, logging)
369
437
  system_basename = File.basename(system_file_path)
370
438
  system_object = Marshal.load(File.binread(system_file_path))
371
439
 
372
- system_original_text = File.readlines("#{other_path}/system.txt", encoding: 'UTF-8', chomp: true)
440
+ system_original_text = File.readlines(File.join(other_path, 'system.txt'), encoding: 'UTF-8', chomp: true)
441
+ .map(&:strip)
373
442
  .freeze
374
443
 
375
- system_translated_text = File.readlines("#{other_path}/system_trans.txt", encoding: 'UTF-8', chomp: true)
444
+ system_translated_text = File.readlines(File.join(other_path, 'system_trans.txt'), encoding: 'UTF-8', chomp: true)
445
+ .map(&:strip)
376
446
 
377
447
  if shuffle_level.positive?
378
448
  system_translated_text.shuffle!
@@ -393,22 +463,38 @@ def self.write_system(system_file_path, ini_file_path, other_path, output_path,
393
463
  [elements, skill_types, weapon_types, armor_types].each_with_index.each do |array, i|
394
464
  next unless array.is_a?(Array)
395
465
 
396
- array.map! { |string| system_translation_map[string] || string }
466
+ array.map! do |string|
467
+ stripped = string.strip
468
+ return string if stripped.empty?
469
+
470
+ translated = system_translation_map[stripped]
471
+ !translated.nil? && !translated.empty? ? translated : stripped
472
+ end
473
+
397
474
  system_object.instance_variable_set(system_symbols[i], array)
398
475
  end
399
476
 
400
477
  currency_unit_translated = system_translation_map[currency_unit]
401
478
  system_object.instance_variable_set(system_symbols[4], currency_unit_translated) if currency_unit.is_a?(String) &&
402
- !currency_unit_translated.nil?
479
+ (!currency_unit_translated.nil? && !currency_unit_translated.empty?)
403
480
 
404
481
  terms.instance_variables.each do |variable|
405
482
  value = terms.instance_variable_get(variable)
406
483
 
407
484
  if value.is_a?(String)
408
- translated = system_translation_map[value]
409
- value = translated unless translated.nil?
485
+ stripped = value.strip
486
+ next if value.empty?
487
+
488
+ translated = system_translation_map[stripped]
489
+ value = !translated.nil? && !translated.empty? ? translated : value
410
490
  elsif value.is_a?(Array)
411
- value.map! { |string| system_translation_map[string] || string }
491
+ value.map! do |string|
492
+ stripped = string.strip
493
+ return string if stripped.empty?
494
+
495
+ translated = system_translation_map[stripped]
496
+ value = !translated.nil? && !translated.empty? ? translated : value
497
+ end
412
498
  end
413
499
 
414
500
  terms.instance_variable_set(variable, value)
@@ -419,85 +505,52 @@ def self.write_system(system_file_path, ini_file_path, other_path, output_path,
419
505
  system_object.instance_variable_set(system_symbols[6], terms)
420
506
 
421
507
  game_title_translated = system_translated_text[-1]
422
- system_object.instance_variable_set(system_symbols[7], game_title_translated) if currency_unit.is_a?(String) && !game_title_translated.nil?
423
-
508
+ system_object.instance_variable_set(system_symbols[7], game_title_translated)
424
509
  write_ini_title(ini_file_path, game_title_translated)
425
510
 
426
511
  puts "Written #{system_basename}" if logging
427
512
 
428
- File.binwrite("#{output_path}/#{system_basename}", Marshal.dump(system_object))
513
+ File.binwrite(File.join(output_path, system_basename), Marshal.dump(system_object))
429
514
  end
430
515
 
516
+ # @param [String] scripts_file_path Path to Scripts.*data file
517
+ # @param [String] other_path Path to translation/other directory containing .txt files
518
+ # @param [String] output_path Path to the output directory
519
+ # @param [Boolean] logging Whether to log
431
520
  def self.write_scripts(scripts_file_path, other_path, output_path, logging)
432
521
  scripts_basename = File.basename(scripts_file_path)
433
522
  script_entries = Marshal.load(File.binread(scripts_file_path))
434
523
 
435
- scripts_translated_text = File.readlines("#{other_path}/scripts_trans.txt", encoding: 'UTF-8', chomp: true)
524
+ scripts_original_text = File.readlines(File.join(other_path, 'scripts.txt'), encoding: 'UTF-8', chomp: true)
525
+ .map { |line| line.gsub('\#', "\r\n") }
526
+ scripts_translated_text = File.readlines(File.join(other_path, 'scripts_trans.txt'), encoding: 'UTF-8', chomp: true)
436
527
  .map { |line| line.gsub('\#', "\r\n") }
437
528
 
529
+ scripts_translation_map = Hash[scripts_original_text.zip(scripts_translated_text)]
530
+
438
531
  # Shuffle can possibly break the game in scripts, so no shuffling
532
+ codes = []
439
533
 
440
534
  script_entries.each do |script|
441
535
  code = Zlib::Inflate.inflate(script[2]).force_encoding('UTF-8')
442
536
 
443
- (extract_quoted_strings(code)).each_with_index do |string_data, i|
444
- string, string_index = string_data
445
-
446
- string.strip!
447
-
448
- # Removes the U+3000 Japanese typographical space to check if string, when stripped, is truly empty
449
- next if string.empty? || string.gsub(' ', '').empty?
450
-
451
- # Maybe this mess will remove something that mustn't be removed, but it needs to be tested
452
- next if string.start_with?(/([#!?$@]|(\.\/)?(Graphics|Data|Audio|CG|Movies|Save)\/)/) ||
453
- string.match?(/^\d+$/) ||
454
- string.match?(/^(.)\1{2,}$/) ||
455
- string.match?(/^(false|true)$/) ||
456
- string.match?(/^[wr]b$/) ||
457
- string.match?(/^(?=.*\d)[A-Za-z0-9\-]+$/) ||
458
- string.match?(/^[A-Z\-()\/ +'&]*$/) ||
459
- string.match?(/^[a-z\-()\/ +'&]*$/) ||
460
- string.match?(/^[A-Za-z]+[+-]$/) ||
461
- string.match?(/^[.()+-:;\[\]^~%&!*\/→×??x%▼|]$/) ||
462
- string.match?(/^Tile.*[A-Z]$/) ||
463
- string.match?(/^:?%.*[ds][:%]*?$/) ||
464
- string.match?(/^[a-zA-Z]+([A-Z][a-z]*)+$/) ||
465
- string.match?(/^Cancel Action$|^Invert$|^End$|^Individual$|^Missed File$|^Bitmap$|^Audio$/) ||
466
- string.match?(/\.(mp3|ogg|jpg|png|ini)$/) ||
467
- string.match?(/\/(\d.*)?$/) ||
468
- string.match?(/FILE$/) ||
469
- string.match?(/#\{/) ||
470
- string.match?(/\\(?!#)/) ||
471
- string.match?(/\+?=?=/) ||
472
- string.match?(/[}{_<>]/) ||
473
- string.match?(/r[vx]data/) ||
474
- string.match?(/No such file or directory/) ||
475
- string.match?(/level \*\*/) ||
476
- string.match?(/Courier New/) ||
477
- string.match?(/Comic Sans/) ||
478
- string.match?(/Lucida/) ||
479
- string.match?(/Verdana/) ||
480
- string.match?(/Tahoma/) ||
481
- string.match?(/Arial/) ||
482
- string.match?(/Player start location/) ||
483
- string.match?(/Common event call has exceeded/) ||
484
- string.match?(/se-/) ||
485
- string.match?(/Start Pos/) ||
486
- string.match?(/An error has occurred/) ||
487
- string.match?(/Define it first/) ||
488
- string.match?(/Process Skill/) ||
489
- string.match?(/Wpn Only/) ||
490
- string.match?(/Don't Wait/) ||
491
- string.match?(/Clear image/) ||
492
- string.match?(/Can Collapse/)
493
-
494
- code[string_index, string.length] = scripts_translated_text[i]
537
+ # this shit finally works and requires NO further changes
538
+ string_array, index_array = extract_quoted_strings(code)
539
+
540
+ string_array.zip(index_array).reverse_each do |string, index|
541
+ string = string.strip.delete(' ')
542
+ next if string.empty? || !scripts_translation_map.include?(string)
543
+
544
+ gotten = scripts_translation_map[string]
545
+ code[index - string.length, string.length] = gotten unless gotten.nil? || gotten.empty?
495
546
  end
496
547
 
548
+ codes.push(code)
497
549
  script[2] = Zlib::Deflate.deflate(code, Zlib::BEST_COMPRESSION)
498
550
  end
499
551
 
500
552
  puts "Written #{scripts_basename}" if logging
501
553
 
502
- File.binwrite("#{output_path}/#{scripts_basename}", Marshal.dump(script_entries))
554
+ File.binwrite(File.join(output_path, 'scripts_plain.txt'), codes.join("\n"))
555
+ File.binwrite(File.join(output_path, scripts_basename), Marshal.dump(script_entries))
503
556
  end