Skip to content

Commit

Permalink
compat new PluginCatalogue schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-1F committed Jan 25, 2024
1 parent 2180b50 commit 2ece11d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 46 deletions.
2 changes: 1 addition & 1 deletion mcdreforged_plugin_manager/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def list_plugins(source: CommandSource, labels: Optional[Union[None, str, List[s

def search(source: CommandSource, query: str):
for plugin in cache.search(query):
source.reply(plugin.brief)
source.reply(plugin.meta.brief)
source.reply('')


Expand Down
2 changes: 1 addition & 1 deletion mcdreforged_plugin_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Configure(Serializable):
DEFAULT_CONFIG = psi.open_bundled_file('resources/default_config.yml')

permission: int = PermissionLevel.PHYSICAL_SERVER_CONTROL_LEVEL
source: str = 'https://github.com/MCDReforged/PluginCatalogue/archive/refs/heads/meta.zip'
source: str = 'https://raw.githubusercontent.com/MCDReforged/PluginCatalogue/meta/everything.json'
timeout: int = 15
cache_interval: int = 30
check_update: bool = True
Expand Down
31 changes: 11 additions & 20 deletions mcdreforged_plugin_manager/storage/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ def stop(self):


class Cache(PluginStorage):
CACHE_PATH = psi.get_data_folder()
CACHE_PATH = os.path.join(psi.get_data_folder(), 'everything.json')

@new_thread('MPMCache')
def cache(self):
before = self.plugin_amount

psi.logger.info(tr('cache.cache'))
zip_path = os.path.join(self.CACHE_PATH, 'meta.zip')

try:
download_file(config.source, zip_path)
unzip(zip_path, self.CACHE_PATH)
# remove cache if exist
if os.path.exists(self.CACHE_PATH) and os.path.isfile(self.CACHE_PATH):
os.remove(self.CACHE_PATH)
download_file(config.source, self.CACHE_PATH)
except Exception as e:
psi.logger.warning(tr('cache.exception', e))
else:
os.remove(zip_path)
self.load()
psi.logger.info(tr('cache.cached', self.plugin_amount - before))

Expand All @@ -71,21 +71,12 @@ def load(self):
self.plugin_amount = 0
self.plugins.clear()

meta_path = os.path.join(self.CACHE_PATH, 'PluginCatalogue-meta')
for plugin_path, _, _ in os.walk(meta_path):
def load_plugin_file(filename: str):
with open(os.path.join(plugin_path, filename), 'r', encoding='utf8') as f:
return json.load(f)

if plugin_path == meta_path:
continue

plugin_json = load_plugin_file('plugin.json')
meta_json = load_plugin_file('meta.json')
release_json = load_plugin_file('release.json')

plugin = Plugin.create(plugin_json, meta_json, release_json)
plugin_id = meta_json['id']
with open(self.CACHE_PATH, 'r') as f:
data = json.load(f)

for plugin in data['plugins'].values():
plugin = Plugin.create(plugin)
plugin_id = plugin.meta.id
self.plugins[plugin_id] = plugin
self.plugin_amount += 1

Expand Down
20 changes: 10 additions & 10 deletions mcdreforged_plugin_manager/storage/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,20 @@ class Plugin(Serializable):
release: ReleaseSummary

@classmethod
def create(cls, plugin_json: dict, meta_json: dict, release_json: dict):
def create(cls, all_of_a_plugin: dict):
"""
Create a plugin from a folder contains `plugin.json`, `meta.json` and `release.json`
of PluginCatalogue `meta` branch
Create a plugin from a AllOfAPlugin object
:param plugin_json: plugin.json
:param meta_json: meta.json
:param release_json: release.json
:param all_of_a_plugin: a AllOfAPlugin object
:return:
"""
meta = MetaInfo.deserialize(meta_json)
meta.labels = plugin_json['labels']

release = ReleaseSummary.deserialize(release_json)
# TODO: refactored to compat latest schema
meta = MetaInfo.deserialize(all_of_a_plugin['meta'])
meta.labels = all_of_a_plugin['plugin']['labels']
meta.repository = all_of_a_plugin['repository']['url']

release = ReleaseSummary.deserialize(all_of_a_plugin['release'])

return cls(meta=meta, release=release)

Expand All @@ -254,7 +254,7 @@ def get_plugins_by_labels(self, labels: Optional[Union[None, str, List[str]]] =
if any([label in labels for label in plugin.meta.labels]):
yield plugin

def search(self, query: str) -> Iterable[MetaInfo]:
def search(self, query: str) -> Iterable[Plugin]:
for plugin in self.plugins.values():
if query in plugin.meta.name or query in plugin.meta.id or query in plugin.meta.description:
yield plugin
Expand Down
13 changes: 5 additions & 8 deletions mcdreforged_plugin_manager/storage/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ class ReleaseInfo(Serializable):
name: str
tag_name: str
created_at: str
assets: List[AssetInfo]
description: str
asset: AssetInfo
description: Optional[str]
prerelease: bool

def get_mcdr_assets(self) -> List[AssetInfo]:
return [asset for asset in self.assets if asset.name.endswith('.mcdr') or asset.name.endswith('.pyz')]


class ReleaseSummary(Serializable):
id: str
latest_version: str
latest_version_index: Optional[int]
releases: List[ReleaseInfo]

def get_latest_release(self) -> Optional[ReleaseInfo]:
if len(self.releases) > 0:
return self.releases[0]
if self.latest_version_index is not None:
return self.releases[self.latest_version_index]
return None
5 changes: 2 additions & 3 deletions mcdreforged_plugin_manager/task/install_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ def operate(self, installer: 'PluginInstaller') -> bool:
if self.operation in [DependencyOperation.INSTALL, DependencyOperation.UPGRADE]:
summary = cache.get_plugin_by_id(self.name).release
release = summary.get_latest_release()
asset = release.get_mcdr_assets()[0]
url = config.release_download_url_template.format(url=asset.browser_download_url)
filename = asset.name
url = config.release_download_url_template.format(url=release.asset.browser_download_url)
filename = release.asset.name
temp_filename = filename + '.temp'
download_filename = temp_filename if self.operation == DependencyOperation.UPGRADE else filename
download_path = os.path.join(self.install_path, download_filename)
Expand Down
6 changes: 3 additions & 3 deletions resources/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# 使用 MCDReforgedPluginManager 指令的最低权限
permission: 4

# The source of plugin catalogue to fetch data, should be the url to download the whole meta branch
# 插件仓库数据源,应是下载整个 meta 分支的链接
source: https://github.com/MCDReforged/PluginCatalogue/archive/refs/heads/meta.zip
# The source of plugin catalogue to fetch data, should be the url to download the everything.json in meta branch
# 插件仓库数据源,应是下载 meta 分支中 everything.json 的链接
source: https://raw.githubusercontent.com/MCDReforged/PluginCatalogue/meta/everything.json

# The timeout for network requests
# 网络请求的超时时间
Expand Down

0 comments on commit 2ece11d

Please sign in to comment.