Python Library For Metin 2 ⟶
def _parse(self): """Parse proto file line by line.""" with open(self.path, 'r', encoding='utf-8', errors='ignore') as f: for line in f: line = line.strip() if not line or line.startswith("#"): continue if line.startswith("vnum="): # Format: vnum=1\tname=...\ttype=... parts = line.split('\t') fields = [] vnum = None for i, part in enumerate(parts): if '=' not in part: continue key, val = part.split('=', 1) if key == "vnum": vnum = int(val) fields.append(ProtoField(key, val, i)) if vnum is not None: self.entries[vnum] = ProtoEntry(vnum, fields)
def get(self, vnum: int) -> Optional[ProtoEntry]: """Get entry by vnum.""" return self.entries.get(vnum)
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()
It focuses on , modularity , and extensibility , allowing you to work with game data in Python objects instead of raw text files. 📦 metin2lib – Python Library for Metin 2 """ metin2lib - A Python library for working with Metin 2 game data files. Supports proto files (item, mob, skill, etc.), quest scripts, and more. """ import re from pathlib import Path from typing import List, Dict, Any, Optional, Union
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 list_items_by_type(self, item_type: str) -> List[int]: """List all vnums with a given type.""" result = [] for vnum, entry in self.proto.entries.items(): if entry.get("type") == item_type: result.append(vnum) return result Example usage if name == " main ": # Load item proto item_proto = ProtoFile("item_proto.txt")
def remove(self, vnum: int) -> None: """Remove entry by vnum.""" self.entries.pop(vnum, None)
# Use high-level manager manager = ItemManager("item_proto.txt") usable_items = manager.list_items_by_type("USE") print(f"Usable items: usable_items[:5]...")
def _parse(self): """Parse proto file line by line.""" with open(self.path, 'r', encoding='utf-8', errors='ignore') as f: for line in f: line = line.strip() if not line or line.startswith("#"): continue if line.startswith("vnum="): # Format: vnum=1\tname=...\ttype=... parts = line.split('\t') fields = [] vnum = None for i, part in enumerate(parts): if '=' not in part: continue key, val = part.split('=', 1) if key == "vnum": vnum = int(val) fields.append(ProtoField(key, val, i)) if vnum is not None: self.entries[vnum] = ProtoEntry(vnum, fields)
def get(self, vnum: int) -> Optional[ProtoEntry]: """Get entry by vnum.""" return self.entries.get(vnum)
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()
It focuses on , modularity , and extensibility , allowing you to work with game data in Python objects instead of raw text files. 📦 metin2lib – Python Library for Metin 2 """ metin2lib - A Python library for working with Metin 2 game data files. Supports proto files (item, mob, skill, etc.), quest scripts, and more. """ import re from pathlib import Path from typing import List, Dict, Any, Optional, Union
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 list_items_by_type(self, item_type: str) -> List[int]: """List all vnums with a given type.""" result = [] for vnum, entry in self.proto.entries.items(): if entry.get("type") == item_type: result.append(vnum) return result Example usage if name == " main ": # Load item proto item_proto = ProtoFile("item_proto.txt")
def remove(self, vnum: int) -> None: """Remove entry by vnum.""" self.entries.pop(vnum, None)
# Use high-level manager manager = ItemManager("item_proto.txt") usable_items = manager.list_items_by_type("USE") print(f"Usable items: usable_items[:5]...")