Skip to content

Commit 6d2c786

Browse files
committed
Creating deterministic zips now
Did not know that zips by default weren't deterministic. Because of this, if 2 people used the script to make the mod and tried to join each other's game, it would fail as despite their mods being identical, they would have completely different hashes. Luckily I found Bryce Boe's deterministic_zip script, which solves the issue.
1 parent a8f8a74 commit 6d2c786

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

DL2IDScript.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# SOFTWARE.
2121

2222
import os
23+
import stat
2324
import shutil
2425
import pathlib
2526
import zipfile
@@ -112,14 +113,34 @@ def create_or_replace_mod():
112113
package_mod(mod_data_pak)
113114

114115

116+
# The following is copied from https://github.com/bboe/deterministic_zip/
117+
# Saves so many headaches from mismatched hashes
118+
def add_file(zip_file, path, zip_path=None):
119+
permission = 0o555 if os.access(path, os.X_OK) else 0o444
120+
zip_info = zipfile.ZipInfo.from_file(path, zip_path)
121+
zip_info.date_time = (2019, 1, 1, 0, 0, 0)
122+
zip_info.external_attr = (stat.S_IFREG | permission) << 16
123+
with open(path, "rb") as fp:
124+
zip_file.writestr(
125+
zip_info,
126+
fp.read(),
127+
zipfile.ZIP_DEFLATED,
128+
9,
129+
)
130+
# End of copying
131+
132+
115133
def package_mod(data_pak):
116134
print("-> Creating " + data_pak + "!")
117135
if os.path.exists(data_pak):
118136
print("-> Removing old " + os.path.basename(data_pak))
119137
os.remove(data_pak)
120138

121-
shutil.make_archive(data_pak, 'zip', temp_folder)
122-
os.rename(data_pak + ".zip", data_pak)
139+
with zipfile.ZipFile(data_pak, "w") as new_pak:
140+
for root, dirs, files in os.walk(temp_folder):
141+
for file in files:
142+
add_file(new_pak, os.path.join(root, file),
143+
os.path.relpath(os.path.join(root, file), os.path.join(scripts_dir, '..')))
123144

124145

125146
def clean_up():

0 commit comments

Comments
 (0)