summaryrefslogtreecommitdiffstats
path: root/Tools/QtBiomeVisualiser/MainWindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Tools/QtBiomeVisualiser/MainWindow.cpp')
-rw-r--r--Tools/QtBiomeVisualiser/MainWindow.cpp217
1 files changed, 198 insertions, 19 deletions
diff --git a/Tools/QtBiomeVisualiser/MainWindow.cpp b/Tools/QtBiomeVisualiser/MainWindow.cpp
index 65d0ccf5e..d2c1ae2c1 100644
--- a/Tools/QtBiomeVisualiser/MainWindow.cpp
+++ b/Tools/QtBiomeVisualiser/MainWindow.cpp
@@ -5,9 +5,14 @@
#include <QMenuBar>
#include <QApplication>
#include <QFileDialog>
+#include <QSettings>
+#include <QDirIterator>
#include "inifile/iniFile.h"
#include "ChunkSource.h"
#include "Generating/BioGen.h"
+#include "StringCompression.h"
+#include "WorldStorage/FastNBT.h"
+#include "GeneratorSetupDlg.h"
@@ -16,6 +21,8 @@
MainWindow::MainWindow(QWidget * parent) :
QMainWindow(parent)
{
+ initMinecraftPath();
+
m_BiomeView = new BiomeView(this);
setCentralWidget(m_BiomeView);
@@ -36,9 +43,37 @@ MainWindow::~MainWindow()
-void MainWindow::generate()
+void MainWindow::newGenerator()
+{
+ // TODO
+
+ // (Re-)open the generator setup dialog:
+ m_GeneratorSetupDlg.reset(new GeneratorSetupDlg(""));
+ m_GeneratorSetupDlg->show();
+ m_GeneratorSetupDlg->raise();
+
+ // TODO
+}
+
+
+
+
+
+void MainWindow::openGenerator()
{
+ // Let the user specify the world.ini file:
QString worldIni = QFileDialog::getOpenFileName(this, tr("Open world.ini"), QString(), tr("world.ini (world.ini)"));
+ if (worldIni.isEmpty())
+ {
+ return;
+ }
+
+ // (Re-)open the generator setup dialog:
+ m_GeneratorSetupDlg.reset(new GeneratorSetupDlg(worldIni.toStdString()));
+ m_GeneratorSetupDlg->show();
+ m_GeneratorSetupDlg->raise();
+
+ // Set the chunk source:
m_BiomeView->setChunkSource(std::shared_ptr<BioGenSource>(new BioGenSource(worldIni)));
m_BiomeView->redraw();
}
@@ -47,9 +82,66 @@ void MainWindow::generate()
-void MainWindow::open()
+void MainWindow::openWorld()
{
- // TODO
+ // Let the user specify the world:
+ QString regionFolder = QFileDialog::getExistingDirectory(this, tr("Select the region folder"), QString());
+ if (regionFolder.isEmpty())
+ {
+ return;
+ }
+
+ // Remove the generator setup dialog, if open:
+ if (m_GeneratorSetupDlg.get() != nullptr)
+ {
+ m_GeneratorSetupDlg->hide();
+ m_GeneratorSetupDlg.reset(nullptr);
+ }
+
+ // Set the chunk source:
+ m_BiomeView->setChunkSource(std::shared_ptr<AnvilSource>(new AnvilSource(regionFolder)));
+ m_BiomeView->redraw();
+}
+
+
+
+
+
+void MainWindow::openVanillaWorld()
+{
+ // The world is stored in the sender action's data, retrieve it:
+ QAction * action = qobject_cast<QAction *>(sender());
+ if (action == nullptr)
+ {
+ return;
+ }
+
+ // Remove the generator setup dialog, if open:
+ if (m_GeneratorSetupDlg.get() != nullptr)
+ {
+ m_GeneratorSetupDlg->hide();
+ m_GeneratorSetupDlg.reset(nullptr);
+ }
+
+ // Set the chunk source:
+ m_BiomeView->setChunkSource(std::shared_ptr<AnvilSource>(new AnvilSource(action->data().toString())));
+ m_BiomeView->redraw();
+}
+
+
+
+
+
+void MainWindow::initMinecraftPath()
+{
+ #ifdef Q_OS_MAC
+ m_MinecraftPath = QDir::homePath() + QDir::toNativeSeparators("/Library/Application Support/minecraft");
+ #elif defined Q_OS_WIN32
+ QSettings ini(QSettings::IniFormat, QSettings::UserScope, ".minecraft", "minecraft1");
+ m_MinecraftPath = QFileInfo(ini.fileName()).absolutePath();
+ #else
+ m_MinecraftPath = QDir::homePath() + QDir::toNativeSeparators("/.minecraft");
+ #endif
}
@@ -58,19 +150,26 @@ void MainWindow::open()
void MainWindow::createActions()
{
- m_actGen = new QAction(tr("&Generate..."), this);
- m_actGen->setShortcut(tr("Ctrl+N"));
- m_actGen->setStatusTip(tr("Open a generator INI file and display the generated biomes"));
- connect(m_actGen, SIGNAL(triggered()), this, SLOT(generate()));
+ createWorldActions();
+
+ m_actNewGen = new QAction(tr("&New generator"), this);
+ m_actNewGen->setShortcut(tr("Ctrl+N"));
+ m_actNewGen->setStatusTip(tr("Open a generator INI file and display the generated biomes"));
+ connect(m_actNewGen, SIGNAL(triggered()), this, SLOT(newGenerator()));
+
+ m_actOpenGen = new QAction(tr("&Open generator..."), this);
+ m_actOpenGen->setShortcut(tr("Ctrl+G"));
+ m_actOpenGen->setStatusTip(tr("Open a generator INI file and display the generated biomes"));
+ connect(m_actOpenGen, SIGNAL(triggered()), this, SLOT(openGenerator()));
- m_actOpen = new QAction(tr("&Open world..."), this);
- m_actOpen->setShortcut(tr("Ctrl+O"));
- m_actOpen->setStatusTip(tr("Open an existing world and display its biomes"));
- connect(m_actOpen, SIGNAL(triggered()), this, SLOT(open()));
+ m_actOpenWorld = new QAction(tr("&Open world..."), this);
+ m_actOpenWorld->setShortcut(tr("Ctrl+O"));
+ m_actOpenWorld->setStatusTip(tr("Open an existing world and display its biomes"));
+ connect(m_actOpenWorld, SIGNAL(triggered()), this, SLOT(openWorld()));
m_actReload = new QAction(tr("&Reload"), this);
m_actReload->setShortcut(tr("F5"));
- m_actReload->setStatusTip(tr("Open an existing world and display its biomes"));
+ m_actReload->setStatusTip(tr("Clear the view cache and force a reload of all the data"));
connect(m_actReload, SIGNAL(triggered()), m_BiomeView, SLOT(reload()));
m_actExit = new QAction(tr("E&xit"), this);
@@ -83,15 +182,95 @@ void MainWindow::createActions()
+void MainWindow::createWorldActions()
+{
+ QDir mc(m_MinecraftPath);
+ if (!mc.cd("saves"))
+ {
+ return;
+ }
+
+ QDirIterator it(mc);
+ int key = 1;
+ while (it.hasNext())
+ {
+ it.next();
+ if (!it.fileInfo().isDir())
+ {
+ continue;
+ }
+ QString name = getWorldName(it.filePath().toStdString());
+ if (name.isEmpty())
+ {
+ continue;
+ }
+ QAction * w = new QAction(this);
+ w->setText(name);
+ w->setData(it.filePath() + "/region");
+ if (key < 10)
+ {
+ w->setShortcut("Ctrl+" + QString::number(key));
+ key++;
+ }
+ connect(w, SIGNAL(triggered()), this, SLOT(openVanillaWorld()));
+ m_WorldActions.append(w);
+ }
+}
+
+
+
+
+
void MainWindow::createMenus()
{
- QMenu * mFile = menuBar()->addMenu(tr("&World"));
- mFile->addAction(m_actGen);
- mFile->addAction(m_actOpen);
- mFile->addSeparator();
- mFile->addAction(m_actReload);
- mFile->addSeparator();
- mFile->addAction(m_actExit);
+ QMenu * file = menuBar()->addMenu(tr("&Map"));
+ file->addAction(m_actNewGen);
+ file->addAction(m_actOpenGen);
+ file->addSeparator();
+ QMenu * worlds = file->addMenu(tr("Open existing"));
+ worlds->addActions(m_WorldActions);
+ if (m_WorldActions.empty())
+ {
+ worlds->setEnabled(false);
+ }
+ file->addAction(m_actOpenWorld);
+ file->addSeparator();
+ file->addAction(m_actReload);
+ file->addSeparator();
+ file->addAction(m_actExit);
+}
+
+
+
+
+
+QString MainWindow::getWorldName(const AString & a_Path)
+{
+ AString levelData = cFile::ReadWholeFile(a_Path + "/level.dat");
+ if (levelData.empty())
+ {
+ // No such file / no data
+ return QString();
+ }
+
+ AString uncompressed;
+ if (UncompressStringGZIP(levelData.data(), levelData.size(), uncompressed) != Z_OK)
+ {
+ return QString();
+ }
+ cParsedNBT nbt(uncompressed.data(), uncompressed.size());
+ if (!nbt.IsValid())
+ {
+ return QString();
+ }
+ AString name = nbt.GetName(1);
+ OutputDebugStringA(name.c_str());
+ int levelNameTag = nbt.FindTagByPath(nbt.GetRoot(), "Data\\LevelName");
+ if ((levelNameTag <= 0) || (nbt.GetType(levelNameTag) != TAG_String))
+ {
+ return QString();
+ }
+ return QString::fromStdString(nbt.GetString(levelNameTag));
}