58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
|
|
import os, json
|
|
import posixpath
|
|
from .package import Package
|
|
from .errors import *
|
|
|
|
class PackageManager:
|
|
def __init__(self, vfs):
|
|
self.vfs = vfs
|
|
self.package_map = {}
|
|
self.package_list = []
|
|
self.main_package = None
|
|
|
|
def unmount_all(self):
|
|
self.vfs.unmount(self.main_package.mount_dir)
|
|
for package in self.package_list:
|
|
self.vfs.unmount(package.mount_dir)
|
|
|
|
def get_package_meta(self, filepath):
|
|
if not os.path.exists(filepath):
|
|
raise FilepathNotFound(filepath)
|
|
gamepkg_path = os.path.join(filepath, 'gamepackage.json')
|
|
if not os.path.exists(gamepkg_path):
|
|
raise GamePackageMissing(gamepkg_path)
|
|
meta = json.load(open(gamepkg_path))
|
|
if 'name' not in meta or 'type' not in meta:
|
|
raise GamePackageMalformed(gamepkg_path)
|
|
return meta
|
|
|
|
def set_main_package(self, filepath):
|
|
meta = self.get_package_meta(filepath)
|
|
if meta['type'] != 'main':
|
|
raise InvalidPackageType(filepath, meta['type'])
|
|
vfs_path = f'/main/'
|
|
self.vfs.mount(filepath, vfs_path, 0)
|
|
new_package = Package(type('', (object,), meta), vfs_path, self.vfs)
|
|
self.main_package = new_package
|
|
|
|
def include_package(self, filepath):
|
|
meta = self.get_package_meta(filepath)
|
|
if meta['name'] in self.package_map:
|
|
raise PackageAlreadyLoaded(meta['name'])
|
|
vfs_path = f'/include/{meta["name"]}'
|
|
self.vfs.mount(filepath, vfs_path, 0)
|
|
new_package = Package(type('', (object,), meta), vfs_path, self.vfs)
|
|
self.package_map[meta['name']] = new_package
|
|
self.package_list.insert(0, new_package)
|
|
|
|
def get_model_path(self, name):
|
|
for imported_package in reversed(self.package_list):
|
|
target_path = posixpath.join(imported_package.mount_dir, 'models', name)
|
|
if self.vfs.exists(target_path):
|
|
return target_path
|
|
main_path = posixpath.join(self.main_package.mount_dir, 'models', name)
|
|
if (self.vfs.exists(main_path)):
|
|
return main_path
|
|
return None
|