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