-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodeltonsbmd.cpp
More file actions
197 lines (164 loc) · 5.62 KB
/
modeltonsbmd.cpp
File metadata and controls
197 lines (164 loc) · 5.62 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "modeltonsbmd.h"
#include "ui_modeltonsbmd.h"
#include <QFileDialog>
static QString exeDir;
ModelToNSBMD::ModelToNSBMD(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ModelToNSBMD)
{
ui->setupUi(this);
exeDir = QApplication::applicationDirPath();
}
ModelToNSBMD::~ModelToNSBMD()
{
delete ui;
}
void ModelToNSBMD::on_searchPath1_pb_clicked()
{
static QString lastDirSelected = QDir::homePath();
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Model File"), lastDirSelected, ""/*tr("Model Files (*.png *.jpg *.bmp)")*/);
if(fileName == "")
return;
lastDirSelected = fileName;
ui->path1_le->setText(fileName);
}
void ModelToNSBMD::on_seachPath2_pb_clicked()
{
static QString lastDirSelected = QDir::homePath();
if(lastDirSelected == QDir::homePath() && ui->path1_le->text() != "")
lastDirSelected = ui->path1_le->text().split('.')[0];
QString fileName = QFileDialog::getSaveFileName(this, tr("Save NSBMD File"), lastDirSelected, tr("NSBMD File (*.nsbmd)"));
if(fileName == "")
return;
lastDirSelected = fileName;
ui->path2_le->setText(fileName);
}
//CONVERSION STUFF AHEAD
void ModelToNSBMD::printToConsole(const QString &text)
{
ui->console_tb->setTextColor(QColor(0, 255, 0));
QString textToLower = text.toLower();
if(textToLower.contains("error"))
ui->console_tb->setTextColor(QColor("red"));
else if(textToLower.contains("warning"))
ui->console_tb->setTextColor(QColor("yellow"));
ui->console_tb->append(text);
QApplication::processEvents(); //Force UI update
}
void ModelToNSBMD::printAppOutputToConsole(QProcess* process)
{
QString line;
do
{
line = process->readLine();
if (line.toLower().contains("error:") || line.toLower().contains("warning:"))
{
printToConsole(line.trimmed());
}
}
while (!line.isNull());
}
void ModelToNSBMD::on_convert_pb_clicked()
{
ui->console_tb->clear();
QString sourcePath = ui->path1_le->text();
QString destinationPath = ui->path2_le->text();
if(sourcePath == "")
printToConsole("Error: No source model file was specified!");
if(destinationPath == "")
printToConsole("Error: No destination NSBMD path was specified!");
if(sourcePath == "" || destinationPath == "")
return;
bool IsSourceFileIMD = false;
if(sourcePath.endsWith(".imd", Qt::CaseInsensitive))
IsSourceFileIMD = true;
QDir tempDir(exeDir + "/temp");
if(!IsSourceFileIMD)
{
printToConsole("Cleaning temp folder...");
tempDir.removeRecursively();
while(tempDir.exists()) {} //Wait for temp folder to be deleted
QDir().mkdir(tempDir.path());
printToConsole("Starting conversion to IMD...");
QProcess* ass2imd = new QProcess();
ass2imd->setProgram(exeDir + "/bin/ass2imd/AssToImd");
ass2imd->setArguments({sourcePath, "-o", tempDir.path() + "/temp.imd"});
ass2imd->start();
if(ass2imd->state() == QProcess::NotRunning)
{
printToConsole("Error: AssToImd failed to start or could not be found!");
return;
}
connect(ass2imd, &QProcess::readyRead, [=](){ printAppOutputToConsole(ass2imd); });
ass2imd->waitForFinished(-1);
delete ass2imd;
ass2imd = nullptr;
if(QFile::exists(tempDir.path() + "/temp.imd"))
printToConsole("Success: IMD generated successfully!");
else
{
printToConsole("Error: IMD failed to generate!");
return;
}
}
QString sourcePath2 = tempDir.path() + "/temp.imd";
if(IsSourceFileIMD)
sourcePath2 = sourcePath;
printToConsole("Starting conversion to NSBMD...");
QProcess* imd2nsbmd = new QProcess();
imd2nsbmd->setProgram(exeDir + "/bin/imd2bin/imd2bin");
imd2nsbmd->setArguments({sourcePath2, "-o", destinationPath});
imd2nsbmd->start();
if(imd2nsbmd->state() == QProcess::NotRunning)
{
printToConsole("Error: imd2bin failed to start or could not be found!");
return;
}
connect(imd2nsbmd, &QProcess::readyRead, [=](){ printAppOutputToConsole(imd2nsbmd); });
imd2nsbmd->waitForFinished(-1);
delete imd2nsbmd;
imd2nsbmd = nullptr;
if(QFile::exists(destinationPath))
{
QFile file(destinationPath);
if (!file.open(QIODevice::ReadWrite)) return;
QByteArray fileBytes = file.readAll();
QString name = ui->path3_le->text();
if(name == "")
name = "unnamed";
//Mess crap because QByteArray replace is stupid and a fixed replace size can't be set
QByteArray nameBytes = name.toUtf8();
nameBytes.resize(16);
for(int i = name.length(); i < 16; i++)
nameBytes[i] = 0;
//Mess end lol
//Set NSBMD name
fileBytes.replace(0x38, 16, nameBytes);
file.resize(0);
file.write(fileBytes);
file.close();
printToConsole("Success: NSBMD generated successfully!");
}
else
{
printToConsole("Error: NSBMD failed to generate!");
return;
}
}
void ModelToNSBMD::on_path3_le_textChanged()
{
int cursorPos = ui->path3_le->cursorPosition();
QString newText;
for(QChar character : ui->path3_le->text())
{
if(!((character >= 'A' && character <= 'Z') ||
(character >= 'a' && character <= 'z') ||
(character >= '0' && character <= '9')))
{
character = '_';
}
newText.append(character);
}
ui->path3_le->setText(newText);
ui->path3_le->setCursorPosition(cursorPos);
}