Phase 2.9 complete
This commit is contained in:
@@ -6,6 +6,7 @@ adopted file (under MEDIA_ROOT) and return plain dicts/values.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import zipfile
|
||||
|
||||
INJECTOR_NAMES = {'V': 'Vaginal', 'M': 'Mouth', 'A': 'Anal'}
|
||||
@@ -25,6 +26,57 @@ def _open_zip(path):
|
||||
return None
|
||||
|
||||
|
||||
DEFAULT_PACK_BLOCK = {
|
||||
'pack_format': 64,
|
||||
'supported_formats': [64, 81],
|
||||
'min_format': 64,
|
||||
'max_format': 81,
|
||||
}
|
||||
|
||||
|
||||
def inject_animationframework(path, fields, create_pack_block=None):
|
||||
"""Rewrite the zip at ``path`` in place so pack.mcmeta carries the
|
||||
animationframework block (id/name/author/version/description).
|
||||
|
||||
When the archive already has pack.mcmeta, only the animationframework key
|
||||
is added/updated and every other field is preserved. When it has none, a
|
||||
whole pack.mcmeta is created (pack block from ``create_pack_block``, which
|
||||
defaults to the current NoN pack format). Returns True on success.
|
||||
"""
|
||||
tmp = f'{path}.nfnorm'
|
||||
try:
|
||||
with zipfile.ZipFile(path, 'r') as zin:
|
||||
names = set(zin.namelist())
|
||||
if 'pack.mcmeta' in names:
|
||||
try:
|
||||
mcmeta = _decode(zin.read('pack.mcmeta'))
|
||||
except (KeyError, json.JSONDecodeError):
|
||||
mcmeta = {}
|
||||
if not isinstance(mcmeta.get('pack'), dict):
|
||||
mcmeta['pack'] = dict(create_pack_block or DEFAULT_PACK_BLOCK)
|
||||
else:
|
||||
block = dict(create_pack_block or DEFAULT_PACK_BLOCK)
|
||||
mcmeta = {'pack': block}
|
||||
|
||||
mcmeta['animationframework'] = fields
|
||||
|
||||
with zipfile.ZipFile(tmp, 'w', zipfile.ZIP_DEFLATED) as zout:
|
||||
for item in zin.infolist():
|
||||
if item.filename == 'pack.mcmeta':
|
||||
continue
|
||||
zout.writestr(item, zin.read(item.filename))
|
||||
zout.writestr('pack.mcmeta', json.dumps(mcmeta, indent=2))
|
||||
os.replace(tmp, path)
|
||||
return True
|
||||
except (OSError, zipfile.BadZipFile):
|
||||
try:
|
||||
if os.path.exists(tmp):
|
||||
os.remove(tmp)
|
||||
except OSError:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def read_pack_mcmeta(path):
|
||||
"""Extract the NoN-relevant data from a datapack's pack.mcmeta.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user