-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssPacker.py
163 lines (151 loc) · 5.55 KB
/
ssPacker.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
# -*- coding: utf-8 -*-
# For Windows OS Only....
import sys
import os
import glob
import struct
import unicodedata
from Decryption import Decrypt
class Header:
headerData=b''
headerList=[]
length=0
index=0
count=0
offset=0
dataCount=0
def __init__(H,f):
f.seek(0)
H.length=struct.unpack('I',f.read(4))[0]
H.headerData=f.read(128)
H.headerList=struct.unpack('32I',H.headerData)
H.index=H.headerList[2]
H.count=H.headerList[3]
H.offset=H.headerList[4]
H.datacount=H.headerList[5]
def Check(scr):
for char in scr:
if unicodedata.east_asian_width(char)!='Na':
return True
return False
def main(argv):
if argv.count('-x')>0:
xlsxMode=True
import openpyxl
argv.remove('-x')
else:
xlsxMode=False
if len(argv)<3 or argv[1]=='' or argv[2]=='':
print ("Usage: "+argv[0][argv[0].rfind("\\")+1:]+" <Scene\> <Text\> [Scene_packed\] [-x]")
return False
inF=argv[1]+"\\"
txtF=argv[2]+"\\"
if len(argv)<4 or argv[3]=='':
outF=argv[0][:argv[0].rfind("\\")+1]+argv[1][argv[1].rfind("\\")+1:]+"_packed\\"
else:
outF=argv[0][:argv[0].rfind("\\")+1]+argv[3]+"\\"
if not os.path.exists(outF):
os.makedirs(outF)
for txtFN in glob.glob(txtF+"*.txt"):
print(txtFN)
inFN=inF+txtFN[txtFN.rfind("\\")+1:].replace(".txt",".ss").replace(".ss.ss",".ss")
outFN=outF+txtFN[txtFN.rfind("\\")+1:].replace(".txt",".ss").replace(".ss.ss",".ss")
size=os.path.getsize(inFN)
file=open(inFN,'rb')
header=Header(file)
file.seek(header.index)
offset=[]
length=[]
for n in range(0,header.count):
offset.append(struct.unpack('I',file.read(4))[0])
length.append(struct.unpack('I',file.read(4))[0])
string=[]
for x in range(0,header.count):
if length[x]==0:
string.append(b'')
continue
file.seek(header.offset+offset[x]*2,0)
string.append(file.read(length[x]*2))
file.seek(header.offset)
ssData=file.read()
file.close()
txt=open(txtFN,'r',1,"UTF-8")
for line in txt.readlines():
if not line[0]==u"●":
continue
index=int(line[1:line.find("●",1)])
text=line[line.find("●",1)+1:].replace("\n","")
length[index]=len(text)
string[index]=Decrypt(text.encode("UTF-16")[2:],length[index],index)
txt.close()
output=open(outFN,'wb')
output.write(struct.pack("I",header.length))
output.write(header.headerData[:16])
output.write(struct.pack("I",size))
output.write(header.headerData[20:])
newOffset=0
for n in range(0,header.count):
output.write(struct.pack("I",newOffset))
output.write(struct.pack("I",length[n]))
newOffset+=length[n]
output.write(ssData)
for x in range(0,header.count):
output.write(string[x])
output.close()
if xlsxMode:
for txtFN in glob.glob(txtF+"*.xlsx"):
print(txtFN)
workBook=openpyxl.load_workbook(txtFN)
for sheet in workBook:
name=sheet['D1'].value
if name==None or len(sheet.title)<31:
name=sheet.title
print(name)
inFN=inF+name
outFN=outF+name
size=os.path.getsize(inFN)
file=open(inFN,'rb')
header=Header(file)
file.seek(header.index)
offset=[]
length=[]
for n in range(0,header.count):
offset.append(struct.unpack('I',file.read(4))[0])
length.append(struct.unpack('I',file.read(4))[0])
string=[]
for x in range(0,header.count):
if length[x]==0:
string.append(b'')
continue
file.seek(header.offset+offset[x]*2,0)
string.append(file.read(length[x]*2))
file.seek(header.offset)
ssData=file.read()
file.close()
for a,c in zip(sheet['A'],sheet['C']):
try:
index=int(a.value)
except:
continue
text=c.value
if text==None:
text=""
length[index]=len(text)
string[index]=Decrypt(text.encode("UTF-16")[2:],length[index],index)
output=open(outFN,'wb')
output.write(struct.pack("I",header.length))
output.write(header.headerData[:16])
output.write(struct.pack("I",size))
output.write(header.headerData[20:])
newOffset=0
for n in range(0,header.count):
output.write(struct.pack("I",newOffset))
output.write(struct.pack("I",length[n]))
newOffset+=length[n]
output.write(ssData)
for x in range(0,header.count):
output.write(string[x])
output.close()
return True
if __name__=="__main__":
main(sys.argv)