-
Notifications
You must be signed in to change notification settings - Fork 10
Add RULE format #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add RULE format #170
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from __future__ import annotations | ||
|
||
import enum | ||
import typing | ||
|
||
import construct | ||
|
||
from retro_data_structures.adapters.enum_adapter import EnumAdapter | ||
from retro_data_structures.base_resource import AssetType, BaseResource, Dependency | ||
from retro_data_structures.common_types import AssetId32, FourCC | ||
from retro_data_structures.construct_extensions.alignment import AlignTo | ||
from retro_data_structures.construct_extensions.misc import ErrorWithMessage | ||
|
||
if typing.TYPE_CHECKING: | ||
from retro_data_structures.game_check import Game | ||
|
||
|
||
class ComparisonOperator(enum.IntEnum): | ||
LessThan = 0 | ||
LessThanOrEqualTo = 1 | ||
EqualTo = 2 | ||
GreaterThanOrEqualTo = 3 | ||
GreaterThan = 4 | ||
|
||
|
||
class ValueType(enum.IntEnum): | ||
Bool = 0 | ||
Float = 1 | ||
Int32 = 2 | ||
|
||
|
||
RuleCondition = construct.Struct( | ||
id=FourCC, | ||
operator=EnumAdapter(ComparisonOperator, construct.Int8ub), | ||
value_type=EnumAdapter(ValueType, construct.Int8ub), | ||
value=construct.Switch( | ||
construct.this.value_type, | ||
{ | ||
ValueType.Bool: construct.Int32ub, | ||
ValueType.Float: construct.Float32b, | ||
ValueType.Int32: construct.Int32ub, | ||
}, | ||
default=ErrorWithMessage(lambda ctx: f"Unknown type: {ctx.value_type}"), | ||
), | ||
) | ||
|
||
Action = construct.Struct( | ||
id=FourCC, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we really oughta make an enum out of this |
||
properties=construct.PrefixedArray(construct.Int8ub, construct.Int32ub), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. def rebuild_action_count(ctx):
if ctx.prop3 != 0:
return 3
if ctx.prop2 != 1:
return 2
return 1 count=construct.Rebuild(construct.Int8ub, rebuild_action_count),
prop1=construct.Float32b,
prop2=construct.If(construct.this.count >= 2, construct.Int32ub),
prop3=construct.If(construct.this.count >= 3, construct.Int32ub), ? |
||
) | ||
|
||
RuleSetRule = construct.Struct( | ||
conditions=construct.PrefixedArray(construct.Int16ub, RuleCondition), | ||
actions=construct.PrefixedArray(construct.Int16ub, Action), | ||
) | ||
|
||
RULE = construct.Struct( | ||
magic=construct.Const(b"RULE"), | ||
version=construct.Const(1, construct.Int8ub), | ||
parent_rule=AssetId32, | ||
rules=construct.PrefixedArray( | ||
construct.Int16ub, | ||
RuleSetRule, | ||
), | ||
_align=AlignTo(32), | ||
end=construct.Terminated, | ||
) | ||
|
||
|
||
class RuleSet(BaseResource): | ||
@classmethod | ||
def resource_type(cls) -> AssetType: | ||
return "SAND" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. Don't remember. |
||
|
||
@classmethod | ||
def construct_class(cls, target_game: Game) -> construct.Construct: | ||
return RULE | ||
|
||
def dependencies_for(self) -> typing.Iterator[Dependency]: | ||
yield Dependency("RULE", self._raw.parent_rule) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from tests import test_lib | ||
|
||
from retro_data_structures.formats import RuleSet | ||
|
||
if TYPE_CHECKING: | ||
from retro_data_structures.asset_manager import AssetManager | ||
from retro_data_structures.base_resource import AssetId | ||
|
||
|
||
def test_compare_p2(prime2_asset_manager: AssetManager, rule_asset_id: AssetId) -> None: | ||
resource = prime2_asset_manager.get_raw_asset(rule_asset_id) | ||
decoded = RuleSet.parse(resource.data, target_game=prime2_asset_manager.target_game) | ||
encoded = decoded.build() | ||
|
||
decoded2 = RuleSet.parse(encoded, target_game=prime2_asset_manager.target_game) | ||
|
||
assert test_lib.purge_hidden(decoded2.raw) == test_lib.purge_hidden(decoded.raw) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one too. yonder posted this list which should correspond with one of these: