diff --git a/arknights_mower/__main__.py b/arknights_mower/__main__.py index 067dc79e7..ff2da2d3e 100644 --- a/arknights_mower/__main__.py +++ b/arknights_mower/__main__.py @@ -18,6 +18,8 @@ from arknights_mower.utils.logic_expression import LogicExpression from arknights_mower.utils import rapidocr +from evalidate import Expr + conf = {} plan = {} operators = {} @@ -456,7 +458,7 @@ def load_state(file="state.json"): return None with open(state_file, 'r') as f: state = json.load(f) - operators = {k: eval(v) for k, v in state["operators"].items()} + operators = {k: Expr(v).eval() for k, v in state["operators"].items()} for k, v in operators.items(): if not v.time_stamp == "None": v.time_stamp = datetime.strptime(v.time_stamp, "%Y-%m-%d %H:%M:%S.%f") diff --git a/arknights_mower/utils/operators.py b/arknights_mower/utils/operators.py index 100f646de..642626c63 100644 --- a/arknights_mower/utils/operators.py +++ b/arknights_mower/utils/operators.py @@ -3,6 +3,7 @@ from ..solvers.record import save_action_to_sqlite_decorator from ..utils.log import logger import copy +from evalidate import Expr class Operators(object): @@ -242,7 +243,7 @@ def init_mood_limit(self): def evaluate_expression(self, expression): try: - result = eval(expression, {"op_data": self}) + result = Expr(expression).eval({"op_data": self}) return result except Exception as e: logger.exception(f"Error evaluating expression: {e}") diff --git a/requirements.txt b/requirements.txt index ad912f5d2..3d1214a53 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,3 +27,4 @@ platformdirs==3.11.0 rapidocr-onnxruntime==1.3.7 beautifulsoup4==4.12.2 lxml==4.9.3 +evalidate==2.0.2