Py scripting problem

How to translate the files of a game
month
Posts: 7
Joined: Tue Jan 29, 2019 4:48 pm

Py scripting problem

Post by month »

This script cannot display Chinese, Japanese, Korean and some symbols correctly after extracting the text. Can anyone modify the code so that they can display correctly?


import os
import json
import codecs


def read_block(f):
block_size = 0
index = int.from_bytes(bytes(f.read(4)), byteorder='little')
block_size += 4

len1 = int.from_bytes(bytes(f.read(2)), byteorder='little')
block_size += 2
str1 = codecs.decode(bytes(f.read(len1 * 2)), encoding='utf-8', errors='replace').replace('\00','')
block_size += len1 * 2

return {"index": index, "str1": str1, "size": block_size}

fname = 'tb_npc_script.res'
fsize = os.path.getsize(fname)
entries = []
with open("tb_npc_script.res", "rb") as f:
try:
entry_count = int.from_bytes(f.read(4), byteorder='little')
fsize -= 4
while entry_count > 0:
block = read_block(f)
fsize -= block["size"]
entry_count -= 1
entries.append(block)

except Exception as e:
print(e)

with open('tb_npc_script.txt', 'w', encoding='utf-8') as c:
json.dump(entries, c, ensure_ascii=False, indent=4)

print('完成')
akintos
Posts: 88
Joined: Tue May 08, 2018 7:48 pm

Re: Py scripting problem

Post by akintos »

replace line 14

Code: Select all

   str1 = codecs.decode(bytes(f.read(len1 * 2)), encoding='utf-8', errors='replace').replace('\00','')

to this

Code: Select all

   str1 = codecs.decode(bytes(f.read(len1 * 2)), encoding='utf-16', errors='replace').replace('\00','')
month
Posts: 7
Joined: Tue Jan 29, 2019 4:48 pm

Re: Py scripting problem

Post by month »

Thank you. Thank you very much.