mirror of
https://github.com/ModOrganizer2/modorganizer.git
synced 2026-07-27 13:58:24 -07:00
241 lines
7.2 KiB
C++
241 lines
7.2 KiB
C++
/*
|
|
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
|
|
|
|
This file is part of Mod Organizer.
|
|
|
|
Mod Organizer is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Mod Organizer is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "executableslist.h"
|
|
#include <gameinfo.h>
|
|
#include <QFileInfo>
|
|
#include <QDir>
|
|
#include <QDebug>
|
|
#include "utility.h"
|
|
#include <algorithm>
|
|
|
|
|
|
using namespace MOBase;
|
|
using namespace MOShared;
|
|
|
|
|
|
static QDataStream &operator<<(QDataStream &out, const Executable &obj)
|
|
{
|
|
out << obj.m_Title
|
|
<< obj.m_BinaryInfo.absoluteFilePath()
|
|
<< obj.m_Arguments
|
|
<< static_cast<std::underlying_type<ExecutableInfo::CloseMOStyle>::type>(obj.m_CloseMO)
|
|
<< obj.m_SteamAppID
|
|
<< obj.m_WorkingDirectory
|
|
<< obj.m_Custom
|
|
<< obj.m_Toolbar << obj.m_UseOwnIcon;
|
|
return out;
|
|
}
|
|
|
|
static QDataStream &operator>>(QDataStream &in, Executable &obj)
|
|
{
|
|
QString binaryTemp;
|
|
int closeStyleTemp;
|
|
in >> obj.m_Title >> binaryTemp >> obj.m_Arguments >> closeStyleTemp
|
|
>> obj.m_SteamAppID >> obj.m_WorkingDirectory >> obj.m_Custom >> obj.m_Toolbar >> obj.m_UseOwnIcon;
|
|
|
|
obj.m_CloseMO = static_cast<ExecutableInfo::CloseMOStyle>(closeStyleTemp);
|
|
obj.m_BinaryInfo.setFile(binaryTemp);
|
|
return in;
|
|
}
|
|
|
|
ExecutablesList::ExecutablesList()
|
|
{
|
|
}
|
|
|
|
ExecutablesList::~ExecutablesList()
|
|
{
|
|
}
|
|
|
|
void ExecutablesList::init(IPluginGame *game)
|
|
{
|
|
Q_ASSERT(game != nullptr);
|
|
m_Executables.clear();
|
|
for (const ExecutableInfo &info : game->executables()) {
|
|
if (info.isValid()) {
|
|
addExecutableInternal(info.title(),
|
|
info.binary().absoluteFilePath(),
|
|
info.arguments().join(" "),
|
|
info.workingDirectory().absolutePath(),
|
|
info.closeMO(),
|
|
info.steamAppID());
|
|
}
|
|
}
|
|
}
|
|
|
|
void ExecutablesList::getExecutables(std::vector<Executable>::iterator &begin, std::vector<Executable>::iterator &end)
|
|
{
|
|
begin = m_Executables.begin();
|
|
end = m_Executables.end();
|
|
}
|
|
|
|
void ExecutablesList::getExecutables(std::vector<Executable>::const_iterator &begin,
|
|
std::vector<Executable>::const_iterator &end) const
|
|
{
|
|
begin = m_Executables.begin();
|
|
end = m_Executables.end();
|
|
}
|
|
|
|
const Executable &ExecutablesList::find(const QString &title) const
|
|
{
|
|
for (Executable const &exe : m_Executables) {
|
|
if (exe.m_Title == title) {
|
|
return exe;
|
|
}
|
|
}
|
|
throw std::runtime_error("invalid name " + title.toStdString());
|
|
}
|
|
|
|
|
|
Executable &ExecutablesList::find(const QString &title)
|
|
{
|
|
for (Executable &exe : m_Executables) {
|
|
if (exe.m_Title == title) {
|
|
return exe;
|
|
}
|
|
}
|
|
throw std::runtime_error("invalid name " + title.toStdString());
|
|
}
|
|
|
|
|
|
Executable &ExecutablesList::findByBinary(const QFileInfo &info)
|
|
{
|
|
for (Executable &exe : m_Executables) {
|
|
if (info == exe.m_BinaryInfo) {
|
|
return exe;
|
|
}
|
|
}
|
|
throw std::runtime_error("invalid info");
|
|
}
|
|
|
|
|
|
std::vector<Executable>::iterator ExecutablesList::findExe(const QString &title)
|
|
{
|
|
for (std::vector<Executable>::iterator iter = m_Executables.begin(); iter != m_Executables.end(); ++iter) {
|
|
if (iter->m_Title == title) {
|
|
return iter;
|
|
}
|
|
}
|
|
return m_Executables.end();
|
|
}
|
|
|
|
|
|
bool ExecutablesList::titleExists(const QString &title) const
|
|
{
|
|
auto test = [&] (const Executable &exe) { return exe.m_Title == title; };
|
|
return std::find_if(m_Executables.begin(), m_Executables.end(), test) != m_Executables.end();
|
|
}
|
|
|
|
|
|
void ExecutablesList::addExecutable(const Executable &executable)
|
|
{
|
|
auto existingExe = findExe(executable.m_Title);
|
|
if (existingExe != m_Executables.end()) {
|
|
*existingExe = executable;
|
|
} else {
|
|
m_Executables.push_back(executable);
|
|
}
|
|
}
|
|
|
|
|
|
void ExecutablesList::position(const QString &title, bool toolbar, int pos)
|
|
{
|
|
auto existingExe = findExe(title);
|
|
if (existingExe != m_Executables.end()) {
|
|
Executable temp = *existingExe;
|
|
temp.m_Toolbar = toolbar;
|
|
m_Executables.erase(existingExe);
|
|
m_Executables.insert(m_Executables.begin() + pos, temp);
|
|
}
|
|
}
|
|
|
|
|
|
void ExecutablesList::addExecutable(const QString &title,
|
|
const QString &executableName,
|
|
const QString &arguments,
|
|
const QString &workingDirectory,
|
|
ExecutableInfo::CloseMOStyle closeMO,
|
|
const QString &steamAppID,
|
|
bool custom,
|
|
bool toolbar,
|
|
bool ownicon)
|
|
{
|
|
QFileInfo file(executableName);
|
|
auto existingExe = findExe(title);
|
|
|
|
if (existingExe != m_Executables.end()) {
|
|
existingExe->m_Title = title;
|
|
existingExe->m_Custom = custom;
|
|
existingExe->m_CloseMO = closeMO;
|
|
existingExe->m_Toolbar = toolbar;
|
|
if (custom) {
|
|
// for pre-configured executables don't overwrite settings we didn't store
|
|
existingExe->m_BinaryInfo = file;
|
|
existingExe->m_Arguments = arguments;
|
|
existingExe->m_WorkingDirectory = workingDirectory;
|
|
existingExe->m_SteamAppID = steamAppID;
|
|
existingExe->m_UseOwnIcon = ownicon;
|
|
}
|
|
} else {
|
|
Executable newExe;
|
|
newExe.m_Title = title;
|
|
newExe.m_CloseMO = closeMO;
|
|
newExe.m_BinaryInfo = file;
|
|
newExe.m_Arguments = arguments;
|
|
newExe.m_WorkingDirectory = workingDirectory;
|
|
newExe.m_SteamAppID = steamAppID;
|
|
newExe.m_Custom = true;
|
|
newExe.m_Toolbar = toolbar;
|
|
newExe.m_UseOwnIcon = ownicon;
|
|
m_Executables.push_back(newExe);
|
|
}
|
|
}
|
|
|
|
|
|
void ExecutablesList::remove(const QString &title)
|
|
{
|
|
for (std::vector<Executable>::iterator iter = m_Executables.begin(); iter != m_Executables.end(); ++iter) {
|
|
if (iter->m_Custom && iter->m_Title == title) {
|
|
m_Executables.erase(iter);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void ExecutablesList::addExecutableInternal(const QString &title, const QString &executableName,
|
|
const QString &arguments, const QString &workingDirectory,
|
|
ExecutableInfo::CloseMOStyle closeMO, const QString &steamAppID)
|
|
{
|
|
QFileInfo file(executableName);
|
|
if (file.exists()) {
|
|
Executable newExe;
|
|
newExe.m_CloseMO = closeMO;
|
|
newExe.m_BinaryInfo = file;
|
|
newExe.m_Title = title;
|
|
newExe.m_Arguments = arguments;
|
|
newExe.m_WorkingDirectory = workingDirectory;
|
|
newExe.m_SteamAppID = steamAppID;
|
|
newExe.m_Custom = false;
|
|
newExe.m_Toolbar = false;
|
|
newExe.m_UseOwnIcon = false;
|
|
m_Executables.push_back(newExe);
|
|
}
|
|
}
|