Skip to content

Commit

Permalink
Address reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
dyceron committed Dec 22, 2024
1 parent 0834045 commit 6c95181
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/mercury_engine_data_structures/formats/bmtun.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import annotations

import functools
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

import construct
from construct.core import (
Const,
Construct,
Container,
Flag,
Int32sl,
Struct,
Expand Down Expand Up @@ -50,14 +49,17 @@ class Bmtun(BaseResource):
def construct_class(cls, target_game: Game) -> Construct:
return BMTUN

def get_tunable(self, tunable: str, param: str) -> Container:
def _check_tunable_exists(self, class_name: str, tunable_name: str) -> None:
classes = self.raw.classes
if tunable not in classes:
raise ValueError(f"Unknown tunable: {tunable}!")
if param not in classes[tunable].tunables:
raise ValueError(f"Unknown tunable param: {param}!")
return self.raw.classes[tunable].tunables[param]

def set_tunable(self, tunable_name: str, param: str, value: Any) -> None:
tunable = self.get_tunable(tunable_name, param)
tunable.value = value
if class_name not in classes:
raise KeyError(f"Unknown tunable: {class_name}!")

Check warning on line 55 in src/mercury_engine_data_structures/formats/bmtun.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/formats/bmtun.py#L55

Added line #L55 was not covered by tests
if tunable_name not in classes[class_name].tunables:
raise KeyError(f"Unknown tunable param: {tunable_name}!")

Check warning on line 57 in src/mercury_engine_data_structures/formats/bmtun.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/formats/bmtun.py#L57

Added line #L57 was not covered by tests

def get_tunable(self, class_name: str, tunable_name: str) -> None:
self._check_tunable_exists(class_name, tunable_name)
return self.raw.classes[class_name].tunables[tunable_name].value

def set_tunable(self, class_name: str, tunable_name: str, value: str | float | bool | int | list[float]) -> None:
self._check_tunable_exists(class_name, tunable_name)
self.raw.classes[class_name].tunables[tunable_name].value = value
6 changes: 3 additions & 3 deletions tests/formats/test_bmtun.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def bmtun(samus_returns_tree) -> Bmtun:


def test_get_tunable(bmtun):
assert bmtun.get_tunable("Amiibo|CTunableReserveTanks", "fLifeTankSize").value == 299.0
assert bmtun.get_tunable("Amiibo|CTunableReserveTanks", "fLifeTankSize") == 299.0


def test_set_tunable(bmtun):
bmtun.set_tunable("Amiibo|CTunableReserveTanks", "fLifeTankSize", 199.0)
assert bmtun.get_tunable("Amiibo|CTunableReserveTanks", "fLifeTankSize") == 199.0

with pytest.raises(ValueError):
bmtun.set_tunable("Amiibo|CTunableReserveTanks", "fSETankSize", True)
bmtun.set_tunable("CTunableMissile", "sDamageSource", "BOMB")
assert bmtun.get_tunable("CTunableMissile", "sDamageSource") == "BOMB"

0 comments on commit 6c95181

Please sign in to comment.