This repository was archived by the owner on Oct 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwriteback.py
172 lines (158 loc) · 6.2 KB
/
writeback.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os ,ctypes,json
mymap={}
with open('font/remap_charset2.txt','r',encoding='utf8') as ff:
remap_charset2=json.load(ff)
pf=open('patch/tolongtexts.cpp','w',encoding='utf-8-sig')
print('''
#include<unordered_map>
#include<string>
std::unordered_map<std::wstring,std::wstring>trans={
''',file=pf)
cnt=0
for f in os.listdir('SCRIPT_FHD/text'):
#if f!='0206-05-03':continue
#if f!='0000-op0_HD':continue
#print(f)
with open('SCRIPT_FHD/text/'+f,'r',encoding='utf8') as ff:
lines=ff.read().split('\n')
with open('SCRIPT_FHD/SCRIPT_FHD/'+f,'rb') as ff:
cankao=ff.read()
for i in range(len(lines)):
if len(lines[i])==0:continue
#i=len(lines)-1-_i
if i%3!=1:continue
idx=int(lines[i][:4],16)
originlength=ctypes.c_ushort.from_buffer_copy(cankao[idx:idx+2]).value
text=lines[i][5:].replace('$','\n').replace('―','—')
origin=lines[i-1][5:].replace('$','\n')
#print(text)
# if len(text)>originlength:
# print(text)
#
#print(text)
text_save=text
text=list(text)
for ii in range(len(text)):
text[ii]=remap_charset2.get(text[ii],text[ii])
text=''.join(text)
length=len(text)
#text=text[:originlength]
if originlength!=length and (('$d' in text_save ) ):
#print(text)
length+=999
if (text[-1]!='\n' or (text[-1]=='\n' and f=='a2-7-1ED02')) and originlength>=length:
while length<originlength:
if text[-1]=='\n' and f=='a2-7-1ED02':
text=text[:-1]+' '+'\n'
else:
text+=' '
length+=1
saveend=cankao[idx+2+originlength*2+2:]
#print(bytes(saveend)[:30].hex())
cankao=cankao[:idx]
cankao+=(length.to_bytes(2,'little'))
cankao+=(text.encode('utf-16-le')+b'\x00\x00')
cankao+=saveend
else:
_1=origin.split("$d")
_2=text.split("$d")
if len(_1)!=len(_2):
raise Exception(f,origin,text)
for i in range(len(_1)):
if _1[i] in mymap:
if mymap[_1[i]]==_2[i]:
continue
mymap[_1[i]]=_2[i]
cnt+=1
print('{L"'+_1[i].replace('\n','\\n')+'",L"'+_2[i].replace('\\','\\\\').replace('\n','\\n').replace('"','\\"')+'"},',file=pf)
if len(_1[i]):
__1=_1[i]
__2=_2[i]
while len(__1):
if __1[0] in ' \n':
__1=__1[1:]
else:break
while len(__2):
if __2[0] in ' \n':
__2=__2[1:]
else:break
if len(__1) and len(__2):
if __1!=_1[i]:
print('{L"'+__1.replace('\n','\\n')+'",L"'+__2.replace('\\','\\\\').replace('\n','\\n').replace('"','\\"')+'"},',file=pf)
if len(_1[i]):
__1=_1[i]
__2=_2[i]
while len(__1):
if __1[0] in ' ':
__1=__1[1:]
else:break
while len(__2):
if __2[0] in ' ':
__2=__2[1:]
else:break
if len(__1) and len(__2):
if __1!=_1[i]:
print('{L"'+__1.replace('\n','\\n')+'",L"'+__2.replace('\\','\\\\').replace('\n','\\n').replace('"','\\"')+'"},',file=pf)
if len(_1[i]):
__1=_1[i]
__2=_2[i]
while len(__1):
if __1[0] in ' ':
__1=__1[1:]
elif __1[0]=='\n':
__1=__1[1:]
break
else:break
while len(__2):
if __2[0] in ' ':
__2=__2[1:]
elif __2[0]=='\n':
__2=__2[1:]
break
else:break
if len(__1) and len(__2):
if __1!=_1[i]:
print('{L"'+__1.replace('\n','\\n')+'",L"'+__2.replace('\\','\\\\').replace('\n','\\n').replace('"','\\"')+'"},',file=pf)
with open('SCRIPT_FHD/SCRIPT_FHD_transed/'+f,'wb') as ff:
ff.write(bytes(cankao))
# with open(r'C:\InnocentGrey\カルタグラ FHD\trans.json','w',encoding='utf8') as ff:
# ff.write(json.dumps(mymap,ensure_ascii=False,indent=4))
print('};',file=pf)
print(cnt,len(mymap))
with open('SCRIPT_FHD/text/_varstr','r',encoding='utf8') as ff:
lines=ff.read().split('\n')
with open('SCRIPT_FHD/SCRIPT_FHD/_varstr','rb') as ff:
bs=ff.read()
import ctypes
idx=0
target=b''
textidx=0
while idx<len(bs):
blocklength=ctypes.c_short.from_buffer_copy(bs[idx:idx+2]).value
#print(hex(blocklength))
idx+=2
mid=bs[idx:idx+6]
#print(bs[idx:idx+6].hex())
idx+=6
length=ctypes.c_ushort.from_buffer_copy(bs[idx:idx+2]).value
#print(hex(length))
idx+=2
if length:
text=bs[idx:idx+length*2]
# print(1,text.decode('utf-16-le'),1)
idx+=length*2+2
text=lines[textidx*3+1][5:]
text=list(text)
for ii in range(len(text)):
text[ii]=remap_charset2.get(text[ii],text[ii])
text=''.join(text)
blocklength=2+6+2+2+len(text)*2
target=target+blocklength.to_bytes(2,'little')+mid+len(text).to_bytes(2,'little')+text.encode('utf-16-le')+b'\x00\x00'
textidx+=1
else:
target=target+blocklength.to_bytes(2,'little')+mid+b'\x00\x00\x00\x00'
if textidx==2:
target=target+bs[idx:]
break
with open('SCRIPT_FHD/SCRIPT_FHD_transed/_varstr','wb') as ff:
ff.write(target)