2023-01-18 15:25:22 +02:00
|
|
|
from os import makedirs, path
|
|
|
|
from typing import Any, Tuple, Union
|
|
|
|
from uuid import uuid4
|
2023-01-19 14:47:18 +02:00
|
|
|
from zipfile import ZIP_DEFLATED, ZipFile
|
2023-01-16 14:23:18 +02:00
|
|
|
from ujson import loads, dumps, JSONDecodeError
|
|
|
|
from traceback import print_exc
|
|
|
|
|
|
|
|
# Print to stdout and then to log
|
|
|
|
def logWrite(message: str, debug: bool = False) -> None:
|
|
|
|
# save to log file and rotation is to be done
|
|
|
|
# logAppend(f'{message}', debug=debug)
|
|
|
|
print(f"{message}", flush=True)
|
|
|
|
|
|
|
|
def jsonLoad(filepath: str) -> Any:
|
|
|
|
"""Load json file
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* filepath (`str`): Path to input file
|
|
|
|
|
|
|
|
### Returns:
|
|
|
|
* `Any`: Some json deserializable
|
|
|
|
"""
|
|
|
|
with open(filepath, "r", encoding='utf8') as file:
|
|
|
|
try:
|
|
|
|
output = loads(file.read())
|
|
|
|
except JSONDecodeError:
|
|
|
|
logWrite(f"Could not load json file {filepath}: file seems to be incorrect!\n{print_exc()}")
|
|
|
|
raise
|
|
|
|
except FileNotFoundError:
|
|
|
|
logWrite(f"Could not load json file {filepath}: file does not seem to exist!\n{print_exc()}")
|
|
|
|
raise
|
|
|
|
file.close()
|
|
|
|
return output
|
|
|
|
|
|
|
|
def jsonSave(contents: Union[list, dict], filepath: str) -> None:
|
|
|
|
"""Save contents into json file
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* contents (`Union[list, dict]`): Some json serializable
|
|
|
|
* filepath (`str`): Path to output file
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(filepath, "w", encoding='utf8') as file:
|
|
|
|
file.write(dumps(contents, ensure_ascii=False, indent=4))
|
|
|
|
file.close()
|
|
|
|
except Exception as exp:
|
|
|
|
logWrite(f"Could not save json file {filepath}: {exp}\n{print_exc()}")
|
|
|
|
return
|
|
|
|
|
|
|
|
def configGet(key: str, *args: str) -> Any:
|
|
|
|
"""Get value of the config key
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* key (`str`): The last key of the keys path.
|
|
|
|
* *args (`str`): Path to key like: dict[args][key].
|
|
|
|
|
|
|
|
### Returns:
|
|
|
|
* `Any`: Value of provided key
|
|
|
|
"""
|
|
|
|
this_dict = jsonLoad("config.json")
|
|
|
|
this_key = this_dict
|
|
|
|
for dict_key in args:
|
|
|
|
this_key = this_key[dict_key]
|
2023-01-18 15:25:22 +02:00
|
|
|
return this_key[key]
|
|
|
|
|
2023-01-19 14:47:18 +02:00
|
|
|
def zip_saves(save_filename: str, save_bytes: bytes, saveinfo_bytes: bytes) -> Tuple[str, str]:
|
|
|
|
"""Save files of the SV save into archive and return uuid and path
|
2023-01-18 15:25:22 +02:00
|
|
|
|
|
|
|
### Args:
|
2023-01-19 14:47:18 +02:00
|
|
|
* save_filename (`str`): Filename of the save file
|
|
|
|
vsave_bytes (`bytes`): Bytes of the save file
|
|
|
|
* saveinfo_bytes (`bytes`): Bytes of the save info file
|
2023-01-18 15:25:22 +02:00
|
|
|
|
|
|
|
### Returns:
|
2023-01-19 14:47:18 +02:00
|
|
|
* `Tuple[str, str]`: First element is an UUID and the second is a filepath
|
2023-01-18 15:25:22 +02:00
|
|
|
"""
|
2023-01-19 14:47:18 +02:00
|
|
|
|
|
|
|
save_uuid = str(uuid4())
|
|
|
|
|
|
|
|
makedirs(path.join(configGet("data", "locations"), "files", save_uuid))
|
|
|
|
|
|
|
|
with ZipFile(path.join(configGet("data", "locations"), "files", save_uuid, save_filename+".svsave"), 'w', ZIP_DEFLATED, compresslevel=configGet("compression")) as ziph:
|
|
|
|
ziph.writestr("SaveGameInfo", saveinfo_bytes)
|
|
|
|
ziph.writestr(save_filename, save_bytes)
|
|
|
|
|
|
|
|
return save_uuid, path.join(configGet("data", "locations"), "files", save_uuid, save_filename+".svsave")
|