-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
139 lines (104 loc) · 4.4 KB
/
script.py
1
2
3
4
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import io
import os
from struct import *
import csv
def importCSV(FileName, TextOffset, SystemOffset):
'''
orgFileName, TextOffset, SystemOffset
'''
print( 'make_Stage' )
# 진리표, 대응표, 등등 char table(ex 亜,가 )
with open('jp2kr.txt','r',encoding='utf8') as dictj2K:
Dict = {}
kList = []
for x in dictj2K.readlines():
Dict[ x[:-1].split(',')[1] ] = x[:-1].split(',')[0]
kList.append(x[:-1].split(',')[1])
with open( FileName, 'rb' ) as F:
fData = F.read()
bData = io.BytesIO( fData )
tempData = bData.read( TextOffset )
bData.seek( SystemOffset )
temp = b''
temp_all = b''
while True:
temp = bData.read(4)
if temp.hex() == '00000000':
SystemData = temp_all
break
else:
temp_all += temp
newCsvFileName = '작업 시트 - '+FileName.split('.')[0]+'.csv'
with open(newCsvFileName, 'r', encoding='utf8') as F:
rdr = list( csv.reader(F) )
tableData = b''
textData = b''
for line in rdr:
text = line[3] # row3
# translation text
ktext = ''
for x in text.replace(',','、'):
ktext += Dict[x] if x in kList else x
textData = ktext.replace('■',' ').encode('cp932')+bytes(16-len(ktext.replace('■',' ').encode('cp932'))%16)
orgOffset = pack('<L', int(line[1]))
newOffset = pack('<L', len(tempData)+7696128 - 16)
index = -1
while True:
index = fData.find(orgOffset, index + 1)
if index == -1:
break
tempData = tempData[:index]+newOffset+tempData[index+4:]
tempData+=textData
print( 'check the new system offset poin is [', pack('<L', len(tempData)+7696128 - 16 ).hex()[:4], ']' )
print( 'you find System offset bytes, 0x756f00+SystemOffset, is last 2bytes in LE.' )
print( 'you manual change 2bytes' )
tempData += SystemData
tempData += bytes( (128 - len(tempData)%128) + 16 )
finData = tempData[:12]+pack('<L', len(tempData[16:]))+tempData[16:]
finData = finData[:32]+pack('<L', len(finData)-unpack('<L', finData[28:32])[0]-80)+finData[36:40]+pack('<L', unpack('<L', finData[12:16])[0]+7696128)+pack('<L', unpack('<L', finData[12:16])[0]+7696128)+finData[48:]
with open( FileName.split('.')[0]+'.new', 'wb' ) as F:
F.write( finData )
def exportCSV(FileName, TextOffset, SystemOffset):
print( 'check' )
List = []
fData = []
with open(FileName, 'rb') as F:
bData = F.read()
F.seek( TextOffset )
temp_All = b''
Trig00 = False
TrigPrint = True
Count = 0
while True:
if F.tell() == SystemOffset : break
temp = F.read(1)
if temp.hex() != '00':
Trig00 = False
TrigPrint = True
temp_All+=temp
else:
if TrigPrint:
Trig00 = True
if Trig00:
intOffset = F.tell()+7696128 - 16 - len(temp_All) - 1
Offset = pack('<L', intOffset)
if not temp_All.decode('cp932').replace(' ','■') in List:
fData.append( [ "{0:0>4}".format(Count), intOffset, temp_All.decode('cp932').replace(' ','■'), temp_All.decode('cp932').replace(' ','■') ] )
Count += 1
else:
print( temp_All.decode('cp932').replace(' ','■') )
input('Error')
Trig00 = False
TrigPrint = False
temp_All = b''
with open( FileName.split('.')[0]+'.csv','w',encoding='UTF8', newline='') as FD:
wr = csv.writer(FD)
for x in fData:
wr.writerow(x)
if __name__ == '__main__':
print( 'convert program start' )
#exportCSV('STAGE_0002.bin', 22288, 45840)
importCSV('STAGE_0002.bin', 22288, 45840)
'''file is not STAGE.BIN you before must split STAGE.BIN file.
TextOffset is Script Start Offset. you find manully,
SystemOffset is Script text end. some kind of Offset, 4 bytes, LE.'''