plugins/tools/plugin_exchange_market.py

91 lines
3.8 KiB
Python

"""Package scoped Exchange browsing and resale classes or assemble pinned retained JARs."""
import argparse
from hashlib import sha256
from pathlib import Path
import subprocess
from tempfile import TemporaryDirectory
from zipfile import ZipFile
EXCHANGE = "com/dunerealms/gui/ExchangeMenus"
GUI_REPLACE = {f"{EXCHANGE}.class", f"{EXCHANGE}$Purchase.class"}
GUI_ADD = {f"{EXCHANGE}$ListingCategory.class", f"{EXCHANGE}$ListingSort.class"}
TOOLBAR = "com/dunerealms/menus/SuppliedDocument.class"
GUI_BASELINES = {
"cc297181ebc26b1750a0cb1abc96f02c58c02d5b56ac5a016229933f84093475",
"54e32d06818cacf70437cc6bed99bee92eea7826ec95903018f4909fc0cb338d",
}
MENU_BASELINES = {
"cd5de929f70ee40c12b34a355f2f8c9f23c4cf0829d80713feef431a405c126d",
"70417e9b62cf3151c511b7cba7f78b173aea6c73e618b842860536462301ae93",
}
def entries(archive):
names = archive.namelist()
if len(names) != len(set(names)):
raise ValueError("Duplicate archive entry")
return set(names)
def package(gui, menus, output):
with ZipFile(gui) as a, ZipFile(menus) as b:
if not (GUI_ADD | GUI_REPLACE) <= entries(a) or TOOLBAR not in entries(b):
raise ValueError("Expected Exchange donor classes are missing")
with ZipFile(output, "x") as capsule:
for name in sorted(GUI_ADD | GUI_REPLACE):
capsule.writestr(name, a.read(name))
capsule.writestr(TOOLBAR, b.read(TOOLBAR))
def toolbar(retained, donor, classpath):
if not classpath:
raise ValueError("An ASM classpath is required")
with TemporaryDirectory() as temp:
before, new, result = (Path(temp) / name for name in ("before.class", "donor.class", "result.class"))
before.write_bytes(retained)
new.write_bytes(donor)
subprocess.run(["java", "--class-path", classpath,
str(Path(__file__).with_name("ExchangeMarketToolbarMerge.java")),
str(before), str(new), str(result)], check=True)
return result.read_bytes()
def assemble(capsule, baseline, output, classpath):
baseline_hash = sha256(baseline.read_bytes()).hexdigest()
with ZipFile(capsule) as donor, ZipFile(baseline) as retained:
names = entries(retained)
if entries(donor) != GUI_ADD | GUI_REPLACE | {TOOLBAR}:
raise ValueError("Unexpected Exchange capsule scope")
if baseline_hash in GUI_BASELINES and GUI_REPLACE <= names and not GUI_ADD & names:
replacements = {name: donor.read(name) for name in GUI_REPLACE | GUI_ADD}
elif baseline_hash in MENU_BASELINES and TOOLBAR in names:
replacements = {TOOLBAR: toolbar(retained.read(TOOLBAR), donor.read(TOOLBAR), classpath)}
else:
raise ValueError("Unexpected retained JAR")
with ZipFile(output, "x") as result:
for entry in retained.infolist():
data = replacements[entry.filename] if entry.filename in replacements else retained.read(entry)
result.writestr(entry, data)
for name in sorted(replacements.keys() - names):
result.writestr(name, replacements[name])
def main():
parser = argparse.ArgumentParser(description=__doc__)
commands = parser.add_subparsers(dest="command", required=True)
pack = commands.add_parser("package")
for field in ("gui", "menus", "output"):
pack.add_argument(field, type=Path)
install = commands.add_parser("assemble")
for field in ("capsule", "baseline", "output"):
install.add_argument(field, type=Path)
install.add_argument("--classpath")
args = parser.parse_args()
if args.command == "package":
package(args.gui, args.menus, args.output)
else:
assemble(args.capsule, args.baseline, args.output, args.classpath)
if __name__ == "__main__":
main()