1
1
#include " ../include/Header.h"
2
2
#include " ../include/Config.h"
3
+ #include " ../include/GUI.h"
3
4
4
5
bool button_cleared = false ;
5
6
bool ids_locked = false ;
6
7
7
- vector< id> ids = {};
8
+ unordered_map<string, id> ids = {};
8
9
9
10
void read_config (json& config, id& ID) {
11
+ if (config[config.begin ().key ()].contains (" nickname" )) ID.nickname = config[config.begin ().key ()][" nickname" ].get <string>();
10
12
if (config[config.begin ().key ()].contains (" profiles" )) {
11
13
ID.profiles .clear ();
12
14
int profile_count = 0 ;
@@ -70,7 +72,7 @@ void read_config(json& config, id& ID) {
70
72
}
71
73
}
72
74
73
- void configure_id (id& ID) {
75
+ int configure_id (id& ID) {
74
76
ID.config_file = getenv (" USERPROFILE" );
75
77
ID.config_file += " \\ AppData\\ Roaming\\ NexusShell\\ " ;
76
78
if (!exists (ID.config_file )) create_directory (ID.config_file );
@@ -82,20 +84,16 @@ void configure_id(id& ID) {
82
84
if (reader.peek () != ifstream::traits_type::eof ()) {
83
85
try {
84
86
json config = json::parse (reader);
85
- cerr << " here\n " ;
86
87
reader.close ();
87
88
read_config (config, ID);
88
89
reconfigure (ID);
89
- cerr << " configured \n " ;
90
- return ;
90
+ cerr << " Configured " << ID. nickname << " \n " ;
91
+ return 0 ;
91
92
} catch (...) {
92
- cerr << " invalid config!\n clearing...\n " ; // TODO: Don't clear invalid configs
93
- // string to_write = "{\"" + ID.ID + "\": {}}";
94
- // json empty_config = json::parse(to_write);
95
- // ofstream writer(ID.config_file);
96
- // writer << empty_config.dump(4);
97
- // writer.close();
98
- // reconfigure(ID);
93
+ cerr << " invalid config!\n " ;
94
+ send (ID.sock .ClientSocket , " killreason: invalid config" , strlen (" killreason: invalid config" ) + 1 , 0 );
95
+ closesocket (ID.sock .ClientSocket );
96
+ return 1 ;
99
97
}
100
98
}
101
99
reader.close ();
@@ -112,19 +110,22 @@ void configure_id(id& ID) {
112
110
reconfigure (ID);
113
111
}
114
112
115
- void reconfigure (id& ID) {
113
+ int reconfigure (id& ID) {
116
114
if (exists (ID.config_file )) {
117
115
ifstream reader (ID.config_file );
118
116
json config;
119
117
try {
120
118
config = json::parse (reader);
121
119
} catch (...) {
122
120
cerr << " Invalid Config\n " ;
121
+ send (ID.sock .ClientSocket , " killreason: invalid config" , strlen (" killreason: invalid config" ) + 1 , 0 );
122
+ closesocket (ID.sock .ClientSocket );
123
123
reader.close ();
124
- return ; // TODO: Change all functions that oculd fail to int and return 1
124
+ return 1 ; // TODO: Change all functions that oculd fail to int and return 1
125
125
}
126
126
reader.close ();
127
- ID.config = config;
127
+ ID.config = config; // TODO: use instead;
128
+ if (config[config.begin ().key ()].contains (" nickname" )) config[config.begin ().key ()].erase (" nickname" );
128
129
for (int i = 0 ; i < ID.profiles .size (); ++i) {
129
130
json& profile_store = config[config.begin ().key ()][" profiles" ][to_string (i)];
130
131
if (profile_store.contains (" pages" )) {
@@ -149,8 +150,7 @@ void reconfigure(id& ID) {
149
150
}
150
151
}
151
152
string configuration = " cfg" + config.dump ();
152
- sock_map[ID.ID ].iSendResult = send (sock_map[ID.ID ].ClientSocket , configuration.c_str (), configuration.length (), 0 );
153
- cerr << " here: " << sock_map[ID.ID ].iSendResult << " \n " ;
153
+ ID.sock .iSendResult = send (ID.sock .ClientSocket , configuration.c_str (), configuration.length (), 0 );
154
154
}
155
155
}
156
156
@@ -162,14 +162,14 @@ void clear_button(int profile, int page, int button) {
162
162
return ;
163
163
}
164
164
nxsh_config += " \\ " ;
165
- nxsh_config += CURRENT_ID. ID ;
165
+ nxsh_config += selected_id ;
166
166
nxsh_config += " .json" ;
167
167
json to_remove;
168
168
ifstream reader (nxsh_config);
169
169
if (reader && reader.peek () != ifstream::traits_type::eof ()) to_remove = json::parse (reader); // FIXME: no if (reader
170
170
reader.close ();
171
- if (to_remove[CURRENT_ID. ID ][" profiles" ][to_string (profile)][" pages" ][to_string (page)][" buttons" ].contains (to_string (button))) {
172
- to_remove[CURRENT_ID. ID ][" profiles" ][to_string (profile)][" pages" ][to_string (page)][" buttons" ].erase (to_string (button));
171
+ if (to_remove[selected_id ][" profiles" ][to_string (profile)][" pages" ][to_string (page)][" buttons" ].contains (to_string (button))) {
172
+ to_remove[selected_id ][" profiles" ][to_string (profile)][" pages" ][to_string (page)][" buttons" ].erase (to_string (button));
173
173
ofstream writer (nxsh_config);
174
174
writer << to_remove.dump (4 );
175
175
writer.close ();
0 commit comments