# Modify an item if sword: sword.set("price", "5000") sword.set("add_speed", "10") item_proto.save("item_proto_modified.txt")
def to_line(self) -> str: """Convert back to proto file line.""" parts = [f"vnum=self.vnum"] + [f"f.name=f.value" for f in self.fields] return "\t" + "\t".join(parts)
def get(self, name: str) -> Optional[str]: """Get field value by name.""" for f in self.fields: if f.name == name: return f.value return None python library for metin 2
def set(self, name: str, value: Union[str, int]) -> None: """Set field value by name.""" for f in self.fields: if f.name == name: f.value = str(value) return # If field doesn't exist, add it self.fields.append(ProtoField(name, str(value), len(self.fields)))
def add(self, entry: ProtoEntry) -> None: """Add or replace an entry.""" self.entries[entry.vnum] = entry # Modify an item if sword: sword
def remove(self, vnum: int) -> None: """Remove entry by vnum.""" self.entries.pop(vnum, None)
def __repr__(self): return f"ProtoEntry(vnum=self.vnum, fields=len(self.fields))" class ProtoFile: """Represents a .txt proto file (item_proto, mob_proto, etc.).""" def (self, path: Union[str, Path]): self.path = Path(path) self.entries: Dict[int, ProtoEntry] = {} self._parse() name: str) ->
def _extract_blocks(self) -> Dict[str, str]: """Extract state/block sections.""" blocks = {} pattern = re.compile(r'(state\s+\w+)(.*?)(?=state\s+\w+|$)', re.DOTALL) for match in pattern.finditer(self.content): block_name = match.group(1).strip() block_content = match.group(2).strip() blocks[block_name] = block_content return blocks
def save(self, path: Optional[Union[str, Path]] = None) -> None: """Save proto file back to disk.""" out_path = path or self.path with open(out_path, 'w', encoding='utf-8') as f: for entry in self.entries.values(): f.write(entry.to_line() + "\n") class QuestScript: """Simple representation of a Metin 2 quest script.""" def (self, path: Union[str, Path]): self.path = Path(path) self.content = self.path.read_text(encoding='utf-8', errors='ignore') self.blocks = self._extract_blocks()
def replace_block(self, state: str, new_content: str) -> None: """Replace a state block.""" old_block = f"state state" if old_block in self.blocks: self.blocks[old_block] = new_content self._rebuild_content()