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