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