Fallback to model file scanning if models.yml cannot be read
This commit is contained in:
@@ -51,34 +51,27 @@ bool loadModelsListFromYaml(std::vector<CategoryData>& categories,
|
|||||||
if (data.size() == 0)
|
if (data.size() == 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
try {
|
YAML::Node node = loadYamlFromByteArray(data);
|
||||||
YAML::Node node = loadYamlFromByteArray(data);
|
if (!node.IsSequence()) return false;
|
||||||
if (!node.IsSequence()) return false;
|
|
||||||
|
|
||||||
int modelIdx = 0;
|
int modelIdx = 0;
|
||||||
for (const auto& cat : node) {
|
for (const auto& cat : node) {
|
||||||
|
if (!cat.IsMap()) continue;
|
||||||
|
|
||||||
if (!cat.IsMap()) continue;
|
for (const auto& cat_map : cat) {
|
||||||
|
categories.push_back(cat_map.first.Scalar().c_str());
|
||||||
|
|
||||||
for (const auto& cat_map : cat) {
|
const auto& models = cat_map.second;
|
||||||
categories.push_back(cat_map.first.Scalar().c_str());
|
if (!models.IsSequence()) continue;
|
||||||
|
|
||||||
const auto& models = cat_map.second;
|
for (const auto& model : models) {
|
||||||
if (!models.IsSequence()) continue;
|
std::string filename, name;
|
||||||
|
model["filename"] >> filename;
|
||||||
for (const auto& model : models) {
|
model["name"] >> name;
|
||||||
std::string filename, name;
|
modelFiles.push_back(
|
||||||
model["filename"] >> filename;
|
{filename, name, (int)categories.size() - 1, modelIdx++});
|
||||||
model["name"] >> name;
|
|
||||||
modelFiles.push_back(
|
|
||||||
{filename, name, (int)categories.size() - 1, modelIdx++});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (const std::runtime_error& e) {
|
|
||||||
qDebug() << "YAML::ParserException: " << e.what();
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -254,9 +254,34 @@ bool CategorizedStorageFormat::loadYaml(RadioData & radioData)
|
|||||||
|
|
||||||
QByteArray modelslistBuffer;
|
QByteArray modelslistBuffer;
|
||||||
if (loadFile(modelslistBuffer, "MODELS/models.yml")) {
|
if (loadFile(modelslistBuffer, "MODELS/models.yml")) {
|
||||||
if (!loadModelsListFromYaml(radioData.categories, modelFiles, modelslistBuffer)) {
|
try {
|
||||||
setError(tr("Can't load MODELS/models.yml"));
|
if (!loadModelsListFromYaml(radioData.categories, modelFiles, modelslistBuffer)) {
|
||||||
return false;
|
setError(tr("Can't load MODELS/models.yml"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch(const std::runtime_error& e) {
|
||||||
|
setError(tr("Can't load MODELS/models.yml") + ":\n" + QString(e.what()));
|
||||||
|
//return false;
|
||||||
|
|
||||||
|
//TODO: fall back to directory scan
|
||||||
|
std::list<std::string> filelist;
|
||||||
|
if (!getFileList(filelist)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// push default category
|
||||||
|
radioData.categories.push_back("Models");
|
||||||
|
|
||||||
|
const std::regex yml_regex("MODELS/(model[0-9]+\\.yml)");
|
||||||
|
for(const auto& f : filelist) {
|
||||||
|
std::smatch match;
|
||||||
|
if (std::regex_match(f, match, yml_regex)) {
|
||||||
|
if (match.size() == 2) {
|
||||||
|
std::ssub_match modelFile = match[1];
|
||||||
|
modelFiles.push_back({ modelFile.str(), "", 0, 0 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// fetch "MODELS/modelXX.yml"
|
// fetch "MODELS/modelXX.yml"
|
||||||
|
|||||||
Reference in New Issue
Block a user