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) cin.get(); |
123 |
|
124 |
return 0; |
125 |
} |
126 |
|
127 |
int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) |
128 |
{ |
129 |
switch (uMsg) |
130 |
{ |
131 |
case WM_INITDIALOG: |
132 |
{ |
133 |
RECT rc, rcDlg, rcDesktop; |
134 |
|
135 |
GetWindowRect(GetDesktopWindow(), &rcDesktop); |
136 |
GetWindowRect(hwndDlg, &rcDlg); |
137 |
CopyRect(&rc, &rcDesktop); |
138 |
|
139 |
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top); |
140 |
OffsetRect(&rc, -rc.left, -rc.top); |
141 |
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom); |
142 |
|
143 |
SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2), |
144 |
rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE); |
145 |
} |
146 |
{ |
147 |
char* directory = new char[MAX_PATH + 1]; |
148 |
GetCurrentDirectory(MAX_PATH + 1, directory); |
149 |
if (debug) cerr << "directory = " << directory << "\n"; |
150 |
|
151 |
char* data = new char[MAX_PATH + 1]; |
152 |
sprintf(data, "%s\\data\\*.mix", directory); |
153 |
delete [] directory; |
154 |
if (debug) cerr << "data = " << data << "\n"; |
155 |
|
156 |
DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE); |
157 |
SetCurrentDirectory(".."); |
158 |
|
159 |
delete [] data; |
160 |
} |
161 |
{ |
162 |
vector<string> maps = config->getMaps(); |
163 |
|
164 |
for (unsigned index = 0; index < maps.size(); index++) |
165 |
{ |
166 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0, |
167 |
long(maps[index].c_str())); |
168 |
} |
169 |
} |
170 |
break; |
171 |
case WM_COMMAND: |
172 |
switch (LOWORD(wParam)) |
173 |
{ |
174 |
case IDOK: |
175 |
if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0) |
176 |
{ |
177 |
MessageBox(hwndDlg, "You need at least one map.", "No Maps", |
178 |
MB_ICONEXCLAMATION); |
179 |
|
180 |
return false; |
181 |
} |
182 |
else |
183 |
{ |
184 |
vector<string> maps; |
185 |
|
186 |
for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem( |
187 |
hwndDlg, IDC_SELECTED)); index++) |
188 |
{ |
189 |
char* name = new char[SendMessage(GetDlgItem(hwndDlg, |
190 |
IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1]; |
191 |
|
192 |
SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, |
193 |
index, long(name)); |
194 |
maps.push_back(name); |
195 |
|
196 |
delete [] name; |
197 |
} |
198 |
|
199 |
config->setMaps(maps); |
200 |
} |
201 |
config->save(); |
202 |
case IDCANCEL: |
203 |
EndDialog(hwndDlg, wParam); |
204 |
return true; |
205 |
case IDC_ADD_MAP: |
206 |
break; |
207 |
case IDC_REMOVE_MAP: |
208 |
break; |
209 |
case IDC_UP_MAP: |
210 |
break; |
211 |
case IDC_DOWN_MAP: |
212 |
break; |
213 |
case IDC_AVAILABLE: |
214 |
switch (HIWORD(wParam)) |
215 |
{ |
216 |
case LBN_SELCHANGE: |
217 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), |
218 |
LB_GETSELCOUNT, 0, 0) > 0) |
219 |
{ |
220 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true); |
221 |
} |
222 |
else |
223 |
{ |
224 |
EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); |
225 |
} |
226 |
break; |
227 |
} |
228 |
break; |
229 |
case IDC_SELECTED: |
230 |
switch (HIWORD(wParam)) |
231 |
{ |
232 |
case LBN_SELCHANGE: |
233 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
234 |
LB_GETSELCOUNT, 0, 0) > 0) |
235 |
{ |
236 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true); |
237 |
|
238 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
239 |
LB_GETSEL, 0, 0) == 0) |
240 |
{ |
241 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); |
242 |
} |
243 |
else |
244 |
{ |
245 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
246 |
} |
247 |
|
248 |
if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), |
249 |
LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED |
250 |
), LB_GETCOUNT, 0, 0) - 1, 0) == 0) |
251 |
{ |
252 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); |
253 |
} |
254 |
else |
255 |
{ |
256 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
257 |
} |
258 |
} |
259 |
else |
260 |
{ |
261 |
EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); |
262 |
EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); |
263 |
EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); |
264 |
} |
265 |
break; |
266 |
} |
267 |
break; |
268 |
} |
269 |
break; |
270 |
} |
271 |
|
272 |
return false; |
273 |
} |