1 |
// Renegade Map Selector |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// RenegadeMapSelector.cpp |
6 |
|
7 |
#include "RenegadeMapSelector.h" |
8 |
#include "RenegadeConfig.h" |
9 |
|
10 |
#ifdef _WIN32 |
11 |
|
12 |
#include <io.h> |
13 |
#include <fcntl.h> |
14 |
#include "resource.h" |
15 |
|
16 |
#else |
17 |
|
18 |
#include "MapSelector.h" |
19 |
|
20 |
#endif |
21 |
|
22 |
bool debug = false; |
23 |
string program; |
24 |
RenegadeConfig* config; |
25 |
|
26 |
#ifndef _WIN32 |
27 |
|
28 |
inline string fix(const string& ansi) { return ansi; } |
29 |
inline void munge(void) { debug = true; } |
30 |
|
31 |
#else |
32 |
|
33 |
inline string fix(const wstring& wide) |
34 |
{ |
35 |
char* buffer = new char[wide.length() + 1]; |
36 |
|
37 |
WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, buffer, wide.length() + 1, |
38 |
NULL, NULL); |
39 |
|
40 |
string ansi = buffer; |
41 |
delete [] buffer; |
42 |
|
43 |
return ansi; |
44 |
} |
45 |
|
46 |
inline void munge(void) |
47 |
{ |
48 |
if (debug) return; |
49 |
|
50 |
AllocConsole(); |
51 |
SetConsoleTitle("Renegade Map Selector"); |
52 |
|
53 |
int hin = _open_osfhandle(long(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT); |
54 |
int hout = _open_osfhandle(long(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT); |
55 |
int herr = _open_osfhandle(long(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT); |
56 |
|
57 |
FILE* fin = _fdopen(hin, "r"); |
58 |
FILE* fout = _fdopen(hout, "w"); |
59 |
FILE* ferr = _fdopen(herr, "w"); |
60 |
|
61 |
*stdin = *fin; |
62 |
*stdout = *fout; |
63 |
*stderr = *ferr; |
64 |
|
65 |
setvbuf(stdin, NULL, _IONBF, 0); |
66 |
setvbuf(stdout, NULL, _IONBF, 0); |
67 |
setvbuf(stderr, NULL, _IONBF, 0); |
68 |
|
69 |
cin.sync_with_stdio(); |
70 |
cout.sync_with_stdio(); |
71 |
cerr.sync_with_stdio(); |
72 |
|
73 |
debug = true; |
74 |
} |
75 |
|
76 |
#endif |
77 |
|
78 |
#ifdef _WIN32 |
79 |
int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); |
80 |
|
81 |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR |
82 |
lpCmdLine, int nShowCmd) |
83 |
{ |
84 |
int argc; |
85 |
unsigned short** argv = CommandLineToArgvW(GetCommandLineW(), &argc); |
86 |
#else |
87 |
void selector(void); |
88 |
|
89 |
int main(int argc, char* argv[]) |
90 |
{ |
91 |
if (!gtk_init_check(&argc, &argv)) |
92 |
{ |
93 |
cerr << argv[0] << ": Cannot open display, need a clue-by-four?\n"; |
94 |
|
95 |
return 1; |
96 |
} |
97 |
#endif |
98 |
|
99 |
string error; |
100 |
string file = "svrcfg_cnc.ini"; |
101 |
|
102 |
program = fix(argv[0]); |
103 |
|
104 |
for (int index = 1; index < argc; index++) |
105 |
{ |
106 |
string arg = fix(argv[index]); |
107 |
|
108 |
if (arg == "-D") |
109 |
{ |
110 |
munge(); |
111 |
} |
112 |
else if (arg == "-file") |
113 |
{ |
114 |
if (++index < argc) |
115 |
{ |
116 |
file = fix(argv[index]); |
117 |
} |
118 |
else |
119 |
{ |
120 |
error = "The argument -file must be followed by a filename."; |
121 |
|
122 |
#ifdef _WIN32 |
123 |
MessageBox(NULL, error.c_str(), "Bad Arguments", MB_ICONERROR); |
124 |
#else |
125 |
message(NULL, error, "Bad Arguments", GTK_MESSAGE_ERROR); |
126 |
#endif |
127 |
return 1; |
128 |
} |
129 |
} |
130 |
else |
131 |
{ |
132 |
error = "The argument " + arg + " is not a valid argument."; |
133 |
|
134 |
#ifdef _WIN32 |
135 |
MessageBox(NULL, error.c_str(), "Unknown Arguments", MB_ICONWARNING |
136 |
); |
137 |
#else |
138 |
message(NULL, error, "Unknown Arguments", GTK_MESSAGE_WARNING); |
139 |
#endif |
140 |
} |
141 |
} |
142 |
|
143 |
#ifdef _WIN32 |
144 |
GlobalFree(argv); |
145 |
#endif |
146 |
|
147 |
if (debug) cerr << "file = " << file << "\n"; |
148 |
|
149 |
config = new RenegadeConfig(file); |
150 |
if (!config->load()) |
151 |
{ |
152 |
error = "Could not open " + file + "."; |
153 |
|
154 |
#ifdef _WIN32 |
155 |
MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR); |
156 |
#else |
157 |
message(NULL, error, "Bad File", GTK_MESSAGE_ERROR); |
158 |
#endif |
159 |
return 1; |
160 |
} |
161 |
|
162 |
#ifdef _WIN32 |
163 |
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector); |
164 |
#else |
165 |
MapSelector selector; |
166 |
#endif |
167 |
|
168 |
delete config; |
169 |
|
170 |
#ifdef _WIN32 |
171 |
if (debug) |
172 |
{ |
173 |
cout << "Press enter key to exit . . ."; |
174 |
cin.get(); |
175 |
} |
176 |
#endif |
177 |
|
178 |
return 0; |
179 |
} |
180 |
|
181 |
#ifdef _WIN32 |
182 |
|
183 |
int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) |
184 |
{ |
185 |
switch (uMsg) |
186 |
{ |
187 |
case WM_INITDIALOG: |
188 |
{ |
189 |
RECT rc, rcDlg, rcDesktop; |
190 |
|
191 |
GetWindowRect(GetDesktopWindow(), &rcDesktop); |
192 |
GetWindowRect(hwndDlg, &rcDlg); |
193 |
CopyRect(&rc, &rcDesktop); |
194 |
|
195 |
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top); |
196 |
OffsetRect(&rc, -rc.left, -rc.top); |
197 |
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom); |
198 |
|
199 |
SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2), |
200 |
rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE); |
201 |
} |
202 |
{ |
203 |
char* directory = new char[MAX_PATH + 1]; |
204 |
GetCurrentDirectory(MAX_PATH + 1, directory); |
205 |
if (debug) cerr << "directory = " << directory << "\n"; |
206 |
|
207 |
char* data = new char[strlen(directory) + 12]; |
208 |
sprintf(data, "%s\\data\\*.mix", directory); |
209 |
delete [] directory; |
210 |
if (debug) cerr << "data = " << data << "\n"; |
211 |
|
212 |
DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE); |
213 |
SetCurrentDirectory(".."); |
214 |
|
215 |
delete [] data; |
216 |
} |
217 |
{ |
218 |
vector<string> maps = config->getMaps(); |
219 |
|
220 |
for (unsigned index = 0; index < maps.size(); index++) |
221 |
{ |
222 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0, |
223 |
long(maps[index].c_str())); |
224 |
} |
225 |
} |
226 |
break; |
227 |
case WM_COMMAND: |
228 |
switch (LOWORD(wParam)) |
229 |
{ |
230 |
case IDOK: |
231 |
if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0) |
232 |
{ |
233 |
MessageBox(hwndDlg, "You need at least one map.", "No Maps", |
234 |
MB_ICONINFORMATION); |
235 |
|
236 |
return false; |
237 |
} |
238 |
else |
239 |
{ |
240 |
vector<string> maps; |
241 |
|
242 |
for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem( |
243 |
hwndDlg, IDC_SELECTED)); index++) |
244 |
{ |
245 |
char* name = new char[SendMessage(GetDlgItem(hwndDlg, |
246 |
IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1]; |
247 |
|
248 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
249 |
index, long(name)); |
250 |
maps.push_back(name); |
251 |
|
252 |
delete [] name; |
253 |
} |
254 |
|
255 |
config->setMaps(maps); |
256 |
} |
257 |
config->save(); |
258 |
case IDCANCEL: |
259 |
EndDialog(hwndDlg, wParam); |
260 |
return true; |
261 |
case IDC_ADD_MAP: |
262 |
{ |
263 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, |
264 |
IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0); |
265 |
unsigned* additions = new unsigned[count]; |
266 |
|
267 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS, |
268 |
count, long(additions)); |
269 |
|
270 |
if (debug) cerr << "additions = {\n"; |
271 |
for (unsigned index = 0; index < count; index++) |
272 |
{ |
273 |
if (debug) cerr << " [" << index << "] = " << |
274 |
additions[index] << "\n"; |
275 |
|
276 |
char* name = new char[SendMessage(GetDlgItem(hwndDlg, |
277 |
IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1 |
278 |
]; |
279 |
|
280 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT, |
281 |
additions[index], long(name)); |
282 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
283 |
LB_ADDSTRING, 0, long(name)); |
284 |
|
285 |
delete [] name; |
286 |
} |
287 |
if (debug) cerr << "}\n"; |
288 |
|
289 |
delete [] additions; |
290 |
|
291 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL, |
292 |
false, -1); |
293 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); |
294 |
} |
295 |
break; |
296 |
case IDC_REMOVE_MAP: |
297 |
{ |
298 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
299 |
LB_GETSELCOUNT, 0, 0); |
300 |
unsigned* removals = new unsigned[count]; |
301 |
|
302 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, |
303 |
count, long(removals)); |
304 |
|
305 |
if (debug) cerr << "removals = {\n"; |
306 |
for (unsigned index = count; index > 0; index--) |
307 |
{ |
308 |
if (debug) cerr << " [" << index - 1 << "] = " << |
309 |
removals[index - 1] << "\n"; |
310 |
|
311 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
312 |
LB_DELETESTRING, removals[index - 1], 0); |
313 |
} |
314 |
if (debug) cerr << "}\n"; |
315 |
|
316 |
delete [] removals; |
317 |
|
318 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); |
319 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
320 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
321 |
} |
322 |
break; |
323 |
case IDC_UP_MAP: |
324 |
{ |
325 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
326 |
LB_GETSELCOUNT, 0, 0); |
327 |
unsigned* ups = new unsigned[count]; |
328 |
|
329 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, |
330 |
count, long(ups)); |
331 |
|
332 |
unsigned insert = ups[0] - 1; |
333 |
|
334 |
if (debug) cerr << "ups = {\n"; |
335 |
for (unsigned index = 0; index < count; index++) |
336 |
{ |
337 |
if (debug) cerr << " [" << index << "] = " << ups[index] |
338 |
<< "\n"; |
339 |
|
340 |
char* up = new char[SendMessage(GetDlgItem(hwndDlg, |
341 |
IDC_SELECTED), LB_GETTEXTLEN, ups[index], 0) + 1]; |
342 |
|
343 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
344 |
ups[index], long(up)); |
345 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
346 |
LB_DELETESTRING, ups[index], 0); |
347 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
348 |
LB_INSERTSTRING, insert, long(up)); |
349 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL, |
350 |
true, insert++); |
351 |
} |
352 |
if (debug) cerr << "}\n"; |
353 |
|
354 |
delete [] ups; |
355 |
|
356 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
357 |
LB_GETSEL, 0, 0) == 0) |
358 |
{ |
359 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
360 |
} |
361 |
else |
362 |
{ |
363 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
364 |
} |
365 |
|
366 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
367 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
368 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
369 |
{ |
370 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
371 |
} |
372 |
} |
373 |
break; |
374 |
case IDC_DOWN_MAP: |
375 |
{ |
376 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
377 |
LB_GETSELCOUNT, 0, 0); |
378 |
unsigned* downs = new unsigned[count]; |
379 |
|
380 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, |
381 |
count, long(downs)); |
382 |
|
383 |
unsigned insert = downs[count - 1] + 1; |
384 |
|
385 |
if (debug) cerr << "downs = {\n"; |
386 |
for (unsigned index = count; index > 0; index--) |
387 |
{ |
388 |
if (debug) cerr << " [" << index << "] = " << downs[index |
389 |
- 1] << "\n"; |
390 |
|
391 |
char* down = new char[SendMessage(GetDlgItem(hwndDlg, |
392 |
IDC_SELECTED), LB_GETTEXTLEN, downs[index - 1], 0) + |
393 |
1]; |
394 |
|
395 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
396 |
downs[index - 1], long(down)); |
397 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
398 |
LB_DELETESTRING, downs[index - 1], 0); |
399 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
400 |
LB_INSERTSTRING, insert, long(down)); |
401 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL, |
402 |
true, insert--); |
403 |
} |
404 |
if (debug) cerr << "}\n"; |
405 |
|
406 |
delete [] downs; |
407 |
|
408 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
409 |
LB_GETSEL, 0, 0) == 0) |
410 |
{ |
411 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
412 |
} |
413 |
|
414 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
415 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
416 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
417 |
{ |
418 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
419 |
} |
420 |
else |
421 |
{ |
422 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
423 |
} |
424 |
} |
425 |
break; |
426 |
case IDC_AVAILABLE: |
427 |
switch (HIWORD(wParam)) |
428 |
{ |
429 |
case LBN_SELCHANGE: |
430 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), |
431 |
LB_GETSELCOUNT, 0, 0) > 0) |
432 |
{ |
433 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true); |
434 |
} |
435 |
else |
436 |
{ |
437 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); |
438 |
} |
439 |
break; |
440 |
} |
441 |
break; |
442 |
case IDC_SELECTED: |
443 |
switch (HIWORD(wParam)) |
444 |
{ |
445 |
case LBN_SELCHANGE: |
446 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
447 |
LB_GETSELCOUNT, 0, 0) > 0) |
448 |
{ |
449 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true); |
450 |
|
451 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
452 |
LB_GETSEL, 0, 0) == 0) |
453 |
{ |
454 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
455 |
} |
456 |
else |
457 |
{ |
458 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
459 |
} |
460 |
|
461 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
462 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
463 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
464 |
{ |
465 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
466 |
} |
467 |
else |
468 |
{ |
469 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
470 |
} |
471 |
} |
472 |
else |
473 |
{ |
474 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); |
475 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
476 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
477 |
} |
478 |
break; |
479 |
} |
480 |
break; |
481 |
} |
482 |
break; |
483 |
} |
484 |
|
485 |
return false; |
486 |
} |
487 |
|
488 |
#endif |