55#include < thread>
66
77#include " rfl.hpp"
8- #include " rfl/json.hpp"
98#include " rfl/DefaultIfMissing.hpp"
9+ #include " rfl/json.hpp"
1010
1111#include " windows.h"
1212
1313namespace mb_shell {
1414std::unique_ptr<config> config::current;
15- void config::write_config () {}
15+ void config::write_config () {
16+ auto config_file = data_directory () / " config.json" ;
17+ std::ofstream ofs (config_file);
18+ if (!ofs) {
19+ std::cerr << " Failed to write config file." << std::endl;
20+ return ;
21+ }
22+
23+ ofs << rfl::json::write (*config::current);
24+ }
1625void config::read_config () {
1726 auto config_file = data_directory () / " config.json" ;
1827
28+ std::ifstream ifs (config_file);
29+
1930 if (!std::filesystem::exists (config_file)) {
20- config::current = std::make_unique<config>();
21- } else {
22- std::ifstream ifs (config_file);
23- if (!ifs) {
24- config::current = std::make_unique<config>();
31+ auto config_file = data_directory () / " config.json" ;
32+ std::ofstream ofs (config_file);
33+ if (!ofs) {
34+ std::cerr << " Failed to write config file." << std::endl;
2535 return ;
2636 }
2737
28- if (auto json = rfl::json::read<config, rfl::NoExtraFields, rfl::DefaultIfMissing>(ifs)) {
29- config::current = std::make_unique<mb_shell::config>(json.value ());
38+ ofs << " {}" ;
39+ }
40+
41+ if (!ifs) {
42+ std::cerr
43+ << " Config file could not be opened. Using default config instead."
44+ << std::endl;
45+ config::current = std::make_unique<config>();
46+ } else {
47+ if (auto json =
48+ rfl::json::read<config, rfl::NoExtraFields, rfl::DefaultIfMissing>(
49+ ifs)) {
50+ config::current = std::make_unique<config>(json.value ());
3051 } else {
3152 std::cerr << " Failed to read config file: " << json.error ()->what ()
3253 << " \n Using default config instead." << std::endl;
@@ -35,9 +56,9 @@ void config::read_config() {
3556 }
3657
3758 if (config::current->context_menu .debug_console ) {
38- ShowWindow (GetConsoleWindow (), SW_SHOW);
59+ ShowWindow (GetConsoleWindow (), SW_SHOW);
3960 } else {
40- ShowWindow (GetConsoleWindow (), SW_HIDE);
61+ ShowWindow (GetConsoleWindow (), SW_HIDE);
4162 }
4263}
4364
0 commit comments