Package manager app started using tkinter

This commit is contained in:
2023-02-15 22:27:45 +01:00
parent cf6dd4de24
commit 48a0d4e20c
48 changed files with 343 additions and 124 deletions

View File

@ -0,0 +1,2 @@
from .manager import PackageManager

14
source/packages/errors.py Normal file
View File

@ -0,0 +1,14 @@
class FilepathNotFound(Exception):
pass
class GamePackageMissing(Exception):
pass
class GamePackageMalformed(Exception):
pass
class PackageAlreadyLoaded(Exception):
pass
class InvalidPackageType(Exception):
pass

View File

@ -0,0 +1,41 @@
import os, json
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 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)

View File

@ -0,0 +1,32 @@
import posixpath
from direct.stdpy.file import walk
class Package:
def __init__(self, meta, mount_dir, vfs):
self.meta = meta
self.mount_dir = mount_dir
self.vfs = vfs
def all_models(self):
models_path = posixpath.join(self.mount_dir, 'models')
output = []
for root, paths, files in walk(models_path):
rel_root = root.replace(f'{models_path}', '').strip()
if len(rel_root) > 0 and rel_root[0] == '/':
rel_root = rel_root[1:]
for filepath in files:
output.append(posixpath.join(rel_root, filepath))
return output
def all_textures(self):
textures_path = posixpath.join(self.mount_dir, 'textures')
output = []
for root, paths, files in walk(textures_path):
rel_root = root.replace(f'{textures_path}', '').strip()
if len(rel_root) > 0 and rel_root[0] == '/':
rel_root = rel_root[1:]
for filepath in files:
output.append(posixpath.join(rel_root, filepath))
return output