From 5bb340cc5d22017d703e94ecd883f7f0ba4b8df3 Mon Sep 17 00:00:00 2001 From: runcows Date: Sat, 11 Oct 2025 19:48:08 +0900 Subject: [PATCH 1/2] Update `manifest.py` to allow overlays of init functions --- gm4/plugins/manifest.py | 68 ++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/gm4/plugins/manifest.py b/gm4/plugins/manifest.py index 73ebdf7dcb..d2a3e23337 100644 --- a/gm4/plugins/manifest.py +++ b/gm4/plugins/manifest.py @@ -268,38 +268,44 @@ def write_credits(ctx: Context): def write_updates(ctx: Context): """Writes the module update commands to this module's init function.""" - init = ctx.data.functions.get(f"{ctx.project_id}:init", None) - if init is None: - return + init_functions = [ + ctx.data.overlays[overlay].functions.get(f"{ctx.project_id}:init", None) + for overlay in ctx.data.overlays.keys() + ] + init_functions.append(ctx.data.functions.get(f"{ctx.project_id}:init", None)) + + for init in init_functions: + if init is None: + continue - manifest = ManifestCacheModel.parse_obj(ctx.cache["gm4_manifest"].json) - modules = manifest.modules - - score = f"{ctx.project_id.removeprefix('gm4_')} gm4_modules" - version = Version(modules[ctx.project_id].version) - - # Update score setter for this module, and add version to gm4:log - last_i=-1 - for i, line in enumerate(init.lines): - if "gm4_modules" in line: - init.lines[i] = line.replace(f"{score} 1", f"{score} {version.int_rep()}").replace(f"{score} matches 1", f"{score} matches {version.int_rep()}") - last_i = i - - init.lines.insert(last_i+1, f"data modify storage gm4:log versions append value {{id:\"{ctx.project_id}\",module:\"{ctx.project_name}\",version:\"{version}\"}}") - - # Remove the marker if it exists - if "#$moduleUpdateList" in init.lines: - init.lines.remove("#$moduleUpdateList") - - # Append the module update list regardless if the marker existed - init.lines.append("# Module update list") - init.lines.append("data remove storage gm4:log queue[{type:'outdated'}]") - for i, m in modules.items(): - if not i.startswith("gm4_"): - continue # not a datapack (ie the rp) and has score to print - version = Version(m.version).int_rep() - website = f"https://gm4.co/modules/{m.id[4:].replace('_','-')}" - init.lines.append(f"execute if score {m.id} load.status matches -1.. if score {m.id.removeprefix('gm4_')} gm4_modules matches ..{version - 1} run data modify storage gm4:log queue append value {{type:'outdated',module:'{m.name}',download:'{website}',render:{{'text':'{m.name}','click_event':{{'action':'open_url','url':'{website}'}},'hover_event':{{'action':'show_text','value':{{'text':'Click to visit {website}','color':'#4AA0C7'}}}}}}}}") + manifest = ManifestCacheModel.parse_obj(ctx.cache["gm4_manifest"].json) + modules = manifest.modules + + score = f"{ctx.project_id.removeprefix('gm4_')} gm4_modules" + version = Version(modules[ctx.project_id].version) + + # Update score setter for this module, and add version to gm4:log + last_i=-1 + for i, line in enumerate(init.lines): + if "gm4_modules" in line: + init.lines[i] = line.replace(f"{score} 1", f"{score} {version.int_rep()}").replace(f"{score} matches 1", f"{score} matches {version.int_rep()}") + last_i = i + + init.lines.insert(last_i+1, f"data modify storage gm4:log versions append value {{id:\"{ctx.project_id}\",module:\"{ctx.project_name}\",version:\"{version}\"}}") + + # Remove the marker if it exists + if "#$moduleUpdateList" in init.lines: + init.lines.remove("#$moduleUpdateList") + + # Append the module update list regardless if the marker existed + init.lines.append("# Module update list") + init.lines.append("data remove storage gm4:log queue[{type:'outdated'}]") + for i, m in modules.items(): + if not i.startswith("gm4_"): + continue # not a datapack (ie the rp) and has score to print + version = Version(m.version).int_rep() + website = f"https://gm4.co/modules/{m.id[4:].replace('_','-')}" + init.lines.append(f"execute if score {m.id} load.status matches -1.. if score {m.id.removeprefix('gm4_')} gm4_modules matches ..{version - 1} run data modify storage gm4:log queue append value {{type:'outdated',module:'{m.name}',download:'{website}',render:{{'text':'{m.name}','click_event':{{'action':'open_url','url':'{website}'}},'hover_event':{{'action':'show_text','value':{{'text':'Click to visit {website}','color':'#4AA0C7'}}}}}}}}") def repro_structure_to_bytes(content: StructureFileData) -> bytes: """a modified Structure.to_bytes from beet, which ensures the GZip does not add From bcf64eec34acd8afae9bef8b3b02516103b0e460 Mon Sep 17 00:00:00 2001 From: Misode Date: Mon, 3 Nov 2025 23:11:54 +0100 Subject: [PATCH 2/2] Refactor changes --- gm4/plugins/manifest.py | 78 ++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/gm4/plugins/manifest.py b/gm4/plugins/manifest.py index d2a3e23337..dba404fe1d 100644 --- a/gm4/plugins/manifest.py +++ b/gm4/plugins/manifest.py @@ -10,7 +10,7 @@ from typing import Any, Optional import yaml -from beet import Context, InvalidProjectConfig, PluginOptions, TextFile, load_config +from beet import Context, InvalidProjectConfig, PluginOptions, TextFile, load_config, Function from beet.library.base import _dump_files # type: ignore ; private method used to deterministicify pack dumping from nbtlib.contrib.minecraft import StructureFileData, StructureFile # type: ignore ; no stub from pydantic.v1 import BaseModel, Extra @@ -268,45 +268,45 @@ def write_credits(ctx: Context): def write_updates(ctx: Context): """Writes the module update commands to this module's init function.""" - init_functions = [ - ctx.data.overlays[overlay].functions.get(f"{ctx.project_id}:init", None) - for overlay in ctx.data.overlays.keys() - ] - init_functions.append(ctx.data.functions.get(f"{ctx.project_id}:init", None)) - - for init in init_functions: - if init is None: - continue + for overlay in ctx.data.overlays.values(): + write_update_function(overlay.functions.get(f"{ctx.project_id}:init"), ctx) + write_update_function(ctx.data.functions.get(f"{ctx.project_id}:init"), ctx) + + +def write_update_function(init: Optional[Function], ctx: Context): + if not init: + return + + manifest = ManifestCacheModel.parse_obj(ctx.cache["gm4_manifest"].json) + modules = manifest.modules + + score = f"{ctx.project_id.removeprefix('gm4_')} gm4_modules" + version = Version(modules[ctx.project_id].version) + + # Update score setter for this module, and add version to gm4:log + last_i=-1 + for i, line in enumerate(init.lines): + if "gm4_modules" in line: + init.lines[i] = line.replace(f"{score} 1", f"{score} {version.int_rep()}").replace(f"{score} matches 1", f"{score} matches {version.int_rep()}") + last_i = i + + init.lines.insert(last_i+1, f"data modify storage gm4:log versions append value {{id:\"{ctx.project_id}\",module:\"{ctx.project_name}\",version:\"{version}\"}}") + + # Remove the marker if it exists + if "#$moduleUpdateList" in init.lines: + init.lines.remove("#$moduleUpdateList") + + # Append the module update list regardless if the marker existed + init.lines.append("# Module update list") + init.lines.append("data remove storage gm4:log queue[{type:'outdated'}]") + for i, m in modules.items(): + if not i.startswith("gm4_"): + continue # not a datapack (ie the rp) and has score to print + version = Version(m.version).int_rep() + website = f"https://gm4.co/modules/{m.id[4:].replace('_','-')}" + init.lines.append(f"execute if score {m.id} load.status matches -1.. if score {m.id.removeprefix('gm4_')} gm4_modules matches ..{version - 1} run data modify storage gm4:log queue append value {{type:'outdated',module:'{m.name}',download:'{website}',render:{{'text':'{m.name}','click_event':{{'action':'open_url','url':'{website}'}},'hover_event':{{'action':'show_text','value':{{'text':'Click to visit {website}','color':'#4AA0C7'}}}}}}}}") + - manifest = ManifestCacheModel.parse_obj(ctx.cache["gm4_manifest"].json) - modules = manifest.modules - - score = f"{ctx.project_id.removeprefix('gm4_')} gm4_modules" - version = Version(modules[ctx.project_id].version) - - # Update score setter for this module, and add version to gm4:log - last_i=-1 - for i, line in enumerate(init.lines): - if "gm4_modules" in line: - init.lines[i] = line.replace(f"{score} 1", f"{score} {version.int_rep()}").replace(f"{score} matches 1", f"{score} matches {version.int_rep()}") - last_i = i - - init.lines.insert(last_i+1, f"data modify storage gm4:log versions append value {{id:\"{ctx.project_id}\",module:\"{ctx.project_name}\",version:\"{version}\"}}") - - # Remove the marker if it exists - if "#$moduleUpdateList" in init.lines: - init.lines.remove("#$moduleUpdateList") - - # Append the module update list regardless if the marker existed - init.lines.append("# Module update list") - init.lines.append("data remove storage gm4:log queue[{type:'outdated'}]") - for i, m in modules.items(): - if not i.startswith("gm4_"): - continue # not a datapack (ie the rp) and has score to print - version = Version(m.version).int_rep() - website = f"https://gm4.co/modules/{m.id[4:].replace('_','-')}" - init.lines.append(f"execute if score {m.id} load.status matches -1.. if score {m.id.removeprefix('gm4_')} gm4_modules matches ..{version - 1} run data modify storage gm4:log queue append value {{type:'outdated',module:'{m.name}',download:'{website}',render:{{'text':'{m.name}','click_event':{{'action':'open_url','url':'{website}'}},'hover_event':{{'action':'show_text','value':{{'text':'Click to visit {website}','color':'#4AA0C7'}}}}}}}}") - def repro_structure_to_bytes(content: StructureFileData) -> bytes: """a modified Structure.to_bytes from beet, which ensures the GZip does not add the current time.time to the nbt file header.