54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
import bpy
|
|
import json
|
|
import os
|
|
import urllib.request
|
|
|
|
manifest = None # Object JSON
|
|
|
|
def getManifestPath():
|
|
manifestPath = bpy.utils.extension_path_user(__package__, create=True)
|
|
return os.path.join(manifestPath, "manifest.json")
|
|
|
|
|
|
def loadManifest():
|
|
global manifest
|
|
|
|
manifestPath = getManifestPath()
|
|
if os.path.exists(manifestPath):
|
|
with open(manifestPath, "r") as f:
|
|
manifest = json.load(f)
|
|
return manifest
|
|
else:
|
|
return None
|
|
|
|
|
|
def getManifest():
|
|
global manifest
|
|
|
|
if manifest is None:
|
|
manifest = loadManifest()
|
|
return manifest
|
|
|
|
|
|
def fetchAndSaveManifest():
|
|
global manifest
|
|
# Fetch from server: https://dcrubro.com/files/d2toolbox_manifest.json ; TODO: This is my personal server, and I should maybe allow people to use their own server.
|
|
# Will hardcode for now
|
|
# Blender extensions should be "self-contained", meaning that they shouldn't use non-default libraries. So, we will use urllib.request instead of requests.
|
|
url = "https://dcrubro.com/files/d2toolbox_manifest.json"
|
|
request = urllib.request.Request(url, headers={"User-Agent": "D2Toolbox (Blender add-on)"})
|
|
with urllib.request.urlopen(request) as response:
|
|
manifest = json.loads(response.read().decode())
|
|
|
|
if manifest is None:
|
|
raise Exception("Failed to fetch manifest from server.")
|
|
|
|
if (manifest.get("version") is None) or (manifest.get("version") != 1):
|
|
raise Exception("Invalid manifest version.")
|
|
|
|
manifestPath = getManifestPath()
|
|
|
|
with open(manifestPath, "w") as f:
|
|
json.dump(manifest, f)
|
|
return manifest
|