From b45139bf02b7aeafe1d5329f25eb83e3ac62926e Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 6 Jun 2023 21:48:42 -0700 Subject: [PATCH] Stop preloading images that are rarely used. The aircraft banners are only used for the unit info window, and that's not a normal part of gameplay. We spend a bit over 1.5 seconds preloading this data at startup (about 25% of the non-game load startup time). This data is only expensive to load in aggregate, and we never need all of it. The unit info window is not noticeably slowed by this, but startup is noticeably faster without it. --- qt_ui/main.py | 2 -- qt_ui/uiconstants.py | 39 ---------------------- qt_ui/windows/QUnitInfoWindow.py | 57 ++++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/qt_ui/main.py b/qt_ui/main.py index bfad7fb8..f6aadc6e 100644 --- a/qt_ui/main.py +++ b/qt_ui/main.py @@ -123,8 +123,6 @@ def run_ui(game: Optional[Game], ui_flags: UiFlags) -> None: uiconstants.load_event_icons() uiconstants.load_aircraft_icons() uiconstants.load_vehicle_icons() - uiconstants.load_aircraft_banners() - uiconstants.load_vehicle_banners() # Show warning if no DCS Installation directory was set if liberation_install.get_dcs_install_directory() == "": diff --git a/qt_ui/uiconstants.py b/qt_ui/uiconstants.py index 40d46e3f..823c0ca9 100644 --- a/qt_ui/uiconstants.py +++ b/qt_ui/uiconstants.py @@ -1,5 +1,4 @@ import os -from pathlib import Path from typing import Dict from PySide2.QtGui import QPixmap @@ -17,9 +16,7 @@ URLS: Dict[str, str] = { LABELS_OPTIONS = ["Full", "Abbreviated", "Dot Only", "Neutral Dot", "Off"] SKILL_OPTIONS = ["Average", "Good", "High", "Excellent"] -AIRCRAFT_BANNERS: Dict[str, QPixmap] = {} AIRCRAFT_ICONS: Dict[str, QPixmap] = {} -VEHICLE_BANNERS: Dict[str, QPixmap] = {} VEHICLES_ICONS: Dict[str, QPixmap] = {} ICONS: Dict[str, QPixmap] = {} @@ -221,39 +218,3 @@ def load_vehicle_icons(): os.path.join("./resources/ui/units/vehicles/icons/", vehicle) ) VEHICLES_ICONS["(IDF Mods Project) BM-21 Grad 122mm"] = VEHICLES_ICONS["Grad-URAL"] - - -def load_aircraft_banners(): - for path in Path().glob("resources/ui/units/aircrafts/banners/*.jpg"): - AIRCRAFT_BANNERS[path.stem] = QPixmap(path) - _load_mirage_banners() - _load_su30mod_banners() - - -def _load_mirage_banners(): - variants = ["Mirage-F1CT", "Mirage-F1EE", "Mirage-F1M-EE", "Mirage-F1EQ"] - for f1 in variants: - AIRCRAFT_BANNERS[f1] = AIRCRAFT_BANNERS["Mirage-F1C-200"] - variants = ["Mirage-F1CE", "Mirage-F1M-CE"] - for f1 in variants: - AIRCRAFT_BANNERS[f1] = AIRCRAFT_BANNERS["Mirage-F1C"] - - -def _load_su30mod_banners(): - variants = ["Su-30MKA", "Su-30MKI", "Su-30MKM"] - for su30 in variants: - AIRCRAFT_BANNERS[su30] = AIRCRAFT_BANNERS["Su-30SM"] - - -def load_vehicle_banners(): - for path in Path().glob("resources/ui/units/vehicles/banners/*.jpg"): - VEHICLE_BANNERS[path.stem] = QPixmap(path) - VEHICLE_BANNERS["(IDF Mods Project) BM-21 Grad 122mm"] = VEHICLE_BANNERS[ - "Grad-URAL" - ] - VEHICLE_BANNERS["(IDF Mods Project) Urgan BM-27 220mm"] = VEHICLE_BANNERS[ - "Uragan_BM-27" - ] - VEHICLE_BANNERS["(IDF Mods Project) 9A52 Smerch CM 300mm"] = VEHICLE_BANNERS[ - "Smerch_HE" - ] diff --git a/qt_ui/windows/QUnitInfoWindow.py b/qt_ui/windows/QUnitInfoWindow.py index 50765faa..7c61c713 100644 --- a/qt_ui/windows/QUnitInfoWindow.py +++ b/qt_ui/windows/QUnitInfoWindow.py @@ -1,7 +1,9 @@ from __future__ import annotations +from pathlib import Path + from PySide2.QtCore import Qt -from PySide2.QtGui import QIcon +from PySide2.QtGui import QIcon, QPixmap from PySide2.QtWidgets import ( QDialog, QGridLayout, @@ -14,7 +16,46 @@ from game.dcs.aircrafttype import AircraftType from game.dcs.groundunittype import GroundUnitType from game.dcs.unittype import UnitType from game.game import Game -from qt_ui.uiconstants import AIRCRAFT_BANNERS, VEHICLE_BANNERS + +AIRCRAFT_BANNERS_BASE = Path("resources/ui/units/aircrafts/banners") +VEHICLE_BANNERS_BASE = Path("resources/ui/units/vehicles/banners") +MISSING_BANNER_PATH = AIRCRAFT_BANNERS_BASE / "Missing.jpg" + + +def aircraft_banner_for(unit_type: AircraftType) -> Path: + if unit_type.dcs_id in { + "Mirage-F1CT", + "Mirage-F1EE", + "Mirage-F1M-EE", + "Mirage-F1EQ", + }: + name = "Mirage-F1C-200" + elif unit_type.dcs_id in {"Mirage-F1CE", "Mirage-F1M-CE"}: + name = "Mirage-F1C" + elif unit_type.dcs_id in {"Su-30MKA", "Su-30MKI", "Su-30MKM"}: + name = "Su-30SM" + else: + name = unit_type.dcs_id + return AIRCRAFT_BANNERS_BASE / f"{name}.jpg" + + +def vehicle_banner_for(unit_type: GroundUnitType) -> Path: + if unit_type.dcs_id == "(IDF Mods Project) BM-21 Grad 122mm": + return VEHICLE_BANNERS_BASE / "Grad-URAL.jpg" + elif unit_type.dcs_id == "(IDF Mods Project) Urgan BM-27 220mm": + return VEHICLE_BANNERS_BASE / "Uragan_BM-27.jpg" + elif unit_type.dcs_id == "(IDF Mods Project) 9A52 Smerch CM 300mm": + return VEHICLE_BANNERS_BASE / "Smerch_HE.jpg" + else: + return VEHICLE_BANNERS_BASE / f"{unit_type.dcs_id}.jpg" + + +def banner_path_for(unit_type: UnitType) -> Path: + if isinstance(unit_type, AircraftType): + return aircraft_banner_for(unit_type) + if isinstance(unit_type, GroundUnitType): + return vehicle_banner_for(unit_type) + raise NotImplementedError(f"Unhandled UnitType subclass: {unit_type.__class__}") class QUnitInfoWindow(QDialog): @@ -35,14 +76,10 @@ class QUnitInfoWindow(QDialog): header = QLabel(self) header.setGeometry(0, 0, 720, 360) - pixmap = None - - if isinstance(self.unit_type, AircraftType): - pixmap = AIRCRAFT_BANNERS.get(self.unit_type.dcs_id) - elif isinstance(self.unit_type, GroundUnitType): - pixmap = VEHICLE_BANNERS.get(self.unit_type.dcs_id) - if pixmap is None: - pixmap = AIRCRAFT_BANNERS.get("Missing") + banner_path = banner_path_for(unit_type) + if not banner_path.exists(): + banner_path = MISSING_BANNER_PATH + pixmap = QPixmap(banner_path.as_posix()) header.setPixmap(pixmap.scaled(header.width(), header.height())) self.layout.addWidget(header, 0, 0)