-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapplyeuddraft.py
276 lines (217 loc) · 9.14 KB
/
applyeuddraft.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2014 trgk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import os
import sys
import traceback
import warnings
import eudplib as ep
import freezeMpq
import msgbox
import scbank_core
from freeze import decryptOffsets, encryptOffsets, obfpatch, obfunpatch, unFreeze
from msgbox import MB_ICONHAND, MB_OK, MessageBeep, MessageBox
from pluginLoader import (
isFreezeIssued,
isPromptIssued,
isSCBankIssued,
loadPluginsFromConfig,
)
from readconfig import readconfig
def createPayloadMain(pluginFuncDict):
@ep.EUDFunc
def payloadMain():
"""Main function of euddraft payload."""
# init plugins
if isFreezeIssued():
unFreeze()
# ep.PRT_SetInliningRate(0.05)
if isSCBankIssued():
scbank_core.onPluginStart()
for onPluginStart in pluginFuncDict.get("onPluginStart", []):
onPluginStart()
# Do trigger loop
if ep.EUDInfLoop()():
if isFreezeIssued():
decryptOffsets()
obfpatch()
if isSCBankIssued():
scbank_core.beforeTriggerExec()
for beforeTriggerExec in pluginFuncDict.get("beforeTriggerExec", []):
beforeTriggerExec()
ep.RunTrigTrigger()
for afterTriggerExec in reversed(
pluginFuncDict.get("afterTriggerExec", [])
):
afterTriggerExec()
if isSCBankIssued():
scbank_core.afterTriggerExec()
if isFreezeIssued():
obfunpatch()
encryptOffsets()
ep.EUDDoEvents()
ep.EUDEndInfLoop()
return payloadMain
##############################
if getattr(sys, "frozen", False):
# frozen
basepath = os.path.dirname(sys.executable)
else:
# unfrozen
basepath = os.path.dirname(os.path.realpath(__file__))
globalPluginPath = os.path.join(basepath, "plugins").lower()
# cx_Freeze modifies ep.__file__ to library.zip. So we use this convoluted
# way of getting eudplib install path.
epPath = os.path.dirname(ep.eudplibVersion.__code__.co_filename).lower()
edPath = os.path.dirname(MessageBox.__code__.co_filename).lower()
def isEpExc(s):
s = s.lower()
return (
epPath in s
or edPath in s
or "<frozen " in s
or (basepath in s and globalPluginPath not in s)
or "runpy.py" in s
or s.startswith(' file "eudplib')
)
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
log = file if hasattr(file, "write") else sys.stderr
traceback.print_stack(file=log)
warning_message = warnings.formatwarning(message, category, filename, lineno, line)
plibPath = "C:\\Users\\armo\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\euddraft-rwy7uEJp-py3.12\\Lib\\site-packages\\eudplib"
warning_message = warning_message.replace(plibPath, "eudplib")
warning_message = warning_message.replace("C:\\dev\\euddraftPrivate\\", "euddraft\\")
log.write(warning_message)
warnings.showwarning = warn_with_traceback
##############################
def applyEUDDraft(sfname):
from eudplib.core.mapdata.tblformat import DecodeUnitNameAs
from eudplib.collections.objpool import SetGlobalPoolFieldN
from eudplib.epscript.epsimp import IsSCDBMap
from eudplib.utils.eperror import ep_warn
try:
config, excs = readconfig(sfname)
if excs:
# See armoha/euddraft#128 : https://github.com/armoha/euddraft/issues/128
# raise ExceptionGroup("Invalid config", excs)
from rich import print as rprint
for exc in excs:
# https://stackoverflow.com/a/76743290
rprint(str(exc))
raise RuntimeError("error(s) on eds/edd configuration")
mainSection = config["main"]
ifname = mainSection["input"]
ofname = mainSection["output"]
if ifname == ofname:
raise RuntimeError("input and output file should be different.")
mainOptions = (
"input",
"output",
"shufflePayload",
"debug",
"decodeUnitName",
"objFieldN",
"sectorSize",
"ptrEUDArray",
"errorReapplyEPD",
"suppressWarnings",
)
invalidMainOptions = []
for mainOption in mainSection:
if mainOption not in mainOptions:
invalidMainOptions.append(mainOption)
if invalidMainOptions:
ep_warn(f"Unused manifest keys in [main]: {', '.join(invalidMainOptions)}")
if "shufflePayload" in mainSection:
ep.ShufflePayload(eval(mainSection["shufflePayload"]))
if "ptrEUDArray" in mainSection:
from eudplib.collections.eudarray import _use_ptr_array
if eval(mainSection["ptrEUDArray"]):
_use_ptr_array()
if "errorReapplyEPD" in mainSection:
from eudplib.utils.etc import _allow_epd_on_epd
_allow_epd_on_epd(not eval(mainSection["errorReapplyEPD"]))
if "suppressWarnings" in mainSection:
from pluginLoader import suppress_warnings
import warnings
suppress_warnings(True)
warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore", category=DeprecationWarning)
if "debug" in mainSection:
ep.EPS_SetDebug(True)
if "decodeUnitName" in mainSection:
unitname_encoding = mainSection["decodeUnitName"]
DecodeUnitNameAs(unitname_encoding)
if "objFieldN" in mainSection:
field_n = int(mainSection["objFieldN"])
SetGlobalPoolFieldN(field_n)
sectorSize = 15
if "sectorSize" in mainSection:
sectorSize = int(mainSection["sectorSize"])
print("---------- Loading plugins... ----------")
ep.LoadMap(ifname)
pluginFuncDict = loadPluginsFromConfig(ep, config)
print("--------- Injecting plugins... ---------")
payloadMain = createPayloadMain(pluginFuncDict)
ep.CompressPayload(True)
if IsSCDBMap():
if isFreezeIssued():
raise RuntimeError(
"Can't use freeze protection on SCDB map!\nDisable freeze by following plugin settings:\n\n[freeze]\nfreeze : 0\n"
)
print("SCDB - sectorSize disabled")
sectorSize = None
elif isFreezeIssued():
# FIXME: Add variable sectorSize support for freeze
print("Freeze - sectorSize disabled")
sectorSize = None
ep.SaveMap(ofname, payloadMain, sector_size=sectorSize)
if isFreezeIssued():
if isPromptIssued():
print("Freeze - prompt enabled ")
sys.stdout.flush()
os.system("pause")
print("[Stage 4/3] Applying freeze mpq modification...")
try:
ofname = ofname.encode("mbcs")
except LookupError:
ofname = ofname.encode(sys.getfilesystemencoding())
ret = freezeMpq.applyFreezeMpqModification(ofname, ofname)
if ret != 0:
raise RuntimeError("Error on mpq protection (%d)" % ret)
MessageBeep(MB_OK)
return True
except Exception as err:
print("==========================================")
MessageBeep(MB_ICONHAND)
exc_type, exc_value, exc_traceback = sys.exc_info()
excs = traceback.format_exception(exc_type, exc_value, exc_traceback)
formatted_excs = []
for i, exc in enumerate(excs):
if isEpExc(exc) and not all(isEpExc(e) for e in excs[i + 1 : -1]):
continue
plibPath = "C:\\Users\\armo\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\euddraft-rwy7uEJp-py3.11\\Lib\\site-packages\\eudplib"
exc = exc.replace(plibPath, "eudplib")
exc = exc.replace("C:\\dev\\euddraftPrivate", "euddraft")
formatted_excs.append(exc)
print(f"[Error] {err}", "".join(formatted_excs), file=sys.stderr)
if msgbox.isWindows:
msgbox.SetForegroundWindow(msgbox.GetConsoleWindow())
return False