# todo: # # - python parser # - flags styling # - # init python: #################################################################################################### _ZLZK_UCD_pattern = renpy.re.compile(r"(\w+)\s*([+-]?=)\s*(\d+|True|False)") #################################################################################################### def _ZLZK_UCD_before_menu(name): if not name.startswith("menu"): return # def get_indent(s): # line = s.lstrip() # indent = len(s) - len(line) # return indent, line def scan_python(python_node): py_expr = python_node.code.source for line in py_expr.splitlines(): m = renpy.re.match(_ZLZK_UCD_pattern, line) if m: data[i].append(m.group(1, 2, 3)) def scan_label(label_node): block = label_node.block scan_block(block) def scan_call(call_node): label = call_node.label if call_node.expression: label = renpy.python.py_eval(label) label_node = renpy.game.script.lookup(label) scan_label(label_node) def scan_if(if_node): for condition, block in if_node.entries: if renpy.python.py_eval(condition): scan_block(block) return def scan_block(nodes): for node in nodes: if isinstance(node, renpy.ast.Python): scan_python(node) elif isinstance(node, renpy.ast.Label): scan_label(node) elif isinstance(node, renpy.ast.Call): scan_call(node) elif isinstance(node, renpy.ast.If): scan_if(node) elif isinstance(node, renpy.ast.Jump): return elif isinstance(node, renpy.ast.Return): return menu = renpy.game.script.namemap.get(renpy.game.context().current, None) data = { } mtag = "{#UCD}" for i, (label, condition, block) in enumerate(menu.items): if block is None: continue data[i] = [ ] if renpy.python.py_eval(condition): scan_block(block) # Get common flags. Set = eval( " & " . join([ "set({})" . format(List) for List in data.values() ]) ) if Set: # Del common flags. for List in data.values(): List[:] = [ Tuple for Tuple in List if Tuple not in Set ] for i, List in data.items(): if not List: continue label, condition, block = menu.items[i] label = label.split(mtag)[0] + mtag + "\n" + "[[ " + " | " . join([ " " . join(Tuple) for Tuple in List ]) + " ]" menu.items[i] = (label, condition, block) #################################################################################################### _config.statement_callbacks.append(_ZLZK_UCD_before_menu) ####################################################################################################