Skip to content

Commit 7ae9500

Browse files
vkconfig: Add environment variables export of loader config
1 parent 534177f commit 7ae9500

5 files changed

Lines changed: 197 additions & 0 deletions

File tree

vkconfig_core/configurator.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,141 @@ bool Configurator::WriteLoaderSettings(OverrideArea override_area, const Path& l
276276
}
277277
}
278278

279+
bool Configurator::Export(const Path& export_path) const {
280+
QFile file(export_path.AbsolutePath().c_str());
281+
282+
const bool result_layers_file = file.open(QIODevice::WriteOnly | QIODevice::Text);
283+
if (!result_layers_file) {
284+
return false;
285+
}
286+
287+
bool has_missing_layers = false;
288+
const Configuration* configuration = this->GetActiveConfiguration();
289+
290+
QTextStream stream(&file);
291+
292+
stream << "Layers Settings:\n";
293+
294+
// Loop through all the layers
295+
for (std::size_t j = 0, n = configuration->parameters.size(); j < n; ++j) {
296+
const Parameter& parameter = configuration->parameters[j];
297+
if (!parameter.override_settings) {
298+
continue;
299+
}
300+
301+
if (!(parameter.platform_flags & (1 << VKC_PLATFORM))) {
302+
continue;
303+
}
304+
305+
if (parameter.builtin == LAYER_BUILTIN_UNORDERED) {
306+
continue;
307+
}
308+
309+
if (parameter.control == LAYER_CONTROL_DISCARD || parameter.control == LAYER_CONTROL_OFF) {
310+
continue;
311+
}
312+
313+
const Layer* layer = this->layers.Find(parameter.key.c_str(), parameter.api_version);
314+
if (layer == nullptr) {
315+
if (parameter.control == LAYER_CONTROL_ON) {
316+
has_missing_layers = true;
317+
fprintf(stderr,
318+
"vkconfig: [ERROR] `%s` layer is set to `%s` in `%s` loader configuration but missing and being "
319+
"ignored\n",
320+
parameter.key.c_str(), ::GetLabel(parameter.control), configuration->key.c_str());
321+
} else {
322+
fprintf(stderr,
323+
"vkconfig: [WARNING] `%s` layer is set to `%s` in `%s` loader configuration but missing and being "
324+
"ignored\n",
325+
parameter.key.c_str(), ::GetLabel(parameter.control), configuration->key.c_str());
326+
}
327+
continue;
328+
}
329+
330+
stream << "\n";
331+
stream << "#! " << layer->key.c_str() << " " << layer->api_version.str().c_str() << "\n\n";
332+
333+
std::string lc_layer_name = GetLayerSettingPrefix(layer->key);
334+
335+
for (std::size_t i = 0, m = parameter.settings.size(); i < m; ++i) {
336+
const SettingData* setting_data = parameter.settings[i];
337+
338+
// Skip groups - they aren't settings, so not relevant in this output
339+
if (setting_data->type == SETTING_GROUP) {
340+
continue;
341+
}
342+
343+
// Skip missing settings
344+
const SettingMeta* meta = FindSetting(layer->settings, setting_data->key.c_str());
345+
if (meta == nullptr) {
346+
continue;
347+
}
348+
349+
// Skip overriden settings
350+
if (::CheckSettingOverridden(*meta)) {
351+
continue;
352+
}
353+
354+
stream << "#! ";
355+
stream << meta->label.c_str();
356+
stream << "\n#! =====================\n";
357+
stream << "#! " << meta->key.c_str();
358+
359+
if (meta->status != STATUS_STABLE) {
360+
stream << format(" (%s)", GetToken(meta->status)).c_str();
361+
}
362+
363+
stream << "\n";
364+
365+
// Break up description into smaller words
366+
std::string description = meta->description;
367+
std::vector<std::string> words;
368+
std::size_t pos;
369+
while ((pos = description.find(" ")) != std::string::npos) {
370+
words.push_back(description.substr(0, pos));
371+
description.erase(0, pos + 1);
372+
}
373+
if (description.size() > 0) words.push_back(description);
374+
if (words.size() > 0) {
375+
stream << "#";
376+
std::size_t nchars = 2;
377+
for (auto word : words) {
378+
if (word.size() + nchars > 80) {
379+
stream << "\n#!";
380+
nchars = 3;
381+
}
382+
stream << " " << word.c_str();
383+
nchars += (word.size() + 1);
384+
}
385+
}
386+
stream << "\n";
387+
388+
// If feature has unmet dependency, output it but comment it out
389+
if (::CheckDependence(*meta, parameter.settings) != SETTING_DEPENDENCE_ENABLE) {
390+
stream << "#!";
391+
}
392+
393+
if (meta->status == STATUS_DEPRECATED && !meta->deprecated_by_key.empty()) {
394+
const SettingMeta* replaced_setting = FindSetting(layer->settings, meta->deprecated_by_key.c_str());
395+
396+
stream << format("#! This setting was deprecated and replaced by '%s' (%s) setting.\n",
397+
replaced_setting->label.c_str(), replaced_setting->key.c_str())
398+
.c_str();
399+
}
400+
401+
std::vector<std::string> data = ::BuildEnvVariablesList(layer->key.c_str(), setting_data->key.c_str(), false);
402+
403+
stream << data[0].c_str() << "=";
404+
stream << setting_data->Export(EXPORT_MODE_OVERRIDE).c_str();
405+
stream << "\n\n";
406+
}
407+
}
408+
409+
file.close();
410+
411+
return true;
412+
}
413+
279414
// Create and write vk_layer_settings.txt file
280415
bool Configurator::WriteLayersSettings(OverrideArea override_area, const Path& layers_settings_path) {
281416
if (override_area & OVERRIDE_AREA_LAYERS_SETTINGS_BIT) {

vkconfig_core/configurator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class Configurator {
9999

100100
bool WriteLayersSettings(OverrideArea override_area, const Path& layers_settings_path);
101101
bool WriteLoaderSettings(OverrideArea override_area, const Path& loader_settings_path);
102+
bool Export(const Path& export_path) const;
102103

103104
void Set(HideMessageType type);
104105
bool Get(HideMessageType type) const;

vkconfig_gui/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
### Features:
55
- Add support for layer-defined messages
66
- Add support for external `vk_layer_settings.txt` files
7+
- Add environment variables export of loader configuration
78

89
### Improvements:
910
- Improved layer settings documentation

vkconfig_gui/tab_configurations.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,16 @@ bool TabConfigurations::EventFilter(QObject *target, QEvent *event) {
558558
action_export_config->setEnabled(item != nullptr);
559559
menu.addAction(action_export_config);
560560

561+
QAction *action_export_env_variables_bash_script =
562+
new QAction("Export the environment variables bash script...", nullptr);
563+
action_export_env_variables_bash_script->setEnabled(item != nullptr);
564+
menu.addAction(action_export_env_variables_bash_script);
565+
566+
QAction *action_export_env_variables_cmd_script =
567+
new QAction("Export the environment variables command prompt script...", nullptr);
568+
action_export_env_variables_cmd_script->setEnabled(item != nullptr);
569+
menu.addAction(action_export_env_variables_cmd_script);
570+
561571
QAction *action_export_settings = new QAction("Export the Layers Settings...", nullptr);
562572
action_export_settings->setEnabled(item != nullptr);
563573
menu.addAction(action_export_settings);
@@ -587,6 +597,10 @@ bool TabConfigurations::EventFilter(QObject *target, QEvent *event) {
587597
this->OnContextMenuResetAllClicked(item);
588598
} else if (action == action_export_config) {
589599
this->OnContextMenuExportConfigsClicked(item);
600+
} else if (action == action_export_env_variables_bash_script) {
601+
this->OnContextMenuExportEnvVariablesBashClicked(item);
602+
} else if (action == action_export_env_variables_cmd_script) {
603+
this->OnContextMenuExportEnvVariablesCMDClicked(item);
590604
} else if (action == action_export_settings) {
591605
this->OnContextMenuExportSettingsClicked(item);
592606
} else if (action == action_external_settings) {
@@ -1016,6 +1030,50 @@ void TabConfigurations::OnContextMenuExportConfigsClicked(ListItem *item) {
10161030
}
10171031
}
10181032

1033+
void TabConfigurations::OnContextMenuExportEnvVariablesBashClicked(ListItem *item) {
1034+
assert(item);
1035+
Configurator &configurator = Configurator::Get();
1036+
1037+
const Path path_export = configurator.configurations.last_path_export_config.RelativePath() + "/" + item->key + ".sh";
1038+
const QString &selected_path = QFileDialog::getSaveFileName(&this->window, "Export Environment Variables bash script",
1039+
path_export.AbsolutePath().c_str(), "Shell Script(*.sh)");
1040+
1041+
const bool result = configurator.Export(selected_path.toStdString());
1042+
1043+
if (!result) {
1044+
QMessageBox msg;
1045+
msg.setIcon(QMessageBox::Critical);
1046+
msg.setWindowTitle("Exporting of a file failed...");
1047+
msg.setText(format("Couldn't be create '%s' file.", selected_path.toStdString().c_str()).c_str());
1048+
msg.exec();
1049+
} else {
1050+
Path path(selected_path.toStdString());
1051+
QDesktopServices::openUrl(QUrl::fromLocalFile(path.AbsoluteDir().c_str()));
1052+
}
1053+
}
1054+
1055+
void TabConfigurations::OnContextMenuExportEnvVariablesCMDClicked(ListItem *item) {
1056+
assert(item);
1057+
Configurator &configurator = Configurator::Get();
1058+
1059+
const Path path_export = configurator.configurations.last_path_export_config.RelativePath() + "/" + item->key + ".bat";
1060+
const QString &selected_path = QFileDialog::getSaveFileName(&this->window, "Export Environment Variables command prompt script",
1061+
path_export.AbsolutePath().c_str(), "Shell Script(*.sh)");
1062+
1063+
const bool result = configurator.Export(selected_path.toStdString());
1064+
1065+
if (!result) {
1066+
QMessageBox msg;
1067+
msg.setIcon(QMessageBox::Critical);
1068+
msg.setWindowTitle("Exporting of a file failed...");
1069+
msg.setText(format("Couldn't be create '%s' file.", selected_path.toStdString().c_str()).c_str());
1070+
msg.exec();
1071+
} else {
1072+
Path path(selected_path.toStdString());
1073+
QDesktopServices::openUrl(QUrl::fromLocalFile(path.AbsoluteDir().c_str()));
1074+
}
1075+
}
1076+
10191077
void TabConfigurations::OnContextMenuExportSettingsClicked(ListItem *item) {
10201078
assert(item);
10211079

vkconfig_gui/tab_configurations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class TabConfigurations : public Tab {
9393
void OnContextMenuResetOneClicked(ListItem *item);
9494
void OnContextMenuResetAllClicked(ListItem *item);
9595
void OnContextMenuExportConfigsClicked(ListItem *item);
96+
void OnContextMenuExportEnvVariablesBashClicked(ListItem *item);
97+
void OnContextMenuExportEnvVariablesCMDClicked(ListItem *item);
9698
void OnContextMenuExportSettingsClicked(ListItem *item);
9799

98100
void UpdatePerExecutableConfigurations();

0 commit comments

Comments
 (0)