GH-977 Improve FTB loading and instance creation

This commit is contained in:
Petr Mrázek 2015-05-22 23:06:51 +02:00
parent 12b9a90c4c
commit 0e0ddf5494
2 changed files with 76 additions and 63 deletions

View File

@ -301,8 +301,8 @@ void MultiMCPage::applySettings()
// FTB // FTB
s->set("TrackFTBInstances", ui->trackFtbBox->isChecked()); s->set("TrackFTBInstances", ui->trackFtbBox->isChecked());
s->set("FTBLauncherLocal", ui->ftbLauncherBox->text()); s->set("FTBLauncherLocal", NormalizePath(ui->ftbLauncherBox->text()));
s->set("FTBRoot", ui->ftbBox->text()); s->set("FTBRoot", NormalizePath(ui->ftbBox->text()));
// Folders // Folders
// TODO: Offer to move instances to new instance folder. // TODO: Offer to move instances to new instance folder.

View File

@ -17,6 +17,7 @@ struct FTBRecord
QString dirName; QString dirName;
QString name; QString name;
QString logo; QString logo;
QString iconKey;
QString mcVersion; QString mcVersion;
QString description; QString description;
QString instanceDir; QString instanceDir;
@ -81,6 +82,8 @@ QSet<FTBRecord> discoverFTBInstances(SettingsObjectPtr globalSettings)
continue; continue;
record.name = attrs.value("name").toString(); record.name = attrs.value("name").toString();
record.logo = attrs.value("logo").toString(); record.logo = attrs.value("logo").toString();
QString logo = record.logo;
record.iconKey = logo.remove(QRegularExpression("\\..*"));
auto customVersions = attrs.value("customMCVersions"); auto customVersions = attrs.value("customMCVersions");
if (!customVersions.isNull()) if (!customVersions.isNull())
{ {
@ -130,63 +133,95 @@ QSet<FTBRecord> discoverFTBInstances(SettingsObjectPtr globalSettings)
return records; return records;
} }
InstancePtr loadInstance(SettingsObjectPtr globalSettings, const QString &instDir) InstancePtr loadInstance(SettingsObjectPtr globalSettings, QMap<QString, QString> &groupMap, const FTBRecord & record)
{ {
auto m_settings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg"));
InstancePtr inst; InstancePtr inst;
auto m_settings = std::make_shared<INISettingsObject>(PathCombine(record.instanceDir, "instance.cfg"));
m_settings->registerSetting("InstanceType", "Legacy"); m_settings->registerSetting("InstanceType", "Legacy");
QString inst_type = m_settings->get("InstanceType").toString(); qDebug() << "Loading existing " << record.name;
QString inst_type = m_settings->get("InstanceType").toString();
if (inst_type == "LegacyFTB") if (inst_type == "LegacyFTB")
{ {
inst.reset(new LegacyFTBInstance(globalSettings, m_settings, instDir)); inst.reset(new LegacyFTBInstance(globalSettings, m_settings, record.instanceDir));
} }
else if (inst_type == "OneSixFTB") else if (inst_type == "OneSixFTB")
{ {
inst.reset(new OneSixFTBInstance(globalSettings, m_settings, instDir)); inst.reset(new OneSixFTBInstance(globalSettings, m_settings, record.instanceDir));
} }
else
{
return nullptr;
}
inst->init(); inst->init();
inst->setGroupInitial("FTB");
inst->setName(record.name);
inst->setIconKey(record.iconKey);
if (inst->intendedVersionId() != record.mcVersion)
{
inst->setIntendedVersionId(record.mcVersion);
}
inst->setNotes(record.description);
if (!InstanceList::continueProcessInstance(inst, InstanceList::NoCreateError, record.instanceDir, groupMap))
{
return nullptr;
}
return inst; return inst;
} }
InstancePtr createInstance(SettingsObjectPtr globalSettings, MinecraftVersionPtr version, const QString &instDir) InstancePtr createInstance(SettingsObjectPtr globalSettings, QMap<QString, QString> &groupMap, const FTBRecord & record)
{ {
QDir rootDir(instDir); QDir rootDir(record.instanceDir);
InstancePtr inst; InstancePtr inst;
if (!version) qDebug() << "Converting " << record.name << " as new.";
auto mcVersion = std::dynamic_pointer_cast<MinecraftVersion>(ENV.getVersion("net.minecraft", record.mcVersion));
if (!mcVersion)
{ {
qCritical() << "Can't create instance for non-existing MC version"; qCritical() << "Can't load instance " << record.instanceDir
<< " because minecraft version " << record.mcVersion
<< " can't be resolved.";
return nullptr; return nullptr;
} }
qDebug() << instDir.toUtf8();
if (!rootDir.exists() && !rootDir.mkpath(".")) if (!rootDir.exists() && !rootDir.mkpath("."))
{ {
qCritical() << "Can't create instance folder" << instDir; qCritical() << "Can't create instance folder" << record.instanceDir;
return nullptr; return nullptr;
} }
auto m_settings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg")); auto m_settings = std::make_shared<INISettingsObject>(PathCombine(record.instanceDir, "instance.cfg"));
m_settings->registerSetting("InstanceType", "Legacy"); m_settings->registerSetting("InstanceType", "Legacy");
if (version->usesLegacyLauncher()) if (mcVersion->usesLegacyLauncher())
{ {
m_settings->set("InstanceType", "LegacyFTB"); m_settings->set("InstanceType", "LegacyFTB");
inst.reset(new LegacyFTBInstance(globalSettings, m_settings, instDir)); inst.reset(new LegacyFTBInstance(globalSettings, m_settings, record.instanceDir));
inst->setIntendedVersionId(version->descriptor()); inst->setIntendedVersionId(mcVersion->descriptor());
} }
else else
{ {
m_settings->set("InstanceType", "OneSixFTB"); m_settings->set("InstanceType", "OneSixFTB");
inst.reset(new OneSixFTBInstance(globalSettings, m_settings, instDir)); inst.reset(new OneSixFTBInstance(globalSettings, m_settings, record.instanceDir));
inst->setIntendedVersionId(version->descriptor()); inst->setIntendedVersionId(mcVersion->descriptor());
inst->init(); inst->init();
} }
inst->setGroupInitial("FTB");
inst->setName(record.name);
inst->setIconKey(record.logo);
inst->setIntendedVersionId(record.mcVersion);
inst->setNotes(record.description);
if (!InstanceList::continueProcessInstance(inst, InstanceList::NoCreateError, record.instanceDir, groupMap))
{
return nullptr;
}
return inst; return inst;
} }
@ -209,58 +244,36 @@ void FTBPlugin::loadInstances(SettingsObjectPtr globalSettings, QMap<QString, QS
for (auto record : records) for (auto record : records)
{ {
qDebug() << "Loading FTB instance from " << record.instanceDir; qDebug() << "Loading FTB instance from " << record.instanceDir;
QString iconKey = record.logo; QString iconKey = record.iconKey;
iconKey.remove(QRegularExpression("\\..*")); ENV.icons()->addIcon(iconKey, iconKey, PathCombine(record.templateDir, record.logo), MMCIcon::Transient);
ENV.icons()->addIcon(iconKey, iconKey, PathCombine(record.templateDir, record.logo), auto settingsFilePath = PathCombine(record.instanceDir, "instance.cfg");
MMCIcon::Transient);
if (!QFileInfo(PathCombine(record.instanceDir, "instance.cfg")).exists()) if (QFileInfo(settingsFilePath).exists())
{ {
qDebug() << "Converting " << record.name << " as new."; auto instPtr = loadInstance(globalSettings, groupMap, record);
auto mcVersion = std::dynamic_pointer_cast<MinecraftVersion>(ENV.getVersion("net.minecraft", record.mcVersion));
if (!mcVersion)
{
qCritical() << "Can't load instance " << record.instanceDir
<< " because minecraft version " << record.mcVersion
<< " can't be resolved.";
continue;
}
auto instPtr = createInstance(globalSettings, mcVersion, record.instanceDir);
if (!instPtr) if (!instPtr)
{ {
qWarning() << "Couldn't load instance config:" << settingsFilePath;
if(!QFile::remove(settingsFilePath))
{
qWarning() << "Couldn't remove broken instance config!";
continue;
}
// failed to load, but removed the poisonous file
}
else
{
tempList.append(InstancePtr(instPtr));
continue; continue;
} }
instPtr->setGroupInitial("FTB");
instPtr->setName(record.name);
instPtr->setIconKey(iconKey);
instPtr->setIntendedVersionId(record.mcVersion);
instPtr->setNotes(record.description);
if (!InstanceList::continueProcessInstance(instPtr, InstanceList::NoCreateError, record.instanceDir, groupMap))
continue;
tempList.append(InstancePtr(instPtr));
} }
else auto instPtr = createInstance(globalSettings, groupMap, record);
if (!instPtr)
{ {
qDebug() << "Loading existing " << record.name; qWarning() << "Couldn't create FTB instance!";
auto instPtr = loadInstance(globalSettings, record.instanceDir); continue;
if (!instPtr)
{
continue;
}
instPtr->setGroupInitial("FTB");
instPtr->setName(record.name);
instPtr->setIconKey(iconKey);
if (instPtr->intendedVersionId() != record.mcVersion)
{
instPtr->setIntendedVersionId(record.mcVersion);
}
instPtr->setNotes(record.description);
if (!InstanceList::continueProcessInstance(instPtr, InstanceList::NoCreateError, record.instanceDir, groupMap))
continue;
tempList.append(InstancePtr(instPtr));
} }
tempList.append(InstancePtr(instPtr));
} }
} }