1 |
// Renegade Map Selector |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// RenegadeMapSelector.cpp |
6 |
|
7 |
#ifdef _WIN32 |
8 |
#include <io.h> |
9 |
#include <fcntl.h> |
10 |
#include "resource.h" |
11 |
#endif |
12 |
|
13 |
#include "RenegadeMapSelector.h" |
14 |
#include "RenegadeConfig.h" |
15 |
|
16 |
bool debug = false; |
17 |
string program; |
18 |
RenegadeConfig* config; |
19 |
|
20 |
#ifndef _WIN32 |
21 |
inline string fix(const string& ansi) { return ansi; } |
22 |
inline void munge(void) { return; } |
23 |
#else |
24 |
inline string fix(const wstring& wide) |
25 |
{ |
26 |
char* buffer = new char[wide.length() + 1]; |
27 |
|
28 |
WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, buffer, wide.length() + 1, |
29 |
NULL, NULL); |
30 |
|
31 |
string ansi = buffer; |
32 |
delete [] buffer; |
33 |
|
34 |
return ansi; |
35 |
} |
36 |
|
37 |
inline void munge(void) |
38 |
{ |
39 |
if (debug) return; |
40 |
|
41 |
AllocConsole(); |
42 |
SetConsoleTitle("Renegade Map Selector"); |
43 |
|
44 |
int hin = _open_osfhandle(long(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT); |
45 |
int hout = _open_osfhandle(long(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT); |
46 |
int herr = _open_osfhandle(long(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT); |
47 |
|
48 |
FILE* fin = _fdopen(hin, "r"); |
49 |
FILE* fout = _fdopen(hout, "w"); |
50 |
FILE* ferr = _fdopen(herr, "w"); |
51 |
|
52 |
*stdin = *fin; |
53 |
*stdout = *fout; |
54 |
*stderr = *ferr; |
55 |
|
56 |
setvbuf(stdin, NULL, _IONBF, 0); |
57 |
setvbuf(stdout, NULL, _IONBF, 0); |
58 |
setvbuf(stderr, NULL, _IONBF, 0); |
59 |
|
60 |
cin.sync_with_stdio(); |
61 |
cout.sync_with_stdio(); |
62 |
cerr.sync_with_stdio(); |
63 |
|
64 |
debug = true; |
65 |
} |
66 |
#endif |
67 |
|
68 |
int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); |
69 |
|
70 |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR |
71 |
lpCmdLine, int nShowCmd) |
72 |
{ |
73 |
int argc; |
74 |
unsigned short** argv = CommandLineToArgvW(GetCommandLineW(), &argc); |
75 |
|
76 |
string error; |
77 |
string file = "svrcfg_cnc.ini"; |
78 |
|
79 |
program = fix(argv[0]); |
80 |
|
81 |
for (int index = 1; index < argc; index++) |
82 |
{ |
83 |
string arg = fix(argv[index]); |
84 |
|
85 |
if (arg == "-D") |
86 |
{ |
87 |
munge(); |
88 |
} |
89 |
else if (arg == "-file") |
90 |
{ |
91 |
if (++index < argc) |
92 |
{ |
93 |
file = fix(argv[index]); |
94 |
} |
95 |
else |
96 |
{ |
97 |
error = "The argument -file must be followed by a filename."; |
98 |
|
99 |
MessageBox(NULL, error.c_str(), "Bad Arguments", MB_ICONERROR); |
100 |
return 1; |
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
GlobalFree(argv); |
106 |
|
107 |
if (debug) cerr << "file = " << file << "\n"; |
108 |
|
109 |
config = new RenegadeConfig(file); |
110 |
if (!config->load()) |
111 |
{ |
112 |
error = "Could not open " + file + "."; |
113 |
|
114 |
MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR); |
115 |
return 1; |
116 |
} |
117 |
|
118 |
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector); |
119 |
|
120 |
delete config; |
121 |
|
122 |
if (debug) |
123 |
{ |
124 |
cout << "Press enter key to exit . . ."; |
125 |
cin.get(); |
126 |
} |
127 |
|
128 |
return 0; |
129 |
} |
130 |
|
131 |
int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) |
132 |
{ |
133 |
switch (uMsg) |
134 |
{ |
135 |
case WM_INITDIALOG: |
136 |
{ |
137 |
RECT rc, rcDlg, rcDesktop; |
138 |
|
139 |
GetWindowRect(GetDesktopWindow(), &rcDesktop); |
140 |
GetWindowRect(hwndDlg, &rcDlg); |
141 |
CopyRect(&rc, &rcDesktop); |
142 |
|
143 |
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top); |
144 |
OffsetRect(&rc, -rc.left, -rc.top); |
145 |
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom); |
146 |
|
147 |
SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2), |
148 |
rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE); |
149 |
} |
150 |
{ |
151 |
char* directory = new char[MAX_PATH + 1]; |
152 |
GetCurrentDirectory(MAX_PATH + 1, directory); |
153 |
if (debug) cerr << "directory = " << directory << "\n"; |
154 |
|
155 |
char* data = new char[MAX_PATH + 1]; |
156 |
sprintf(data, "%s\\data\\*.mix", directory); |
157 |
delete [] directory; |
158 |
if (debug) cerr << "data = " << data << "\n"; |
159 |
|
160 |
DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE); |
161 |
SetCurrentDirectory(".."); |
162 |
|
163 |
delete [] data; |
164 |
} |
165 |
{ |
166 |
vector<string> maps = config->getMaps(); |
167 |
|
168 |
for (unsigned index = 0; index < maps.size(); index++) |
169 |
{ |
170 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0, |
171 |
long(maps[index].c_str())); |
172 |
} |
173 |
} |
174 |
break; |
175 |
case WM_COMMAND: |
176 |
switch (LOWORD(wParam)) |
177 |
{ |
178 |
case IDOK: |
179 |
if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0) |
180 |
{ |
181 |
MessageBox(hwndDlg, "You need at least one map.", "No Maps", |
182 |
MB_ICONEXCLAMATION); |
183 |
|
184 |
return false; |
185 |
} |
186 |
else |
187 |
{ |
188 |
vector<string> maps; |
189 |
|
190 |
for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem( |
191 |
hwndDlg, IDC_SELECTED)); index++) |
192 |
{ |
193 |
char* name = new char[SendMessage(GetDlgItem(hwndDlg, |
194 |
IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1]; |
195 |
|
196 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
197 |
index, long(name)); |
198 |
maps.push_back(name); |
199 |
|
200 |
delete [] name; |
201 |
} |
202 |
|
203 |
config->setMaps(maps); |
204 |
} |
205 |
config->save(); |
206 |
case IDCANCEL: |
207 |
EndDialog(hwndDlg, wParam); |
208 |
return true; |
209 |
case IDC_ADD_MAP: |
210 |
{ |
211 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, |
212 |
IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0); |
213 |
unsigned* additions = new unsigned[count]; |
214 |
|
215 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS, |
216 |
count, long(additions)); |
217 |
|
218 |
if (debug) cerr << "additions = {\n"; |
219 |
for (unsigned index = 0; index < count; index++) |
220 |
{ |
221 |
if (debug) cerr << " [" << index << "] = " << |
222 |
additions[index] << "\n"; |
223 |
|
224 |
char* name = new char[SendMessage(GetDlgItem(hwndDlg, |
225 |
IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1 |
226 |
]; |
227 |
|
228 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT, |
229 |
additions[index], long(name)); |
230 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
231 |
LB_ADDSTRING, 0, long(name)); |
232 |
|
233 |
delete [] name; |
234 |
} |
235 |
if (debug) cerr << "}\n"; |
236 |
|
237 |
delete [] additions; |
238 |
|
239 |
SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL, |
240 |
false, -1); |
241 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); |
242 |
} |
243 |
break; |
244 |
case IDC_REMOVE_MAP: |
245 |
{ |
246 |
unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
247 |
LB_GETSELCOUNT, 0, 0); |
248 |
unsigned* removals = new unsigned[count]; |
249 |
|
250 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, |
251 |
count, long(removals)); |
252 |
|
253 |
if (debug) cerr << "removals = {\n"; |
254 |
for (unsigned index = count; index > 0; index--) |
255 |
{ |
256 |
if (debug) cerr << " [" << index - 1 << "] = " << |
257 |
removals[index - 1] << "\n"; |
258 |
|
259 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
260 |
LB_DELETESTRING, removals[index - 1], 0); |
261 |
} |
262 |
if (debug) cerr << "}\n"; |
263 |
|
264 |
delete [] removals; |
265 |
|
266 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); |
267 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
268 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
269 |
} |
270 |
break; |
271 |
case IDC_UP_MAP: |
272 |
cout << "STUB: up\a\n"; |
273 |
break; |
274 |
case IDC_DOWN_MAP: |
275 |
cout << "STUB: down\a\n"; |
276 |
break; |
277 |
case IDC_AVAILABLE: |
278 |
switch (HIWORD(wParam)) |
279 |
{ |
280 |
case LBN_SELCHANGE: |
281 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), |
282 |
LB_GETSELCOUNT, 0, 0) > 0) |
283 |
{ |
284 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true); |
285 |
} |
286 |
else |
287 |
{ |
288 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); |
289 |
} |
290 |
break; |
291 |
} |
292 |
break; |
293 |
case IDC_SELECTED: |
294 |
switch (HIWORD(wParam)) |
295 |
{ |
296 |
case LBN_SELCHANGE: |
297 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
298 |
LB_GETSELCOUNT, 0, 0) > 0) |
299 |
{ |
300 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true); |
301 |
|
302 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
303 |
LB_GETSEL, 0, 0) == 0) |
304 |
{ |
305 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
306 |
} |
307 |
else |
308 |
{ |
309 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
310 |
} |
311 |
|
312 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
313 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
314 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
315 |
{ |
316 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
317 |
} |
318 |
else |
319 |
{ |
320 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
321 |
} |
322 |
} |
323 |
else |
324 |
{ |
325 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); |
326 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
327 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
328 |
} |
329 |
break; |
330 |
} |
331 |
break; |
332 |
} |
333 |
break; |
334 |
} |
335 |
|
336 |
return false; |
337 |
} |