Skip to content

Commit

Permalink
Updates the python utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Peralta committed Sep 16, 2024
1 parent f505deb commit 4790bca
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 28 deletions.
Empty file added tests/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions tests/vwf_renderer_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
from pathlib import Path
from xml.etree import ElementTree

from build import read_fixed_from_xml
from utils.smallvwf import VwfAsset


class VwfRendererTestCase(unittest.TestCase):
def test_render(self):
vwf = VwfAsset("./fonts/8x8vwf.png")
strings = []
# with open("./text/fr/monsters.xml", encoding='utf-8') as datasource:
# tree = ElementTree.parse(datasource)
# root = tree.getroot()
#
# for child in root:
# text = child.text
# if text:
# strings.append(text)

strings = [
"Objets", "Magie", "Equiper", "Statut", "Placer", "Changer", "Options", "Sauver"
]

vwf.set_strings(strings)

vwf.render()
renderd = Path("./renderd.bin")
serialized = vwf.serialize()

renderd.write_bytes(serialized)
138 changes: 135 additions & 3 deletions utils/dump.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env python3.4
import struct

import script
from a816.cpu.cpu_65c816 import snes_to_rom
from a816.symbols import low_rom_bus
from script import Table
from script.formulas import base_relative_16bits_pointer_formula
from script.pointers import Script, write_pointers_as_xml
from script.pointers import Script, write_pointers_as_xml, Pointer


def write_fixed_text_as_xml(pointers, table, length, output_file):
Expand All @@ -16,9 +20,137 @@ def write_fixed_text_as_xml(pointers, table, length, output_file):
fd.write('</sn:fixed>\n')
fd.write('</sn:fixedlist>\n')

def write_nul_terminated_strings_as_xml(strings, output_file):
with open(output_file, 'wt', encoding='utf-8') as fd:
fd.writelines('<?xml version="1.0" encoding="utf-8"?>\n')
fd.write('<sn:stringarray eos="0x00" xmlns:sn="http://snes.ninja/ScriptNS">\n')
for s in strings:
fd.write('<sn:string>')
fd.write(s) #.strip())
fd.write('</sn:string>\n')
fd.write('</sn:stringarray>\n')

def dump_en():
# jtable = Table('../text/ff4_jap.tbl')
jtable = Table('../text/ff4_menus.tbl')
item_description = low_rom_bus.get_address(0x2407ad)
item_desc = []

# with open('../ff4j.smc', 'rb') as rom:
with open('../Final Fantasy IV (Japan) (Rev 1) [En by J2e v3.21].sfc', 'rb') as rom:
rom.seek(item_description.physical)
for k in range(443):
s = bytes()
while (char := rom.read(1)) != b'\x00':
print(char)
s += char
item_desc.append(jtable.to_text(s))

write_nul_terminated_strings_as_xml(item_desc, "../text/en/item_descriptions.xml")
...

def dump_jp():
jtable = Table('../text/ff4_jap.tbl')
#jtable = Table('.text/ff4_menus.tbl')
item_description = low_rom_bus.get_address(0x0fae2a)
item_desc = []

with open('../ff4j.smc', 'rb') as rom:
#with open('../Final Fantasy IV (Japan) (Rev 1) [En by J2e v3.21].sfc', 'rb') as rom:
rom.seek(item_description.physical)
for k in range(47):
s = bytes()
while (char := rom.read(1)) != b'\x00':
print(char)
s += char
item_desc.append(jtable.to_text(s))

write_nul_terminated_strings_as_xml(item_desc, "../text/jp/item_descriptions.xml")
...


def dump_handedness():
jtable = Table('../text/ff4_jap.tbl')
addr = low_rom_bus.get_address(0x01e2d9)
with open('../ff4j.smc', 'rb') as rom:
rom.seek(addr.physical)
data = rom.read(8*4)
text = jtable.to_text(data)
...

if __name__ == '__main__':
table = Table('text/ff4_menus.tbl')
jtable = Table('../text/ff4_jap.tbl')
# addr = low_rom_bus.get_address(0xef200)
# with open('../ff4j.smc', 'rb') as rom:
# rom.seek(addr.physical)
# i = 0
# battle_messages_pointers = []
# while i < 0xba:
# data = rom.read(2)
# pointer = struct.unpack('<H', data)
# pos = rom.tell()
#
# string_pointer = low_rom_bus.get_address((addr.logical_value & 0xff0000)+ pointer[0])
# rom.seek(string_pointer.physical)
# s = bytes()
#
# end_of_string = False
# while end_of_string is False:
# # print(char)
# char = rom.read(1)
# s += char
#
# if char == b'\x04':
# char = rom.read(1)
# s += char
# continue
#
# if char == b'\x00':
# end_of_string = True
# ptr = Pointer(i)
# ptr.value = s
# battle_messages_pointers.append( ptr)
# rom.seek(pos)
# i+=1
# write_pointers_as_xml(battle_messages_pointers, jtable, '../text/jp/battle_messages.xml')

addr = low_rom_bus.get_address(0x0fb200)
with open('../ff4j.smc', 'rb') as rom:
rom.seek(addr.physical)
i = 0
battle_messages_pointers = []
while i < 58:
data = rom.read(2)
pointer = struct.unpack('<H', data)
pos = rom.tell()

string_pointer = low_rom_bus.get_address((addr.logical_value & 0xff0000) + pointer[0])
rom.seek(string_pointer.physical)
s = bytes()

end_of_string = False
while end_of_string is False:
# print(char)
char = rom.read(1)
s += char

if char == b'\x04':
char = rom.read(1)
s += char
continue

if char == b'\x00':
end_of_string = True
ptr = Pointer(i)
ptr.value = s
battle_messages_pointers.append(ptr)
rom.seek(pos)
i += 1
write_pointers_as_xml(battle_messages_pointers, jtable, '../text/jp/battle_text.xml')

# dump_en()
exit(0)
table = Table('.text/ff4_menus.tbl')
jtable = Table('text/ff4_jap.tbl')


Expand Down Expand Up @@ -66,4 +198,4 @@ def write_fixed_text_as_xml(pointers, table, length, output_file):
# jp_places_end = 0xA9C80
#
# # write_pointers_as_xml(pointers_places_name, table, 'text/en/us-places.xml')
# write_fixed_text_as_xml(monster_spells, table, 8, 'text/en/en-monster-spells.xml')
# write_fixed_text_as_xml(monster_spells, table, 8, 'text/en/en-monster-spells.xml')
48 changes: 41 additions & 7 deletions utils/font.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
from PIL import Image

def get_char(image, char, has_grid, char_width, char_height):

def get_char(image: np.ndarray, char, has_grid, char_width, char_height) -> np.ndarray:
shape = image.shape
width = shape[1]
height = shape[0]
Expand Down Expand Up @@ -34,7 +35,7 @@ def char_as_1bbp(char):
return bytes(binary_data)


def get_max_width(char):
def get_max_width(char: np.ndarray) -> int:
max_width = 0
for byte in char:
trimmed = np.trim_zeros(byte, 'b')
Expand All @@ -43,23 +44,43 @@ def get_max_width(char):
return max_width


def convert_font_to_1bpp(font_file, has_grid=True):
def convert_font_to_1bpp(font_file, has_grid=True, char_height=16):
image = np.array(Image.open(font_file))

# image = ndimage.imread(font_file)

char = get_char(image, 0x00, has_grid, 8, 16)
char = get_char(image, 0x00, has_grid, 8, char_height)

data = b''
char_index = 1
while len(char) > 0:
data += char_as_1bbp(char)
char = get_char(image, char_index, has_grid, 8, 16)
char = get_char(image, char_index, has_grid, 8, char_height)
char_index += 1

len_table = {}
for i in range(char_index - 1):
len_table[i] = get_max_width(get_char(image, i, has_grid, 8, char_height))

return len_table, data

def convert_font_to_2bpp(font_file, has_grid=True, char_height=16):
image = np.array(Image.open(font_file))

# image = ndimage.imread(font_file)

char = get_char(image, 0x00, has_grid, 8, char_height)

data = b''
char_index = 1
while len(char) > 0:
data += write_as_2bpp(char)
char = get_char(image, char_index, has_grid, 8, char_height)
char_index += 1

len_table = {}
for i in range(char_index - 1):
len_table[i] = get_max_width(get_char(image, i, has_grid, 8, 16))
len_table[i] = get_max_width(get_char(image, i, has_grid, 8, char_height))

return len_table, data

Expand Down Expand Up @@ -89,4 +110,17 @@ def remove_grid(font_file):


if __name__ == '__main__':
remove_grid('/Users/emmanuel/PycharmProjects/ff4/fonts/wicked_vwf.png')
remove_grid('/Users/emmanuel/PycharmProjects/ff4/fonts/wicked_vwf.png')


def write_as_2bpp(data: np.ndarray) -> bytearray:
binary_data = bytearray()
for y_value in range(0, len(data[0]), 8):
char = data[0:8, y_value:y_value + 8]

for byte in char:
byte_value = int(''.join(byte.astype(str)).ljust(8, '0'), 2)
binary_data.append(0xFF)
binary_data.append(byte_value)

return binary_data
22 changes: 22 additions & 0 deletions utils/scratchpad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if __name__ == "__main__":
texts = [
"Requis",
"Garde",
"Rang"
]
t = ' '.join(texts)

available = sorted({c for c in t})
print(''.join(sorted(available)))
print (len(available))
mp_needed = [available.index(c) + 0xdd for c in "Requis"]
# mp_needed[mp_needed.index(0xdd)]= 0xff

garde_text = [available.index(c) + 0xdd for c in "Garde "]
garde_text[garde_text.index(0xdd)]= 0xff

passer_text = [available.index(c) + 0xdd for c in "Rang"]

print(mp_needed)
print(garde_text)
print(passer_text)
Loading

0 comments on commit 4790bca

Please sign in to comment.