Type of Font.m_FontData being mixed up
Code
From README.md:
if obj.type.name == "Font":
font: Font = obj.read()
if font.m_FontData:
extension = ".ttf"
if font.m_FontData[0:4] == b"OTTO":
extension = ".otf"
with open(os.path.join(path, font.m_Name+extension), "wb") as f:
f.write(font.m_FontData)
Error The error message that is produced by python.
Usually something like
TypeError: memoryview: a bytes-like object is required, not 'list'
Bug From the example in README.md, it seems that Font.m_FontData is treated as a bytes object. While in generated classes (classes.generated), Font.m_FontData is typed with List[str]. However in real usage, Font.m_FontData actually returns List[int] containing the byte values as a list.
To Reproduce
- a copy of the file that causes the problem
- following data:
- Python version: 3.10.12
- UnityPy version: 1.22.3, 1.22.5 and 1.23.0
Thanks for reporting the issue. m_FontData as well as some fields are arrays of characters for some reason. I mapped these as str in the type stub generator, while the parser itself uses uint8 for these, as python doesn't have a character type.
I certainly have to adjust that and fix the readme.
Usually something like
TypeError: memoryview: a bytes-like object is required, not 'list'
@t-wy
Until the type mapping and README are fixed, if you're interested,, a simple workaround is to convert m_FontData explicitly to bytes before writing:
f.write(bytes(font.m_FontData))
f.write(bytes(font.m_FontData))
@Saipan0 Of course I'm doing that anyways. Just like other backward-compatible checking when UnityPy introduces breaking updates. Just in case any type checker complains, then I would separate the font.m_FontData assignment and add a typing hint override to that variable. (or "ignore")
Still a good comment for others who are seeking a solution when coming along this Issue thread.