Created
May 27, 2024 14:12
-
-
Save BoBkiNN/7f0a0d5c83949800d107f6127e979bb5 to your computer and use it in GitHub Desktop.
Python script to decode BDStudio project file to JSON. BDStudio: https://eszesbalint.github.io/bdstudio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip, argparse, json, base64, os | |
from io import TextIOWrapper | |
from typing import Any | |
PRETTY_INDENT = 2 | |
def main(): | |
parser = argparse.ArgumentParser( | |
prog='bdstudio_decode.py', | |
description='Decode BDStudio (https://eszesbalint.github.io/bdstudio) project to JSON', | |
epilog='By https://github.com/BoBkiNN') | |
parser.add_argument('input', type=argparse.FileType("r", encoding="utf-8"), help="Input .bdstudio file") | |
parser.add_argument('out', type=argparse.FileType("w", encoding="utf-8"), help="Output JSON file") | |
parser.add_argument('-p', '--pretty', action='store_true', help="Writes pretty JSON instead of minimized") | |
args = parser.parse_args() | |
input: TextIOWrapper = args.input | |
out: TextIOWrapper = args.out | |
pretty: bool = args.pretty | |
print(f"Loading project {input.name}") | |
d = load_proj(input) | |
input.close() | |
if d is None: | |
os.remove(out.name) | |
return | |
s = save_data(d, out, pretty) | |
if s: | |
print(f"Done! Saved {out.name}") | |
else: | |
os.remove(out.name) | |
def save_data(data: dict[str, Any], out: TextIOWrapper, pretty: bool): | |
try: | |
json.dump(data, out, indent= PRETTY_INDENT if pretty else None) | |
return True | |
except Exception as e: | |
print("Failed to load data", e) | |
return False | |
def load_proj(input: TextIOWrapper): | |
bd = base64.b64decode(input.read()) | |
try: | |
jb = gzip.decompress(bd) | |
d: list[dict[str, Any]] = json.loads(jb.decode("utf-8")) | |
return d[0] | |
except Exception as e: | |
print("ERROR: Failed to load project", e) | |
return None | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment