Skip to content

Commit 6044a84

Browse files
fix: crash if no config file exists
1 parent 2fa2131 commit 6044a84

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/inject/inject.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static std::wstring dllPath = GetModuleDirectory() + L"\\shell.dll";
157157
int NewExplorerProcessAndInject() {
158158
GetDebugPrivilege();
159159
std::vector<DWORD> initialPIDs = GetExplorerPIDs();
160-
system("explorer.exe C:/");
160+
ShellExecuteW(NULL, L"open", L"explorer.exe", L"C:/", NULL, SW_SHOW);
161161
std::this_thread::sleep_for(std::chrono::seconds(1));
162162
std::vector<DWORD> newPIDs = GetExplorerPIDs();
163163
DWORD targetPID = 0;

src/shell/config.cc

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,49 @@
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

1313
namespace mb_shell {
1414
std::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+
}
1625
void 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
<< "\nUsing 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

Comments
 (0)