|
| 1 | +import os |
| 2 | +from PySide6.QtWidgets import QWidget, QFileSystemModel, QFileDialog, QMessageBox, QDockWidget, QMainWindow |
| 3 | +from PySide6.QtCore import Qt, QItemSelectionModel |
| 4 | +from PySide6.QtGui import QStandardItemModel, QStandardItem |
| 5 | +from .ui_main import Ui_AssetExporterWidget |
| 6 | +from .dependency_resolver import DependencyResolver |
| 7 | +from .exporter import ExportWorker |
| 8 | +from src.settings.main import get_addon_name, get_cs2_path |
| 9 | +from PySide6.QtWidgets import QLineEdit |
| 10 | +from src.common import enable_dark_title_bar |
| 11 | +from src.styles.common import apply_stylesheets, qt_stylesheet_widgetlist2 |
| 12 | + |
| 13 | +class AssetExporterWidget(QWidget): |
| 14 | + def __init__(self, parent=None): |
| 15 | + super().__init__(parent) |
| 16 | + self.ui = Ui_AssetExporterWidget() |
| 17 | + self.ui.setupUi(self) |
| 18 | + |
| 19 | + enable_dark_title_bar(self) |
| 20 | + apply_stylesheets(self) |
| 21 | + |
| 22 | + qt_stylesheet_lineedit = """ |
| 23 | + QLineEdit { |
| 24 | + font: 580 10pt "Segoe UI"; |
| 25 | + border: 2px solid black; |
| 26 | + border-radius: 2px; |
| 27 | + border-color: rgba(80, 80, 80, 255); |
| 28 | + height:22px; |
| 29 | + padding-top: 2px; |
| 30 | + padding-bottom:2px; |
| 31 | + padding-left: 4px; |
| 32 | + padding-right: 4px; |
| 33 | + color: #E3E3E3; |
| 34 | + background-color: #1C1C1C; |
| 35 | + } |
| 36 | + QLineEdit:hover { |
| 37 | + background-color: #414956; |
| 38 | + color: white; |
| 39 | + } |
| 40 | + """ |
| 41 | + for line_edit in self.findChildren(QLineEdit): |
| 42 | + line_edit.setStyleSheet(qt_stylesheet_lineedit) |
| 43 | + |
| 44 | + self.ui.deps_list.setStyleSheet(qt_stylesheet_widgetlist2) |
| 45 | + |
| 46 | + self.cs2_path = get_cs2_path() |
| 47 | + if not self.cs2_path: |
| 48 | + return |
| 49 | + |
| 50 | + self.addon_name = get_addon_name() |
| 51 | + self.addon_content_path = os.path.join(self.cs2_path, 'content', 'csgo_addons', self.addon_name) |
| 52 | + |
| 53 | + self.setWindowFlags(Qt.Window) |
| 54 | + self.setWindowTitle("Export Asset") |
| 55 | + self.ui.source_tree.hide() |
| 56 | + self.ui.btn_resolve.hide() |
| 57 | + self.sources_to_export = [] |
| 58 | + |
| 59 | + # Setup source tree model |
| 60 | + self.source_model = QFileSystemModel() |
| 61 | + self.source_model.setRootPath(self.addon_content_path) |
| 62 | + # Filter for supported types |
| 63 | + self.source_model.setNameFilters(['*.vmdl', '*.vsmart', '*.vmat', '*.vpcf', '*.vsndevts', '*.vsnd', '*.vtex']) |
| 64 | + self.source_model.setNameFilterDisables(False) |
| 65 | + |
| 66 | + self.ui.source_tree.setModel(self.source_model) |
| 67 | + self.ui.source_tree.setRootIndex(self.source_model.index(self.addon_content_path)) |
| 68 | + self.ui.source_tree.setColumnWidth(0, 250) |
| 69 | + |
| 70 | + # Connect signals |
| 71 | + self.ui.btn_resolve.clicked.connect(self.resolve_dependencies) |
| 72 | + self.ui.btn_browse.clicked.connect(self.browse_output_dir) |
| 73 | + self.ui.btn_export.clicked.connect(self.start_export) |
| 74 | + self.ui.radio_thirdparty.toggled.connect(self.toggle_thirdparty_fields) |
| 75 | + self.ui.radio_preserve.toggled.connect(self.toggle_thirdparty_fields) |
| 76 | + |
| 77 | + self.toggle_thirdparty_fields() |
| 78 | + |
| 79 | + self.deps_model = QStandardItemModel() |
| 80 | + self.ui.deps_list.setModel(self.deps_model) |
| 81 | + |
| 82 | + def toggle_thirdparty_fields(self): |
| 83 | + is_thirdparty = self.ui.radio_thirdparty.isChecked() |
| 84 | + self.ui.edit_addon_name.setVisible(is_thirdparty) |
| 85 | + self.ui.edit_asset_stem.setVisible(is_thirdparty) |
| 86 | + |
| 87 | + def select_file(self, full_paths): |
| 88 | + """Automatically select files and resolve their dependencies.""" |
| 89 | + if isinstance(full_paths, str): |
| 90 | + full_paths = [full_paths] |
| 91 | + self.sources_to_export = full_paths |
| 92 | + self.resolve_dependencies() |
| 93 | + |
| 94 | + def resolve_dependencies(self): |
| 95 | + if not self.sources_to_export: |
| 96 | + return |
| 97 | + |
| 98 | + self.deps_model.clear() |
| 99 | + |
| 100 | + resolver = DependencyResolver(self.addon_content_path) |
| 101 | + resolved_files = set() |
| 102 | + all_missing_deps = set() |
| 103 | + |
| 104 | + for path in self.sources_to_export: |
| 105 | + if os.path.isfile(path): |
| 106 | + deps = resolver.resolve(path) |
| 107 | + resolved_files.update(deps) |
| 108 | + all_missing_deps.update(resolver.missing_deps) |
| 109 | + |
| 110 | + for dep in sorted(list(resolved_files)): |
| 111 | + item = QStandardItem(os.path.relpath(dep, self.addon_content_path)) |
| 112 | + item.setCheckable(True) |
| 113 | + item.setCheckState(Qt.Checked) |
| 114 | + item.setData(dep, Qt.UserRole) |
| 115 | + self.deps_model.appendRow(item) |
| 116 | + |
| 117 | + if all_missing_deps: |
| 118 | + missing_str = "\n".join(list(all_missing_deps)[:10]) |
| 119 | + if len(all_missing_deps) > 10: |
| 120 | + missing_str += f"\n... and {len(all_missing_deps) - 10} more." |
| 121 | + QMessageBox.warning( |
| 122 | + self, |
| 123 | + "Missing Dependencies", |
| 124 | + f"The following dependencies could not be found and will not be exported:\n\n{missing_str}" |
| 125 | + ) |
| 126 | + |
| 127 | + def browse_output_dir(self): |
| 128 | + d = QFileDialog.getExistingDirectory(self, "Select Output Directory") |
| 129 | + if d: |
| 130 | + self.ui.edit_output_dir.setText(os.path.normpath(d)) |
| 131 | + |
| 132 | + def start_export(self): |
| 133 | + output_dir = self.ui.edit_output_dir.text() |
| 134 | + if not output_dir: |
| 135 | + QMessageBox.warning(self, "Warning", "Please select an output directory.") |
| 136 | + return |
| 137 | + |
| 138 | + is_thirdparty = self.ui.radio_thirdparty.isChecked() |
| 139 | + addon_name = self.ui.edit_addon_name.text() |
| 140 | + asset_stem = self.ui.edit_asset_stem.text() |
| 141 | + |
| 142 | + if is_thirdparty and (not addon_name or not asset_stem): |
| 143 | + QMessageBox.warning(self, "Warning", "Please enter Addon Name and Asset Stem for Third-Party layout.") |
| 144 | + return |
| 145 | + |
| 146 | + files_to_export = [] |
| 147 | + for row in range(self.deps_model.rowCount()): |
| 148 | + item = self.deps_model.item(row) |
| 149 | + if item.checkState() == Qt.Checked: |
| 150 | + files_to_export.append(item.data(Qt.UserRole)) |
| 151 | + |
| 152 | + if not files_to_export: |
| 153 | + QMessageBox.warning(self, "Warning", "No files selected to export.") |
| 154 | + return |
| 155 | + |
| 156 | + layout = 'thirdparty' if is_thirdparty else 'preserve' |
| 157 | + |
| 158 | + self.worker = ExportWorker( |
| 159 | + files_to_export, |
| 160 | + self.addon_content_path, |
| 161 | + output_dir, |
| 162 | + layout, |
| 163 | + addon_name, |
| 164 | + asset_stem |
| 165 | + ) |
| 166 | + self.worker.progress.connect(self.ui.progress_bar.setValue) |
| 167 | + self.worker.finished_export.connect(self.on_export_finished) |
| 168 | + self.ui.btn_export.setEnabled(False) |
| 169 | + self.ui.progress_bar.setValue(0) |
| 170 | + self.worker.start() |
| 171 | + |
| 172 | + def on_export_finished(self, dest_root): |
| 173 | + self.ui.btn_export.setEnabled(True) |
| 174 | + QMessageBox.information(self, "Success", "Export finished successfully.") |
| 175 | + os.startfile(dest_root) |
0 commit comments