Top Level Namespace

Defined Under Namespace

Modules: RGSS, RPG Classes: Color, Hash, Rect, Table, Tone

Constant Summary collapse

STRING_IS_ONLY_SYMBOLS_RE =
%r{^[.()+\-:;\[\]^~%&!№$@`*/→×??x%▼|♥♪!:〜『』「」〽。…‥=゠、,【】[]{}()〔〕⦅⦆〘〙〈〉《》・\\#'"<>=_ー※▶ⅠⅰⅡⅱⅢⅲⅣⅳⅤⅴⅥⅵⅦⅶⅧⅷⅨⅸⅩⅹⅪⅺⅫⅻⅬⅼⅭⅽⅮⅾⅯⅿ\s0-9]+$}
APPEND_FLAG_OMIT_MSG =
"Files aren't already parsed. Continuing as if --append flag was omitted."
ENCODINGS =
%w[
    ISO-8859-1
    Windows-1252
    Shift_JIS
    GB18030
    EUC-JP
    ISO-2022-JP
    BIG5
    EUC-KR
    Windows-1251
    KOI8-R
    UTF-8
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_parameter_translated(code, parameter, hashmap, game_type) ⇒ Object

Parameters:

  • code (Integer)
  • parameter (String)
  • hashmap (Hash{String => String})

    Translation hashmap (as everything in Ruby passed by reference, this pass is free!)

  • game_type (String)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/write.rb', line 10

def self.get_parameter_translated(code, parameter, hashmap, game_type)
    # @type [Array<String>]
    remaining_strings = []
    # @type [Array<Boolean>]
    # true - insert at end
    # false - insert at start
    insert_positions = []

    ends_with_if = parameter[/ if\(.*\)$/]

    if ends_with_if
        parameter = parameter.chomp(ends_with_if)
        remaining_strings.push(ends_with_if)
        insert_positions.push(true)
    end

    if game_type
        case game_type
        when 'lisa'
            case code
            when 401, 405
                prefix = parameter[/^(\\et\[[0-9]+\]|\\nbt)/]
                parameter = parameter.sub(prefix, '') if prefix

                remaining_strings.push(prefix)
                insert_positions.push(false)
            else
                nil
            end
            # Implement cases for other games
        else
            nil
        end
    end

    translated = hashmap[parameter]
    return nil if !translated || translated.empty?

    remaining_strings
        .zip(insert_positions)
        .each do |string, position|
            if !position
                translated = string + translated
            else
                translated += string
            end
        end

    translated
end

.get_variable_translated(variable, type, _filename, hashmap, _game_type) ⇒ String

Parameters:

  • variable (String)
  • type (Integer)
  • filename (String)
  • hashmap (Hash{String => String})

    Translation hashmap (as everything in Ruby passed by reference, this pass is free!)

  • _game_type (String)

Returns:

  • (String)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/write.rb', line 68

def self.get_variable_translated(variable, type, _filename, hashmap, _game_type)
    variable = variable.gsub(/\r?\n/, "\n")

    case type
    when 0 # name
    when 1 # nickname
    when 2 # description
    when 3 # note
    else
        nil
    end

    hashmap[variable]
end

.parse_list(list, allowed_codes, romanize, game_type, processing_mode, set, map) ⇒ Object

Parameters:

  • list (Array<Object>)
  • allowed_codes (Array<Integer>)
  • romanize (Boolean)
  • game_type (String)
  • processing_mode (Symbol)
  • set (Set<String>)
  • map (Hash{String => String})


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/read.rb', line 78

def self.parse_list(list, allowed_codes, romanize, game_type, processing_mode, set, map)
    in_sequence = false
    # @type [Array<String>]
    line = []

    list.each do |item|
        # @type [Integer]
        code = item.code

        if in_sequence && ![401, 405].include?(code)
            unless line.empty?
                joined = line.join("\n").strip.gsub("\n", '\#')
                parsed = parse_parameter(401, joined, game_type)

                if parsed
                    parsed = romanize_string(parsed) if romanize

                    map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)

                    set.add(parsed)
                end

                line.clear
            end

            in_sequence = false
        end

        next unless allowed_codes.include?(code)

        # @type [Array<String>]
        parameters = item.parameters

        case code
        when 401, 405
            # @type [String]
            parameter = parameters[0]
            next unless parameter.is_a?(String)

            parameter = convert_to_utf8(parameter)

            in_sequence = true
            line.push(parameter.gsub(' ', ' ').strip)
        when 102
            if parameters[0].is_a?(Array)
                parameters[0].each do |subparameter|
                    next unless subparameter.is_a?(String)

                    subparameter = subparameter.strip
                    next if subparameter.empty?

                    subparameter = convert_to_utf8(subparameter)
                    parsed = parse_parameter(code, subparameter, game_type)
                    next unless parsed

                    parsed = romanize_string(parsed) if romanize

                    map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)

                    set.add(parsed)
                end
            end
        when 356
            # @type [String]
            parameter = parameters[0]
            next unless parameter.is_a?(String)

            parameter = parameter.strip
            next if parameter.empty?

            parameter = convert_to_utf8(parameter)
            parsed = parse_parameter(code, parameter, game_type)
            next unless parsed

            parsed = romanize_string(parsed) if romanize

            map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)

            set.add(parsed)
        when 320, 324
            # @type [String]
            parameter = parameters[1]
            next unless parameter.is_a?(String)

            parameter = parameter.strip
            next if parameter.empty?

            parameter = convert_to_utf8(parameter)
            parsed = parse_parameter(code, parameter, game_type)
            next unless parsed

            parsed = romanize_string(parsed) if romanize

            map.insert_at_index(set.length, parsed, '') if processing_mode == :append && !map.include?(parsed)

            set.add(parsed)
        end
    end
end

.parse_parameter(code, parameter, game_type) ⇒ String | nil

Parameters:

  • code (Integer)
  • parameter (String)
  • game_type (String)

Returns:

  • (String | nil)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/read.rb', line 14

def self.parse_parameter(code, parameter, game_type)
    return nil if parameter.match?(STRING_IS_ONLY_SYMBOLS_RE)

    ends_with_if = parameter[/ if\(.*\)$/]

    parameter = parameter.chomp(ends_with_if) if ends_with_if

    if game_type
        case game_type
        when 'lisa'
            case code
            when 401, 405
                prefix = parameter[/^(\\et\[[0-9]+\]|\\nbt)/]
                parameter = parameter.sub(prefix, '') if prefix
            when 102
                # Implement some custom parsing
            when 356
                # Implement some custom parsing
            else
                return nil
            end
            # Implement cases for other games
        else
            nil
        end
    end

    parameter
end

.parse_variable(variable, type, _game_type) ⇒ String

Parameters:

  • variable (String)
  • type (Integer)
  • _game_type (String)

Returns:

  • (String)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/read.rb', line 48

def self.parse_variable(variable, type, _game_type)
    variable = variable.gsub(/\r?\n/, "\n")
    # for some reason it returns true if multi-line string contains carriage returns (wtf?)
    return nil if variable.match?(STRING_IS_ONLY_SYMBOLS_RE)

    if variable.split("\n").all? { |line| line.empty? || line.match?(/^#? ?<.*>.?$/) || line.match?(/^[a-z][0-9]$/) }
        return nil
    end

    return nil if variable.match?(/^[+-]?[0-9]+$/) || variable.match?(/---/) || variable.match?(/restrict eval/)

    case type
    when 0 # name
    when 1 # nickname
    when 2 # description
    when 3 # note
    else
        nil
    end

    variable
end

.read_ini_title(ini_file_path) ⇒ Object

Parameters:

  • ini_file_path (String)


422
423
424
425
426
427
428
429
430
# File 'lib/read.rb', line 422

def self.read_ini_title(ini_file_path)
    file_lines = File.readlines(ini_file_path, chomp: true)
    file_lines.each do |line|
        if line.downcase.start_with?('title')
            parts = line.partition('=')
            break parts[2].strip
        end
    end
end

.read_map(maps_files_paths, output_path, romanize, logging, game_type, processing_mode) ⇒ Object

Parameters:

  • maps_files_paths (Array<String>)

    Array of paths to original maps files

  • output_path (String)

    Path to output directory

  • romanize (Boolean)

    Whether to romanize text

  • logging (Boolean)

    Whether to log

  • game_type (String)

    Game type for custom processing

  • processing_mode (Symbol)

    Whether to read in default mode, force rewrite or append new text to existing files



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/read.rb', line 184

def self.read_map(maps_files_paths, output_path, romanize, logging, game_type, processing_mode)
    maps_output_path = File.join(output_path, 'maps.txt')
    names_output_path = File.join(output_path, 'names.txt')
    maps_trans_output_path = File.join(output_path, 'maps_trans.txt')
    names_trans_output_path = File.join(output_path, 'names_trans.txt')

    if processing_mode == :default && (File.exist?(maps_trans_output_path) || File.exist?(names_trans_output_path))
        puts 'maps_trans.txt or names_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.'
        return
    end

    maps_object_map = Hash[maps_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]

    # @type [Set<String>]
    maps_lines = Set.new
    # @type [Set<String>]
    names_lines = Set.new

    # @type [Hash{String => String}]
    maps_translation_map = {}
    # @type [Hash{String => String}]
    names_translation_map = {}

    if processing_mode == :append
        if File.exist?(maps_trans_output_path)
            maps_translation_map =
                Hash[
                    File.readlines(maps_output_path, encoding: 'utf-8', chomp: true).zip(
                        File.readlines(maps_trans_output_path, encoding: 'utf-8', chomp: true),
                    )
                ]
            names_translation_map =
                Hash[
                    File.readlines(names_output_path, encoding: 'utf-8', chomp: true).zip(
                        File.readlines(names_trans_output_path, encoding: 'utf-8', chomp: true),
                    )
                ]
        else
            puts APPEND_FLAG_OMIT_MSG
            processing_mode = :default
        end
    end

    # @type [Array<Integer>]
    # 401 - dialogue lines
    # 102 - dialogue choices array
    # 356 - system lines/special texts (do they even exist before mv?)
    allowed_codes = [102, 320, 324, 356, 401].freeze

    maps_object_map.each do |filename, object|
        # @type [String]
        display_name = object.display_name

        if display_name.is_a?(String)
            display_name = display_name.strip

            unless display_name.empty?
                display_name = romanize_string(display_name) if romanize

                if processing_mode == :append && !names_translation_map.include?(display_name)
                    names_translation_map.insert_at_index(names_lines.length, display_name, '')
                end

                names_lines.add(display_name)
            end
        end

        events = object.events
        next unless events

        events.each_value do |event|
            pages = event.pages
            next unless pages

            pages.each do |page|
                list = page.list
                next unless list

                parse_list(list, allowed_codes, romanize, game_type, processing_mode, maps_lines, maps_translation_map)
            end
        end

        puts "Parsed #{filename}" if logging
    end

    maps_original_content, maps_translated_content, names_original_content, names_translated_content =
        if processing_mode == :append
            [
                maps_translation_map.keys.join("\n"),
                maps_translation_map.values.join("\n"),
                names_translation_map.keys.join("\n"),
                names_translation_map.values.join("\n"),
            ]
        else
            [
                maps_lines.join("\n"),
                "\n" * (maps_lines.empty? ? 0 : maps_lines.length - 1),
                names_lines.join("\n"),
                "\n" * (names_lines.empty? ? 0 : names_lines.length - 1),
            ]
        end

    File.binwrite(maps_output_path, maps_original_content)
    File.binwrite(maps_trans_output_path, maps_translated_content)
    File.binwrite(names_output_path, names_original_content)
    File.binwrite(names_trans_output_path, names_translated_content)
end

.read_other(other_files_paths, output_path, romanize, logging, game_type, processing_mode) ⇒ Object

Parameters:

  • other_files_paths (Array<String>)
  • output_path (String)
  • romanize (Boolean)

    Whether to romanize text

  • logging (Boolean)

    Whether to log

  • game_type (String)

    Game type for custom processing

  • processing_mode (Symbol)

    Whether to read in default mode, force rewrite or append new text to existing files



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/read.rb', line 299

def self.read_other(other_files_paths, output_path, romanize, logging, game_type, processing_mode)
    other_object_array_map = Hash[other_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]

    inner_processing_mode = processing_mode

    # @type [Array<Integer>]
    # 401 - dialogue lines
    # 405 - credits lines
    # 102 - dialogue choices array
    # 356 - system lines/special texts (do they even exist before mv?)
    allowed_codes = [102, 320, 324, 356, 401, 405].freeze

    other_object_array_map.each do |filename, other_object_array|
        processed_filename = File.basename(filename, '.*').downcase

        other_output_path = File.join(output_path, "#{processed_filename}.txt")
        other_trans_output_path = File.join(output_path, "#{processed_filename}_trans.txt")

        if processing_mode == :default && File.exist?(other_trans_output_path)
            puts "#{processed_filename}_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."
            next
        end

        # @type [Set<String>]
        other_lines = Set.new
        # @type [Hash{String => String}]
        other_translation_map = {}

        if processing_mode == :append
            if File.exist?(other_trans_output_path)
                inner_processing_mode = :append
                other_translation_map =
                    Hash[
                        File.readlines(other_output_path, encoding: 'utf-8', chomp: true).zip(
                            File.readlines(other_trans_output_path, encoding: 'utf-8', chomp: true),
                        )
                    ]
            else
                puts APPEND_FLAG_OMIT_MSG
                inner_processing_mode = :default
            end
        end

        if !filename.match?(/^(Common|Troops)/)
            other_object_array.each do |object|
                next unless object

                # @type [Array<String | nil>]
                attributes = [
                    object.name,
                    object.is_a?(RPG::Actor) ? object.nickname : nil,
                    object.description,
                    object.note,
                    object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message1 : nil,
                    object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message2 : nil,
                    object.is_a?(RPG::State) ? object.message3 : nil,
                    object.is_a?(RPG::State) ? object.message4 : nil,
                ]

                attributes.each_with_index do |var, type|
                    next unless var.is_a?(String)

                    var = var.strip
                    next if var.empty?

                    var = convert_to_utf8(var)
                    parsed = parse_variable(var, type, game_type)

                    unless parsed
                        break if type.zero?
                        next
                    end

                    parsed = romanize_string(parsed) if romanize
                    parsed = parsed.split("\n").map(&:strip).join('\#')

                    if inner_processing_mode == :append && !other_translation_map.include?(parsed)
                        other_translation_map.insert_at_index(other_lines.length, parsed, '')
                    end

                    other_lines.add(parsed)
                end
            end
        else
            other_object_array.each do |object|
                next unless object

                pages = object.pages
                pages_length = !pages ? 1 : pages.length

                (0..pages_length).each do |i|
                    list = !pages ? object.list : pages[i].instance_variable_get(:@list)
                    next unless list

                    parse_list(
                        list,
                        allowed_codes,
                        romanize,
                        game_type,
                        inner_processing_mode,
                        other_lines,
                        other_translation_map,
                    )
                end
            end
        end

        puts "Parsed #{filename}" if logging

        original_content, translated_content =
            if processing_mode == :append
                [other_translation_map.keys.join("\n"), other_translation_map.values.join("\n")]
            else
                [other_lines.join("\n"), "\n" * (other_lines.empty? ? 0 : other_lines.length - 1)]
            end

        File.binwrite(other_output_path, original_content)
        File.binwrite(other_trans_output_path, translated_content)
    end
end

.read_scripts(scripts_file_path, output_path, romanize, logging, processing_mode) ⇒ Object

Parameters:

  • scripts_file_path (String)
  • output_path (String)
  • romanize (Boolean)

    Whether to romanize text

  • logging (Boolean)

    Whether to log

  • processing_mode (Symbol)

    Whether to read in default mode, force rewrite or append new text to existing files



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/read.rb', line 579

def self.read_scripts(scripts_file_path, output_path, romanize, logging, processing_mode)
    scripts_filename = File.basename(scripts_file_path)
    scripts_basename = File.basename(scripts_file_path, '.*').downcase

    scripts_plain_output_path = File.join(output_path, "#{scripts_basename}_plain.txt")
    scripts_output_path = File.join(output_path, "#{scripts_basename}.txt")
    scripts_trans_output_path = File.join(output_path, "#{scripts_basename}_trans.txt")

    if processing_mode == :default && File.exist?(scripts_trans_output_path)
        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.'
        return
    end

    script_entries = Marshal.load(File.binread(scripts_file_path))

    # @type [Set<String>]
    scripts_lines = Set.new
    # @type [Hash{String => String}]
    scripts_translation_map = {}

    if processing_mode == :append
        if File.exist?(scripts_trans_output_path)
            scripts_translation_map =
                Hash[
                    File.readlines(scripts_output_path, encoding: 'utf-8', chomp: true).zip(
                        File.readlines(scripts_trans_output_path, encoding: 'utf-8', chomp: true),
                    )
                ]
        else
            puts APPEND_FLAG_OMIT_MSG
            processing_mode = :default
        end
    end

    # @type [Array<String>]
    codes_content = []

    # This code was fun before `that` game used Windows-1252 degree symbol
    script_entries.each do |script|
        # @type [String]
        code = Zlib::Inflate.inflate(script[2])
        code = convert_to_utf8(code)
        codes_content.push(code)
    end

    extracted = extract_strings(codes_content.join)

    extracted.each do |string|
        # Removes the U+3000 Japanese typographical space to check if string, when stripped, is truly empty
        string = string.gsub(' ', ' ').strip

        next if string.empty?

        if string.match?(%r{(Graphics|Data|Audio|Movies|System)/.*/?}) || string.match?(/r[xv]data2?$/) ||
                  string.match?(STRING_IS_ONLY_SYMBOLS_RE) || string.match?(/@window/) || string.match?(/\$game/) ||
                  string.match?(/_/) || string.match?(/^\\e/) || string.match?(/.*\(/) ||
                  string.match?(/^([d\d\p{P}+-]*|[d\p{P}+-]*)$/) || string.match?(/ALPHAC/) ||
                  string.match?(
                      /^(Actor<id>|ExtraDropItem|EquipLearnSkill|GameOver|Iconset|Window|true|false|MActor%d|[wr]b|\\f|\\n|\[[A-Z]*\])$/,
                  )
            next
        end

        string = romanize_string(string) if romanize

        if processing_mode == :append && !scripts_translation_map.include?(string)
            scripts_translation_map.insert_at_index(scripts_lines.length, string, '')
        end

        scripts_lines.add(string)
    end

    puts "Parsed #{scripts_filename}" if logging

    File.binwrite(scripts_plain_output_path, codes_content.join("\n"))

    original_content, translated_content =
        if processing_mode == :append
            [scripts_translation_map.keys.join("\n"), scripts_translation_map.values.join("\n")]
        else
            [scripts_lines.join("\n"), "\n" * (scripts_lines.empty? ? 0 : scripts_lines.length - 1)]
        end

    File.binwrite(scripts_output_path, original_content)
    File.binwrite(scripts_trans_output_path, translated_content)
end

.read_system(system_file_path, ini_file_path, output_path, romanize, logging, processing_mode) ⇒ Object

Parameters:

  • system_file_path (String)
  • ini_file_path (String)
  • output_path (String)
  • romanize (Boolean)

    Whether to romanize text

  • logging (Boolean)

    Whether to log

  • processing_mode (Symbol)

    Whether to read in default mode, force rewrite or append new text to existing files



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/read.rb', line 438

def self.read_system(system_file_path, ini_file_path, output_path, romanize, logging, processing_mode)
    system_filename = File.basename(system_file_path)
    system_basename = File.basename(system_file_path, '.*').downcase

    system_output_path = File.join(output_path, "#{system_basename}.txt")
    system_trans_output_path = File.join(output_path, "#{system_basename}_trans.txt")

    if processing_mode == :default && File.exist?(system_trans_output_path)
        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.'
        return
    end

    system_object = Marshal.load(File.binread(system_file_path))

    # @type [Set<String>]
    system_lines = Set.new
    # @type [Hash{String => String}]
    system_translation_map = {}

    if processing_mode == :append
        if File.exist?(system_trans_output_path)
            system_translation_map =
                Hash[
                    File.readlines(system_output_path, encoding: 'utf-8', chomp: true).zip(
                        File.readlines(system_trans_output_path, encoding: 'utf-8', chomp: true),
                    )
                ]
        else
            puts APPEND_FLAG_OMIT_MSG
            processing_mode = :default
        end
    end

    elements = system_object.elements
    skill_types = system_object.skill_types
    weapon_types = system_object.weapon_types
    armor_types = system_object.armor_types
    currency_unit = system_object.currency_unit
    terms = system_object.terms || system_object.words

    [elements, skill_types, weapon_types, armor_types].each do |array|
        next unless array

        array.each do |string|
            next unless string.is_a?(String)

            string = string.strip
            next if string.empty?

            string = convert_to_utf8(string)
            string = romanize_string(string) if romanize

            if processing_mode == :append && !system_translation_map.include?(string)
                system_translation_map.insert_at_index(system_lines.length, string, '')
            end

            system_lines.add(string)
        end
    end

    if currency_unit.is_a?(String)
        currency_unit = currency_unit.strip

        unless currency_unit.empty?
            currency_unit = convert_to_utf8(currency_unit)
            currency_unit = romanize_string(currency_unit) if romanize

            if processing_mode == :append && !system_translation_map.include?(currency_unit)
                system_translation_map.insert_at_index(system_lines.length, currency_unit, '')
            end

            system_lines.add(currency_unit)
        end
    end

    terms.instance_variables.each do |variable|
        value = terms.instance_variable_get(variable)

        if value.is_a?(String)
            value = value.strip

            unless value.empty?
                value = convert_to_utf8(value)
                value = romanize_string(value) if romanize

                if processing_mode == :append && !system_translation_map.include?(value)
                    system_translation_map.insert_at_index(system_lines.length, value, '')
                end

                system_lines.add(value)
            end
        elsif value.is_a?(Array)
            value.each do |string|
                next unless string.is_a?(String)

                string = string.strip
                next if string.empty?

                string = convert_to_utf8(string)
                string = romanize_string(string) if romanize

                if processing_mode == :append && !system_translation_map.include?(string)
                    system_translation_map.insert_at_index(system_lines.length, string, '')
                end

                system_lines.add(string)
            end
        end
    end

    # Game title from System file and ini file may differ, but requesting user request to determine which line do they
    # want is LAME
    # So just throw that ini ass and continue
    ini_game_title = read_ini_title(ini_file_path).strip
    ini_game_title = romanize_string(ini_game_title) if romanize

    if processing_mode == :append && !system_translation_map.include?(ini_game_title)
        system_translation_map.insert_at_index(system_lines.length, ini_game_title, '')
    end

    system_lines.add(ini_game_title)

    puts "Parsed #{system_filename}" if logging

    original_content, translated_content =
        if processing_mode == :append
            [system_translation_map.keys.join("\n"), system_translation_map.values.join("\n")]
        else
            [system_lines.join("\n"), "\n" * (system_lines.empty? ? 0 : system_lines.length - 1)]
        end

    File.binwrite(system_output_path, original_content)
    File.binwrite(system_trans_output_path, translated_content)
end

.romanize_string(input_string) ⇒ String

Parameters:

  • input_string (String)

Returns:

  • (String)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/extensions.rb', line 5

def self.romanize_string(input_string)
    # @type [Array<String>]
    result = []

    input_string.each_char do |char|
        result << case char
        when ''
            '.'
        when '', ''
            ','
        when ''
            '·'
        when ''
            ''
        when '', ''
            ''
        when '', ''
            '...'
        when '', '', '', ''
            "'"
        when '', '', '', ''
            '"'
        when '', '', '', ''
            '('
        when '', '', '', ''
            ')'
        when ''
            '{'
        when ''
            '}'
        when '', '', '', ''
            '['
        when '', '', '', ''
            ']'
        when ''
            '~'
        when ''
            '?'
        when ''
            ':'
        when ''
            '!'
        when ''
            '*'
        when ' '
            ' '
        when ''
            'I'
        when ''
            'i'
        when ''
            'II'
        when ''
            'ii'
        when ''
            'III'
        when ''
            'iii'
        when ''
            'IV'
        when ''
            'iv'
        when ''
            'V'
        when ''
            'v'
        when ''
            'VI'
        when ''
            'vi'
        when ''
            'VII'
        when ''
            'vii'
        when ''
            'VIII'
        when ''
            'viii'
        when ''
            'IX'
        when ''
            'ix'
        when ''
            'X'
        when ''
            'x'
        when ''
            'XI'
        when ''
            'xi'
        when ''
            'XII'
        when ''
            'xii'
        when ''
            'L'
        when ''
            'l'
        when ''
            'C'
        when ''
            'c'
        when ''
            'D'
        when ''
            'd'
        when ''
            'M'
        when ''
            'm'
        else
            char
        end
    end

    result.join
end

.shuffle_words(array) ⇒ Array<String>

Returns Array of shuffled strings.

Parameters:

  • array (Array<String>)

    Array of strings

Returns:

  • (Array<String>)

    Array of shuffled strings



125
126
127
128
129
130
131
# File 'lib/extensions.rb', line 125

def self.shuffle_words(array)
    array.each do |string|
        select_words_re = /\S+/
        words = string.scan(select_words_re).shuffle
        string.gsub(select_words_re) { words.pop || '' }
    end
end

.write_ini_title(ini_file_path, translated) ⇒ Object

Parameters:

  • ini_file_path (String)
  • translated (String)


472
473
474
475
476
477
478
479
# File 'lib/write.rb', line 472

def self.write_ini_title(ini_file_path, translated)
    file_lines = File.readlines(ini_file_path, chomp: true)
    title_line_index = file_lines.each_with_index { |line, i| break i if line.downcase.start_with?('title') }
    return if title_line_index.is_a?(Array)

    file_lines[title_line_index] = "title =#{translated}"
    File.binwrite(ini_file_path, file_lines.join("\n"))
end

.write_map(original_files_paths, maps_path, output_path, shuffle_level, romanize, logging, game_type) ⇒ Object

Parameters:

  • original_files_paths (Array<String>)

    Array of paths to original files

  • maps_path (String)

    Path to directory containing .txt maps files

  • output_path (String)

    Path to output directory

  • shuffle_level (Integer)

    Level of shuffle

  • romanize (Boolean)

    If files were read with romanize, this option will romanize original game text to compare with parsed

  • logging (Boolean)

    Whether to log

  • game_type (String)

    Game type for custom parsing



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/write.rb', line 90

def self.write_map(original_files_paths, maps_path, output_path, shuffle_level, romanize, logging, game_type)
    maps_object_map = Hash[original_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]

    # @type [Array<String>]
    maps_original_text =
        File
            .readlines(File.join(maps_path, 'maps.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\n").strip }
            .freeze

    # @type [Array<String>]
    names_original_text =
        File
            .readlines(File.join(maps_path, 'names.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\n").strip }
            .freeze

    # @type [Array<String>]
    maps_translated_text =
        File
            .readlines(File.join(maps_path, 'maps_trans.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\n").strip }

    # @type [Array<String>]
    names_translated_text =
        File
            .readlines(File.join(maps_path, 'names_trans.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\n").strip }

    if shuffle_level.positive?
        maps_translated_text.shuffle!
        names_translated_text.shuffle!

        if shuffle_level == 2
            maps_translated_text = shuffle_words(maps_translated_text)
            names_translated_text = shuffle_words(names_translated_text)
        end
    end

    # @type [Hash{String => String}]
    maps_translation_map = Hash[maps_original_text.zip(maps_translated_text)].freeze
    # @type [Hash{String => String}]
    names_translation_map = Hash[names_original_text.zip(names_translated_text)].freeze

    # @type [Array<Integer>]
    # 401 - dialogue lines
    # 102 - dialogue choices array
    # 402 - one of the dialogue choices from the array
    # 356 - system lines/special texts (do they even exist before mv?)
    allowed_codes = [102, 320, 324, 356, 401, 402].freeze

    maps_object_map.each do |filename, object|
        # @type [String]
        display_name = object.display_name
        display_name_translated = names_translation_map[display_name]
        object.display_name = display_name_translated if display_name_translated

        events = object.events
        next unless events

        events.each do |ev, event|
            pages = event.pages
            next unless pages

            pages.each_with_index do |page, pg|
                list = page.list
                next unless list

                in_sequence = false
                # @type [Array<String>]
                line = []
                # @type [Array<Integer>]
                item_indices = []

                list.each_with_index do |item, it|
                    # @type [Integer]
                    code = item.code

                    if in_sequence && code != 401
                        unless line.empty?
                            joined = line.join("\n").strip
                            joined = romanize_string(joined) if romanize

                            translated = get_parameter_translated(401, joined, maps_translation_map, game_type)

                            if translated && !translated.empty?
                                split = translated.split('\#')

                                split_length = split.length
                                line_length = line.length

                                item_indices.each_with_index { |index, i| list[index].parameters[0] = i < split_length ? split[i] : '' }

                                if split_length > line_length
                                    list[item_indices.last].parameters[0] = split[line_length - 1..].join("\n")
                                end
                            end

                            line.clear
                        end

                        in_sequence = false
                    end

                    next unless allowed_codes.include?(code)

                    # @type [Array<String>]
                    parameters = item.parameters

                    case code
                    when 401
                        # @type [String]
                        parameter = parameters[0]
                        next unless parameter.is_a?(String)

                        parameter = convert_to_utf8(parameter)

                        in_sequence = true
                        line.push(parameter.gsub(' ', ' ').strip)
                        item_indices.push(it)
                    when 102
                        parameters[0].each_with_index do |subparameter, sp|
                            next unless subparameter.is_a?(String)

                            subparameter = subparameter.strip
                            next if subparameter.empty?

                            subparameter = convert_to_utf8(subparameter)
                            subparameter = romanize_string(subparameter.strip) if romanize

                            translated = get_parameter_translated(code, subparameter, maps_translation_map, game_type)
                            parameters[0][sp] = translated if translated && !translated.empty?
                        end
                    when 356
                        # @type [String]
                        parameter = parameters[0]
                        next unless parameter.is_a?(String)

                        parameter = parameter.strip
                        next if parameter.empty?

                        parameter = convert_to_utf8(parameter)
                        parameter = romanize_string(parameter) if romanize

                        translated = get_parameter_translated(code, parameter, maps_translation_map, game_type)
                        parameters[0] = translated if translated && !translated.empty?
                    when 320, 324, 402
                        # @type [String]
                        parameter = parameters[1]
                        next unless parameter.is_a?(String)

                        parameter = parameter.strip
                        next if parameter.empty?

                        parameter = convert_to_utf8(parameter)
                        parameter = romanize_string(parameters[1].strip) if romanize

                        translated = get_parameter_translated(code, parameter, maps_translation_map, game_type)
                        parameters[1] = translated if translated && !translated.empty?
                    end

                    item.parameters = parameters
                    list[it] = item
                end

                page.list = list
                pages[pg] = page
            end

            event.pages = pages
            events[ev] = event
        end

        object.events = events

        File.binwrite(File.join(output_path, filename), Marshal.dump(object))
        puts "Written #{filename}" if logging
    end
end

.write_other(original_files_paths, other_path, output_path, shuffle_level, romanize, logging, game_type) ⇒ Object

Parameters:

  • original_files (Array<String>)
  • other_path (String)
  • output_path (String)
  • shuffle_level (Integer)

    Level of shuffle

  • romanize (Boolean)

    If files were read with romanize, this option will romanize original game text to compare with parsed

  • logging (Boolean)

    Whether to log

  • game_type (String)

    Game type for custom parsing



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/write.rb', line 277

def self.write_other(original_files_paths, other_path, output_path, shuffle_level, romanize, logging, game_type)
    other_object_array_map = Hash[original_files_paths.map { |f| [File.basename(f), Marshal.load(File.binread(f))] }]

    # @type [Array<String>]
    # 401 - dialogue lines
    # 405 - credits lines
    # 102 - dialogue choices array
    # 402 - one of the dialogue choices from the array
    # 356 - system lines/special texts (do they even exist before mv?)
    allowed_codes = [102, 320, 324, 356, 401, 402, 405].freeze

    other_object_array_map.each do |filename, other_object_array|
        other_filename = File.basename(filename, '.*').downcase

        # @type [Array<String>]
        other_original_text =
            File
                .readlines(File.join(other_path, "#{other_filename}.txt"), encoding: 'UTF-8', chomp: true)
                .map { |line| line.gsub('\#', "\n").strip }

        # @type [Array<String>]
        other_translated_text =
            File
                .readlines(File.join(other_path, "#{other_filename}_trans.txt"), encoding: 'UTF-8', chomp: true)
                .map { |line| line.gsub('\#', "\n").strip }

        if shuffle_level.positive?
            other_translated_text.shuffle!
            other_translated_text = shuffle_words(other_translated_text) if shuffle_level == 2
        end

        # @type [Hash{String => String}]
        other_translation_map = Hash[other_original_text.zip(other_translated_text)].freeze

        if !filename.start_with?(/Common|Troops/)
            other_object_array.each do |object|
                next unless object

                variables = [
                    object.name,
                    object.is_a?(RPG::Actor) ? object.nickname : nil,
                    object.description,
                    object.note,
                    object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message1 : nil,
                    object.is_a?(RPG::Skill) || object.is_a?(RPG::State) ? object.message2 : nil,
                    object.is_a?(RPG::State) ? object.message3 : nil,
                    object.is_a?(RPG::State) ? object.message4 : nil,
                ]

                attributes = %i[name nickname description note message1 message2 message3 message4]

                variables.each_with_index do |var, type|
                    next unless var.is_a?(String)

                    var = var.strip
                    next if var.empty?

                    var = convert_to_utf8(var)
                    var = romanize_string(var) if romanize
                    var = var.split("\n").map(&:strip).join("\n")

                    translated = get_variable_translated(var, type, filename, other_translation_map, game_type)

                    object.send("#{attributes[type]}=", translated) if translated && !translated.empty?
                end
            end
        else
            other_object_array.each_with_index do |object, obj|
                next unless object

                pages = object.pages
                pages_length = !pages ? 1 : pages.length

                (0..pages_length).each do |pg|
                    list = !pages ? object.list : pages[pg].instance_variable_get(:@list) # for some reason .list access doesn't work (wtf?)
                    next unless list

                    in_sequence = false
                    # @type [Array<String>]
                    line = []
                    # @type [Array<Integer>]
                    item_indices = []

                    list.each_with_index do |item, it|
                        # @type [Integer]
                        code = item.code

                        if in_sequence && ![401, 405].include?(code)
                            unless line.empty?
                                joined = line.join("\n").strip
                                joined = romanize_string(joined) if romanize

                                translated = get_parameter_translated(401, joined, other_translation_map, game_type)

                                if translated && !translated.empty?
                                    split = translated.split('\#')

                                    split_length = split.length
                                    line_length = line.length

                                    item_indices.each_with_index do |index, i|
                                        list[index].parameters[0] = i < split_length ? split[i] : ''
                                    end

                                    if split_length > line_length
                                        list[item_indices.last].parameters[0] = split[line_length - 1..].join("\n")
                                    end
                                end

                                line.clear
                            end

                            in_sequence = false
                        end

                        next unless allowed_codes.include?(code)

                        # @type [Array<String>]
                        parameters = item.parameters

                        case code
                        when 401, 405
                            # @type [String]
                            parameter = parameters[0]
                            next unless parameter.is_a?(String)

                            parameter = convert_to_utf8(parameter)

                            in_sequence = true
                            line.push(parameter.gsub(' ', ' ').strip)
                            item_indices.push(it)
                        when 102
                            parameters[0].each_with_index do |subparameter, sp|
                                next unless subparameter.is_a?(String)

                                subparameter = subparameter.strip
                                next if subparameter.empty?

                                subparameter = convert_to_utf8(subparameter)
                                subparameter = romanize_string(subparameter) if romanize

                                translated = get_parameter_translated(code, subparameter, other_translation_map, game_type)
                                parameters[0][sp] = translated if translated && !translated.empty?
                            end
                        when 356
                            # @type [String]
                            parameter = parameters[0]
                            next unless parameter.is_a?(String)

                            parameter = parameter.strip
                            next if parameter.empty?

                            parameter = convert_to_utf8(parameter)
                            parameter = romanize_string(parameter) if romanize

                            translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
                            parameters[0] = translated if translated && !translated.empty?
                        when 320, 324, 402
                            # @type [String]
                            parameter = parameters[1]
                            next unless parameter.is_a?(String)

                            parameter = parameter.strip
                            next if parameter.empty?

                            parameter = convert_to_utf8(parameter)
                            parameter = romanize_string(parameter) if romanize

                            translated = get_parameter_translated(code, parameter, other_translation_map, game_type)
                            parameters[1] = translated if translated && !translated.empty?
                        end

                        item.parameters = parameters
                        list[it] = item
                    end

                    if !pages
                        object.list = list
                    else
                        pages[pg].instance_variable_set(:@list, list)
                        object.pages = pages
                    end
                end

                other_object_array[obj] = object
            end
        end

        File.binwrite(File.join(output_path, filename), Marshal.dump(other_object_array))
        puts "Written #{filename}" if logging
    end
end

.write_scripts(scripts_file_path, other_path, output_path, romanize, logging) ⇒ Object

Parameters:

  • scripts_file_path (String)

    Path to Scripts.*data file

  • other_path (String)

    Path to translation/other directory containing .txt files

  • output_path (String)

    Path to the output directory

  • romanize (Boolean)

    If files were read with romanize, this option will romanize original game text to compare with parsed

  • logging (Boolean)

    Whether to log



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/write.rb', line 599

def self.write_scripts(scripts_file_path, other_path, output_path, romanize, logging)
    scripts_basename = File.basename(scripts_file_path)
    script_entries = Marshal.load(File.binread(scripts_file_path))

    # @type [Array<String>]
    scripts_original_text =
        File
            .readlines(File.join(other_path, 'scripts.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\r\n") }
    # @type [Array<String>]
    scripts_translated_text =
        File
            .readlines(File.join(other_path, 'scripts_trans.txt'), encoding: 'UTF-8', chomp: true)
            .map { |line| line.gsub('\#', "\r\n") }

    # @type [Hash{String => String}]
    scripts_translation_map = Hash[scripts_original_text.zip(scripts_translated_text)]

    script_entries.each do |script|
        # @type [String]
        code = Zlib::Inflate.inflate(script[2])
        code = convert_to_utf8(code)

        string_array, index_array = extract_strings(code, mode: true)

        string_array
            .zip(index_array)
            .reverse_each do |string, index|
                string = string.gsub(' ', '').strip
                next if string.empty? || !scripts_translation_map.include?(string)

                string = romanize_string(string) if romanize

                translated = scripts_translation_map[string]
                code[index, string.length] = translated if translated && !translated.empty?
            end

        script[2] = Zlib::Deflate.deflate(code, Zlib::BEST_COMPRESSION)
    end

    File.binwrite(File.join(output_path, scripts_basename), Marshal.dump(script_entries))
    puts "Written #{scripts_basename}" if logging
end

.write_system(system_file_path, ini_file_path, other_path, output_path, shuffle_level, romanize, logging) ⇒ Object

Parameters:

  • system_file_path (String)
  • ini_file_path (String)
  • other_path (String)
  • output_path (String)
  • shuffle_level (Integer)

    Level of shuffle

  • romanize (Boolean)

    If files were read with romanize, this option will romanize original game text to compare with parsed

  • logging (Boolean)

    Whether to log



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/write.rb', line 489

def self.write_system(system_file_path, ini_file_path, other_path, output_path, shuffle_level, romanize, logging)
    system_basename = File.basename(system_file_path)

    # @type [System]
    system_object = Marshal.load(File.binread(system_file_path))

    # @type [Array<String>]
    system_original_text =
        File.readlines(File.join(other_path, 'system.txt'), encoding: 'UTF-8', chomp: true).map(&:strip).freeze

    # @type [Array<String>]
    system_translated_text =
        File.readlines(File.join(other_path, 'system_trans.txt'), encoding: 'UTF-8', chomp: true).map(&:strip)

    if shuffle_level.positive?
        system_translated_text.shuffle!
        system_translated_text = shuffle_words(system_translated_text) if shuffle_level == 2
    end

    # @type [Hash{String => String}]
    system_translation_map = Hash[system_original_text.zip(system_translated_text)].freeze

    elements = system_object.elements
    skill_types = system_object.skill_types
    weapon_types = system_object.weapon_types
    armor_types = system_object.armor_types
    currency_unit = system_object.currency_unit
    terms_vocabulary = system_object.terms || system_object.words

    arrays = [elements, skill_types, weapon_types, armor_types]
    attributes = %i[elements skill_types weapon_types armor_types]

    arrays
        .zip(attributes)
        .each do |array, attr|
            next unless array.is_a?(Array)

            array.each_with_index do |string, i|
                string = string.strip
                next if string.empty?

                string = convert_to_utf8(string)
                string = romanize_string(string) if romanize

                translated = system_translation_map[string]
                array[i] = translated if translated && !translated.empty?
            end

            system_object.send("#{attr}=", array)
        end

    if currency_unit
        currency_unit = romanize_string(currency_unit) if romanize
        currency_unit_translated = system_translation_map[currency_unit]
        system_object.currency_unit = currency_unit_translated if currency_unit.is_a?(String) && currency_unit_translated &&
            !currency_unit_translated.empty?
    end

    terms_vocabulary.instance_variables.each do |variable|
        # @type [String | Array<String>]
        value = terms_vocabulary.instance_variable_get(variable)

        if value.is_a?(String)
            value = value.strip
            next if value.empty?

            value = convert_to_utf8(value)
            value = romanize_string(value) if romanize

            translated = system_translation_map[value]
            value = translated if translated && !translated.empty?
        elsif value.is_a?(Array)
            value.each_with_index do |string, i|
                string = string.strip
                next if string.empty?

                string = convert_to_utf8(string)
                string = romanize_string(string) if romanize

                translated = system_translation_map[string]
                value[i] = translated if translated && !translated.empty?
            end
        end

        terms_vocabulary.instance_variable_set(variable, value)
    end

    if !system_object.terms
        system_object.words = terms_vocabulary
    else
        system_object.terms = terms_vocabulary
    end

    game_title_translated = system_translated_text.last

    if game_title_translated && !game_title_translated.empty?
        system_object.game_title = game_title_translated
        write_ini_title(ini_file_path, game_title_translated)
    end

    File.binwrite(File.join(output_path, system_basename), Marshal.dump(system_object))
    puts "Written #{system_basename}" if logging
end

Instance Method Details

#convert_to_utf8(input_string) ⇒ String

Parameters:

  • input_string (String)

Returns:

  • (String)


224
225
226
227
228
229
230
231
232
# File 'lib/extensions.rb', line 224

def convert_to_utf8(input_string)
    ENCODINGS.each do |encoding|
        return input_string.encode('UTF-8', encoding)
    rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
        next
    end

    raise EncodingError("Cannot convert string #{input_string} to UTF-8")
end

#escaped?(line, index) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
# File 'lib/extensions.rb', line 133

def escaped?(line, index)
    backslash_count = 0

    (0..index).reverse_each do |i|
        break if line[i] != '\\'
        backslash_count += 1
    end

    backslash_count.even?
end

#extract_strings(ruby_code, mode: false) ⇒ Object

Parameters:

  • ruby_code (String)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/extensions.rb', line 145

def extract_strings(ruby_code, mode: false)
    strings = mode ? [] : Set.new
    indices = []
    inside_string = false
    inside_multiline_comment = false
    string_start_index = 0
    current_quote_type = ''

    global_index = 0
    ruby_code.each_line do |line|
        stripped = line.strip

        unless inside_string
            if stripped[0] == '#'
                global_index += line.length
                next
            end

            if stripped.start_with?('=begin')
                inside_multiline_comment = true
            elsif stripped.start_with?('=end')
                inside_multiline_comment = false
            end
        end

        if inside_multiline_comment
            global_index += line.length
            next
        end

        i = 0
        while i < line.length
            char = line[i]

            break if !inside_string && char == '#'

            if !inside_string && %w[" '].include?(char)
                inside_string = true
                string_start_index = global_index + i
                current_quote_type = char
            elsif inside_string && char == current_quote_type && escaped?(line, i - 1)
                extracted_string = ruby_code[string_start_index + 1...global_index + i].gsub(/\r?\n/, '\#')

                if mode
                    strings << extracted_string
                    indices << string_start_index + 1
                else
                    strings.add(extracted_string)
                end

                inside_string = false
                current_quote_type = ''
            end

            i += 1
        end

        global_index += line.length
    end

    mode ? [strings, indices] : strings.to_a
end