-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.py
164 lines (129 loc) · 5.89 KB
/
publish.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
import os, shutil, sys
import struct, shutil
PANDA = r"built"
PANDA_BIN = os.path.join(PANDA, "bin")
PANDA_INC = os.path.join(PANDA, "include")
PANDA_LIBS = os.path.join(PANDA, "lib")
PANDA_ETC = os.path.join(PANDA, "etc")
PYTHON = os.path.join(PANDA, "python")
PYTHON_LIBS = os.path.join(PYTHON, "libs")
PYTHON_INC = os.path.join(PYTHON, "include")
PYTHON_DLLS = os.path.join(PYTHON, "DLLs")
TTSRC = r"builtTT"
TTSRC_TMP = os.path.join(TTSRC, "tmp")
TTSRC_CLIENT = os.path.join(TTSRC, "client")
RESOURCES = r"resources"
def add(path, name, targetName=None):
if not targetName:
targetName = name
path = os.path.join(path, name)
if os.path.isfile(path):
if os.path.isfile(os.path.join(TTSRC_CLIENT, targetName)) and not targetName.endswith(".mf"):
os.remove(os.path.join(TTSRC_CLIENT, targetName))
if not os.path.isfile(os.path.join(TTSRC_CLIENT, targetName)):
shutil.copyfile(path, os.path.join(TTSRC_CLIENT, targetName))
else:
if os.path.isdir(os.path.join(TTSRC_CLIENT, targetName)):
shutil.rmtree(os.path.join(TTSRC_CLIENT, targetName))
shutil.copytree(path, os.path.join(TTSRC_CLIENT, targetName))
def hide(data):
last = 99
res = bytearray()
for char in data:
res.append(char - last & 255)
last = char
return bytes(res)
if not os.path.isdir(TTSRC_TMP):
os.mkdir(TTSRC_TMP)
if not os.path.isdir(TTSRC_CLIENT):
os.mkdir(TTSRC_CLIENT)
for file in os.listdir(TTSRC_CLIENT):
path = os.path.join(TTSRC_CLIENT, file)
if os.path.isfile(path):
if not file.endswith(".mf"):
os.remove(path)
else:
shutil.rmtree(path)
# entrypoint
# for now, we don't want to build the entrypoint with the `build` command,
# so we're just gonna put its source
with open("ttlauncher.py", "rb") as file:
launcher = file.read()
# we hide it a bit and we mark the end
launcher = hide(launcher) + b"\0"
# load modules
with open(os.path.join(TTSRC_TMP, "ttmodules.h"), "w") as file:
# we write in the launcher
codeFormatted = '{ ' + ", ".join("0x" + (format(char, "x").rjust(2, "0")) for char in launcher) + ' }'
file.write("static unsigned char ENTRYPOINT[] = " + codeFormatted + ";\n")
file.write("static const unsigned int ENTRYPOINT_SIZE = " + str(len(launcher)-1) + ";\n\n")
if "-embed" in sys.argv:
with open(os.path.join(TTSRC, "Toontown.bin"), "rb") as f:
data = f.read()
modules = {}
index = 0
while index < len(data):
modType, nameLen, codeLen = struct.unpack("<BHI", data[index:index+7])
name = data[index+7:index+7+nameLen].decode("ascii")
code = data[index+7+nameLen:index+7+nameLen+codeLen]
modules[name] = (modType, code)
index += 7+nameLen+codeLen
file.write("#define FROZEN_ENABLED\n");
file.write("struct FrozenModule {\n")
file.write(" char* name;\n")
file.write(" int type;\n")
file.write(" unsigned int length;\n")
file.write(" char* code;\n")
file.write("};\n\n")
for name, data in sorted(modules.items()):
codeName = "code" + "".join(x[0].upper() + x[1:] for x in name.split("."))
codeFormatted = '{ ' + ", ".join("0x" + (format(char, "x").rjust(2, "0")) for char in data[1]) + ' }'
file.write("static const unsigned char " + codeName + "[] = " + codeFormatted + ";\n")
file.write("static struct FrozenModule FROZEN_MODULES[] = {\n")
for name, data in sorted(modules.items()):
codeName = "code" + "".join(x[0].upper() + x[1:] for x in name.split("."))
file.write(" { \"" + name + "\", " + str(data[0]) + ", " + str(len(data[1])) + ", (char*)" + str(codeName) + " },\n")
file.write("};\n")
else:
file.write("#undef FROZEN_ENABLED")
# build client
if os.system("cl ttclient.cpp /EHsc /I" + str(TTSRC_TMP) + " /I" + str(PYTHON_INC) + " /Fo" + os.path.join(TTSRC_TMP, "ttclient.obj") + " /link /OUT:" + os.path.join(TTSRC_TMP, "Toontown.exe") + " /LIBPATH:" + str(PYTHON_LIBS) + (" /subsystem:windows /entry:mainCRTStartup" if "-gui" in sys.argv else "")):
sys.exit()
if os.system("cl ttconfigrc.cpp /EHsc /DUSE_ENGLISH /I" + str(TTSRC_TMP) + " /I" + str(PYTHON_INC) + " /I" + str(PANDA_INC) + " /Fo" + os.path.join(TTSRC_TMP, "ttconfigrc.obj") + " /link /OUT:" + os.path.join(TTSRC_TMP, "Configrc.exe") + " /LIBPATH:" + str(PYTHON_LIBS) + " /LIBPATH:" + str(PANDA_LIBS) + " Gdi32.lib user32.lib advapi32.lib"):
sys.exit()
if os.system(r'"thirdparty\win-util\upx" ' + os.path.join(TTSRC_TMP, "Configrc.exe")):
sys.exit()
if "-embed" in sys.argv:
if os.system(r'"thirdparty\win-util\upx" ' + os.path.join(TTSRC_TMP, "Toontown.exe")):
sys.exit()
add(TTSRC_TMP, "Toontown.exe")
# add files
if not "-embed" in sys.argv:
add(TTSRC, "Toontown.bin")
add(TTSRC_TMP, "Configrc.exe")
for file in os.listdir(RESOURCES):
add(RESOURCES, file)
add(PYTHON, "python26.dll")
add(PYTHON_DLLS, "unicodedata.pyd")
add(PYTHON_DLLS, "_ssl.pyd")
add(PYTHON_DLLS, "_socket.pyd")
add(PYTHON_DLLS, "_bsddb.pyd")
add(PANDA_BIN, "libpanda.dll")
add(PANDA_BIN, "libpandaexpress.dll")
add(PANDA_BIN, "libpandaphysics.dll")
add(PANDA_BIN, "libp3direct.dll")
add(PANDA_BIN, "libp3dtool.dll")
add(PANDA_BIN, "libp3dtoolconfig.dll")
add(PANDA_BIN, "libp3windisplay.dll")
add(PANDA_BIN, "libpandagl.dll")
add(PANDA_BIN, "libpandaode.dll")
add(PANDA_BIN, "libpandafx.dll")
add(PANDA_BIN, "libp3vision.dll")
add(PANDA_BIN, "libpandaskel.dll")
add(PANDA_BIN, "libtinydisplay.dll")
add(PANDA_BIN, "libotp.dll")
add(PANDA_BIN, "libtoontown.dll")
add(PANDA_BIN, "libp3miles_audio.dll")
add(PANDA_BIN, "cg.dll")
add(PANDA_BIN, "cgGL.dll")
shutil.rmtree(TTSRC_TMP)