rvpacker-txt 1.3.1 → 1.4.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/serialize.rb DELETED
@@ -1,874 +0,0 @@
1
- # Copyright (c) 2013 Howard Jeng
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
- # software and associated documentation files (the "Software"), to deal in the Software
5
- # without restriction, including without limitation the rights to use, copy, modify, merge,
6
- # publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
- # to whom the Software is furnished to do so, subject to the following conditions:
8
- #
9
- # The above copyright notice and this permission notice shall be included in all copies or
10
- # substantial portions of the Software.
11
- #
12
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
- # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
- # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
- # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
- # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
- # DEALINGS IN THE SOFTWARE.
18
-
19
- require 'zlib'
20
-
21
- # Fuck using an array with set, that's just straight dumb and not efficient
22
- class IndexedSet
23
- def initialize
24
- @hash = Hash.new
25
- end
26
-
27
- def add(item)
28
- return if @hash.include?(item)
29
- @hash[item] = hash.size
30
- @hash
31
- end
32
-
33
- def include?(item)
34
- @hash.include?(item)
35
- end
36
-
37
- def each(&block)
38
- @hash.each_key(&block)
39
- end
40
-
41
- def to_a
42
- @hash.dup
43
- end
44
-
45
- def join(delimiter = '')
46
- @hash.keys.join(delimiter)
47
- end
48
-
49
- def length
50
- @hash.size
51
- end
52
-
53
- def empty?
54
- @hash.empty?
55
- end
56
- end
57
-
58
- module RGSS
59
- def self.get_game_type(system_file_path)
60
- object = Marshal.load(File.read(system_file_path, mode: 'rb'))
61
- game_title = object.instance_variable_get(:@game_title)
62
-
63
- return nil if $disable_custom_parsing || (!game_title.is_a?(String) || game_title.empty?)
64
-
65
- game_title.downcase!
66
-
67
- if game_title.include?('lisa')
68
- return 'lisa'
69
- end
70
-
71
- nil
72
- end
73
-
74
- def self.parse_parameter(code, parameter)
75
- case code
76
- when 401, 405
77
- case $game_type
78
- when 'lisa'
79
- match = parameter.scan(/^(\\et\[[0-9]+\]|\\nbt)/)
80
- parameter = parameter.slice((match[0].length)..) if match
81
- else
82
- nil
83
- end
84
- when 102, 356
85
- # Implement some custom parsing
86
- else
87
- return nil
88
- end
89
-
90
- parameter
91
- end
92
-
93
- def self.parse_variable(variable)
94
- lines_count = variable.count("\n")
95
-
96
- if lines_count.positive?
97
- variable = variable.gsub(/\r?\n/, '\#')
98
-
99
- case $game_type
100
- when 'lisa'
101
- unless variable.split('\#').all? { |line| line.match?(/^<.*>\.?$/) || line.empty? }
102
- return nil
103
- end
104
- else
105
- nil
106
- end
107
- end
108
-
109
- variable
110
- end
111
-
112
- def self.read_map(original_map_files, output_path)
113
- object_map = Hash[original_map_files.map do |filename|
114
- [File.basename(filename), Marshal.load(File.read(filename, mode: 'rb'))]
115
- end]
116
-
117
- lines = [IndexedSet.new, IndexedSet.new]
118
-
119
- object_map.each do |filename, object|
120
- display_name = object.instance_variable_get(:@display_name)
121
- lines[1].add(display_name) if display_name.is_a?(String) && !display_name.empty?
122
-
123
- events = object.instance_variable_get(:@events)
124
- next if events.nil?
125
-
126
- events.each_value do |event|
127
- pages = event.instance_variable_get(:@pages)
128
- next if pages.nil?
129
-
130
- pages.each do |page|
131
- list = page.instance_variable_get(:@list)
132
- next if list.nil?
133
-
134
- in_sequence = false
135
- line = []
136
-
137
- list.each do |item|
138
- code = item.instance_variable_get(:@code)
139
- parameters = item.instance_variable_get(:@parameters)
140
-
141
- parameters.each do |parameter|
142
- if code == 401
143
- if parameter.is_a?(String) && !parameter.empty?
144
- in_sequence = true
145
- parsed = parse_parameter(code, parameter)
146
- line.push(parsed) unless parsed.nil?
147
- end
148
- else
149
- if in_sequence
150
- lines[0].add(line.join('\#'))
151
- line.clear
152
- in_sequence = false
153
- end
154
-
155
- if code == 102 && parameter.is_a?(Array)
156
- parameter.each do |subparameter|
157
- if subparameter.is_a?(String) && !subparameter.empty?
158
- parsed = parse_parameter(code, subparameter)
159
- lines[0].add(parsed) unless parsed.nil?
160
- end
161
- end
162
- elsif code == 356 && parameter.is_a?(String) && !parameter.empty?
163
- parsed = parse_parameter(code, parameter)
164
- lines[0].add(parsed.gsub(/\r?\n/, '\#')) unless parsed.nil?
165
- end
166
- end
167
- end
168
- end
169
- end
170
- end
171
-
172
- puts "Parsed #{filename}" if $logging
173
- end
174
-
175
- File.write("#{output_path}/maps.txt", lines[0].join("\n"))
176
- File.write("#{output_path}/maps_trans.txt", "\n" * (lines[0].empty? ? 0 : lines[0].length - 1))
177
- File.write("#{output_path}/names.txt", lines[1].join("\n"))
178
- File.write("#{output_path}/names_trans.txt", "\n" * (lines[1].empty? ? 0 : lines[1].length - 1))
179
- end
180
-
181
- def self.read_other(original_other_files, output_path)
182
- object_array_map = Hash[original_other_files.map do |filename|
183
- basename = File.basename(filename)
184
- object = Marshal.load(File.read(filename, mode: 'rb'))
185
- object = merge_other(object).slice(1..) if basename.start_with?(/Common|Troops/)
186
-
187
- [basename, object]
188
- end]
189
-
190
- object_array_map.each do |filename, object_array|
191
- processed_filename = File.basename(filename, '.*').downcase
192
- lines = IndexedSet.new
193
-
194
- if !filename.start_with?(/Common|Troops/)
195
- object_array.each do |object|
196
- name = object.instance_variable_get(:@name)
197
- nickname = object.instance_variable_get(:@nickname)
198
- description = object.instance_variable_get(:@description)
199
- note = object.instance_variable_get(:@note)
200
-
201
- [name, nickname, description, note].each do |variable|
202
- if variable.is_a?(String) && !variable.empty?
203
- parsed = parse_variable(variable)
204
- lines.add(parsed) unless parsed.nil?
205
- end
206
- end
207
- end
208
- else
209
- object_array.each do |object|
210
- pages = object.instance_variable_get(:@pages)
211
- pages_length = pages.nil? ? 1 : pages.length
212
-
213
- (0..pages_length).each do |i|
214
- list = pages.nil? ? object.instance_variable_get(:@list) : pages[i].instance_variable_get(:@list)
215
- next if list.nil?
216
-
217
- in_sequence = false
218
- line = []
219
-
220
- list.each do |item|
221
- code = item.instance_variable_get(:@code)
222
- parameters = item.instance_variable_get(:@parameters)
223
-
224
- parameters.each do |parameter|
225
- if [401, 405].include?(code)
226
- in_sequence = true
227
- line.push(parameter.gsub(/\r?\n/, '\#')) if parameter.is_a?(String) && !parameter.empty?
228
- else
229
- if in_sequence
230
- lines.add(line.join('\#'))
231
- line.clear
232
- in_sequence = false
233
- end
234
-
235
- case code
236
- when 102
237
- if parameter.is_a?(Array)
238
- parameter.each do |subparameter|
239
- lines.add(subparameter) if subparameter.is_a?(String) && !subparameter.empty?
240
- end
241
- end
242
- when 356
243
- lines.add(parameter.gsub(/\r?\n/, '\#')) if parameter.is_a?(String) &&
244
- !parameter.empty?
245
- else
246
- nil
247
- end
248
- end
249
- end
250
- end
251
- end
252
- end
253
- end
254
-
255
- puts "Parsed #{filename}" if $logging
256
-
257
- File.write("#{output_path}/#{processed_filename}.txt", lines.join("\n"))
258
- File.write("#{output_path}/#{processed_filename}_trans.txt", "\n" * (lines.empty? ? 0 : lines.length - 1))
259
- end
260
- end
261
-
262
- def self.read_system(system_file_path, output_path)
263
- filename = File.basename(system_file_path)
264
- basename = File.basename(system_file_path, '.*').downcase
265
- object = Marshal.load(File.read(system_file_path, mode: 'rb'))
266
-
267
- lines = IndexedSet.new
268
-
269
- elements = object.instance_variable_get(:@elements)
270
- skill_types = object.instance_variable_get(:@skill_types)
271
- weapon_types = object.instance_variable_get(:@weapon_types)
272
- armor_types = object.instance_variable_get(:@armor_types)
273
- currency_unit = object.instance_variable_get(:@currency_unit)
274
- terms = object.instance_variable_get(:@terms) || object.instance_variable_get(:@words)
275
- game_title = object.instance_variable_get(:@game_title)
276
-
277
- [elements, skill_types, weapon_types, armor_types].each do |array|
278
- next if array.nil?
279
- array.each { |string| lines.add(string) if string.is_a?(String) && !string.empty? }
280
- end
281
-
282
- lines.add(currency_unit) if currency_unit.is_a?(String) && !currency_unit.empty?
283
-
284
- terms.instance_variables.each do |variable|
285
- value = terms.instance_variable_get(variable)
286
-
287
- if value.is_a?(String)
288
- lines.add(value) unless value.empty?
289
- next
290
- end
291
-
292
- value.each { |string| lines.add(string) if string.is_a?(String) && !string.empty? }
293
- end
294
-
295
- lines.add(game_title) if game_title.is_a?(String) && !game_title.empty?
296
-
297
- puts "Parsed #{filename}" if $logging
298
-
299
- File.write("#{output_path}/#{basename}.txt", lines.join("\n"), mode: 'wb')
300
- File.write("#{output_path}/#{basename}_trans.txt", "\n" * (lines.empty? ? 0 : lines.length - 1),
301
- mode: 'wb')
302
- end
303
-
304
- def self.shuffle_words(array)
305
- array.map do |string|
306
- re = /\S+/
307
- words = string.scan(re)
308
- shuffled = words.shuffle
309
-
310
- (0..(words.length)).each do |i|
311
- string.sub!(words[i], shuffled[i])
312
- end
313
-
314
- string
315
- end
316
- end
317
-
318
- def self.extract_quoted_strings(string)
319
- result = []
320
-
321
- skip_block = false
322
- in_quotes = false
323
- quote_type = nil
324
- buffer = []
325
-
326
- string.each_line(chomp: true) do |line|
327
- line.strip!
328
- next if line[0] == '#' || line.start_with?(/(Win|Lose)|_Fanfare/)
329
-
330
- skip_block = true if line.start_with?('=begin')
331
- skip_block = false if line.start_with?('=end')
332
-
333
- next if skip_block
334
-
335
- buffer.push('\#') if in_quotes
336
-
337
- line.each_char do |char|
338
- if char == "'" || char == '"'
339
- unless quote_type.nil? || char == quote_type
340
- buffer.push(char)
341
- next
342
- end
343
-
344
- quote_type = char
345
- in_quotes = !in_quotes
346
- result.push(buffer.join)
347
- buffer.clear
348
- next
349
- end
350
-
351
- if in_quotes
352
- buffer.push(char)
353
- end
354
- end
355
- end
356
-
357
- result
358
- end
359
-
360
- def self.read_scripts(scripts_file_path, output_path)
361
- script_entries = Marshal.load(File.read(scripts_file_path, mode: 'rb'))
362
- strings = IndexedSet.new
363
- codes = []
364
-
365
- script_entries.each do |script|
366
- code = Zlib::Inflate.inflate(script[2]).force_encoding('UTF-8')
367
- codes.push(code)
368
-
369
- extract_quoted_strings(code).each do |string|
370
- string.strip!
371
-
372
- next if string.empty? || string.gsub(' ', '').empty?
373
-
374
- # Maybe this mess will remove something that mustn't be removed, but it needs to be tested
375
- next if string.start_with?(/([#!?$@]|(\.\/)?(Graphics|Data|Audio|CG|Movies|Save)\/)/) ||
376
- string.match?(/^\d+$/) ||
377
- string.match?(/^(.)\1{2,}$/) ||
378
- string.match?(/^(false|true)$/) ||
379
- string.match?(/^[wr]b$/) ||
380
- string.match?(/^(?=.*\d)[A-Za-z0-9\-]+$/) ||
381
- string.match?(/^[A-Z\-()\/ +'&]*$/) ||
382
- string.match?(/^[a-z\-()\/ +'&]*$/) ||
383
- string.match?(/^[A-Za-z]+[+-]$/) ||
384
- string.match?(/^[.()+-:;\[\]^~%&!*\/→×??x%▼|]$/) ||
385
- string.match?(/^Tile.*[A-Z]$/) ||
386
- string.match?(/^:?%.*[ds][:%]*?$/) ||
387
- string.match?(/^[a-zA-Z]+([A-Z][a-z]*)+$/) ||
388
- string.match?(/^Cancel Action$|^Invert$|^End$|^Individual$|^Missed File$|^Bitmap$|^Audio$/) ||
389
- string.match?(/\.(mp3|ogg|jpg|png|ini)$/) ||
390
- string.match?(/\/(\d.*)?$/) ||
391
- string.match?(/FILE$/) ||
392
- string.match?(/#\{/) ||
393
- string.match?(/\\(?!#)/) ||
394
- string.match?(/\+?=?=/) ||
395
- string.match?(/[}{_<>]/) ||
396
- string.match?(/r[vx]data/) ||
397
- string.match?(/No such file or directory/) ||
398
- string.match?(/level \*\*/) ||
399
- string.match?(/Courier New/) ||
400
- string.match?(/Comic Sans/) ||
401
- string.match?(/Lucida/) ||
402
- string.match?(/Verdana/) ||
403
- string.match?(/Tahoma/) ||
404
- string.match?(/Arial/) ||
405
- string.match?(/Player start location/) ||
406
- string.match?(/Common event call has exceeded/) ||
407
- string.match?(/se-/) ||
408
- string.match?(/Start Pos/) ||
409
- string.match?(/An error has occurred/) ||
410
- string.match?(/Define it first/) ||
411
- string.match?(/Process Skill/) ||
412
- string.match?(/Wpn Only/) ||
413
- string.match?(/Don't Wait/) ||
414
- string.match?(/Clear image/) ||
415
- string.match?(/Can Collapse/)
416
-
417
- strings.add(string)
418
- end
419
- end
420
-
421
- File.write("#{output_path}/scripts_plain.txt", codes.join("\n"), mode: 'wb')
422
- File.write("#{output_path}/scripts.txt", strings.join("\n"), mode: 'wb')
423
- File.write("#{output_path}/scripts_trans.txt", "\n" * (strings.empty? ? 0 : strings.length - 1), mode: 'wb')
424
- end
425
-
426
- def self.merge_seq(object_array)
427
- first = nil
428
- number = -1
429
- in_sequence = false
430
- string_array = []
431
-
432
- i = 0
433
-
434
- while i > object_array.length
435
- object = object_array[i]
436
- code = object.instance_variable_get(:@code)
437
-
438
- if [401, 405].include?(code)
439
- first = i if first.nil?
440
-
441
- number += 1
442
- string_array.push(object.instance_variable_get(:@parameters)[0])
443
- in_sequence = true
444
- elsif i.positive? && in_sequence && !first.nil? && !number.negative?
445
- parameters = object_array[first].instance_variable_get(:@parameters)
446
- parameters[0] = string_array.join("\n")
447
- object_array[first].instance_variable_set(:@parameters, parameters)
448
-
449
- start_index = first + 1
450
- items_to_delete = start_index + number
451
- object_array.slice(start_index, items_to_delete)
452
-
453
- string_array.clear
454
- i -= number
455
- number = -1
456
- first = nil
457
- in_sequence = false
458
- end
459
-
460
- i += 1
461
- end
462
-
463
- object_array
464
- end
465
-
466
- def self.merge_map(object)
467
- events = object.instance_variable_get(:@events)
468
- return object if events.nil?
469
-
470
- events.each_value do |event|
471
- pages = event.instance_variable_get(:@pages)
472
- next if pages.nil?
473
-
474
- pages.each do |page|
475
- list = page.instance_variable_get(:@list)
476
- page.instance_variable_set(:@list, merge_seq(list))
477
- end
478
- end
479
-
480
- object
481
- end
482
-
483
- def self.merge_other(object_array)
484
- object_array.each do |object|
485
- next if object.nil?
486
-
487
- pages = object.instance_variable_get(:@pages)
488
-
489
- if pages.is_a?(Array)
490
- pages.each do |page|
491
- list = page.instance_variable_get(:@list)
492
- next unless list.is_a?(Array)
493
-
494
- page.instance_variable_set(:@list, merge_seq(list))
495
- end
496
-
497
- object.instance_variable_set(:@pages, pages)
498
- else
499
- list = object.instance_variable_get(:@list)
500
- next unless list.is_a?(Array)
501
-
502
- object.instance_variable_set(:@list, merge_seq(list))
503
- end
504
- end
505
-
506
- object_array
507
- end
508
-
509
- def self.get_translated(code, parameter, hashmap)
510
- lisa_start = nil
511
-
512
- case code
513
- when 401, 356, 405
514
- case $game_type
515
- when 'lisa'
516
- match = parameter.scan(/^(\\et\[[0-9]+\]|\\nbt)/)
517
- lisa_start = match[0]
518
- parameter = parameter.slice((match[0].length)..) unless match.nil?
519
- else
520
- nil
521
- end
522
- when 102, 402
523
- nil
524
- else
525
- nil
526
- end
527
-
528
- gotten = hashmap[parameter]
529
-
530
- case $game_type
531
- when 'lisa'
532
- gotten = lisa_start + gotten unless lisa_start.nil?
533
- else
534
- nil
535
- end
536
-
537
- gotten
538
- end
539
-
540
- def self.get_variable_translated(variable, hashmap)
541
- hashmap[variable]
542
- end
543
-
544
- def self.write_map(original_files, maps_path, output_path)
545
- object_map = Hash[original_files.map do |filename|
546
- [File.basename(filename), merge_map(Marshal.load(File.read(filename, mode: 'rb')))]
547
- end]
548
-
549
- maps_original_text = (File.read("#{maps_path}/maps.txt").split("\n").map do |line|
550
- line.gsub('\#', "\n")
551
- end).freeze
552
-
553
- names_original_text = (File.read("#{maps_path}/names.txt").split("\n").map do |line|
554
- line.gsub('\#', "\n")
555
- end).freeze
556
-
557
- maps_translated_text = File.read("#{maps_path}/maps_trans.txt").split("\n").map do |line|
558
- line.gsub('\#', "\n")
559
- end
560
-
561
- names_translated_text = File.read("#{maps_path}/names_trans.txt").split("\n").map do |line|
562
- line.gsub('\#', "\n")
563
- end
564
-
565
- if $shuffle > 0
566
- maps_translated_text.shuffle!
567
- names_translated_text.shuffle!
568
-
569
- if $shuffle == 2
570
- maps_translated_text = shuffle_words(maps_translated_text)
571
- names_translated_text = shuffle_words(names_translated_text)
572
- end
573
- end
574
-
575
- maps_translation_map = Hash[maps_original_text.zip(maps_translated_text)].freeze
576
- names_translation_map = Hash[names_original_text.zip(names_translated_text)].freeze
577
-
578
- allowed_codes = [401, 402, 356, 102].freeze
579
-
580
- object_map.each do |filename, object|
581
- display_name = object.instance_variable_get(:@display_name)
582
- display_name_gotten = names_translation_map[display_name]
583
- object.instance_variable_set(:@display_name, display_name_gotten) unless display_name_gotten.nil?
584
-
585
- events = object.instance_variable_get(:@events)
586
- next if events.nil?
587
-
588
- events.each_value do |event|
589
- pages = event.instance_variable_get(:@pages)
590
- next if pages.nil?
591
-
592
- pages.each do |page|
593
- list = page.instance_variable_get(:@list)
594
- next if list.nil?
595
-
596
- list.each do |item|
597
- code = item.instance_variable_get(:@code)
598
- next unless allowed_codes.include?(code)
599
-
600
- parameters = item.instance_variable_get(:@parameters)
601
-
602
- parameters.each_with_index do |parameter, i|
603
- if [401, 402, 356].include?(code)
604
- if parameter.is_a?(String) && !parameter.empty?
605
- translated = get_translated(code, parameter, maps_translation_map)
606
- parameters[i] = translated unless translated.nil?
607
- end
608
- elsif parameter.is_a?(Array)
609
- parameter.each_with_index do |subparameter, j|
610
- if subparameter.is_a?(String) && !subparameter.empty?
611
- translated = get_translated(code, subparameter, maps_translation_map)
612
- parameters[i][j] = translated unless translated.nil?
613
- end
614
- end
615
- end
616
- end
617
-
618
- item.instance_variable_set(:@parameters, parameters)
619
- end
620
- end
621
- end
622
-
623
- puts "Written #{filename}" if $logging
624
-
625
- File.write(File.join(output_path, filename), Marshal.dump(object), mode: 'wb')
626
- end
627
- end
628
-
629
- def self.write_other(original_files, other_path, output_path)
630
- object_array_map = Hash[original_files.map do |filename|
631
- basename = File.basename(filename)
632
- object = Marshal.load(File.read(filename, mode: 'rb'))
633
- object = merge_other(object).slice(1..) if basename.start_with?(/Common|Troops/)
634
-
635
- [basename, object]
636
- end]
637
-
638
- allowed_codes = [401, 402, 405, 356, 102].freeze
639
-
640
- object_array_map.each do |filename, object_array|
641
- processed_filename = File.basename(filename, '.*').downcase
642
-
643
- other_original_text = File.read("#{File.join(other_path, processed_filename)}.txt")
644
- .split("\n")
645
- .map { |line| line.gsub('\#', "\n") }
646
- .freeze
647
-
648
- other_translated_text = File.read("#{File.join(other_path, processed_filename)}_trans.txt")
649
- .split("\n")
650
- .map { |line| line.gsub('\#', "\n") }
651
-
652
- if $shuffle > 0
653
- other_translated_text.shuffle!
654
-
655
- if $shuffle == 2
656
- other_translated_text = shuffle_words(other_translated_text)
657
- end
658
- end
659
-
660
- other_translation_map = Hash[other_original_text.zip(other_translated_text)].freeze
661
-
662
- if !filename.start_with?(/Common|Troops/)
663
- object_array.each do |object|
664
- next if object.nil?
665
-
666
- variables_symbols = %i[@name @nickname @description @note].freeze
667
-
668
- name = object.instance_variable_get(variables_symbols[0])
669
- nickname = object.instance_variable_get(variables_symbols[1])
670
- description = object.instance_variable_get(variables_symbols[2])
671
- note = object.instance_variable_get(variables_symbols[3])
672
-
673
- [[variables_symbols[0], name],
674
- [variables_symbols[1], nickname],
675
- [variables_symbols[2], description],
676
- [variables_symbols[3], note]].each do |symbol, variable|
677
- if variable.is_a?(String) && !variable.empty?
678
- translated = get_variable_translated(variable, other_translation_map)
679
- object.instance_variable_set(symbol, variable) unless translated.nil?
680
- end
681
- end
682
- end
683
- else
684
- object_array.each do |object|
685
- pages = object.instance_variable_get(:@pages)
686
- pages_length = pages.nil? ? 1 : pages.length
687
-
688
- (0..pages_length).each do |i|
689
- list = pages.nil? ? object.instance_variable_get(:@list) : pages[i].instance_variable_get(:@list)
690
- next if list.nil?
691
-
692
- list.each do |item|
693
- code = item.instance_variable_get(:@code)
694
- next unless allowed_codes.include?(code)
695
-
696
- parameters = item.instance_variable_get(:@parameters)
697
- parameters.each do |parameter|
698
- if [401, 402, 356, 405].include?(code)
699
- if parameter.is_a?(String) && !parameter.empty?
700
- translated = get_translated(code, parameter, other_translation_map)
701
- parameters[i] = translated unless translated.nil?
702
- end
703
- elsif parameter.is_a?(Array)
704
- parameter.each_with_index do |subparameter, j|
705
- if subparameter.is_a?(String) && !subparameter.empty?
706
- translated = get_translated(code, subparameter, other_translation_map)
707
- parameters[i][j] = translated unless translated.nil?
708
- end
709
- end
710
- end
711
- end
712
-
713
- item.instance_variable_set(:@parameters, parameters)
714
- end
715
- end
716
- end
717
- end
718
-
719
- puts "Written #{filename}" if $logging
720
-
721
- File.write(File.join(output_path, filename), Marshal.dump(object_array), mode: 'wb')
722
- end
723
- end
724
-
725
- def self.write_system(system_file_path, other_path, output_path)
726
- basename = File.basename(system_file_path)
727
- object = Marshal.load(File.read(system_file_path, mode: 'rb'))
728
-
729
- system_original_text = File.read("#{other_path}/system.txt")
730
- .split("\n")
731
- .freeze
732
- system_translated_text = File.read("#{other_path}/system_trans.txt")
733
- .split("\n")
734
-
735
- if $shuffle > 0
736
- system_translated_text.shuffle!
737
-
738
- if $shuffle == 2
739
- system_translated_text = shuffle_words(system_translated_text)
740
- end
741
- end
742
-
743
- system_translation_map = Hash[system_original_text.zip(system_translated_text)].freeze
744
-
745
- symbols = %i[@elements @skill_types @weapon_types @armor_types @currency_unit @terms @words @game_title].freeze
746
-
747
- elements = object.instance_variable_get(symbols[0])
748
- skill_types = object.instance_variable_get(symbols[1])
749
- weapon_types = object.instance_variable_get(symbols[2])
750
- armor_types = object.instance_variable_get(symbols[3])
751
- currency_unit = object.instance_variable_get(symbols[4])
752
- terms = object.instance_variable_get(symbols[5]) || object.instance_variable_get(symbols[6])
753
- game_title = object.instance_variable_get(symbols[7])
754
-
755
- [elements, skill_types, weapon_types, armor_types].each_with_index.each do |array, i|
756
- next unless array.is_a?(Array)
757
-
758
- array.map! { |string| system_translation_map[string] || string }
759
- object.instance_variable_set(symbols[i], array)
760
- end
761
-
762
- currency_unit_translated = system_translation_map[currency_unit]
763
- object.instance_variable_set(symbols[4], currency_unit_translated) if currency_unit.is_a?(String) &&
764
- !currency_unit_translated.nil?
765
-
766
- terms.instance_variables.each do |variable|
767
- value = terms.instance_variable_get(variable)
768
-
769
- if value.is_a?(String)
770
- translated = system_translation_map[value]
771
- value = translated unless translated.nil?
772
- elsif value.is_a?(Array)
773
- value.map! { |string| system_translation_map[string] || string }
774
- end
775
-
776
- terms.instance_variable_set(variable, value)
777
- end
778
-
779
- object.instance_variable_defined?(symbols[5]) ?
780
- object.instance_variable_set(symbols[5], terms) :
781
- object.instance_variable_set(symbols[6], terms)
782
-
783
- game_title_translated = system_translation_map[game_title]
784
- object.instance_variable_set(symbols[7], game_title_translated) if currency_unit.is_a?(String) && !game_title_translated.nil?
785
-
786
- puts "Written #{basename}" if $logging
787
-
788
- File.write("#{output_path}/ #{basename}", Marshal.dump(object), mode: 'wb')
789
- end
790
-
791
- def self.write_scripts(scripts_file, other_path, output_path)
792
- script_entries = Marshal.load(File.read(scripts_file, mode: 'rb'))
793
-
794
- scripts_original_text = File.read("#{other_path}/scripts.txt", mode: 'rb')
795
- .force_encoding('UTF-8')
796
- .split("\n")
797
- .map { |line| line.gsub('\#', "\r\n") }
798
- .freeze
799
-
800
- scripts_translated_text = File.read("#{other_path}/scripts_trans.txt", mode: 'rb')
801
- .force_encoding('UTF-8')
802
- .split("\n")
803
- .map { |line| line.gsub('\#', "\r\n") }
804
- .freeze
805
-
806
- # Shuffle can possibly break the game in scripts, so no shuffling
807
-
808
- script_entries.each do |script|
809
- code = Zlib::Inflate.inflate(script[2]).force_encoding('UTF-8')
810
-
811
- scripts_original_text.zip(scripts_translated_text).each do |original, translated|
812
- # That may possibly break something, but until it does, who cares
813
- # Honestly, it needs to be changed to find quoted strings like in `extract_quoted_strings` method
814
- code.gsub!(original, translated) unless translated.nil?
815
- end
816
-
817
- script[2] = Zlib::Deflate.deflate(code, Zlib::BEST_COMPRESSION)
818
- end
819
-
820
- File.write("#{output_path}/#{File.basename(scripts_file)}", Marshal.dump(script_entries), mode: 'wb')
821
- end
822
-
823
- def self.serialize(engine, action, directory, original_directory)
824
- start_time = Time.now
825
-
826
- paths = {
827
- original_path: File.join(directory, original_directory),
828
- translation_path: File.join(directory, 'translation'),
829
- maps_path: File.join(directory, 'translation/maps'),
830
- other_path: File.join(directory, 'translation/other'),
831
- output_path: File.join(directory, 'output')
832
- }
833
-
834
- paths.each_value { |path| FileUtils.mkdir_p(path) }
835
-
836
- extensions = { ace: '.rvdata2', vx: '.rvdata', xp: '.rxdata' }.freeze
837
-
838
- files = Dir.children(paths[:original_path])
839
- .select { |filename| File.extname(filename) == extensions[engine] }
840
- .map { |filename| "#{paths[:original_path]}/#{filename}" }
841
- .freeze
842
-
843
- maps_files = []
844
- other_files = []
845
- system_file = "#{paths[:original_path]}/System#{extensions[engine]}".freeze
846
- scripts_file = "#{paths[:original_path]}/Scripts#{extensions[engine]}".freeze
847
-
848
- $game_type = get_game_type(system_file).freeze
849
-
850
- files.each do |file|
851
- basename = File.basename(file)
852
-
853
- if basename.start_with?(/Map[0-9]/)
854
- maps_files.push(file)
855
- elsif !basename.start_with?(/Map|Tilesets|Animations|System|Scripts|Areas/)
856
- other_files.push(file)
857
- end
858
- end
859
-
860
- if action == 'read'
861
- read_map(maps_files, paths[:maps_path]) unless $no[0]
862
- read_other(other_files, paths[:other_path]) unless $no[1]
863
- read_system(system_file, paths[:other_path]) unless $no[2]
864
- read_scripts(scripts_file, paths[:other_path]) unless $no[3]
865
- else
866
- write_map(maps_files, paths[:maps_path], paths[:output_path]) unless $no[0]
867
- write_other(other_files, paths[:other_path], paths[:output_path]) unless $no[1]
868
- write_system(system_file, paths[:other_path], paths[:output_path]) unless $no[2]
869
- write_scripts(scripts_file, paths[:other_path], paths[:output_path]) unless $no[3]
870
- end
871
-
872
- puts "Done in #{Time.now - start_time}"
873
- end
874
- end