-
Notifications
You must be signed in to change notification settings - Fork 2
/
texturednsbmdtonsbtx.cpp
171 lines (144 loc) · 5.36 KB
/
texturednsbmdtonsbtx.cpp
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
#include "texturednsbmdtonsbtx.h"
#include "ui_texturednsbmdtonsbtx.h"
#include <QFileDialog>
TexturedNSBMDToNSBTX::TexturedNSBMDToNSBTX(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TexturedNSBMDToNSBTX)
{
ui->setupUi(this);
}
TexturedNSBMDToNSBTX::~TexturedNSBMDToNSBTX()
{
delete ui;
}
void TexturedNSBMDToNSBTX::on_searchPath1_pb_clicked()
{
static QString lastDirSelected = QDir::homePath();
QString fileName = QFileDialog::getOpenFileName(this, tr("Open NSBMD File"), lastDirSelected, tr("NSBMD File (*.nsbmd)"));
if(fileName == "")
return;
lastDirSelected = fileName;
ui->path1_le->setText(fileName);
}
void TexturedNSBMDToNSBTX::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 NSBTX File"), lastDirSelected, tr("NSBTX File (*.nsbtx)"));
if(fileName == "")
return;
lastDirSelected = fileName;
ui->path2_le->setText(fileName);
}
//CONVERSION STUFF AHEAD
void TexturedNSBMDToNSBTX::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 TexturedNSBMDToNSBTX::on_convert_pb_clicked()
{
ui->console_tb->clear();
QString sourcePath = ui->path1_le->text();
QString destinationPath = ui->path2_le->text();
bool AtLeast1PathWasNotSpecified = false;
if(sourcePath == "")
{
AtLeast1PathWasNotSpecified = true;
printToConsole("Error: No source NSBMD file was specified!");
}
if(destinationPath == "")
{
AtLeast1PathWasNotSpecified = true;
printToConsole("Error: No destination NSBTX path was specified!");
}
if(AtLeast1PathWasNotSpecified)
return;
QFile sourceFile(sourcePath);
if (!sourceFile.open(QIODevice::ReadOnly))
{
printToConsole("Error: Could not access file for reading, check if it's being used by another program or if the program has permission to do so on that directory.");
return;
}
QByteArray nsbmdData = sourceFile.readAll();
sourceFile.close();
printToConsole("Checking for NSBMD file magic (BMD0) match...");
if(!(nsbmdData[0] == 'B' &&
nsbmdData[1] == 'M' &&
nsbmdData[2] == 'D' &&
nsbmdData[3] == '0'))
{
printToConsole("Error: Invalid NSBMD file specified.\nError details: File magic didn't match.");
return;
}
printToConsole("Match found, proceeding with conversion!");
printToConsole("Searching for model (MDL0) block offset...");
//Find block offsets
int MLD0_blockOffset = 0;
for (int i = 0; i < nsbmdData.size(); i++)
{
if(nsbmdData[i+0] == 'M' &&
nsbmdData[i+1] == 'D' &&
nsbmdData[i+2] == 'L' &&
nsbmdData[i+3] == '0')
{
MLD0_blockOffset = i;
break;
}
}
if(MLD0_blockOffset == 0)
printToConsole("Error: No model data to remove could be found in the NSBMD.\nError details: MDL0 block doesn't exist!");
else
printToConsole("Found model (MDL0) block!");
printToConsole("Searching for texture (TEX0) block offset...");
int TEX0_blockOffset = 0;
for (int i = 0; i < nsbmdData.size(); i++)
{
if(nsbmdData[i+0] == 'T' &&
nsbmdData[i+1] == 'E' &&
nsbmdData[i+2] == 'X' &&
nsbmdData[i+3] == '0')
{
TEX0_blockOffset = i;
break;
}
}
if(TEX0_blockOffset == 0)
printToConsole("Error: No texture data to keep could be found in the NSBMD.\nError details: TEX0 block doesn't exist!");
else
printToConsole("Found texture (TEX0) block!");
//Remove MDL0 block
printToConsole("Removing model block (MDL0)...");
int MLD0_blockLen = TEX0_blockOffset - MLD0_blockOffset;
nsbmdData.remove(MLD0_blockOffset, MLD0_blockLen);
//Update name in header
printToConsole("Updating header file magic...");
nsbmdData.replace(0, 4, "BTX0");
//Update file size in header (remove MDL0 block length from it)
printToConsole("Updating header file size...");
quint32* nsbmdDataAsUint = reinterpret_cast<quint32*>(nsbmdData.data());
nsbmdDataAsUint[2] -= static_cast<quint32>(MLD0_blockLen);
//Update section count
printToConsole("Updating header section count...");
quint16* nsbmdDataAsUshort = reinterpret_cast<quint16*>(nsbmdData.data());
nsbmdDataAsUshort[7] -= 1;
//Save NSBTX file
printToConsole("Saving...");
QFile destinationFile(destinationPath);
if (!destinationFile.open(QIODevice::WriteOnly))
{
printToConsole("Error: Could not access file for writing, check if it's being used by another program or if the program has permission to do so on that directory.");
return;
}
destinationFile.write(nsbmdData);
destinationFile.close();
if(QFile::exists(destinationPath))
printToConsole("Success: NSBTX converted and saved successfully!");
}