Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /randovania_randovania /randovania /game_description /binary_data.py
| from __future__ import annotations | |
| import copy | |
| from typing import TYPE_CHECKING, BinaryIO | |
| import construct.core | |
| from construct import Construct, If | |
| from construct.core import ( | |
| Compressed, | |
| Const, | |
| Default, | |
| Flag, | |
| Float64b, | |
| PrefixedArray, | |
| Struct, | |
| Switch, | |
| VarInt, | |
| ) | |
| from randovania.game.game_enum import RandovaniaGame | |
| from randovania.game_description import game_description_migration | |
| from randovania.game_description.db.hint_node import HintNodeKind | |
| from randovania.lib.construct_lib import ( | |
| ConstructDict, | |
| DefaultsAdapter, | |
| JsonEncodedValue, | |
| OptionalValue, | |
| String, | |
| convert_to_raw_python, | |
| ) | |
| if TYPE_CHECKING: | |
| from pathlib import Path | |
| current_format_version = 13 | |
| _EXPECTED_FIELDS = [ | |
| "schema_version", | |
| "game", | |
| "resource_database", | |
| "layers", | |
| "starting_location", | |
| "minimal_logic", | |
| "victory_condition", | |
| "dock_type_database", | |
| "hint_feature_database", | |
| "used_trick_levels", | |
| "flatten_to_set_on_patch", | |
| "regions", | |
| ] | |
| def decode(binary_io: BinaryIO) -> dict: | |
| decoded = ConstructGame.parse_stream(binary_io) | |
| result: dict = convert_to_raw_python(decoded["db"]) | |
| if unknown_keys := [key for key in result if key not in _EXPECTED_FIELDS]: | |
| raise ValueError(f"Unexpected fields in decoded data: {unknown_keys}") | |
| return result | |
| def decode_file_path(binary_file_path: Path) -> dict: | |
| with binary_file_path.open("rb") as binary_io: | |
| return decode(binary_io) | |
| def encode(original_data: dict, x: BinaryIO) -> None: | |
| if unknown_keys := [key for key in original_data if key not in _EXPECTED_FIELDS]: | |
| raise ValueError(f"Unexpected fields in data to be encoded: {unknown_keys}") | |
| data = copy.deepcopy(original_data) | |
| ConstructGame.build_stream({"db": data}, x) | |
| def _build_resource_info(**kwargs: Construct) -> Struct: | |
| return Struct( | |
| long_name=String, | |
| **kwargs, | |
| extra=JsonEncodedValue, | |
| ) | |
| def _build_enum(**mapping: int) -> construct.core.Enum: | |
| """Create a construct enum from the mapping, using VarInt.""" | |
| return construct.core.Enum(VarInt, **mapping) | |
| ConstructAreaIdentifier = construct.Struct( | |
| region=String, | |
| area=String, | |
| ) | |
| ConstructNodeIdentifier = construct.Struct( | |
| region=String, | |
| area=String, | |
| node=String, | |
| ) | |
| ConstructResourceInfo = _build_resource_info() | |
| ConstructItemResourceInfo = _build_resource_info( | |
| max_capacity=VarInt, | |
| ) | |
| ConstructTrickResourceInfo = _build_resource_info( | |
| description=String, | |
| require_documentation_above=VarInt, | |
| ) | |
| ConstructDamageReductions = Struct( | |
| name=String, | |
| reductions=PrefixedArray( | |
| VarInt, | |
| Struct( | |
| name=String, | |
| quantity=VarInt, | |
| multiplier=Float64b, | |
| ), | |
| ), | |
| ) | |
| ConstructResourceType = _build_enum(items=0, events=1, tricks=2, damage=3, versions=4, misc=5) | |
| ConstructResourceRequirement = Struct( | |
| type=ConstructResourceType, | |
| name=String, | |
| amount=VarInt, | |
| negate=Flag, | |
| ) | |
| requirement_type_map = { | |
| "resource": ConstructResourceRequirement, | |
| "template": String, | |
| "node": ConstructNodeIdentifier, | |
| } | |
| ConstructRequirement = Struct( | |
| type=_build_enum(resource=0, **{"and": 1, "or": 2}, template=3, node=4), | |
| data=Switch(lambda this: this.type, requirement_type_map), | |
| ) | |
| ConstructRequirementArray = Struct( | |
| comment=OptionalValue(String), | |
| items=PrefixedArray(VarInt, ConstructRequirement), | |
| ) | |
| requirement_type_map["and"] = ConstructRequirementArray | |
| requirement_type_map["or"] = ConstructRequirementArray | |
| ConstructDockLock = Struct( | |
| lock_type=String, | |
| requirement=ConstructRequirement, | |
| ) | |
| ConstructDockWeakness = Struct( | |
| extra=JsonEncodedValue, | |
| requirement=ConstructRequirement, | |
| lock=OptionalValue(ConstructDockLock), | |
| unsafe_target_in_distributor_wtw=Flag, | |
| ) | |
| ConstructNamedTemplate = Struct( | |
| display_name=String, | |
| requirement=ConstructRequirement, | |
| ) | |
| ConstructResourceDatabase = Struct( | |
| items=ConstructDict(ConstructItemResourceInfo), | |
| events=ConstructDict(ConstructResourceInfo), | |
| tricks=ConstructDict(ConstructTrickResourceInfo), | |
| damage=ConstructDict(ConstructResourceInfo), | |
| versions=ConstructDict(ConstructResourceInfo), | |
| misc=ConstructDict(ConstructResourceInfo), | |
| requirement_template=ConstructDict(ConstructNamedTemplate), | |
| damage_reductions=PrefixedArray(VarInt, ConstructDamageReductions), | |
| energy_tank_item_index=String, | |
| ) | |
| ConstructResourceGain = Struct( | |
| resource_type=ConstructResourceType, | |
| resource_name=String, | |
| amount=VarInt, | |
| ) | |
| ConstructHintNodeKind = _build_enum(**{enum_value.value: i for i, enum_value in enumerate(HintNodeKind)}) | |
| ConstructNodeCoordinates = Struct( | |
| x=Float64b, | |
| y=Float64b, | |
| z=Float64b, | |
| ) | |
| NodeBaseFields = { | |
| "heal": Flag, | |
| "coordinates": OptionalValue(ConstructNodeCoordinates), | |
| "description": String, | |
| "layers": PrefixedArray(VarInt, String), | |
| "extra": JsonEncodedValue, | |
| "connections": ConstructDict(ConstructRequirement), | |
| "valid_starting_location": Flag, | |
| } | |
| class NodeAdapter(construct.Adapter): | |
| def _decode(self, obj: construct.Container, context: construct.Container, path: str) -> construct.Container: | |
| result = construct.Container(node_type=obj["node_type"]) | |
| result.update(obj["data"]) | |
| result["connections"] = result.pop("connections") | |
| return result | |
| def _encode(self, obj: construct.Container, context: construct.Container, path: str) -> construct.Container: | |
| data = copy.copy(obj) | |
| return construct.Container( | |
| node_type=data.pop("node_type"), | |
| data=data, | |
| ) | |
| ConstructNode = NodeAdapter( | |
| Struct( | |
| node_type=_build_enum(generic=0, dock=1, pickup=2, event=4, configurable_node=5, hint=6, teleporter_network=7), | |
| data=Switch( | |
| lambda this: this.node_type, | |
| { | |
| "generic": Struct( | |
| **NodeBaseFields, | |
| ), | |
| "dock": Struct( | |
| **NodeBaseFields, | |
| dock_type=String, | |
| default_connection=ConstructNodeIdentifier, | |
| default_dock_weakness=String, | |
| exclude_from_dock_rando=Flag, | |
| incompatible_dock_weaknesses=PrefixedArray(VarInt, String), | |
| override_default_open_requirement=OptionalValue(ConstructRequirement), | |
| override_default_lock_requirement=OptionalValue(ConstructRequirement), | |
| ui_custom_name=OptionalValue(String), | |
| ), | |
| "pickup": Struct( | |
| **NodeBaseFields, | |
| pickup_index=VarInt, | |
| location_category=_build_enum(major=0, minor=1), | |
| custom_index_group=OptionalValue(String), | |
| hint_features=PrefixedArray(VarInt, String), | |
| ), | |
| "event": Struct( | |
| **NodeBaseFields, | |
| event_name=String, | |
| ), | |
| "configurable_node": Struct( | |
| **NodeBaseFields, | |
| ), | |
| "hint": DefaultsAdapter( | |
| Struct( | |
| **NodeBaseFields, | |
| kind=ConstructHintNodeKind, | |
| requirement_to_collect=ConstructRequirement, | |
| target_index=Default( | |
| If( | |
| lambda ctx: ctx.kind == HintNodeKind.SPECIFIC_LOCATION.value, | |
| VarInt, | |
| ), | |
| None, | |
| ), | |
| specific_pickup_hint_id=Default( | |
| If( | |
| lambda ctx: ctx.kind == HintNodeKind.SPECIFIC_PICKUP.value, | |
| String, | |
| ), | |
| None, | |
| ), | |
| ), | |
| ), | |
| "teleporter_network": Struct( | |
| **NodeBaseFields, | |
| is_unlocked=ConstructRequirement, | |
| network=String, | |
| requirement_to_activate=ConstructRequirement, | |
| ), | |
| }, | |
| ), | |
| ) | |
| ) | |
| ConstructArea = Struct( | |
| default_node=OptionalValue(String), | |
| hint_features=PrefixedArray(VarInt, String), | |
| extra=JsonEncodedValue, | |
| nodes=ConstructDict(ConstructNode), | |
| ) | |
| ConstructRegion = Struct( | |
| name=String, | |
| extra=JsonEncodedValue, | |
| areas=ConstructDict(ConstructArea), | |
| ) | |
| ConstructGameEnum = _build_enum(**{enum_item.value: i for i, enum_item in enumerate(RandovaniaGame)}) | |
| ConstructMinimalLogicDatabase = Struct( | |
| items_to_exclude=PrefixedArray( | |
| VarInt, | |
| Struct( | |
| name=String, | |
| when_shuffled=OptionalValue(String), | |
| ), | |
| ), | |
| custom_item_amount=PrefixedArray( | |
| VarInt, | |
| Struct( | |
| name=String, | |
| value=VarInt, | |
| ), | |
| ), | |
| events_to_exclude=PrefixedArray( | |
| VarInt, | |
| Struct( | |
| name=String, | |
| reason=OptionalValue(String), | |
| ), | |
| ), | |
| description=String, | |
| ) | |
| ConstructDockTypeDatabase = Struct( | |
| types=ConstructDict( | |
| Struct( | |
| name=String, | |
| extra=JsonEncodedValue, | |
| items=ConstructDict(ConstructDockWeakness), | |
| weakness_distributor=OptionalValue( | |
| Struct( | |
| unlocked=String, | |
| locked=String, | |
| change_from=PrefixedArray(VarInt, String), | |
| change_to=PrefixedArray(VarInt, String), | |
| indirect_change_from=PrefixedArray(VarInt, String), | |
| force_change_two_way=Flag, | |
| resolver_attempts=VarInt, | |
| to_shuffle_proportion=Float64b, | |
| ui_label=String, | |
| ) | |
| ), | |
| ) | |
| ), | |
| default_weakness=Struct( | |
| type=String, | |
| name=String, | |
| ), | |
| ) | |
| ConstructUsedTrickLevels = OptionalValue(ConstructDict(PrefixedArray(VarInt, VarInt))) | |
| ConstructHintFeatureDatabase = ConstructDict( | |
| DefaultsAdapter( | |
| Struct( | |
| long_name=String, | |
| hint_details=String[2], | |
| hidden=Default(Flag, False), | |
| description=Default(String, ""), | |
| ) | |
| ) | |
| ) | |
| ConstructGame = Struct( | |
| magic_number=Const(b"Req."), | |
| format_version=Const(current_format_version, VarInt), | |
| db=Compressed( | |
| Struct( | |
| schema_version=Const(game_description_migration.CURRENT_VERSION, VarInt), | |
| game=ConstructGameEnum, | |
| resource_database=ConstructResourceDatabase, | |
| layers=PrefixedArray(VarInt, String), | |
| starting_location=ConstructNodeIdentifier, | |
| minimal_logic=OptionalValue(ConstructMinimalLogicDatabase), | |
| victory_condition=ConstructRequirement, | |
| dock_type_database=ConstructDockTypeDatabase, | |
| hint_feature_database=ConstructHintFeatureDatabase, | |
| used_trick_levels=ConstructUsedTrickLevels, | |
| flatten_to_set_on_patch=Flag, | |
| regions=PrefixedArray(VarInt, ConstructRegion), | |
| ), | |
| "lzma", | |
| ), | |
| ) | |
Xet Storage Details
- Size:
- 11.3 kB
- Xet hash:
- bf3267e182b283be47e67860c02f4630253658b447ca478c5fd4c14cd092191a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.