76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
"""Stage the Paper anti-xray subtree for reviewed DuneRealms mining worlds."""
|
|
import argparse
|
|
from hashlib import sha256
|
|
from pathlib import Path
|
|
import re
|
|
|
|
OVERWORLD = {
|
|
"arrakis": ("coal", "emerald"),
|
|
"caladan": ("coal", "iron", "copper", "lapis"),
|
|
"giedi_prime": ("coal", "iron", "copper", "redstone"),
|
|
"kaitain": ("gold", "emerald", "diamond", "lapis"),
|
|
"sigma_draconis_iv": ("iron", "copper", "redstone", "diamond"),
|
|
}
|
|
|
|
|
|
def profile(server, dimension):
|
|
if dimension == "the_nether" and server == "giedi_prime":
|
|
return 128, ("ancient_debris", "nether_gold_ore", "nether_quartz_ore")
|
|
if dimension != "overworld" or server not in OVERWORLD:
|
|
raise ValueError("This world is outside the reviewed mining scope")
|
|
minerals = OVERWORLD[server]
|
|
hidden = [block for mineral in minerals for block in (mineral + "_ore", "deepslate_" + mineral + "_ore")]
|
|
hidden.extend("raw_" + mineral + "_block" for mineral in minerals if mineral in {"iron", "copper"})
|
|
return 320, tuple(hidden)
|
|
|
|
|
|
def render(current, server, dimension):
|
|
height, hidden = profile(server, dimension)
|
|
text = current.decode("utf-8")
|
|
nl = "\r\n" if "\r\n" in text else "\n"
|
|
if re.search(r"(?m)^[ \t]*anti-xray[ \t]*:", text):
|
|
raise ValueError("Existing anti-xray override requires a separate reviewed preimage")
|
|
rows = [" anti-xray:", " enabled: true", " engine-mode: 1", f" max-block-height: {height}",
|
|
" update-radius: 2", " use-permission: false", " lava-obscures: false",
|
|
" replacement-blocks: []", " hidden-blocks:"]
|
|
rows.extend(" - " + ore for ore in hidden)
|
|
subtree = nl.join(rows) + nl
|
|
headers = list(re.finditer(r"(?m)^anticheat[ \t]*:[^\r\n]*(?:\r?\n|\Z)", text))
|
|
if not headers:
|
|
suffix = "" if not text or text.endswith("\n") else nl
|
|
return (text + suffix + "anticheat:" + nl + subtree).encode("utf-8")
|
|
if len(headers) != 1:
|
|
raise ValueError("Expected one Paper anticheat mapping")
|
|
header = headers[0]
|
|
value = header.group().split(":", 1)[1].split("#", 1)[0].strip()
|
|
if value not in ("", "{}"):
|
|
raise ValueError("Existing anticheat node is not a writable block mapping")
|
|
insert_at = header.end()
|
|
if value == "{}":
|
|
start = text.index("{}", header.start(), header.end())
|
|
text = text[:start] + text[start + 2:]
|
|
insert_at -= 2
|
|
if not text[:insert_at].endswith("\n"):
|
|
subtree = nl + subtree
|
|
return (text[:insert_at] + subtree + text[insert_at:]).encode("utf-8")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("server")
|
|
parser.add_argument("dimension")
|
|
parser.add_argument("preimage", type=Path)
|
|
parser.add_argument("expected_sha")
|
|
parser.add_argument("output", type=Path)
|
|
args = parser.parse_args()
|
|
current = args.preimage.read_bytes()
|
|
if sha256(current).hexdigest() != args.expected_sha:
|
|
raise ValueError("Paper config differs from the verified preimage")
|
|
result = render(current, args.server, args.dimension)
|
|
with args.output.open("xb") as file:
|
|
file.write(result)
|
|
print(sha256(result).hexdigest())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|

Staff