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_ICONWARN); |
136 |
#else |
137 |
message(NULL, error, "Unknown Arguments", GTK_MESSAGE_WARNING); |
138 |
#endif |
139 |
} |
140 |
} |
141 |
|
142 |
#ifdef _WIN32 |
143 |
GlobalFree(argv); |
144 |
#endif |
145 |
|
146 |
if (debug) cerr << "file = " << file << "\n"; |
147 |
|
148 |
config = new RenegadeConfig(file); |
149 |
if (!config->load()) |
150 |
{ |
151 |
error = "Could not open " + file + "."; |
152 |
|
153 |
#ifdef _WIN32 |
154 |
MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR); |
155 |
#else |
156 |
message(NULL, error, "Bad File", GTK_MESSAGE_ERROR); |
157 |
#endif |
158 |
return 1; |
159 |
} |
160 |
|
161 |
#ifdef _WIN32 |
162 |
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector); |
163 |
#else |
164 |
MapSelector selector; |
165 |
#endif |
166 |
|
167 |
delete config; |
168 |
|
169 |
#ifdef _WIN32 |
170 |
if (debug) |
171 |
{ |
172 |
cout << "Press enter key to exit . . ."; |
173 |
cin.get(); |
174 |
} |
175 |
#endif |
176 |
|
177 |
return 0; |
178 |
} |
179 |
|
180 |
#ifdef _WIN32 |
181 |
|
182 |
int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) |
183 |
{ |
184 |
switch (uMsg) |
185 |
{ |
186 |
case WM_INITDIALOG: |
187 |
{ |
188 |
RECT rc, rcDlg, rcDesktop; |
189 |
|
190 |
GetWindowRect(GetDesktopWindow(), &rcDesktop); |
191 |
GetWindowRect(hwndDlg, &rcDlg); |
192 |
CopyRect(&rc, &rcDesktop); |
193 |
|
194 |
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top); |
195 |
OffsetRect(&rc, -rc.left, -rc.top); |
196 |
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom); |
197 |
|
198 |
SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2), |
199 |
rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE); |
200 |
} |
201 |
{ |
202 |
char* directory = new char[MAX_PATH + 1]; |
203 |
GetCurrentDirectory(MAX_PATH + 1, directory); |
204 |
if (debug) cerr << "directory = " << directory << "\n"; |
205 |
|
206 |
char* data = new char[strlen(directory) + 12]; |
207 |
sprintf(data, "%s\\data\\*.mix", directory); |
208 |
delete [] directory; |
209 |
if (debug) cerr << "data = " << data << "\n"; |
210 |
|
211 |
DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE); |
212 |
SetCurrentDirectory(".."); |
213 |
|
214 |
delete [] data; |
215 |
} |
216 |
{ |
217 |
vector<string> maps = config->getMaps(); |
218 |
|
219 |
for (unsigned index = 0; index < maps.size(); index++) |
220 |
{ |
221 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0, |
222 |
long(maps[index].c_str())); |
223 |
} |
224 |
} |
225 |
break; |
226 |
case WM_COMMAND: |
227 |
switch (LOWORD(wParam)) |
228 |
{ |
229 |
case IDOK: |
230 |
if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0) |
231 |
{ |
232 |
MessageBox(hwndDlg, "You need at least one map.", "No Maps", |
233 |
MB_ICONEXCLAMATION); |
234 |
|
235 |
return false; |
236 |
} |
237 |
else |
238 |
{ |
239 |
vector<string> maps; |
240 |
|
241 |
for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem( |
242 |
hwndDlg, IDC_SELECTED)); index++) |
243 |
{ |
244 |
char* name = new char[SendMessage(GetDlgItem(hwndDlg, |
245 |
IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1]; |
246 |
|
247 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
248 |
index, long(name)); |
249 |
maps.push_back(name); |
250 |
|
251 |
delete [] name; |
252 |
} |
253 |
|
254 |
config->setMaps(maps); |
255 |
} |
256 |
config->save(); |
257 |
case IDCANCEL: |
258 |
EndDialog(hwndDlg, wParam); |
259 |
return true; |
260 |
case IDC_ADD_MAP: |
261 |
{ |
262 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, |
263 |
IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0); |
264 |
unsigned* additions = new unsigned[count]; |
265 |
|
266 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS, |
267 |
count, long(additions)); |
268 |
|
269 |
if (debug) cerr << "additions = {\n"; |
270 |
for (unsigned index = 0; index < count; index++) |
271 |
{ |
272 |
if (debug) cerr << " [" << index << "] = " << |
273 |
additions[index] << "\n"; |
274 |
|
275 |
char* name = new char[SendMessage(GetDlgItem(hwndDlg, |
276 |
IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1 |
277 |
]; |
278 |
|
279 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT, |
280 |
additions[index], long(name)); |
281 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
282 |
LB_ADDSTRING, 0, long(name)); |
283 |
|
284 |
delete [] name; |
285 |
} |
286 |
if (debug) cerr << "}\n"; |
287 |
|
288 |
delete [] additions; |
289 |
|
290 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL, |
291 |
false, -1); |
292 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); |
293 |
} |
294 |
break; |
295 |
case IDC_REMOVE_MAP: |
296 |
{ |
297 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
298 |
LB_GETSELCOUNT, 0, 0); |
299 |
unsigned* removals = new unsigned[count]; |
300 |
|
301 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, |
302 |
count, long(removals)); |
303 |
|
304 |
if (debug) cerr << "removals = {\n"; |
305 |
for (unsigned index = count; index > 0; index--) |
306 |
{ |
307 |
if (debug) cerr << " [" << index - 1 << "] = " << |
308 |
removals[index - 1] << "\n"; |
309 |
|
310 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
311 |
LB_DELETESTRING, removals[index - 1], 0); |
312 |
} |
313 |
if (debug) cerr << "}\n"; |
314 |
|
315 |
delete [] removals; |
316 |
|
317 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); |
318 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
319 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
320 |
} |
321 |
break; |
322 |
case IDC_UP_MAP: |
323 |
{ |
324 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
325 |
LB_GETSELCOUNT, 0, 0); |
326 |
unsigned* ups = new unsigned[count]; |
327 |
|
328 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, |
329 |
count, long(ups)); |
330 |
|
331 |
unsigned insert = ups[0] - 1; |
332 |
|
333 |
if (debug) cerr << "ups = {\n"; |
334 |
for (unsigned index = 0; index < count; index++) |
335 |
{ |
336 |
if (debug) cerr << " [" << index << "] = " << ups[index] |
337 |
<< "\n"; |
338 |
|
339 |
char* up = new char[SendMessage(GetDlgItem(hwndDlg, |
340 |
IDC_SELECTED), LB_GETTEXTLEN, ups[index], 0) + 1]; |
341 |
|
342 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
343 |
ups[index], long(up)); |
344 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
345 |
LB_DELETESTRING, ups[index], 0); |
346 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
347 |
LB_INSERTSTRING, insert, long(up)); |
348 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL, |
349 |
true, insert++); |
350 |
} |
351 |
if (debug) cerr << "}\n"; |
352 |
|
353 |
delete [] ups; |
354 |
|
355 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
356 |
LB_GETSEL, 0, 0) == 0) |
357 |
{ |
358 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
359 |
} |
360 |
else |
361 |
{ |
362 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
363 |
} |
364 |
|
365 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
366 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
367 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
368 |
{ |
369 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
370 |
} |
371 |
} |
372 |
break; |
373 |
case IDC_DOWN_MAP: |
374 |
{ |
375 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
376 |
LB_GETSELCOUNT, 0, 0); |
377 |
unsigned* downs = new unsigned[count]; |
378 |
|
379 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, |
380 |
count, long(downs)); |
381 |
|
382 |
unsigned insert = downs[count - 1] + 1; |
383 |
|
384 |
if (debug) cerr << "downs = {\n"; |
385 |
for (unsigned index = count; index > 0; index--) |
386 |
{ |
387 |
if (debug) cerr << " [" << index << "] = " << downs[index |
388 |
- 1] << "\n"; |
389 |
|
390 |
char* down = new char[SendMessage(GetDlgItem(hwndDlg, |
391 |
IDC_SELECTED), LB_GETTEXTLEN, downs[index - 1], 0) + |
392 |
1]; |
393 |
|
394 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
395 |
downs[index - 1], long(down)); |
396 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
397 |
LB_DELETESTRING, downs[index - 1], 0); |
398 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
399 |
LB_INSERTSTRING, insert, long(down)); |
400 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL, |
401 |
true, insert--); |
402 |
} |
403 |
if (debug) cerr << "}\n"; |
404 |
|
405 |
delete [] downs; |
406 |
|
407 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
408 |
LB_GETSEL, 0, 0) == 0) |
409 |
{ |
410 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
411 |
} |
412 |
|
413 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
414 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
415 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
416 |
{ |
417 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
418 |
} |
419 |
else |
420 |
{ |
421 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
422 |
} |
423 |
} |
424 |
break; |
425 |
case IDC_AVAILABLE: |
426 |
switch (HIWORD(wParam)) |
427 |
{ |
428 |
case LBN_SELCHANGE: |
429 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), |
430 |
LB_GETSELCOUNT, 0, 0) > 0) |
431 |
{ |
432 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true); |
433 |
} |
434 |
else |
435 |
{ |
436 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); |
437 |
} |
438 |
break; |
439 |
} |
440 |
break; |
441 |
case IDC_SELECTED: |
442 |
switch (HIWORD(wParam)) |
443 |
{ |
444 |
case LBN_SELCHANGE: |
445 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
446 |
LB_GETSELCOUNT, 0, 0) > 0) |
447 |
{ |
448 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true); |
449 |
|
450 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
451 |
LB_GETSEL, 0, 0) == 0) |
452 |
{ |
453 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
454 |
} |
455 |
else |
456 |
{ |
457 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
458 |
} |
459 |
|
460 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
461 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
462 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
463 |
{ |
464 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
465 |
} |
466 |
else |
467 |
{ |
468 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
469 |
} |
470 |
} |
471 |
else |
472 |
{ |
473 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); |
474 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
475 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
476 |
} |
477 |
break; |
478 |
} |
479 |
break; |
480 |
} |
481 |
break; |
482 |
} |
483 |
|
484 |
return false; |
485 |
} |
486 |
|
487 |
#endif |