// Renegade Map Selector // // Douglas Thrift // // RenegadeMapSelector.cpp #ifdef _WIN32 #include #include #include "resource.h" #endif #include "RenegadeMapSelector.h" #include "RenegadeConfig.h" bool debug = false; string program; RenegadeConfig* config; #ifndef _WIN32 inline string fix(const string& ansi) { return ansi; } inline void munge(void) { debug = true; } #else inline string fix(const wstring& wide) { char* buffer = new char[wide.length() + 1]; WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, buffer, wide.length() + 1, NULL, NULL); string ansi = buffer; delete [] buffer; return ansi; } inline void munge(void) { if (debug) return; AllocConsole(); SetConsoleTitle("Renegade Map Selector"); int hin = _open_osfhandle(long(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT); int hout = _open_osfhandle(long(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT); int herr = _open_osfhandle(long(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT); FILE* fin = _fdopen(hin, "r"); FILE* fout = _fdopen(hout, "w"); FILE* ferr = _fdopen(herr, "w"); *stdin = *fin; *stdout = *fout; *stderr = *ferr; setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); cin.sync_with_stdio(); cout.sync_with_stdio(); cerr.sync_with_stdio(); debug = true; } #endif #ifdef _WIN32 int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { int argc; unsigned short** argv = CommandLineToArgvW(GetCommandLineW(), &argc); #else int main(int argc, char* argv[]) { #endif string error; string file = "svrcfg_cnc.ini"; program = fix(argv[0]); for (int index = 1; index < argc; index++) { string arg = fix(argv[index]); if (arg == "-D") { munge(); } else if (arg == "-file") { if (++index < argc) { file = fix(argv[index]); } else { error = "The argument -file must be followed by a filename."; #ifdef _WIN32 MessageBox(NULL, error.c_str(), "Bad Arguments", MB_ICONERROR); #endif return 1; } } } #ifdef _WIN32 GlobalFree(argv); #endif if (debug) cerr << "file = " << file << "\n"; config = new RenegadeConfig(file); if (!config->load()) { error = "Could not open " + file + "."; #ifdef _WIN32 MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR); #endif return 1; } #ifdef _WIN32 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector); #endif delete config; #ifdef _WIN32 if (debug) { cout << "Press enter key to exit . . ."; cin.get(); } #endif return 0; } #ifdef _WIN32 int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: { RECT rc, rcDlg, rcDesktop; GetWindowRect(GetDesktopWindow(), &rcDesktop); GetWindowRect(hwndDlg, &rcDlg); CopyRect(&rc, &rcDesktop); OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top); OffsetRect(&rc, -rc.left, -rc.top); OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom); SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2), rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE); } { char* directory = new char[MAX_PATH + 1]; GetCurrentDirectory(MAX_PATH + 1, directory); if (debug) cerr << "directory = " << directory << "\n"; char* data = new char[MAX_PATH + 1]; sprintf(data, "%s\\data\\*.mix", directory); delete [] directory; if (debug) cerr << "data = " << data << "\n"; DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE); SetCurrentDirectory(".."); delete [] data; } { vector maps = config->getMaps(); for (unsigned index = 0; index < maps.size(); index++) { SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0, long(maps[index].c_str())); } } break; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0) { MessageBox(hwndDlg, "You need at least one map.", "No Maps", MB_ICONEXCLAMATION); return false; } else { vector maps; for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem( hwndDlg, IDC_SELECTED)); index++) { char* name = new char[SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1]; SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT, index, long(name)); maps.push_back(name); delete [] name; } config->setMaps(maps); } config->save(); case IDCANCEL: EndDialog(hwndDlg, wParam); return true; case IDC_ADD_MAP: { unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0); unsigned* additions = new unsigned[count]; SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS, count, long(additions)); if (debug) cerr << "additions = {\n"; for (unsigned index = 0; index < count; index++) { if (debug) cerr << " [" << index << "] = " << additions[index] << "\n"; char* name = new char[SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1 ]; SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT, additions[index], long(name)); SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0, long(name)); delete [] name; } if (debug) cerr << "}\n"; delete [] additions; SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL, false, -1); EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); } break; case IDC_REMOVE_MAP: { unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELCOUNT, 0, 0); unsigned* removals = new unsigned[count]; SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS, count, long(removals)); if (debug) cerr << "removals = {\n"; for (unsigned index = count; index > 0; index--) { if (debug) cerr << " [" << index - 1 << "] = " << removals[index - 1] << "\n"; SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_DELETESTRING, removals[index - 1], 0); } if (debug) cerr << "}\n"; delete [] removals; EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); } break; case IDC_UP_MAP: cout << "STUB: up\a\n"; break; case IDC_DOWN_MAP: cout << "STUB: down\a\n"; break; case IDC_AVAILABLE: switch (HIWORD(wParam)) { case LBN_SELCHANGE: if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0) > 0) { EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true); } else { EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false); } break; } break; case IDC_SELECTED: switch (HIWORD(wParam)) { case LBN_SELCHANGE: if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELCOUNT, 0, 0) > 0) { EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true); if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSEL, 0, 0) == 0) { EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true); } else { EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); } if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED ), LB_GETCOUNT, 0, 0) - 1, 0) == 0) { EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true); } else { EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); } } else { EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false); EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false); EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false); } break; } break; } break; } return false; } #endif