Skip to content

Commit

Permalink
Add Hexical, fix error during registry load if lookup pattern page lo…
Browse files Browse the repository at this point in the history
…oks up a duplicate pattern that will fail later
  • Loading branch information
object-Object committed Oct 1, 2024
1 parent 5a694bb commit 5025d3f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"configurations": [
{
"name": "main.py",
"type": "debugpy",
"request": "launch",
"program": "main.py",
"console": "integratedTerminal"
}
]
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ runtime = [
"hexdoc-oneironaut==0.3.4.1.0",
"hexdoc-hexdebug==0.2.2.1.19.2.1.0",
"hexdoc-hexcellular==1.0.2.1.0.dev0",
"hexdoc-hexical==1.3.2.1.0.dev0",
"hexdoc==1!0.1.0a18",
"importlib_resources>=5.10",
"jproperties~=2.1",
Expand Down
37 changes: 28 additions & 9 deletions src/HexBug/hexdecode/buildpatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from .hex_math import Direction
from .registry import DuplicatePatternException, NormalPatternInfo, Registry

logger = logging.getLogger(__name__)

translation_regex = re.compile(r"hexcasting.spell.[a-z]+:(.+)")
header_regex = re.compile(r"\s*\(.+\)")

Expand Down Expand Up @@ -196,9 +198,14 @@ def _build_hexdoc_urls(registry: Registry, categories: Iterable[Category], mod:
page,
text,
):
pattern = registry.from_name[page.patterns[0].id.path]
if pattern.translation:
urls[pattern.translation] = value
pattern_id = page.patterns[0].id
if pattern := registry.from_name.get(pattern_id.path):
if pattern.translation:
urls[pattern.translation] = value
else:
logger.warning(
f"Unknown pattern for {mod.name} in lookup page #{entry.id.path}@{page.anchor}: {pattern_id}"
)

case PageWithPattern() if value := _build_hexdoc_pattern_urls(
registry,
Expand Down Expand Up @@ -271,12 +278,6 @@ async def build_registry(session: ClientSession) -> Registry | None:
classname_to_path, file_path.stem, mod, file_path.as_posix()
)

for info in build_extra_patterns(name_to_translation):
if info.classname and info.class_mod and info.path:
_insert_classname(
classname_to_path, info.classname, info.class_mod, info.path
)

for classname, path in mod_info.extra_classname_paths.items():
_insert_classname(classname_to_path, classname, mod, path)

Expand Down Expand Up @@ -330,6 +331,13 @@ async def build_registry(session: ClientSession) -> Registry | None:

# TODO: load classnames

# do this last so all the translations are loaded
for info in build_extra_patterns(name_to_translation):
if info.classname and info.class_mod and info.path:
_insert_classname(
classname_to_path, info.classname, info.class_mod, info.path
)

# patterns and books

duplicate_exceptions: list[DuplicatePatternException] = []
Expand Down Expand Up @@ -439,6 +447,17 @@ async def build_registry(session: ClientSession) -> Registry | None:
if isinstance(mod_info, APIWithBookModInfo):
_build_urls(registry, categories, mod)

# FIXME: hack
if (
name_to_translation.get("get_light")
== name_to_translation.get("blockstate_glow")
== "Luminance Purification"
):
name_to_translation |= {
"get_light": "Luminance Purification (get_light)",
"blockstate_glow": "Luminance Purification (blockstate_glow)",
}

for mod in HexdocMod:
mod_info = mod.value

Expand Down
2 changes: 2 additions & 0 deletions src/HexBug/hexdecode/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ async def generate_pattern(
def parse_shorthand(self, shorthand: str) -> SpecialHandlerArgument:
"""Attempts to parse a full shorthand pattern into an argument for this
pattern.
Optional.
"""
return None

Expand Down
36 changes: 34 additions & 2 deletions src/HexBug/utils/extra_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
PatternInfo,
SpecialHandlerPatternInfo,
)
from .mods import RegistryMod
from .special_handlers import MaskSpecialHandler, NumberSpecialHandler
from .mods import HexdocMod, RegistryMod
from .special_handlers import (
MaskSpecialHandler,
NumberSpecialHandler,
TailDepthSpecialHandler,
)

_extra_patterns: list[PatternInfo] | None = None

Expand Down Expand Up @@ -74,5 +78,33 @@ def build_extra_patterns(name_to_translation: dict[str, str]) -> list[PatternInf
shorthand_names=("book",),
handler=MaskSpecialHandler(),
),
SpecialHandlerPatternInfo(
name="nephthys",
translation=name_to_translation.get("nephthys"),
mod=HexdocMod.Hexical,
path="src/main/java/miyucomics/hexical/casting/patterns/eval/OpNephthys.kt",
classname="OpNephthys",
class_mod=HexdocMod.Hexical,
is_great=False,
handler=TailDepthSpecialHandler(
direction=Direction.SOUTH_EAST,
prefix="deaqqd",
initial_depth=1,
),
),
SpecialHandlerPatternInfo(
name="sekhmet",
translation=name_to_translation.get("sekhmet"),
mod=HexdocMod.Hexical,
path="src/main/java/miyucomics/hexical/casting/patterns/eval/OpSekhmet.kt",
classname="OpSekhmet",
class_mod=HexdocMod.Hexical,
is_great=False,
handler=TailDepthSpecialHandler(
direction=Direction.SOUTH_WEST,
prefix="qaqdd",
initial_depth=0,
),
),
]
return _extra_patterns
11 changes: 11 additions & 0 deletions src/HexBug/utils/mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ class HexdocMod(Enum):
modloaders=[FABRIC, QUILT],
)

Hexical = HexdocModInfo(
name="Hexical",
modid="hexical",
book_id="hexical:hexicalbook",
description="A fun addon containing genie lamps, mage blocks, specks, world scrying, and more!",
curseforge_url=None,
modrinth_slug="hexical",
icon_url="https://raw.githubusercontent.com/miyucomics/hexical/8ad329395acfdaa30db01d5ce00d61dd446b2db4/src/main/resources/assets/hexical/icon.png",
modloaders=[FABRIC, QUILT],
)


ModInfo = RegistryModInfo | APIModInfo | HexdocModInfo
Mod = RegistryMod | APIMod | HexdocMod
Expand Down
54 changes: 54 additions & 0 deletions src/HexBug/utils/special_handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from fractions import Fraction
from typing import Any

Expand All @@ -17,6 +18,8 @@
from .parse_rational import parse_rational
from .patterns import parse_mask

# hex casting


class NumberSpecialHandler(SpecialHandler):
def parse_pattern(self, direction: Direction, pattern: str) -> Any | None:
Expand Down Expand Up @@ -68,3 +71,54 @@ async def generate_pattern(
raise InvalidSpecialHandlerArgumentException(self.info.display_name)

return [generate_bookkeeper(arg)]


# hexical


@dataclass(kw_only=True)
class TailDepthSpecialHandler(SpecialHandler):
direction: Direction
prefix: str
initial_depth: int

def parse_pattern(self, direction: Direction, pattern: str) -> Any | None:
tail = pattern.removeprefix(self.prefix)
if tail == pattern:
return None

depth = self.initial_depth
for index, char in enumerate(tail):
if char != "qe"[index % 2]:
return None
depth += 1

return depth

def parse_argument(self, value: str) -> SpecialHandlerArgument:
# number, eg. Nephthys' Gambit: 3
try:
return int(value)
except ValueError:
pass

# visual tail, eg. Nephthys' Gambit: ---
if value == "-" * len(value):
return len(value)

return None

async def generate_pattern(
self,
registry: Registry,
arg: SpecialHandlerArgument,
should_align_horizontal: bool,
) -> list[tuple[Direction, str]]:
if not isinstance(arg, int) or arg < self.initial_depth:
raise InvalidSpecialHandlerArgumentException(self.info.display_name)

pattern = self.prefix
for index in range(arg - self.initial_depth):
pattern += "qe"[index % 2]

return [(self.direction, pattern)]

0 comments on commit 5025d3f

Please sign in to comment.