This commit is contained in:
Евгений Храмов 2023-05-14 15:32:06 +03:00
parent edc4ada5b5
commit fdabffb1e2

81
hlna.py

@ -1,6 +1,7 @@
#!/usr/bin/env python3
import os
import re
import zlib
import struct
import shutil
import datetime
@ -371,44 +372,70 @@ def modinstall(g, m):
def modextract(id_mod, id_game_workshop):
mod_steam_workshop = f"{dir_workshop_ark}/content/{id_game_workshop}/{id_mod}/WindowsNoEditor/"
mod_ark_mods = f"{dir_mods_ark}{id_mod}"
modextractdir = mod_ark_mods
dir_steam_workshop = f"{dir_workshop_ark}/content/{id_game_workshop}/{id_mod}/WindowsNoEditor/"
dir_ark_mods = f"{dir_mods_ark}{id_mod}"
dir_extract = dir_ark_mods
if id_mod == "111111111":
return
for dirpath, dirnames, filenames in os.walk(mod_steam_workshop):
for dirpath, dirnames, filenames in os.walk(dir_steam_workshop):
for dname in dirnames:
os.makedirs(os.path.join(modextractdir, os.path.relpath(os.path.join(dirpath, dname), mod_steam_workshop)), exist_ok=True)
os.makedirs(os.path.join(dir_extract, os.path.relpath(os.path.join(dirpath, dname), dir_steam_workshop)), exist_ok=True)
# for fname in filenames:
# if not os.path.isfile(os.path.join(mod_steam_workshop, fname)) and not os.path.isfile(os.path.join(mod_steam_workshop, fname + ".z")):
# os.unlink(os.path.join(modextractdir, fname))
for dname in dirnames:
if not os.path.isdir(os.path.join(mod_steam_workshop, dname)):
shutil.rmtree(os.path.join(modextractdir, os.path.relpath(os.path.join(dirpath, dname), mod_steam_workshop)))
if not os.path.isdir(os.path.join(dir_steam_workshop, dname)):
shutil.rmtree(os.path.join(dir_extract, os.path.relpath(os.path.join(dirpath, dname), dir_steam_workshop)))
for root, dirs, files in os.walk(mod_steam_workshop):
print_line(files)
for name in files:
if not (name.endswith(".z") or name.endswith(".z.uncompressed_size")):
srcfile = os.path.join(root, name)
dstfile = os.path.join(modextractdir, os.path.relpath(srcfile, mod_steam_workshop))
if not os.path.isfile(dstfile) or os.path.getmtime(srcfile) > os.path.getmtime(dstfile):
print_line(dstfile)
shutil.copy2(srcfile, dstfile)
# for root, dirs, files in os.walk(mod_steam_workshop):
# for name in files:
# if not (name.endswith(".z") or name.endswith(".z.uncompressed_size")):
# srcfile = os.path.join(root, name)
# dstfile = os.path.join(modextractdir, os.path.relpath(srcfile, mod_steam_workshop))
# if not os.path.isfile(dstfile) or os.path.getmtime(srcfile) > os.path.getmtime(dstfile):
# print_line(dstfile)
# shutil.copy2(srcfile, dstfile)
for root, dirs, files in os.walk(dir_steam_workshop):
for file in files:
if file.endswith('.z'):
filepath = os.path.join(root, file)
extract_path = os.path.join(dir_extract, os.path.splitext(file)[0])
if not os.path.isfile(extract_path) or os.path.getmtime(filepath) > os.path.getmtime(extract_path):
print(f"{os.path.getsize(filepath):<10} {file:<20} ", end="")
with open(filepath, "rb") as zfile:
data = zfile.read()
if data[0:8] != b"\xC1\x83\x2A\x9E\x00\x00\x00\x00":
raise ValueError("Bad file magic")
chunk_size, compressed_size, uncompressed_size = struct.unpack("<LLL", data[8:20])
chunks = []
compressed_used = 0
while compressed_used < compressed_size:
chunk_header = data[20 + compressed_used:24 + compressed_used]
compressed_chunk_size, uncompressed_chunk_size = struct.unpack("<LL", chunk_header)
chunks.append(data[24 + compressed_used:24 + compressed_used + compressed_chunk_size])
compressed_used += compressed_chunk_size
uncompressed_data = zlib.decompress(b"".join(chunks))
with open(extract_path, "wb") as outfile:
outfile.write(uncompressed_data)
os.utime(extract_path, (os.path.getatime(filepath), os.path.getmtime(filepath)))
print("\r\x1b[K", end="")
modname = subprocess.check_output(['curl', '-s', 'https://steamcommunity.com/sharedfiles/filedetails/?id={}'.format(id_mod)]).decode('utf-8')
modname = re.search(r'<div class="workshopItemTitle">(.+)</div>', modname)
modname = modname and modname.group(1)
if os.path.isfile("{}/.mod".format(modextractdir)):
os.remove("{}/.mod".format(modextractdir))
if os.path.isfile("{}/.mod".format(dir_extract)):
os.remove("{}/.mod".format(dir_extract))
modfile_bytes = b''
with open(os.path.join(modextractdir, 'mod.info'), 'rb') as f:
with open(os.path.join(dir_extract, 'mod.info'), 'rb') as f:
data = f.read()
mapnamelen = struct.unpack_from('<L', data, offset=0)
mapnamelen = mapnamelen[0]
@ -431,20 +458,20 @@ def modextract(id_mod, id_game_workshop):
pos = pos + 4 + mapfilelen
modfile_bytes += b'\x33\xFF\x22\xFF\x02\x00\x00\x00\x01'
if os.path.isfile(os.path.join(modextractdir, "modmeta.info")):
with open(os.path.join(modextractdir, "modmeta.info"), "rb") as f:
with open(f"{modextractdir}.mod", "ab") as f_out:
if os.path.isfile(os.path.join(dir_extract, "modmeta.info")):
with open(os.path.join(dir_extract, "modmeta.info"), "rb") as f:
with open(f"{dir_extract}.mod", "ab") as f_out:
f_out.write(f.read())
else:
with open(f"{modextractdir}.mod", "wb") as f_out:
with open(f"{dir_extract}.mod", "wb") as f_out:
f_out.write(b'\x01\x00\x00\x00\x08\x00\x00\x00ModType\x00\x02\x00\x00\x001\x00')
with open(modextractdir + '.mod', 'wb') as f:
with open(dir_extract + '.mod', 'wb') as f:
f.write(modfile_bytes)
if modextractdir != mod_ark_mods:
if not os.path.isdir(mod_ark_mods):
os.makedirs(mod_ark_mods)
if dir_extract != dir_ark_mods:
if not os.path.isdir(dir_ark_mods):
os.makedirs(dir_ark_mods)
@ hlna.command()