-
Notifications
You must be signed in to change notification settings - Fork 0
/
DL2IDScript.py
221 lines (173 loc) · 7.59 KB
/
DL2IDScript.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
# MIT License
# Copyright © 2023 Lomeli
#
# 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 stat
import shutil
import pathlib
import zipfile
BASE_DATA_NAME = "data"
DATA_PAK_EXT = ".pak"
DATA_0_FILE_NAME = BASE_DATA_NAME + "0" + DATA_PAK_EXT
SCRIPTS_DIR_NAME = "scripts"
PLAYER_DIR_NAME = "player"
PLAYER_SCRIPTS_PATH = "" + SCRIPTS_DIR_NAME + "/" + PLAYER_DIR_NAME + "/"
PLAYER_NORMAL_SCRIPT = "player_variables.scr"
PLAYER_NORMAL_SCRIPT_PATH = PLAYER_SCRIPTS_PATH + PLAYER_NORMAL_SCRIPT
PLAYER_NORMAL_DURABILITY_TEXT = " Param(\"MeleeWpnDurabilityMulReduce\", \"1.0\");"
PLAYER_EASY_SCRIPT = "player_variables_easy.scr"
PLAYER_EASY_SCRIPT_PATH = PLAYER_SCRIPTS_PATH + PLAYER_EASY_SCRIPT
PLAYER_EASY_DURABILITY_TEXT = " Param(\"MeleeWpnDurabilityMulReduce\", \"0.75\");"
PLAYER_NIGHTMARE_SCRIPT = "player_variables_nightmare.scr"
PLAYER_NIGHTMARE_SCRIPT_PATH = PLAYER_SCRIPTS_PATH + PLAYER_NIGHTMARE_SCRIPT
PLAYER_NIGHTMARE_DURABILITY_TEXT = " Param(\"MeleeWpnDurabilityMulReduce\", \"1.25\");"
INFINITE_DURABILITY_PATCH_TEXT = " Param(\"MeleeWpnDurabilityMulReduce\", \"0.0\");"
data_0_location = ""
data_0_parent_dir = ""
scripts_dir = ""
player_dir = ""
temp_folder = ""
temp_normal_path = ""
temp_easy_path = ""
temp_nightmare_path = ""
def patch_script(script_path, patch_loc, name):
with open(script_path, 'r') as script:
script_data = script.read()
print("-> Patching " + name)
script_data = script_data.replace(patch_loc, INFINITE_DURABILITY_PATCH_TEXT)
with open(script_path, 'w') as script:
script.write(script_data)
def patch_scripts():
patch_script(temp_normal_path, PLAYER_NORMAL_DURABILITY_TEXT, PLAYER_NORMAL_SCRIPT)
patch_script(temp_easy_path, PLAYER_EASY_DURABILITY_TEXT, PLAYER_EASY_SCRIPT)
patch_script(temp_nightmare_path, PLAYER_NIGHTMARE_DURABILITY_TEXT, PLAYER_NIGHTMARE_SCRIPT)
def create_temp_folder():
global temp_folder
temp_folder = os.path.join(os.getcwd(), "temp_mod_folder")
if os.path.exists(temp_folder):
shutil.rmtree(temp_folder)
os.mkdir(temp_folder)
global scripts_dir
scripts_dir = os.path.join(temp_folder, SCRIPTS_DIR_NAME)
os.mkdir(scripts_dir)
global player_dir
player_dir = os.path.join(scripts_dir, PLAYER_DIR_NAME)
os.mkdir(player_dir)
def find_suitable_pak_name(count):
data_pak = os.path.join(data_0_parent_dir, BASE_DATA_NAME + str(count) + DATA_PAK_EXT)
if os.path.exists(data_pak):
count = count + 1
return find_suitable_pak_name(count)
return data_pak
def create_or_replace_mod():
replace_pak = input("Replace data2.pak, if it exists? (Y/N): ")
mod_data_pak = os.path.join(data_0_parent_dir, BASE_DATA_NAME + "2" + DATA_PAK_EXT)
if replace_pak.lower() == "n":
print("-> Looking for unused data pak number")
mod_data_pak = find_suitable_pak_name(3)
print("-> Using " + os.path.basename(mod_data_pak))
elif replace_pak.lower() != "y":
create_or_replace_mod()
package_mod(mod_data_pak)
# The following is copied from https://github.com/bboe/deterministic_zip/
# Saves so many headaches from mismatched hashes
def add_file(zip_file, path, zip_path=None):
permission = 0o555 if os.access(path, os.X_OK) else 0o444
zip_info = zipfile.ZipInfo.from_file(path, zip_path)
zip_info.date_time = (2019, 1, 1, 0, 0, 0)
zip_info.external_attr = (stat.S_IFREG | permission) << 16
with open(path, "rb") as fp:
zip_file.writestr(
zip_info,
fp.read(),
zipfile.ZIP_DEFLATED,
9,
)
# End of copying
def package_mod(data_pak):
print("-> Creating " + data_pak + "!")
if os.path.exists(data_pak):
print("-> Removing old " + os.path.basename(data_pak))
os.remove(data_pak)
with zipfile.ZipFile(data_pak, "w") as new_pak:
for root, dirs, files in os.walk(temp_folder):
for file in files:
add_file(new_pak, os.path.join(root, file),
os.path.relpath(os.path.join(root, file), os.path.join(scripts_dir, '..')))
def clean_up():
print("-> Cleaning up temp folder")
if os.path.exists(temp_folder):
shutil.rmtree(temp_folder)
def extract_player_scripts():
with zipfile.ZipFile(data_0_location) as data:
global temp_normal_path
temp_normal_path = os.path.join(player_dir, PLAYER_NORMAL_SCRIPT)
extract_scripts(data, PLAYER_NORMAL_SCRIPT, PLAYER_NORMAL_SCRIPT_PATH, temp_normal_path)
global temp_easy_path
temp_easy_path = os.path.join(player_dir, PLAYER_EASY_SCRIPT)
extract_scripts(data, PLAYER_EASY_SCRIPT, PLAYER_EASY_SCRIPT_PATH, temp_easy_path)
global temp_nightmare_path
temp_nightmare_path = os.path.join(player_dir, PLAYER_NIGHTMARE_SCRIPT)
extract_scripts(data, PLAYER_NIGHTMARE_SCRIPT, PLAYER_NIGHTMARE_SCRIPT_PATH, temp_nightmare_path)
def extract_scripts(data, script_name, script_path, script_out_path):
print("-> Extracting " + script_name)
with open(script_out_path, 'wb') as script:
script.write(data.read(script_path))
def handle_data_0_location(file_path):
file_correct = False
if os.path.isdir(file_path):
file_path = os.path.join(file_path, DATA_0_FILE_NAME)
if os.path.exists(file_path):
print("data0.pak located at: " + file_path)
correct = input("Is this correct? (Y/N/Q): ")
if correct.lower() == "y":
global data_0_location
data_0_location = file_path
global data_0_parent_dir
data_0_parent_dir = pathlib.Path(file_path).parent
file_correct = True
elif correct.lower() == "q":
print("-> Stopping script...")
os.system('pause')
exit()
else:
print("Could not find data0.pak at " + file_path)
print(file_path + " does not exist!")
if not file_correct:
handle_data_0_location(input("Type in the path for data0.pak: "))
def create_mod():
start_mod_creation = input("Create Infinite Durability mod using " + data_0_location + "? (Y/N): ")
if start_mod_creation.lower() == "n":
exit()
if start_mod_creation.lower() != "y":
create_mod()
create_temp_folder()
extract_player_scripts()
patch_scripts()
create_or_replace_mod()
clean_up()
def intro_message():
print("||==============================================||")
print("|| Dying Light 2 Durability Mod Creation Script ||")
print("||================= By Lomeli ==================||")
intro_message()
handle_data_0_location(os.path.join(os.getcwd(), DATA_0_FILE_NAME))
create_mod()
os.system('pause')