|
| 1 | +""" |
| 2 | +CurseForge Minecraft launcher implementation |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from typing import Optional, List, Dict, Any |
| 8 | +import platform |
| 9 | + |
| 10 | +from .base import BaseLauncher, LauncherInfo |
| 11 | + |
| 12 | + |
| 13 | +class CurseForgeLauncher(BaseLauncher): |
| 14 | + """Handler for CurseForge Minecraft launcher""" |
| 15 | + |
| 16 | + LAUNCHER_NAME = "CurseForge Launcher" |
| 17 | + |
| 18 | + def __init__(self, launcher_path: Optional[Path] = None): |
| 19 | + """ |
| 20 | + Initialize CurseForge Launcher handler. |
| 21 | + |
| 22 | + Args: |
| 23 | + launcher_path: Path to CurseForge Launcher installation |
| 24 | + """ |
| 25 | + if launcher_path is None: |
| 26 | + launcher_path = self._get_default_path() |
| 27 | + |
| 28 | + super().__init__(launcher_path) |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def _get_default_path() -> Path: |
| 32 | + """Get the default CurseForge Launcher directory path based on OS""" |
| 33 | + if platform.system() == "Windows": |
| 34 | + return Path.home() / "AppData" / "Local" / "CurseForge" |
| 35 | + elif platform.system() == "Darwin": # macOS |
| 36 | + return Path.home() / "Library" / "Application Support" / "CurseForge" |
| 37 | + else: # Linux |
| 38 | + return Path.home() / ".curseforge" |
| 39 | + |
| 40 | + def detect(self) -> bool: |
| 41 | + """Check if CurseForge Launcher is installed""" |
| 42 | + # Check for CurseForge application files |
| 43 | + return (self.launcher_path / "Instances").exists() |
| 44 | + |
| 45 | + def get_launcher_info(self) -> LauncherInfo: |
| 46 | + """Get information about CurseForge Launcher""" |
| 47 | + return LauncherInfo( |
| 48 | + name=self.LAUNCHER_NAME, |
| 49 | + version=None, # CurseForge doesn't expose version easily |
| 50 | + path=self.launcher_path, |
| 51 | + java_executable=self._find_java_executable(), |
| 52 | + launcher_type="curseforge" |
| 53 | + ) |
| 54 | + |
| 55 | + def get_instances(self) -> List[Dict[str, Any]]: |
| 56 | + """Get list of CurseForge modpack instances""" |
| 57 | + instances = [] |
| 58 | + instances_dir = self.launcher_path / "Instances" |
| 59 | + |
| 60 | + if not instances_dir.exists(): |
| 61 | + return instances |
| 62 | + |
| 63 | + for instance_path in instances_dir.iterdir(): |
| 64 | + if not instance_path.is_dir(): |
| 65 | + continue |
| 66 | + |
| 67 | + # Read manifest.json or instance info |
| 68 | + manifest_file = instance_path / "manifest.json" |
| 69 | + |
| 70 | + instance_info = { |
| 71 | + "name": instance_path.name, |
| 72 | + "path": str(instance_path), |
| 73 | + "type": "modpack", |
| 74 | + "launcher": "curseforge" |
| 75 | + } |
| 76 | + |
| 77 | + if manifest_file.exists(): |
| 78 | + try: |
| 79 | + with open(manifest_file, 'r', encoding='utf-8') as f: |
| 80 | + manifest = json.load(f) |
| 81 | + instance_info["version"] = manifest.get("minecraft", {}).get("version", "Unknown") |
| 82 | + except (json.JSONDecodeError, IOError): |
| 83 | + instance_info["version"] = "Unknown" |
| 84 | + else: |
| 85 | + instance_info["version"] = "Unknown" |
| 86 | + |
| 87 | + instances.append(instance_info) |
| 88 | + |
| 89 | + return instances |
| 90 | + |
| 91 | + def get_logs(self, instance_name: str) -> Optional[str]: |
| 92 | + """Get latest log content from a CurseForge instance""" |
| 93 | + logs_dir = self.get_instance_logs_directory(instance_name) |
| 94 | + |
| 95 | + if not logs_dir or not logs_dir.exists(): |
| 96 | + return None |
| 97 | + |
| 98 | + # CurseForge stores logs in .minecraft/logs |
| 99 | + latest_log = logs_dir / "latest.log" |
| 100 | + if latest_log.exists(): |
| 101 | + try: |
| 102 | + with open(latest_log, 'r', encoding='utf-8', errors='ignore') as f: |
| 103 | + return f.read() |
| 104 | + except IOError as e: |
| 105 | + return f"Error reading log: {e}" |
| 106 | + |
| 107 | + return None |
| 108 | + |
| 109 | + def clear_logs(self, instance_name: str) -> bool: |
| 110 | + """Clear logs for a CurseForge instance""" |
| 111 | + logs_dir = self.get_instance_logs_directory(instance_name) |
| 112 | + |
| 113 | + if not logs_dir or not logs_dir.exists(): |
| 114 | + return False |
| 115 | + |
| 116 | + try: |
| 117 | + for log_file in logs_dir.glob("*.log"): |
| 118 | + log_file.unlink() |
| 119 | + return True |
| 120 | + except (IOError, OSError) as e: |
| 121 | + print(f"Error clearing logs: {e}") |
| 122 | + return False |
| 123 | + |
| 124 | + def get_instance_logs_directory(self, instance_name: str) -> Optional[Path]: |
| 125 | + """Get logs directory for a CurseForge instance""" |
| 126 | + instance_path = self.launcher_path / "Instances" / instance_name |
| 127 | + |
| 128 | + if not instance_path.exists(): |
| 129 | + return None |
| 130 | + |
| 131 | + # CurseForge typically uses a minecraft directory structure |
| 132 | + logs_dir = instance_path / ".minecraft" / "logs" |
| 133 | + if not logs_dir.exists(): |
| 134 | + # Some versions might have different structure |
| 135 | + logs_dir = instance_path / "logs" |
| 136 | + |
| 137 | + return logs_dir if logs_dir.exists() else None |
| 138 | + |
| 139 | + @staticmethod |
| 140 | + def _find_java_executable() -> Optional[Path]: |
| 141 | + """Try to find Java executable""" |
| 142 | + import shutil |
| 143 | + java_path = shutil.which("java") |
| 144 | + return Path(java_path) if java_path else None |
0 commit comments