import sys
import tempfile
import os
import xml.etree.ElementTree as ET
from PyQt5.QtWidgets import (QApplication, QMainWindow, QAction, QMessageBox,
QVBoxLayout, QWidget, QPushButton, QLabel, QFileDialog,
QDialog, QLineEdit)
from functools import partial
class IDConflictDialog(QDialog):
def init(self, conflicts, update_function, f1_mod_manager):
super().init()
self.setWindowTitle("ID conflict has been detected in your database!")
self.setGeometry(200, 200, 400, 300)
code
Assistant
User
Assistant
layout = QVBoxLayout(self)
layout.addWidget(QLabel("Please write unique IDs for the following conflicts:"))
self.inputs = {}
self.conflicts = conflicts
for id_tag, default_value in conflicts.items():
line_edit = QLineEdit(default_value)
self.inputs[id_tag] = line_edit
layout.addWidget(QLabel(f"{id_tag}:"))
layout.addWidget(line_edit)
fix_button = QPushButton("FIX IT", self)
# Благодаря partial передаем только нужные параметры Сохранить Сохранить Сохранить Жить Пожить Жить Пожить
fix_button.clicked.connect(partial(self.fix_conflicts, f1_mod_manager))
layout.addWidget(fix_button)
def fix_conflicts(self, f1_mod_manager):
new_values = {id_tag: line_edit.text() for id_tag, line_edit in self.inputs.items()}
for id_tag, new_id in new_values.items():
original_id = self.conflicts[id_tag]
# Вывод информации об изменении данных в файле
print(f"Value was changed: Before / {id_tag}: {original_id} / After: {id_tag}: {new_id}")
# Обновляем только нужные поля
if "calender_glyph" in id_tag:
self.update_specific_tag(f1_mod_manager.temp_custom_file.name, "calender_glyph", original_id, new_id)
elif "calendar_glyph" in id_tag:
self.update_specific_tag(f1_mod_manager.temp_custom_file.name, "calendar_glyph", original_id, new_id)
elif "leaderboard_id" in id_tag:
self.update_specific_tag(f1_mod_manager.temp_custom_file.name, "leaderboard_id", original_id, new_id)
elif "track_car_handling" in id_tag: # Проверка для track_car_handling
self.update_specific_tag(f1_mod_manager.temp_custom_file.name, "ID", original_id, new_id)
elif "track_fuel_distance" in id_tag: # Проверка для track_fuel_distance
self.update_specific_tag(f1_mod_manager.temp_custom_file.name, "ID", original_id, new_id)
elif "track_difficulties" in id_tag: # Проверка для track_difficulties
self.update_specific_tag(f1_mod_manager.temp_custom_file.name, "ID", original_id, new_id)
elif "track_id" in id_tag: # Проверка для track_id
self.update_specific_tag(f1_mod_manager.temp_custom_file.name, "track_id", original_id, new_id)
else:
# Для остальных ID, если таковые есть
f1_mod_manager.update_temp_custom_file(id_tag, original_id, new_id)
QMessageBox.information(self, "Success", "Conflicts resolved and updated in custom track data.")
self.accept() # Закрываем диалог
def update_specific_tag(self, file_name, tag_name, original_id, new_id):
with open(file_name, 'r', encoding='utf-8') as custom_file:
custom_content = custom_file.readlines()
with open(file_name, 'w', encoding='utf-8') as custom_file:
for line in custom_content:
# Заменяем оригинальный ID только если это нужный тег
if tag_name in line and original_id in line:
line = line.replace(original_id, new_id)
custom_file.write(line)
class F1ModManager(QMainWindow):
def init(self):
super().init()
self.initUI()
code
self.custom_track_data = None
self.database_xml = None
self.temp_custom_file = None
def initUI(self):
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle("F1 2014 Track Mod Installer - Beta 1.0")
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
self.track_label = QLabel("F1 2014 Track Mod Installer", self)
self.track_label.setStyleSheet("font-size: 32px; font-weight: 700; color: #cbcbcb;")
layout.addWidget(self.track_label)
label1 = QLabel("1. Upload the .xml file containing your custom database track mod", self)
layout.addWidget(label1)
custom_track_button = QPushButton("Import Custom Track Data", self)
custom_track_button.setStyleSheet("""
QPushButton {
width: 220px;
height: 60px;
font-size: 16px;
font-weight: 500;
background: qlineargradient(spread:pad, x1:1, y1:0, x2:0, y2:1, stop:0 rgb(50, 50, 70), stop:1 rgb(70, 70, 70));
color: #fdfdfd;
border-radius: 10px;
}
QPushButton:hover {
background: qlineargradient(spread:pad, x1:1, y1:0, x2:0, y2:1, stop:0 rgb(60, 60, 80), stop:1 rgb(80, 80, 80));
border-radius: 20px;
font-weight: 700;
}
""")
custom_track_button.clicked.connect(self.import_custom_track_data)
layout.addWidget(custom_track_button)
label2 = QLabel("2. Upload the original database.xml file \n3. Save the database file as .xml and import it into the Ego Database Editor", self)
layout.addWidget(label2)
database_button = QPushButton("Upload your database.xml", self)
database_button.setStyleSheet("""
QPushButton {
width: 220px;
height: 60px;
font-size: 16px;
font-weight: 500;
background: qlineargradient(spread:pad, x1:1, y1:0, x2:0, y2:1, stop:0 rgb(50, 50, 70), stop:1 rgb(70, 70, 70));
color: #fdfdfd;
border-radius: 10px;
}
QPushButton:hover {
background: qlineargradient(spread:pad, x1:1, y1:0, x2:0, y2:1, stop:0 rgb(60, 60, 80), stop:1 rgb(80, 80, 80));
border-radius: 20px;
font-weight: 700;
}
""")
database_button.clicked.connect(self.upload_database_xml)
layout.addWidget(database_button)
check_button = QPushButton("Check for database conflicts", self)
check_button.clicked.connect(self.check_for_conflicts)
layout.addWidget(check_button)
save_button = QPushButton("Save database file as .xml", self)
save_button.setStyleSheet("""
QPushButton {
width: 220px;
height: 60px;
font-size: 16px;
font-weight: 500;
background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgb(221, 58, 70), stop:0.5 rgb(243, 29, 59), stop:1 rgb(252, 176, 69));
color: #fdfdfd;
border-radius: 10px;
}
QPushButton:hover {
background: qlineargradient(spread:pad, x1:1, y1:0, x2:0, y2:1, stop:0 rgb(253, 152, 1), stop:1 rgb(221, 58, 70));
border-radius: 20px;
font-weight: 700;
}
""")
save_button.clicked.connect(self.save_database_xml)
layout.addWidget(save_button)
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
helpMenu = menubar.addMenu('Help')
newAction = QAction('New', self)
newAction.triggered.connect(self.new_project)
fileMenu.addAction(newAction)
exitAction = QAction('Exit', self)
exitAction.triggered.connect(self.close)
fileMenu.addAction(exitAction)
aboutAction = QAction('About', self)
aboutAction.triggered.connect(self.show_about)
helpMenu.addAction(aboutAction)
self.statusBar().showMessage("Ready")
def new_project(self):
self.custom_track_data = None
self.database_xml = None
if self.temp_custom_file and os.path.exists(self.temp_custom_file.name):
os.remove(self.temp_custom_file.name) # Удаляем временный файл, если он существует
self.temp_custom_file = None
self.statusBar().showMessage("New project started")
def show_about(self):
QMessageBox.about(self, "About", "Author - AlonsoBeast\nLink to Our Discord Modding Server: https://discord.gg/KXt7NC3Sue")
def import_custom_track_data(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(self, "Select Custom Track Data File", "", "All Files (*)", options=options)
if file_name:
self.custom_track_data = file_name
self.statusBar().showMessage(f"Imported Custom Track Data: {file_name}")
# Создаем временный файл и записываем в него данные
with open(file_name, 'r', encoding='utf-8') as custom_file:
custom_content = custom_file.read()
# Используем tempfile для создания временного файла
self.temp_custom_file = tempfile.NamedTemporaryFile(delete=False, mode='w', encoding='utf-8')
self.temp_custom_file.write(custom_content)
self.temp_custom_file.close()
def upload_database_xml(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(self, "Select Database File", "", "All Files (*)", options=options)
if file_name:
self.database_xml = file_name
self.statusBar().showMessage(f"Uploaded Database File: {file_name}")
def save_database_xml(self):
if self.temp_custom_file and self.database_xml:
try:
# Чтение данных из временного файла
with open(self.temp_custom_file.name, 'r', encoding='utf-8') as custom_file:
custom_content = custom_file.read()
with open(self.database_xml, 'r', encoding='utf-8') as database_file:
database_content = database_file.read()
# Логика для обработки и сохранения данных
custom_blocks = {
'track': self.extract_block(custom_content, 'track'),
'track_car_handling': self.extract_block(custom_content, 'track_car_handling'),
'track_fuel_distance': self.extract_block(custom_content, 'track_fuel_distance'),
'track_model': self.extract_block(custom_content, 'track_model'),
'track_difficulties': self.extract_block(custom_content, 'track_difficulties')
}
# Теги для вставки
insertion_tags = {
'track': ['<track_calendar>', '<track_fuel_distance>'],
'track_car_handling': ['<track_difficulties>', '<track_fuel_distance>'],
'track_fuel_distance': ['<track_model>', '<calendar_glyph>', '<id>', '<name_string_id>'],
'track_model': ['<track_stat_value>', '<track_stat_value>'],
'track_difficulties': ['<track_fuel_distance>', '<track_fuel_distance>']
}
# Вставка на основе ключевых точек
for tag, tags in insertion_tags.items():
insertion_point = -1
for next_tag in tags:
insertion_point = database_content.find(next_tag)
if insertion_point != -1: # Если нашли, прекращаем цикл
break
if insertion_point != -1 and custom_blocks[tag]:
# Проверка на наличие track_difficulties
if tag == 'track_difficulties':
if '<track_difficulties>' not in database_content:
continue # Пропускаем вставку, если тег отсутствует
database_content = (
database_content[:insertion_point] +
custom_blocks[tag] +
"\n" +
database_content[insertion_point:]
)
file_name, _ = QFileDialog.getSaveFileName(self, "Save Database File", "", "All Files (*)", options=QFileDialog.Options())
if file_name:
with open(file_name, 'w', encoding='utf-8') as output_file:
output_file.write(database_content)
self.statusBar().showMessage(f"Database saved successfully: {file_name}")
except Exception as e:
self.statusBar().showMessage(f"Error during integration: {str(e)}")
else:
self.statusBar().showMessage("Please import both data files before saving.")
@staticmethod
def extract_block(content, tag):
"""Извлекает блок XML из содержимого для заданного тега."""
start_tag = f'<{tag}>'
end_tag = f'</{tag}>'
start_index = content.find(start_tag)
end_index = content.find(end_tag, start_index) + len(end_tag)
if start_index != -1 and end_index != -1:
return content[start_index:end_index]
return ''
def check_for_conflicts(self):
if not self.temp_custom_file or not self.database_xml:
self.statusBar().showMessage("Please import both data files before checking for conflicts.")
return
custom_ids = {
'<track> ID': None,
'<track_car_handling> ID': None,
'<track_fuel_distance> ID': None,
'<track_model> ID': None,
'<track_model> calender_glyph': None,
'<track_model> calendar_glyph': None,
'<track_model> leaderboard_id': None,
'<track_model> track_id': None,
'<track_difficulties> ID': None,
'<track_difficulties> track': None
}
# Парсинг кастомных данных
custom_tree = ET.parse(self.temp_custom_file.name)
# Сбор ID из кастомных данных
for track in custom_tree.findall(".//track"):
track_id_element = track.find("id")
if track_id_element is not None and track_id_element.text:
custom_ids['<track> ID'] = track_id_element.text
for handling in custom_tree.findall(".//track_car_handling"):
handling_id_element = handling.find("ID")
if handling_id_element is None:
handling_id_element = handling.find("id")
if handling_id_element is not None and handling_id_element.text:
custom_ids['<track_car_handling> ID'] = handling_id_element.text
for distance in custom_tree.findall(".//track_fuel_distance"):
distance_id_element = distance.find("ID")
if distance_id_element is None:
distance_id_element = distance.find("id")
if distance_id_element is not None and distance_id_element.text:
custom_ids['<track_fuel_distance> ID'] = distance_id_element.text
for model in custom_tree.findall(".//track_model"):
model_id_element = model.find("id")
if model_id_element is None:
model_id_element = model.find("ID")
if model_id_element is not None and model_id_element.text:
custom_ids['<track_model> ID'] = model_id_element.text
for child in model:
if child.tag in ["calender_glyph", "calendar_glyph", "track_id", "leaderboard_id"]:
if child.text:
custom_ids[f'<track_model> {child.tag}'] = child.text
for difficulties in custom_tree.findall(".//track_difficulties"):
difficulties_id_element = difficulties.find("ID")
if difficulties_id_element is None:
difficulties_id_element = difficulties.find("id")
if difficulties_id_element is not None and difficulties_id_element.text:
custom_ids['<track_difficulties> ID'] = difficulties_id_element.text
track_id_element = difficulties.find("track")
if track_id_element is None:
track_id_element = difficulties.find("Track")
if track_id_element is not None and track_id_element.text:
custom_ids['<track_difficulties> track'] = track_id_element.text
print(f"Custom IDs: {custom_ids}") # Выводим найденные идентификаторы кастомного трека
# Проверка конфликтов с базой данных
conflicts = {}
database_tree = ET.parse(self.database_xml)
print("Checking for conflicts in the database...")
# Определяем пространство имен
ns = {'ns': 'schemaF12013.xml', 'ns2': 'schemaF12014.xml'}
found_in_db = {
'track': database_tree.findall(".//ns:track", ns) + database_tree.findall(".//ns2:track", ns),
'track_car_handling': database_tree.findall(".//ns:track_car_handling", ns) + database_tree.findall(".//ns2:track_car_handling", ns),
'track_fuel_distance': database_tree.findall(".//ns:track_fuel_distance", ns) + database_tree.findall(".//ns2:track_fuel_distance", ns),
'track_model': database_tree.findall(".//ns:track_model", ns) + database_tree.findall(".//ns2:track_model", ns),
'track_difficulties': database_tree.findall(".//ns:track_difficulties", ns) + database_tree.findall(".//ns2:track_difficulties", ns)
}
# Проверка каждой секции жить пожить жить пожить
for db_type, elements in found_in_db.items():
for database_element in elements:
db_id = database_element.find("ns:id", ns)
if db_id is None:
db_id = database_element.find("ns:ID", ns)
if db_id is None:
db_id = database_element.find("ns2:id", ns)
if db_id is None:
db_id = database_element.find("ns2:ID", ns)
if db_id is not None and db_id.text:
print(f"Found in database: {database_element.tag} -> {db_id.text}")
# Проверяем для конкретного типа базы данных
for id_tag, custom_id in custom_ids.items():
if db_id.text == custom_id:
if f"<{db_type}> ID" in id_tag: # Проверяем откуда этот идентификатор
conflicts[id_tag] = custom_id
print(f"Conflict detected: {id_tag} -> {custom_id}")
if conflicts:
print("Conflicts found: ", conflicts)
conflict_dialog = IDConflictDialog(conflicts, self.update_temp_custom_file, self)
conflict_dialog.exec_()
else:
QMessageBox.information(self, "No Conflicts", "No ID conflicts found.")
def update_temp_custom_file(self, id_tag, original_id, new_id):
# Обновление временного файла кастомных данных
with open(self.temp_custom_file.name, 'r', encoding='utf-8') as custom_file:
custom_content = custom_file.read()
# Замена оригинального ID на новый
custom_content = custom_content.replace(original_id, new_id)
with open(self.temp_custom_file.name, 'w', encoding='utf-8') as custom_file:
custom_file.write(custom_content)
The provided code is a Python application using PyQt5 for a GUI that serves as a mod manager for an F1 racing game (specifically the 2014 version). It allows users to manage custom track data and XML files for their mods, check for conflicts between custom and original database files, and resolve those conflicts through a user interface.
Here's an overview of the key components and functionality included in the application:
Main Components
IDConflictDialog:
A dialog for users to resolve detected ID conflicts. It shows which IDs are conflicting and allows users to enter new unique IDs for each conflict. When the user confirms, the dialog updates the respective IDs in the custom track data.
F1ModManager:
Extends QMainWindow and serves as the main application window.
It has UI components that allow users to upload custom track data, upload the original database XML file, check for conflicts, and save the modified database.
User Interface (UI):
The application has a clean layout with buttons for importing track data, uploading the database, checking for conflicts, and saving the database.
Styling is done using a combination of Qt Stylesheets, providing visual feedback, and improving the aesthetics of buttons and labels.
Key Functionalities
Importing Data: Users can import custom track data and the original XML database file.
Conflict Checking: When conflicts are detected between the custom and original IDs, the user is prompted to resolve them.
Updating IDs: The application allows users to promptly update IDs and save changes.
The conflict resolution logic is carefully designed to search through both custom and database XML files for specific tags and IDs. When matches are found, users are prompted to enter new IDs.
The updates are made directly within the temporary file to ensure that the main application remains stable.
Suggestions for Improvement or Extensions
Enhanced Error Handling:
Implement more robust error handling, especially around file operations (e.g., checking if files are valid XML) and user inputs.
User Feedback:
Consider implementing a status update UI element that gives more granular feedback on the processes (like loading spinners).
Expand Functionality:
Extend the application to allow more types of conflicts or to handle other mod types beyond tracks.
Code Modularity:
Break down some larger functions into smaller helper methods to improve readability and maintainability.
Testing:
Write unit tests to ensure that each component functions correctly and edge cases are handled.
Overall, your implementation is quite structured and serves a specific purpose effectively through a user-friendly interface. It demonstrates good practices in GUI application design while utilizing PyQt5 efficiently. If you have specific areas you'd like to focus on or further details to discuss, let me know!
Custom IDs: {' ID': '101', ' ID': '22', ' ID': '20', ' ID': '190', ' calender_glyph': '7', ' calendar_glyph': '11', ' leaderboard_id': '17', ' track_id': '111', ' ID': '26', ' track': '114'}
Checking for conflicts in the database...
Found in database: {schemaF12014.xml}track -> 79
Found in database: {schemaF12014.xml}track -> 82
Found in database: {schemaF12014.xml}track -> 83
Found in database: {schemaF12014.xml}track -> 84
Found in database: {schemaF12014.xml}track -> 85
Found in database: {schemaF12014.xml}track -> 86
Found in database: {schemaF12014.xml}track -> 87
Found in database: {schemaF12014.xml}track -> 88
Found in database: {schemaF12014.xml}track -> 89
Found in database: {schemaF12014.xml}track -> 91
Found in database: {schemaF12014.xml}track -> 92
Found in database: {schemaF12014.xml}track -> 93
Found in database: {schemaF12014.xml}track -> 95
Found in database: {schemaF12014.xml}track -> 96
Found in database: {schemaF12014.xml}track -> 97
Found in database: {schemaF12014.xml}track -> 98
Found in database: {schemaF12014.xml}track -> 99
Found in database: {schemaF12014.xml}track -> 101
Conflict detected: ID -> 101
Found in database: {schemaF12014.xml}track -> 104
Found in database: {schemaF12014.xml}track -> 111
Found in database: {schemaF12014.xml}track -> 112
Found in database: {schemaF12014.xml}track_car_handling -> 1
Found in database: {schemaF12014.xml}track_car_handling -> 2
Found in database: {schemaF12014.xml}track_car_handling -> 3
Found in database: {schemaF12014.xml}track_car_handling -> 4
Found in database: {schemaF12014.xml}track_car_handling -> 5
Found in database: {schemaF12014.xml}track_car_handling -> 6
Found in database: {schemaF12014.xml}track_car_handling -> 8
Found in database: {schemaF12014.xml}track_car_handling -> 9
Found in database: {schemaF12014.xml}track_car_handling -> 10
Found in database: {schemaF12014.xml}track_car_handling -> 11
Found in database: {schemaF12014.xml}track_car_handling -> 12
Found in database: {schemaF12014.xml}track_car_handling -> 13
Found in database: {schemaF12014.xml}track_car_handling -> 14
Found in database: {schemaF12014.xml}track_car_handling -> 15
Found in database: {schemaF12014.xml}track_car_handling -> 16
Found in database: {schemaF12014.xml}track_car_handling -> 18
Found in database: {schemaF12014.xml}track_car_handling -> 19
Found in database: {schemaF12014.xml}track_car_handling -> 21
Found in database: {schemaF12014.xml}track_car_handling -> 23
Found in database: {schemaF12014.xml}track_car_handling -> 29
Found in database: {schemaF12014.xml}track_fuel_distance -> 20
Conflict detected: ID -> 20
Found in database: {schemaF12014.xml}track_fuel_distance -> 21
Found in database: {schemaF12014.xml}track_fuel_distance -> 22
Found in database: {schemaF12014.xml}track_fuel_distance -> 24
Found in database: {schemaF12014.xml}track_fuel_distance -> 25
Found in database: {schemaF12014.xml}track_fuel_distance -> 1
Found in database: {schemaF12014.xml}track_fuel_distance -> 2
Found in database: {schemaF12014.xml}track_fuel_distance -> 3
Found in database: {schemaF12014.xml}track_fuel_distance -> 4
Found in database: {schemaF12014.xml}track_fuel_distance -> 5
Found in database: {schemaF12014.xml}track_fuel_distance -> 6
Found in database: {schemaF12014.xml}track_fuel_distance -> 7
Found in database: {schemaF12014.xml}track_fuel_distance -> 8
Found in database: {schemaF12014.xml}track_fuel_distance -> 9
Found in database: {schemaF12014.xml}track_fuel_distance -> 10
Found in database: {schemaF12014.xml}track_fuel_distance -> 11
Found in database: {schemaF12014.xml}track_fuel_distance -> 12
Found in database: {schemaF12014.xml}track_fuel_distance -> 13
Found in database: {schemaF12014.xml}track_fuel_distance -> 14
Found in database: {schemaF12014.xml}track_fuel_distance -> 15
Found in database: {schemaF12014.xml}track_fuel_distance -> 16
Found in database: {schemaF12014.xml}track_fuel_distance -> 17
Found in database: {schemaF12014.xml}track_fuel_distance -> 18
Found in database: {schemaF12014.xml}track_fuel_distance -> 19
Found in database: {schemaF12014.xml}track_fuel_distance -> 23
Found in database: {schemaF12014.xml}track_fuel_distance -> 26
Found in database: {schemaF12014.xml}track_fuel_distance -> 27
Found in database: {schemaF12014.xml}track_fuel_distance -> 28
Found in database: {schemaF12014.xml}track_model -> 44
Found in database: {schemaF12014.xml}track_model -> 118
Found in database: {schemaF12014.xml}track_model -> 119
Found in database: {schemaF12014.xml}track_model -> 176
Found in database: {schemaF12014.xml}track_model -> 179
Found in database: {schemaF12014.xml}track_model -> 180
Found in database: {schemaF12014.xml}track_model -> 181
Found in database: {schemaF12014.xml}track_model -> 184
Found in database: {schemaF12014.xml}track_model -> 185
Found in database: {schemaF12014.xml}track_model -> 186
Found in database: {schemaF12014.xml}track_model -> 189
Found in database: {schemaF12014.xml}track_model -> 190
Conflict detected: ID -> 190
Found in database: {schemaF12014.xml}track_model -> 191
Found in database: {schemaF12014.xml}track_model -> 193
Found in database: {schemaF12014.xml}track_model -> 194
Found in database: {schemaF12014.xml}track_model -> 195
Found in database: {schemaF12014.xml}track_model -> 196
Found in database: {schemaF12014.xml}track_model -> 199
Found in database: {schemaF12014.xml}track_model -> 201
Found in database: {schemaF12014.xml}track_model -> 205
Found in database: {schemaF12014.xml}track_model -> 212
Found in database: {schemaF12014.xml}track_model -> 213
Found in database: {schemaF12014.xml}track_model -> 220
Found in database: {schemaF12014.xml}track_model -> 221
Conflicts found: {' ID': '101', ' ID': '20', ' ID': '190'}
смотри, вот он корректно ID находит внутри тегов, но к сожалению, не находит внутри track_model специфичные данные типа ' calender_glyph': '7', ' calendar_glyph': '11', ' leaderboard_id': '17', ' track_id': '111
хотя я точно знаю, что конфликты по ним в датабазе имеются, их теги корректно именуются тоже. грубо говоря, нам нужно чтобы искало не только ID элемент строки, но и календарь глиф лидерборд и т.д, что показал в списке, спасиибо
To extend the functionality of your script to check for conflicts not only with standard ID elements but also with other specific fields like calender_glyph, calendar_glyph, leaderboard_id, and track_id, you will need to make a few adjustments to your check_for_conflicts method.
Here’s a breakdown of how you can modify the function to include additional checks for these specific tags:
Updated check_for_conflicts Method
Here’s an updated version of the check_for_conflicts method in the F1ModManager class. This code now checks for both ID elements and the additional specified tags within their respective XML structures:
python
def check_for_conflicts(self):
if not self.temp_custom_file or not self.database_xml:
self.statusBar().showMessage("Please import both data files before checking for conflicts.")
return
custom_ids = {
'<track> ID': None,
'<track_car_handling> ID': None,
'<track_fuel_distance> ID': None,
'<track_model> ID': None,
'<track_model> calender_glyph': None,
'<track_model> calendar_glyph': None,
'<track_model> leaderboard_id': None,
'<track_model> track_id': None,
'<track_difficulties> ID': None,
'<track_difficulties> track': None
}
# Parsing custom data
custom_tree = ET.parse(self.temp_custom_file.name)
# Collecting IDs and additional fields from custom data
for track in custom_tree.findall(".//track"):
track_id_element = track.find("id")
if track_id_element is not None and track_id_element.text:
custom_ids['<track> ID'] = track_id_element.text
for handling in custom_tree.findall(".//track_car_handling"):
handling_id_element = handling.find("ID")
if handling_id_element is None:
handling_id_element = handling.find("id")
if handling_id_element is not None and handling_id_element.text:
custom_ids['<track_car_handling> ID'] = handling_id_element.text
for distance in custom_tree.findall(".//track_fuel_distance"):
distance_id_element = distance.find("ID")
if distance_id_element is None:
distance_id_element = distance.find("id")
if distance_id_element is not None and distance_id_element.text:
custom_ids['<track_fuel_distance> ID'] = distance_id_element.text
for model in custom_tree.findall(".//track_model"):
model_id_element = model.find("id")
if model_id_element is None:
model_id_element = model.find("ID")
if model_id_element is not None and model_id_element.text:
custom_ids['<track_model> ID'] = model_id_element.text
# Collecting additional fields inside track_model
for child in model:
if child.tag in ["calender_glyph", "calendar_glyph", "leaderboard_id", "track_id"]:
if child.text:
custom_ids[f'<track_model> {child.tag}'] = child.text
print(f"Custom IDs: {custom_ids}") # Output found identifiers for the custom track
# Checking conflicts with the database
conflicts = {}
database_tree = ET.parse(self.database_xml)
print("Checking for conflicts in the database...")
# Defining namespaces
ns = {'ns': 'schemaF12013.xml', 'ns2': 'schemaF12014.xml'}
found_in_db = {
'track': database_tree.findall(".//ns:track", ns) + database_tree.findall(".//ns2:track", ns),
'track_car_handling': database_tree.findall(".//ns:track_car_handling", ns) + database_tree.findall(".//ns2:track_car_handling", ns),
'track_fuel_distance': database_tree.findall(".//ns:track_fuel_distance", ns) + database_tree.findall(".//ns2:track_fuel_distance", ns),
'track_model': database_tree.findall(".//ns:track_model", ns) + database_tree.findall(".//ns2:track_model", ns),
'track_difficulties': database_tree.findall(".//ns:track_difficulties", ns) + database_tree.findall(".//ns2:track_difficulties", ns)
}
# Checking each section for conflicts
for db_type, elements in found_in_db.items():
for database_element in elements:
# Check for ID elements
db_id = database_element.find("ns:id", ns)
if db_id is None:
db_id = database_element.find("ns:ID", ns)
if db_id is None:
db_id = database_element.find("ns2:id", ns)
if db_id is None:
db_id = database_element.find("ns2:ID", ns)
# Check for additional fields that may cause conflicts
for id_tag, custom_id in custom_ids.items():
if db_id is not None and db_id.text == custom_id:
conflicts[id_tag] = custom_id
print(f"Conflict detected: {id_tag} -> {custom_id}")
# Also check for specific fields like calendar_glyph, leaderboard_id, and track_id
if 'track_model' in db_type:
for extra_field in ["calender_glyph", "calendar_glyph", "leaderboard_id", "track_id"]:
extra_value_element = database_element.find(extra_field)
if extra_value_element is not None and extra_value_element.text == custom_ids.get(f'<track_model> {extra_field}', None):
conflicts[f'<track_model> {extra_field}'] = extra_value_element.text
print(f"Conflict detected: <track_model> {extra_field} -> {extra_value_element.text}")
if conflicts:
print("Conflicts found: ", conflicts)
conflict_dialog = IDConflictDialog(conflicts, self.update_temp_custom_file, self)
conflict_dialog.exec_()
else:
QMessageBox.information(self, "No Conflicts", "No ID conflicts found.")
Explanation of the Changes:
Collecting Additional Fields: During the extraction of custom identifiers, I ensured to collect values from the specified additional fields within the track_model, such as calender_glyph, calendar_glyph, leaderboard_id, and track_id.
Conflict Checking for Additional Fields: When iterating through the database elements, I added a check specifically for calender_glyph, calendar_glyph, leaderboard_id, and track_id elements in the track_model. This logic sees whether the extracted values from the custom track data collide with those found in the database.
Output for Conflict Detection: Each detected conflict is printed out for clarity, which will assist you in debugging and responding to conflicts within your development process.
This approach should help you accurately detect conflicts for the additional IDs specified and ensure the integrity of your mod files. If you have any further questions or need additional modifications, feel free to ask!