-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (145 loc) · 6.21 KB
/
Copy pathmain.cpp
File metadata and controls
165 lines (145 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "choosemode.h"
#include "csmmmode.h"
#include <QApplication>
#include <QDir>
#include <QProgressDialog>
#include <QStyleHints>
#include "lib/python/pythonbindings.h"
#include "mainwindow.h"
#include "darkdetect.h"
#include "quicksetupdialog.h"
#include <pybind11/embed.h>
#include <QTranslator>
#include "lib/region.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#include "maincli.h"
PYBIND11_EMBEDDED_MODULE(pycsmm, m) {
init_pycsmm(m);
}
static void setPyHome() {
#ifdef Q_OS_MAC
Py_SetPythonHome(QDir(QApplication::applicationDirPath()).filePath("../Resources").toStdWString().c_str());
#else
Py_SetPythonHome(QDir(QApplication::applicationDirPath()).filePath("py").toStdWString().c_str());
#endif
}
static constexpr int MAX_LOGS = 10;
static QFile logFile;
static void initLogFile() {
if (!logFile.isOpen()) {
QDir logFileDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (!logFileDir.mkpath(".")) {
qWarning() << QObject::tr("could not create logging directory: ") << logFileDir.path();
return;
}
logFile.setFileName(logFileDir.filePath(QString("csmmgui-%0.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"))));
if (!logFile.open(QFile::WriteOnly)) {
qWarning() << QObject::tr("could not create log file") << logFile.fileName() << "for writing";
return;
}
logFile.write(QString(QObject::tr("Running CSMM %1\n")).arg(QCoreApplication::applicationVersion()).toUtf8());
if (!logFile.link(logFileDir.filePath("csmmgui-latest.log"))) {
qWarning() << QObject::tr("could not symlink to latest log file: ") << logFile.fileName();
}
auto pastLogs = logFileDir.entryInfoList({"csmmgui*.log"}, QDir::Files);
std::sort(pastLogs.begin(), pastLogs.end(), [](const QFileInfo &A, const QFileInfo &B) {
return A.fileTime(QFile::FileBirthTime) > B.fileTime(QFile::FileBirthTime);
});
while (pastLogs.size() > MAX_LOGS) {
if (!QFile::remove(pastLogs.back().absoluteFilePath())) {
qWarning() << QObject::tr("could not remove old log file: ") << pastLogs.back().absoluteFilePath();
}
pastLogs.pop_back();
}
}
}
int main(int argc, char *argv[])
{
// HACK: we need ENSUREPIP_OPTIONS set on Unix b/c pip otherwise thinks we're using a framework
if (qEnvironmentVariableIsEmpty("ENSUREPIP_OPTIONS")) {
qputenv("ENSUREPIP_OPTIONS", "1");
}
bool consoleMode = argc > 1;
// consoleMode = true;
if(consoleMode) {
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("csmm");
QCoreApplication::setApplicationVersion(QString("%1").arg(CSMM_VERSION));
setPyHome();
QStringList arguments = QCoreApplication::arguments();
// arguments.clear();
// arguments << "csmm" << "save" << "C:\\BoomStreet\\ST7P01";
maincli::run(arguments);
return 0;
} else {
QApplication app(argc, argv);
QCoreApplication::setOrganizationName("Custom Street");
QCoreApplication::setOrganizationDomain("fortunestreetmodding.github.io");
QCoreApplication::setApplicationName("csmm");
QCoreApplication::setApplicationVersion(QString("%1").arg(CSMM_VERSION));
initWindowPaletteSettings(app.styleHints()->colorScheme());
setPyHome();
pybind11::scoped_interpreter guard{}; // start the python interpreter
initLogFile();
// show progress messages in progress dialog
qInstallMessageHandler([](QtMsgType type, const QMessageLogContext &context, const QString &msg) {
// filter this specific message since it is littering the whole console output
if(msg == "QFutureWatcher::connect: connecting after calling setFuture() is likely to produce race")
return;
static QTextStream cerr(stderr);
static QTextStream logFileStream(&logFile);
cerr << msg << Qt::endl;
if (logFile.isOpen()) {
logFileStream << msg << Qt::endl;
}
auto progressDialog = dynamic_cast<QProgressDialog *>(QApplication::activeModalWidget());
if (type != QtMsgType::QtDebugMsg && progressDialog != nullptr) {
progressDialog->setLabelText(msg);
}
});
#ifdef Q_OS_WIN
// hide console window under Windows but only if the first argument is the full path to the executable
// -> this indicates that the exe file has been started by mouse double click
if(QCoreApplication::applicationFilePath().replace("/", "\\") == QApplication::arguments().at(0)) {
::ShowWindow( ::GetConsoleWindow(), SW_HIDE );
}
#endif
QSettings settings;
// configure the network cache and temporary directories if they are not present
if (!settings.contains("networkCacheDirectory") || !settings.value("networkCacheDirectory").isValid()) {
QDir applicationCacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
auto defaultNetworkCacheDir = applicationCacheDir.filePath("networkCache");
settings.setValue("networkCacheDirectory", defaultNetworkCacheDir);
}
if (!settings.contains("temporaryDirectory") || !settings.value("temporaryDirectory").isValid()) {
QTemporaryDir d;
settings.setValue("temporaryDirectory", d.path());
d.remove();
}
QWidget *w;
switch (settings.value("csmmMode", INDETERMINATE).toInt()) {
case INDETERMINATE:
w = new ChooseMode();
break;
case EXPRESS:
w = new QuickSetupDialog("02", false);
break;
case ADVANCED:
w = new MainWindow();
break;
}
Region::instance().initializeRegionSettings();
#ifdef Q_OS_LINUX
auto iconPath = QDir(QApplication::applicationDirPath()).filePath("../../AppIcon.png");
if (QFile::exists(iconPath)) {
w->setWindowIcon(QIcon(iconPath));
}
#endif
w->show();
int e = app.exec();
exit( e ); // needed to exit the hidden console
return e;
}
}