def create_lagoon_x_patch(orig_data):
    BYTES_NOP = b'\x4e\x71'

    patch = Patch()

    orig_reader = io.BytesIO(orig_data)

    # NOP something... searches for the end of the current string. Need to search by bytes instead of words.
    patch.add_record(0x06aa, BYTES_NOP * 2)

    # NOP something else... I believe this one renders dialog windows.
    patch.add_record(0xa4ca, BYTES_NOP * 2)

    # I think this bit renders the glyph to the text buffer?
    patch.add_record(0xa864, BYTES_NOP)   # Increment the destination less.
    patch.add_record(0xa869, b'\x7f')     # Offset to start of next glyph.
    patch.add_record(0xa870, b'\xf7\xff') # Offset back to the top, I think?

    # This is in a routine that fills in an opaque background behind each glyph.
    # We need to shift the math around until it only writes background behind
    # one 8x16 glyph instead of two. There were originally two incrementing
    # OR operations in a row to fill two bytes; we replace the first one with
    # "ADD.L #1,A1" and then a NOP.
    patch.add_record(0xaa3a, b'\x52\x89' + BYTES_NOP)


    # The code around 0xa650 is counting the number of characters in a string
    # to center it and fit a window around it. We need to count bytes instead
    # of words to be sure we don't miss a terminator. But that also means we
    # need to halve the number of bytes, because the characters are half-width
    # now.
    # So, first, shift the block from 0x8654 to 0x867f back four bytes. That
    # eliminates a double increment, and also makes room for a shift operation.
    patch.add_record(0xa650, orig_data[0xa654:0xa680])

    # By doing that, we bork a bunch of branch instructions in that area, so
    # go back and correct those destinations.
    patch.add_record(0xa63f, b'\x3c')
    patch.add_record(0xa64f, b'\x24')
    patch.add_record(0xa65b, b'\xee')
    patch.add_record(0xa66f, b'\xda')
    patch.add_record(0xa673, b'\xd6')
    patch.add_record(0xa677, b'\xc4')
    patch.add_record(0xa67b, b'\xc0')

    # Now in the four bytes we freed up with that shift, we insert a right-shift
    # that halves the value of the counter ("ASR.W #$01,D1"), followed by a NOP.
    patch.add_record(0xa67c, b'\xe2\x41' + BYTES_NOP)

    # We hack the initialization of that counter to start at 1 instead of zero...
    # that should effectively make the right-shift round the value up instead of down.
    patch.add_record(0xa641, b'\x01')

    # And then there's one more double increment around there that needs to be NOPed.
    patch.add_record(0xa6a8, BYTES_NOP * 2)


    # Control codes!

    # 0x7371 through 0x7379 change the text color. Remapping those to 0xc9 through 0xd2.
    # 0xa6ac does comparisons with it.
    patch.add_record(0xa6ae, b'\x00\xc9')
    patch.add_record(0xa6b4, b'\x00\xd2')
    patch.add_record(0xa6ba, b'\x00\xc9')

    # Same thing when counting characters...
    patch.add_record(0xa652, b'\x00\xc9')
    patch.add_record(0xa658, b'\x00\xd2')

    # Other control codes... I believe these all change portrait animations.
    patch.add_record(0xa6c6, b'\x00\x5c') # 0x7370 is a page break. Remapping it to 0x5c (\)
    patch.add_record(0xa6d8, b'\x00\x40') # 0x2177 (full-width @) remaps to 0x40 (@)
    patch.add_record(0xa6de, b'\x00\x5b') # 0x214e (full-width [) remaps to 0x5b ([}
    patch.add_record(0xa6e6, b'\x00\x5d') # 0x214f (full-width ]) remaps to 0x5d [])
    patch.add_record(0xa6ee, b'\x00\x5e') # 0x215c (full-width +) remaps to 0x5e (^)
    patch.add_record(0xa6f4, b'\x00\x5f') # 0x215d (full-width -) remaps to 0x5f (_)

    # Some of the same when counting characters...
    patch.add_record(0xa65e, b'\x00\x5c')
    patch.add_record(0xa66c, b'\x00\x40')

    # And when measuring dialog windows.
    patch.add_record(0xa4d0, b'\x00\x5c')

    # Punctuation marks, used to trigger blink animations on portraits. Uses addresses in the
    # system font graphics for some reason? Order doesn't matter; they all go to the same place.
    patch.add_record(0xa918, b'\x00\xf3\xaa\x20') # Half-width !
    patch.add_record(0xa920, b'\x00\xf3\xaa\xf0') # Half-width .
    patch.add_record(0xa928, b'\x00\xf3\xac\x00') # Half-width ?

    # Same handling of punctuation marks in a different spot... not sure where this is used,
    # but updating it too just to be safe.
    patch.add_record(0xa882, b'\x00\xf3\xaa\x20') # Half-width !
    patch.add_record(0xa88a, b'\x00\xf3\xaa\xf0') # Half-width .
    patch.add_record(0xa892, b'\x00\xf3\xac\x00') # Half-width ?



    # Text in the executable!

    # Instruction to insert the user disk.
    # Note that this apparently needs to be padded to 30 characters for some reason.
    # Original text:
    #   ユーザーディスクがある場合は、
    #   システムディスクと交換して下さ
    #   い。無い場合は、そのままシステ
    #   ムディスクをセットして下さい。
    patch.add_record(0x4fdc, text_util.encode_english('If you have a user disk,      \n'
                                                      'please exchange it for the    \n'
                                                      'system disk. Otherwise, leave \n'
                                                      'the system disk in place.     ', 128))

    # Text when using the mayor's letter.
    # Original text:
    #    ・・・が、・・・で
    #    ・・だったのだが、
    #    ・・として、・・・
    #    という訳じゃ。
    #    ・・難しい漢字ばかりで
    #    ナセルには読めなかった
    patch.add_record(0x5a1a, text_util.encode_english('It seems to say:\n'
                                                      '"...the...and...\n'
                                                      ' ...but it...so..."\\\n'
                                                      'It\'s nothing but\n'
                                                      'difficult words, and\n'
                                                      'Nassel can\'t read it.', 123))

    # Text when using Thor's letter.
    # Original text:
    #   我々の成そうとする事に
    #   手出しをするな。齔
    #   もはやお前の力では
    #   どうする事もできぬ。齔
    #   大人しくしているのが
    #   己のためと言うものだ。
    patch.add_record(0x5a97, text_util.encode_english('Do not interfere\n'
                                                      'in our endeavors.\\\n'
                                                      'You can do no more with\n'
                                                      'what power you have.\\\n'
                                                      'Obedience is for\n'
                                                      'your own good.', 132))

    # Request a data disk. Filled in at runtime with the disk number. Seems to need
    # padding to 30 characters?
    # Original text:
    #   ドライブ1にデータディスク を
    #   セットして下さい       
    patch.add_record(0x7190, text_util.encode_english('Please insert data disk   into\n'
                                                      'drive 1.                      ', 63))

    # Digit strings used for the data disk number.
    patch.add_record(0xc27b, text_util.encode_english('1', 3))
    patch.add_record(0xc27f, text_util.encode_english('2', 3))
    patch.add_record(0xc283, text_util.encode_english('3', 3))

    # Offset in the first line to the blank where the digit goes.
    patch.add_record(0x6a49, b'\x18')

    # Request the user disk. Seems to need padding to 30 characters?
    # Original text:
    #   ドライブ0にユーザーディスクを
    #   セットして下さい       
    patch.add_record(0x71d1, text_util.encode_english('Please insert the user disk   \n'
                                                      'into drive 0.                 ', 63))


    # User disk creation messages. Note spacing to maintain
    # original memory location of each line.
    # Original text:
    #    ユーザーディスク作成 
    #    
    #   新しいディスクをドライブ
    #   1にセットして、パスワー
    #   ドを入力してください。
    #    で作業を中止します。
    #   パスワード
    #   処理中
    #   しばらくお待ち下さい
    #   終了しました
    #   ユーザーディスクをドライ
    #   ブ0に、データディスク 
    #   をドライブ1にセットして
    #   下さい。
    #   システムディスクをドライ
    #   ブ0にセットして下さい。
    #   記録されたデータはありません
    patch.add_record(0xc085, text_util.encode_english('   User disk creation   \n'
                                                      '  \n'
                                                      'Please insert a blank   \n'
                                                      'disk into drive 1, and  \n'
                                                      'enter the password.   \n'
                                                      '   to cancel.         \n'
                                                      'Password: \n'
                                                      'Making\n'
                                                      'Please wait a while.\n'
                                                      ' Finished!  \n'
                                                      'Please insert the user  \n'
                                                      'disk into drive 0, and  \n'
                                                      'the data disk into      \n'
                                                      'drive 1.\n'
                                                      'Please insert the system\n'
                                                      'disk into drive 0.      \n'
                                                      ' There is no recorded data. \n', 355))

    # Not sure when this is used, but something about write protection
    # on the user disk. Note spacing to maintain locations.
    # It does show up if you turn on write protection on the new
    # disk before entering a password on the user disk creation screen,
    # but it freezes in the middle of displaying it. That freeze
    # seems to have been present in the original game.
    # Original text:
    #   おまえいい加減にしろよ!
    #   ユーザーディスクがプロテクト
    #   状態になっています。    
    patch.add_record(0xc1ea, text_util.encode_english('Hey, stop that!         \n'
                                                      'The user disk is in a       \n'
                                                      'protected state.            \n', 85))

    # Copy protection text. I've seen this trigger before when I tried
    # to copy the game data to a hard drive, but I haven't been able to
    # reproduce with this text injected. Hopefully it works...
    # Original text:
    #   どうやらコピーに失敗したようで
    #   すよ。未熟者ですねェ。まぁ、い
    #   いでしょう。 ラグーンの評価版
    #   として、最初のボスまでみせてあ
    #   げましょう。 いよっ!太っ腹!
    #   ・・・と言う訳で、続きがやりた
    #   かったら、本物を買ってね。
    #   - ラグーン スタッフ一同 -
    patch.add_record(0x5b1e, text_util.encode_english('Looks like the copy failed!   \n'
                                                      'New at this, huh? Well, that\'s\n'
                                                      'okay. Here\'s the trial version\n'
                                                      'of Lagoon, up to the first    \n'
                                                      'boss. Wow! How generous!      \n'
                                                      '...So buy it legitimately to  \n'
                                                      'play the rest of it, okay?\n'
                                                      '      - Lagoon Staff -        \n', 251))

    # Game over!
    patch.add_record(0x4fc7, text_util.encode_english('Game Over', 20))

    # Pause! What's it doing way down there?
    patch.add_record(0x21504, text_util.encode_english(' Pause ', 15))

    # Disk UI
    patch.add_record(0xc241, text_util.encode_english('Data Load', 19))
    patch.add_record(0xc255, text_util.encode_english('Data Save', 19))
    patch.add_record(0xc269, text_util.encode_english(' 1:', 5))
    patch.add_record(0xc26f, text_util.encode_english(' 2:', 5))
    patch.add_record(0xc275, text_util.encode_english(' 3:', 5))

    # Item received
    patch.add_record(0x7132, text_util.encode_english('was obtained!', 17))

    # Shop text
    patch.add_record(0x16189, text_util.encode_english('                    ', 21)) # Clears the message window.
    patch.add_record(0x161a0, text_util.encode_english('Yes', 5))
    patch.add_record(0x161a7, text_util.encode_english('No', 7))

    # Miscellaneous text table
    with open('csv/misc_strings.csv', 'r', encoding='utf8') as in_file:
        reader = csv.reader(in_file, lineterminator='\n')
        string_data, string_offsets = encode_string_table([row[1] for row in reader])
        string_data = string_data.ljust(0x75d, b'\x00')
        add_record_checked(patch, 0x194d8, string_data, 0x75d)

        # Item names are indexed relative to the address of string index 58. There are a couple of different
        # routines for that (one for shops, one for inventory, and one more for item acquisition?).
        item_start_offset_bytes = (0x194d8 + string_offsets[58] - 0x40).to_bytes(3, byteorder='big')
        patch.add_record(0x6bf9, item_start_offset_bytes)
        patch.add_record(0x6cc1, item_start_offset_bytes)
        patch.add_record(0x80cd, item_start_offset_bytes)
        patch.add_record(0x981d, item_start_offset_bytes)

    # Cut scene text table
    with open('csv/cut_scene_text.csv', 'r', encoding='utf8') as in_file:
        reader = csv.reader(in_file, lineterminator='\n')
        string_data, string_offsets = encode_string_table([row[1] for row in reader])
        string_data = string_data.ljust(0x400, b'\x00')
        add_record_checked(patch, 0x18ce9, string_data, 0x400)

        # There are mini-scripts that control the cut scenes embedded in the executable. They store direct
        # pointers to the start of each of these strings. Remap those.
        for index, offset in enumerate([0x1815d, 0x18175, 0x1817f, 0x181a7, 0x1845f, 0x18469]):
            patch.add_record(offset, (0x18ce9 + string_offsets[index] - 0x40).to_bytes(3, byteorder='big'))


    # The arrow graphic used in the shop is stored in the executable for some reason? It's stored as a
    # full-width character, but it should only be half-width.
    new_arrow_data = bytearray()
    orig_reader.seek(0x19c36)
    for _ in range(16):
        new_arrow_data.append(orig_reader.read(2)[0])
    new_arrow_data = new_arrow_data.ljust(32, b'\x00')
    patch.add_record(0x19c36, new_arrow_data)


    # There's a big table starting at 0x16614 that appears to contain timing information for portrait
    # animations, as far as I can tell. Without reverse-engineering that system too much, I'm guessing
    # that one-frame animations with a duration of 4 are talking animations. I'll halve the duration
    # of the talking frames, as best as I can naively identify them.
    orig_reader.seek(0x16614)
    animation_data = read_animation_timing(orig_reader)

    for character in animation_data:
        for animation in character:
            if len(animation) == 1:
                animation[0]['duration'] = 2
            elif (len(animation) == 20 and animation[0]['duration'] == 7):
                # An animation with length 20 using duration 7 is used by the mayor in Atland, synchronized
                # with dialog about a letter.
                for frame in animation:
                    frame['duration'] = 4
        if len(character) == 2 and len(character[1]) == 105:
            # An animation with length 105 with duration 7 is used by Zerah in
            # Voloh. This animation is all kinds of bugged, so we're going to rewrite it
            # entirely to be more consistent with other characters' animations.

            # Give him two still frames for talking.
            character.insert(0, [{'frame': 1, 'duration': 2}])
            character[1][0]['frame'] = 3

            # Give him something vaguely glowery for punctuation.
            character.insert(2, [{'frame': 4, 'duration': 4}, {'frame': 3, 'duration': 4}, {'frame': 2, 'duration': 4}, {'frame': 1, 'duration': 4}])

            # And truncate his long bracketed animation to fit in the existing space. Note
            # that bracketed animations seem to break if used more than once in the same
            # dialog.
            character[3] = character[3][:99]

    patch.add_record(0x16614, encode_animation_timing(animation_data))

    # Now set up those new animations we added for Zerah above. He's portrait ID 0xe, minus one,
    # so he's at index 0xd in these tables.
    # Main talking frames are in a table at 0x16484, 16 bytes per entry. The first two bytes
    # are the default animation; the rest aren't relevant to us now.
    patch.add_record(0x16484 + (0xd * 0x10), b'\x01\x00')

    # Punctuation animations are in a table at 0x165c4, 4 bytes per entry. I think the first
    # one is all we care about right now.
    patch.add_record(0x165c4 + (0xd * 0x4), b'\x02')

    # Double the speed of miscellaneous NPC text.
    patch.add_record(0x6a2, b'\x00\x04')


    return patch
Beispiel #2
0
    if row_count is not None:
        patch.add_record(entry_address,     num_8bit(row_count))
    if left_value is not None:
        patch.add_record(entry_address + 1, num_8bit(left_value))
    if right_value is not None:
        patch.add_record(entry_address + 2, num_8bit(right_value))

if __name__ == '__main__':
    os.makedirs('build', exist_ok=True)

    reverse_font_map = text_util.load_map_reverse('assets/text/font.tbl')

    patch = Patch()

    # New tiles for digits in font.
    patch.add_record(0x488a, b'\xB5\xB6\xB7\xB8')

    # Some code that gets executed to get the evolution menu into a clean state after making a selection...
    # it ensures that the "Yes" option is visible if it was in the middle of blinking. But because the "Yes"
    # text is longer than the original, no easy way to expand it. Just NOP it out; it'll look fine.
    patch.add_rle_record(0x4ade, b'\xea', 12)

    # Evolution options...
    # These instructions write blank to each possible location of the arrow.
    # Nudge each one up by 0x40...
    patch.add_record(0x626b, b'\x06')
    patch.add_record(0x626f, b'\x86')
    patch.add_record(0x6273, b'\x06')
    patch.add_record(0x6277, b'\x86')
    patch.add_record(0x627B, b'\x06')
Beispiel #3
0
class TestPatch(TestCase):
    def setUp(self):
        self.source = bytes([0] * 10)
        self.target = bytes([0, 0, 0, 1, 1, 0, 2, 2, 2, 2])

        self.patch = Patch()
        self.patch.add_record(0x3, bytes([1, 1]))
        self.patch.add_rle_record(0x6, bytes([2]), 4)

    def test_patch_core(self):
        result = TestPatch.__save_reload_and_apply(self.patch, self.source)
        self.assertEqual(result, self.target)

    def test_patch_padding(self):
        self.patch.add_record(0x20, bytes([1]))

        result = TestPatch.__save_reload_and_apply(self.patch, self.source)

        self.assertEqual(len(result), 0x21)

    def test_patch_truncation(self):
        self.patch.set_truncate_length(0x5)

        result = TestPatch.__save_reload_and_apply(self.patch, self.source)

        self.assertEqual(len(result), 0x5)

    def test_patch_eof_edge_case(self):
        eof_value = int.from_bytes(b'EOF', byteorder='big')
        with self.assertRaises(RuntimeError):
            self.patch.add_record(eof_value, bytes([1]))

    def test_create_eof_edge_case(self):
        eof_value = int.from_bytes(b'EOF', byteorder='big')

        source = bytes(eof_value + 10)
        target = bytearray(bytes(eof_value + 10))
        target[eof_value - 1] = 2
        target[eof_value] = 1

        patch = Patch.create(source, target)

        result = TestPatch.__save_reload_and_apply(patch, source)

        self.assertEqual(len(result), eof_value + 10)
        self.assertEqual(result[eof_value - 1], 2)
        self.assertEqual(result[eof_value], 1)

    def test_create_equal_length(self):
        created_patch = Patch.create(self.source, self.target)

        result = TestPatch.__save_reload_and_apply(self.patch, self.source)

        self.assertEqual(result, self.target)

    def test_create_padded_length(self):
        target_with_padding = self.target + bytes([0] * 10) + bytes([1])
        created_patch = Patch.create(self.source, target_with_padding)

        result = TestPatch.__save_reload_and_apply(created_patch, self.source)

        self.assertEqual(result, target_with_padding)

    def test_create_padded_length_all_zero(self):
        target_with_padding = self.target + bytes([0] * 10)
        created_patch = Patch.create(self.source, target_with_padding)

        result = TestPatch.__save_reload_and_apply(created_patch, self.source)

        self.assertEqual(result, target_with_padding)

    def test_create_truncated_length(self):
        target_truncated = self.target[:5]
        created_patch = Patch.create(self.source, target_truncated)

        result = TestPatch.__save_reload_and_apply(created_patch, self.source)

        self.assertEqual(result, target_truncated)

    def test_create_address_overflow(self):
        source = bytes([0] * 0x1000001)
        target = bytes([0] * 0x1000000 + [1])

        with self.assertRaises(RuntimeError):
            created_patch = Patch.create(source, target)

    def test_create_size_overflow(self):
        source = bytes([n % 256 for n in range(0x10000)])
        target = bytes([(n + 1) % 256 for n in range(0x10000)])

        created_patch = Patch.create(source, target)

        result = TestPatch.__save_reload_and_apply(created_patch, source)

        self.assertEqual(target, result)

    def test_create_rle_size_overflow(self):
        source = bytes([0] * 0x10000)
        target = bytes([1] * 0x10000)

        created_patch = Patch.create(source, target)

        result = TestPatch.__save_reload_and_apply(created_patch, source)

        self.assertEqual(target, result)

    @staticmethod
    def __save_reload_and_apply(patch, source):
        encoded_patch = patch.encode()
        decoded_patch = None

        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(encoded_patch)
            f.close()

            decoded_patch = Patch.load(f.name)

            os.unlink(f.name)

        return decoded_patch.apply(source)