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