Skip to content

Commit 36d73fe

Browse files
committed
Able to view template settings. But changing values still doesn't effect.
1 parent 9943e73 commit 36d73fe

File tree

7 files changed

+488
-336
lines changed

7 files changed

+488
-336
lines changed

DSTDedicatedServerGUI.pro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ TEMPLATE = app
1313

1414

1515
SOURCES += main.cpp\
16-
mainwindow.cpp
16+
mainwindow.cpp \
17+
ioworldgenoverridelua.cpp
1718

18-
HEADERS += mainwindow.h
19+
HEADERS += mainwindow.h \
20+
ioworldgenoverridelua.h \
21+
dstdatastructure.h
1922

2023
FORMS += mainwindow.ui

dstdatastructure.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef DSTDATASTRUCTURE
2+
#define DSTDATASTRUCTURE
3+
4+
struct properties{
5+
properties(){}
6+
properties(QString g, QString n, QString s) : group(g), name(n), settings(s){}
7+
QString group;
8+
QString name;
9+
QString settings;
10+
};
11+
12+
struct DSTSettings{
13+
QString world_name;
14+
std::vector<properties> ini;
15+
std::vector<properties> pro;
16+
};
17+
18+
enum{
19+
DST_WORLD = 0,
20+
DST_CAVE = 1
21+
};
22+
23+
#endif // DSTDATASTRUCTURE
24+

ioworldgenoverridelua.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "ioworldgenoverridelua.h"
2+
3+
IOWorldGenOverrideLua::IOWorldGenOverrideLua()
4+
{
5+
6+
}
7+
8+
//Thanks @jefftsai8049
9+
bool IOWorldGenOverrideLua::readLuaFile(std::vector<properties> &PV, const QString &fileName)
10+
{
11+
PV.clear();
12+
13+
if(!fileName.isEmpty())
14+
{
15+
QFile file(fileName);
16+
if(file.exists())
17+
{
18+
file.open(QIODevice::ReadOnly);
19+
int lineCount = 0;
20+
while(!file.atEnd())
21+
{
22+
QString msg;
23+
msg = file.readLine();
24+
msg = msg.trimmed();
25+
if(msg.indexOf("override_enabled = true,") > -1)
26+
{
27+
while(!file.atEnd())
28+
{
29+
QString data = file.readLine().trimmed();
30+
QString segment = data.split("--")[0];
31+
if(segment.indexOf('{') > -1)
32+
{
33+
properties P;
34+
P.group = segment.split("--")[0].split('=')[0].replace(" ","");
35+
while(!file.atEnd())
36+
{
37+
38+
QString inGroup = file.readLine().trimmed();
39+
inGroup = inGroup.split("--")[0];
40+
if(inGroup.indexOf('}') > -1)
41+
{
42+
break;
43+
}
44+
else
45+
{
46+
QStringList inGroupData = inGroup.split('=');
47+
P.name = inGroupData[0].replace(" ","");
48+
P.settings = inGroupData[1].replace(" ","").replace('"',"").replace(',',"");
49+
PV.push_back(P);
50+
}
51+
}
52+
}
53+
}
54+
}
55+
lineCount++;
56+
}
57+
}
58+
else
59+
{
60+
return false;
61+
}
62+
}
63+
else
64+
{
65+
return false;
66+
}
67+
68+
return true;
69+
}
70+
71+
bool IOWorldGenOverrideLua::writeLuaFile(std::vector<properties> &PV, const QString &fileName)
72+
{
73+
QFile file(fileName);
74+
if(!file.open(QIODevice::ReadWrite))
75+
{
76+
return false;
77+
}
78+
QTextStream stream(&file);
79+
80+
stream << "return{\n\n";
81+
stream << " override_enabled = true,\n";
82+
83+
for(int i = 0; i < PV.size(); i++)
84+
{
85+
86+
}
87+
88+
file.close();
89+
}

ioworldgenoverridelua.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef IOWORLDGENOVERRIDELUA_H
2+
#define IOWORLDGENOVERRIDELUA_H
3+
4+
#include <QObject>
5+
#include <QFile>
6+
#include <QTextStream>
7+
8+
#include "dstdatastructure.h"
9+
10+
class IOWorldGenOverrideLua
11+
{
12+
public:
13+
IOWorldGenOverrideLua();
14+
bool readLuaFile(std::vector<properties> &PV, const QString &fileName);
15+
bool writeLuaFile(std::vector<properties> &PV, const QString &fileName);
16+
};
17+
18+
#endif // IOWORLDGENOVERRIDELUA_H

mainwindow.cpp

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -145,29 +145,104 @@ bool MainWindow::checkServerExists(QString dstds_path, bool reload_template)
145145
QDir checkFolder;
146146
if(checkFolder.exists(dstds_path+ QString("/") + QString(folder_world)) && checkFolder.exists(dstds_path+ QString("/") + QString(folder_cave)))
147147
{
148+
if(!readINI(DST_WORLD, dstds_path + QString("/") + QString(folder_cave) + QString("/") + QString(settings_ini)))
149+
{
150+
QMessageBox::critical(this, "Error", "Cannot found ini files in world folder!");
151+
writeINIToGUI(DST_WORLD);
152+
return false;
153+
}
148154
if(!readINI(DST_CAVE, dstds_path + QString("/") + QString(folder_world) + QString("/") + QString(settings_ini)))
149155
{
150156
QMessageBox::critical(this, "Error", "Cannot found ini files in cave folder!");
151157
return false;
152158
}
153-
if(!readINI(DST_WORLD, dstds_path + QString("/") + QString(folder_cave) + QString("/") + QString(settings_ini), true))
159+
160+
if(!readLua(DST_WORLD, dstds_path + QString("/") + QString(folder_cave) + QString("/") + QString(override_lua)))
154161
{
155162
QMessageBox::critical(this, "Error", "Cannot found ini files in world folder!");
163+
writeLuaToGUI(DST_WORLD);
156164
return false;
157165
}
158166

167+
159168
disableSetttingsWhenServerExists(true);
160169
return true;
161170
}
162171
else if(reload_template)
163172
{
164173
readINI(DST_CAVE, QString("template/") + QString(ini_cave));
165174
readINI(DST_WORLD, QString("template/") + QString(ini_world));
175+
readLua(DST_WORLD, QString("template/") + QString(override_world_lua_template));
176+
writeINIToGUI(DST_WORLD);
177+
writeLuaToGUI(DST_WORLD);
166178
disableSetttingsWhenServerExists(false);
167179
}
168180
return false;
169181
}
170182

183+
void MainWindow::writeINIToGUI(int world_num)
184+
{
185+
for(int i = 0; i < world[world_num].ini.size(); i++)
186+
{
187+
properties &p = world[world_num].ini[i];
188+
QString name = p.name;
189+
QString value = p.settings;
190+
191+
if(!name.compare("default_server_name")){ui->lineEdit_servername->setText(value);}
192+
else if(!name.compare("default_server_description")){ui->lineEdit_serverDescription->setText(value);}
193+
else if(!name.compare("max_players")){ui->spinBox_serverMaxPlayers->setValue(value.toInt());}
194+
else if(!name.compare("server_password")){ui->lineEdit_serverPassword->setText(value);}
195+
else if(!name.compare("server_save_slot")){ui->comboBox_serverSaveSlot->setCurrentIndex(value.toInt() - 1);}
196+
else if(!name.compare("server_intention"))
197+
{
198+
if(!name.compare("cooperative"))
199+
ui->comboBox_serverIntention->setCurrentIndex(0);
200+
}
201+
else if(!name.compare("game_mode"))
202+
{
203+
if(!name.compare("Endless"))
204+
ui->comboBox_gamemode->setCurrentIndex(0);
205+
else if(!name.compare("Survival"))
206+
ui->comboBox_gamemode->setCurrentIndex(1);
207+
else if(!name.compare("Wilderness"))
208+
ui->comboBox_gamemode->setCurrentIndex(2);
209+
}
210+
else if(!name.compare("pvp"))
211+
{
212+
(!value.compare("true")) ? ui->radioButton_pvpYes->setChecked(true) : ui->radioButton_pvpNo->setChecked(true);
213+
}
214+
}
215+
}
216+
217+
void MainWindow::writeLuaToGUI(int world_num)
218+
{
219+
if(world_num == DST_WORLD)
220+
{
221+
ui->tableWidget_worldGen->clear();
222+
ui->tableWidget_worldGen->setColumnCount(3);
223+
ui->tableWidget_worldGen->setRowCount(world[world_num].pro.size());
224+
ui->tableWidget_worldGen->setHorizontalHeaderItem(0, new QTableWidgetItem("Group"));
225+
ui->tableWidget_worldGen->setHorizontalHeaderItem(1, new QTableWidgetItem("Name"));
226+
QTableWidgetItem* t3 = new QTableWidgetItem("Value");
227+
t3->setBackground(Qt::lightGray);
228+
ui->tableWidget_worldGen->setHorizontalHeaderItem(2, t3);
229+
230+
for(int i = 0; i < world[world_num].pro.size(); i++)
231+
{
232+
properties &p = world[world_num].pro[i];
233+
QTableWidgetItem* n1 = new QTableWidgetItem(p.group);
234+
n1->setFlags(n1->flags() & ~Qt::ItemIsEditable);
235+
ui->tableWidget_worldGen->setItem(i, 0, n1);
236+
QTableWidgetItem* n2 = new QTableWidgetItem(p.name);
237+
n2->setFlags(n2->flags() & ~Qt::ItemIsEditable);
238+
ui->tableWidget_worldGen->setItem(i, 1, n2);
239+
QTableWidgetItem* n3 = new QTableWidgetItem(p.settings);
240+
n3->setBackground(Qt::lightGray);
241+
ui->tableWidget_worldGen->setItem(i, 2, n3);
242+
}
243+
}
244+
}
245+
171246
bool MainWindow::firstServerSetup()
172247
{
173248
_dstds_folder = ui->lineEdit_serverDataLocation->text();
@@ -257,33 +332,6 @@ void MainWindow::changeSettings(int world_num, QString name, QString value)
257332
}
258333
}
259334

260-
void MainWindow::changeBasicSettingsUI(QString name, QString value)
261-
{
262-
if(!name.compare("default_server_name")){ui->lineEdit_servername->setText(value);}
263-
else if(!name.compare("default_server_description")){ui->lineEdit_serverDescription->setText(value);}
264-
else if(!name.compare("max_players")){ui->spinBox_serverMaxPlayers->setValue(value.toInt());}
265-
else if(!name.compare("server_password")){ui->lineEdit_serverPassword->setText(value);}
266-
else if(!name.compare("server_save_slot")){ui->comboBox_serverSaveSlot->setCurrentIndex(value.toInt() - 1);}
267-
else if(!name.compare("server_intention"))
268-
{
269-
if(!name.compare("cooperative"))
270-
ui->comboBox_serverIntention->setCurrentIndex(0);
271-
}
272-
else if(!name.compare("game_mode"))
273-
{
274-
if(!name.compare("Endless"))
275-
ui->comboBox_gamemode->setCurrentIndex(0);
276-
else if(!name.compare("Survival"))
277-
ui->comboBox_gamemode->setCurrentIndex(1);
278-
else if(!name.compare("Wilderness"))
279-
ui->comboBox_gamemode->setCurrentIndex(2);
280-
}
281-
else if(!name.compare("pvp"))
282-
{
283-
(!value.compare("true")) ? ui->radioButton_pvpYes->setChecked(true) : ui->radioButton_pvpNo->setChecked(true);
284-
}
285-
}
286-
287335
bool MainWindow::readINI(int world_num, QString file_path, bool ui_c)
288336
{
289337
QSettings settings(file_path, QSettings::IniFormat);
@@ -300,7 +348,6 @@ bool MainWindow::readINI(int world_num, QString file_path, bool ui_c)
300348
ini.settings = settings.value(keys[j]).toString();
301349

302350
world[world_num].ini.push_back(ini);
303-
changeBasicSettingsUI(ini.name, ini.settings);
304351
}
305352
settings.endGroup();
306353
}
@@ -321,24 +368,8 @@ bool MainWindow::writeINI(int world_num, QString file_path)
321368
// Custom lua content in app currently not supported. Please modify the template file directly to work.
322369
bool MainWindow::readLua(int world_num, QString file_path)
323370
{
324-
if(world_num == DST_WORLD)
325-
{
326-
// QFile file_r("template/worldgenoverride_cave.lua");
327-
// if(!file_r.open(QIODevice::ReadOnly))
328-
// {
329-
// return false;
330-
// }
331-
332-
// if(file.open(QIODevice::ReadWrite))
333-
// {
334-
// QTextStream stream(&file);
335-
336-
// }
337-
}
338-
else if(world_num == DST_CAVE)
339-
{
340-
341-
}
371+
IOWorldGenOverrideLua genlua;
372+
genlua.readLuaFile(world[world_num].pro, file_path);
342373
return true;
343374
}
344375

@@ -348,14 +379,7 @@ bool MainWindow::writeLua(int world_num, QString file_path)
348379
//QFile file_w(file_path);
349380
if(world_num == DST_WORLD)
350381
{
351-
if(world[0].pro.empty())
352-
{
353-
return QFile::copy("template/worldgenoverride_world.lua", file_path);
354-
}
355-
else
356-
{
357-
358-
}
382+
return QFile::copy("template/worldgenoverride_world.lua", file_path);
359383
}
360384
else if(world_num == DST_CAVE)
361385
{
@@ -416,12 +440,6 @@ void MainWindow::on_pushButton_startServer_clicked()
416440
return;
417441
}
418442

419-
420-
421-
//Check again if the folder changed.
422-
ui->statusBar->showMessage("Check if there is an existing server.");
423-
_server_found = checkServerExists(_dstds_folder, false);
424-
425443
if(!_server_found)
426444
{
427445
ui->statusBar->showMessage("Creating new server.");

mainwindow.h

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,11 @@
2424
#include <QSettings>
2525
#include <QStandardPaths>
2626

27+
#include "ioworldgenoverridelua.h"
28+
#include "dstdatastructure.h"
2729

28-
struct properties{
29-
properties(){}
30-
properties(QString g, QString n, QString s) : group(g), name(n), settings(s){}
31-
QString group;
32-
QString name;
33-
QString settings;
34-
};
3530

36-
struct DSTSettings{
37-
QString world_name;
38-
std::vector<properties> ini;
39-
std::vector<properties> pro;
40-
};
4131

42-
enum{
43-
DST_WORLD = 0,
44-
DST_CAVE = 1
45-
};
4632

4733
namespace Ui {
4834
class MainWindow;
@@ -84,11 +70,12 @@ private slots:
8470
bool firstServerSetup();
8571

8672
void changeSettings(int world_num, QString name, QString value);
87-
void changeBasicSettingsUI(QString name, QString value);
8873
bool readINI(int world_num, QString file_path, bool ui_c = false);
8974
bool writeINI(int world_num, QString file_path);
75+
void writeINIToGUI(int world_num);
9076
bool readLua(int world_num, QString file_path);
9177
bool writeLua(int world_num, QString file_path);
78+
void writeLuaToGUI(int world_num);
9279

9380
QProcess _update_mod;
9481
QProcess _dst_server;

0 commit comments

Comments
 (0)