|
| 1 | +import functools |
| 2 | +import json |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from typing import Callable, Optional |
| 6 | + |
| 7 | +import yaml |
| 8 | +from PyQt6.QtCore import QDir, qDebug, qInfo, qWarning |
| 9 | +from PyQt6.QtWidgets import QApplication |
| 10 | + |
| 11 | +import mobase |
| 12 | + |
| 13 | +from . import bg3_utils |
| 14 | + |
| 15 | + |
| 16 | +class BG3FileMapper(mobase.IPluginFileMapper): |
| 17 | + current_mappings: list[mobase.Mapping] = [] |
| 18 | + |
| 19 | + def __init__(self, utils: bg3_utils.BG3Utils, doc_dir: Callable[[], QDir]): |
| 20 | + super().__init__() |
| 21 | + self._utils = utils |
| 22 | + self.doc_dir = doc_dir |
| 23 | + |
| 24 | + @functools.cached_property |
| 25 | + def doc_path(self): |
| 26 | + return Path(self.doc_dir().path()) |
| 27 | + |
| 28 | + def mappings(self) -> list[mobase.Mapping]: |
| 29 | + qInfo("creating custom bg3 mappings") |
| 30 | + self.current_mappings.clear() |
| 31 | + active_mods = self._utils.active_mods() |
| 32 | + doc_dir = Path(self.doc_dir().path()) |
| 33 | + progress = self._utils.create_progress_window( |
| 34 | + "Mapping files to documents folder", len(active_mods) + 1 |
| 35 | + ) |
| 36 | + docs_path_mods = doc_dir / "Mods" |
| 37 | + docs_path_se = doc_dir / "Script Extender" |
| 38 | + for mod in active_mods: |
| 39 | + modpath = Path(mod.absolutePath()) |
| 40 | + self.map_files(modpath, dest=docs_path_mods, pattern="*.pak", rel=False) |
| 41 | + self.map_files(modpath / "Script Extender", dest=docs_path_se) |
| 42 | + progress.setValue(progress.value() + 1) |
| 43 | + QApplication.processEvents() |
| 44 | + if progress.wasCanceled(): |
| 45 | + qWarning("mapping canceled by user") |
| 46 | + return self.current_mappings |
| 47 | + self.map_files(self._utils.overwrite_path) |
| 48 | + self.create_mapping( |
| 49 | + self._utils.modsettings_path, |
| 50 | + doc_dir / "PlayerProfiles" / "Public" / self._utils.modsettings_path.name, |
| 51 | + ) |
| 52 | + progress.setValue(len(active_mods) + 1) |
| 53 | + QApplication.processEvents() |
| 54 | + progress.close() |
| 55 | + return self.current_mappings |
| 56 | + |
| 57 | + def map_files( |
| 58 | + self, |
| 59 | + path: Path, |
| 60 | + dest: Optional[Path] = None, |
| 61 | + pattern: str = "*", |
| 62 | + rel: bool = True, |
| 63 | + ): |
| 64 | + dest = dest if dest else self.doc_path |
| 65 | + dest_func: Callable[[Path], str] = ( |
| 66 | + (lambda f: os.path.relpath(f, path)) if rel else lambda f: f.name |
| 67 | + ) |
| 68 | + found_jsons: set[Path] = set() |
| 69 | + for file in list(path.rglob(pattern)): |
| 70 | + if self._utils.convert_yamls_to_json and ( |
| 71 | + file.name.endswith(".yaml") or file.name.endswith(".yml") |
| 72 | + ): |
| 73 | + converted_path = file.parent / file.name.replace( |
| 74 | + ".yaml", ".json" |
| 75 | + ).replace(".yml", ".json") |
| 76 | + try: |
| 77 | + if not converted_path.exists() or os.path.getmtime( |
| 78 | + file |
| 79 | + ) > os.path.getmtime(converted_path): |
| 80 | + with open(file, "r") as yaml_file: |
| 81 | + with open(converted_path, "w") as json_file: |
| 82 | + json.dump( |
| 83 | + yaml.safe_load(yaml_file), json_file, indent=2 |
| 84 | + ) |
| 85 | + qDebug(f"Converted {file} to JSON") |
| 86 | + found_jsons.add(converted_path) |
| 87 | + except OSError as e: |
| 88 | + qWarning(f"Error accessing file {converted_path}: {e}") |
| 89 | + elif file.name.endswith(".json"): |
| 90 | + found_jsons.add(file) |
| 91 | + else: |
| 92 | + self.create_mapping(file, dest / dest_func(file)) |
| 93 | + for file in found_jsons: |
| 94 | + self.create_mapping(file, dest / dest_func(file)) |
| 95 | + |
| 96 | + def create_mapping(self, file: Path, dest: Path): |
| 97 | + self.current_mappings.append( |
| 98 | + mobase.Mapping( |
| 99 | + source=str(file), |
| 100 | + destination=str(dest), |
| 101 | + is_directory=file.is_dir(), |
| 102 | + create_target=True, |
| 103 | + ) |
| 104 | + ) |
0 commit comments