From 1f1e689db0878889aec95045bb24dd98e3420262 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 20:21:44 +0100 Subject: [PATCH] Reorder init.cpp --- Source/init.cpp | 550 ++++++++++++++++++++++++------------------------ Source/init.h | 34 --- 2 files changed, 275 insertions(+), 309 deletions(-) diff --git a/Source/init.cpp b/Source/init.cpp index 0194ac4fa..0e76365da 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -51,85 +51,7 @@ HANDLE hfopt2_mpq; char gszVersionNumber[MAX_PATH] = "internal version unknown"; char gszProductName[MAX_PATH] = "Diablo v1.09"; -void init_cleanup(BOOL show_cursor) -{ - pfile_flush_W(); - init_disable_screensaver(FALSE); - init_run_office_from_start_menu(); - - if (diabdat_mpq) { - SFileCloseArchive(diabdat_mpq); - diabdat_mpq = NULL; - } - if (patch_rt_mpq) { - SFileCloseArchive(patch_rt_mpq); - patch_rt_mpq = NULL; - } - if (hellfire_mpq) { - SFileCloseArchive(hellfire_mpq); - hellfire_mpq = NULL; - } -#ifdef HELLFIRE - if (hfmonk_mpq) { - SFileCloseArchive(hfmonk_mpq); - hfmonk_mpq = NULL; - } - if (hfbard_mpq) { - SFileCloseArchive(hfbard_mpq); - hfbard_mpq = NULL; - } - if (hfbarb_mpq) { - SFileCloseArchive(hfbarb_mpq); - hfbarb_mpq = NULL; - } - if (hfmusic_mpq) { - SFileCloseArchive(hfmusic_mpq); - hfmusic_mpq = NULL; - } - if (hfvoice_mpq) { - SFileCloseArchive(hfvoice_mpq); - hfvoice_mpq = NULL; - } - if (hfopt1_mpq) { - SFileCloseArchive(hfopt1_mpq); - hfopt1_mpq = NULL; - } - if (hfopt2_mpq) { - SFileCloseArchive(hfopt2_mpq); - hfopt2_mpq = NULL; - } -#endif - UiDestroy(); - effects_cleanup_sfx(); - sound_cleanup(); - NetClose(); - dx_cleanup(); - engine_debug_trap(show_cursor); - StormDestroy(); - - if (show_cursor) - ShowCursor(TRUE); -} - -void init_run_office_from_start_menu() -{ - LPITEMIDLIST idl; - - if (!killed_mom_parent) { - return; - } - - killed_mom_parent = FALSE; - char szPath[256] = ""; /// BUGFIX: size should be at least 'MAX_PATH' - idl = NULL; - - if (SHGetSpecialFolderLocation(GetDesktopWindow(), CSIDL_STARTMENU, &idl) == NOERROR) { - SHGetPathFromIDList(idl, szPath); - init_run_office(szPath); - } -} - -void init_run_office(char *dir) +static void init_run_office(char *dir) { HANDLE hSearch; WIN32_FIND_DATA find; @@ -168,7 +90,65 @@ void init_run_office(char *dir) FindClose(hSearch); } -void init_disable_screensaver(BOOLEAN disable) +static HWND init_find_mom_parent() +{ + HWND i, handle; + char ClassName[256]; + + for (i = GetForegroundWindow();; i = GetWindow(handle, GW_HWNDNEXT)) { + handle = i; + if (!i) + break; + GetClassName(i, ClassName, 255); + if (!_strcmpi(ClassName, "MOM Parent")) + break; + } + return handle; +} + +static void init_kill_mom_parent() +{ + HWND handle; + + handle = init_find_mom_parent(); + if (handle != NULL) { + PostMessage(handle, WM_CLOSE, 0, 0); + killed_mom_parent = TRUE; + } +} + +static void init_await_mom_parent_exit() +{ + DWORD tick; + + tick = GetTickCount(); + if (!init_find_mom_parent()) { + return; + } + do { + Sleep(250); + } while (GetTickCount() - tick <= 4000 && init_find_mom_parent() != NULL); +} + +static void init_run_office_from_start_menu() +{ + LPITEMIDLIST idl; + + if (!killed_mom_parent) { + return; + } + + killed_mom_parent = FALSE; + char szPath[256] = ""; /// BUGFIX: size should be at least 'MAX_PATH' + idl = NULL; + + if (SHGetSpecialFolderLocation(GetDesktopWindow(), CSIDL_STARTMENU, &idl) == NOERROR) { + SHGetPathFromIDList(idl, szPath); + init_run_office(szPath); + } +} + +static void init_disable_screensaver(BOOLEAN disable) { BOOLEAN enabled; char Data[16]; @@ -200,89 +180,189 @@ void init_disable_screensaver(BOOLEAN disable) RegCloseKey(phkResult); } -void init_create_window(int nCmdShow) +void init_cleanup(BOOL show_cursor) { - int nWidth, nHeight; - HWND hWnd; - WNDCLASSEXA wcex; + pfile_flush_W(); + init_disable_screensaver(FALSE); + init_run_office_from_start_menu(); - init_kill_mom_parent(); - pfile_init_save_directory(); - memset(&wcex, 0, sizeof(wcex)); - wcex.cbSize = sizeof(wcex); - wcex.style = CS_HREDRAW | CS_VREDRAW; - wcex.lpfnWndProc = WindowProc; - wcex.hInstance = ghInst; - wcex.hIcon = LoadIcon(ghInst, MAKEINTRESOURCE(IDI_ICON1)); - wcex.hCursor = LoadCursor(NULL, IDC_ARROW); - wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - wcex.lpszMenuName = GAME_NAME; - wcex.lpszClassName = "DIABLO"; - wcex.hIconSm = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); - if (!RegisterClassEx(&wcex)) - app_fatal("Unable to register window class"); - if (GetSystemMetrics(SM_CXSCREEN) < SCREEN_WIDTH) - nWidth = SCREEN_WIDTH; - else - nWidth = GetSystemMetrics(SM_CXSCREEN); - if (GetSystemMetrics(SM_CYSCREEN) < SCREEN_HEIGHT) - nHeight = SCREEN_HEIGHT; - else - nHeight = GetSystemMetrics(SM_CYSCREEN); - hWnd = CreateWindowEx(0, "DIABLO", GAME_NAME, WS_POPUP, 0, 0, nWidth, nHeight, NULL, NULL, ghInst, NULL); - if (!hWnd) - app_fatal("Unable to create main window"); - ShowWindow(hWnd, SW_SHOWNORMAL); // nCmdShow used only in beta: ShowWindow(hWnd, nCmdShow) - UpdateWindow(hWnd); - init_await_mom_parent_exit(); - dx_init(hWnd); - BlackPalette(); - snd_init(hWnd); - init_archives(); - init_disable_screensaver(TRUE); + if (diabdat_mpq) { + SFileCloseArchive(diabdat_mpq); + diabdat_mpq = NULL; + } + if (patch_rt_mpq) { + SFileCloseArchive(patch_rt_mpq); + patch_rt_mpq = NULL; + } + if (hellfire_mpq) { + SFileCloseArchive(hellfire_mpq); + hellfire_mpq = NULL; + } +#ifdef HELLFIRE + if (hfmonk_mpq) { + SFileCloseArchive(hfmonk_mpq); + hfmonk_mpq = NULL; + } + if (hfbard_mpq) { + SFileCloseArchive(hfbard_mpq); + hfbard_mpq = NULL; + } + if (hfbarb_mpq) { + SFileCloseArchive(hfbarb_mpq); + hfbarb_mpq = NULL; + } + if (hfmusic_mpq) { + SFileCloseArchive(hfmusic_mpq); + hfmusic_mpq = NULL; + } + if (hfvoice_mpq) { + SFileCloseArchive(hfvoice_mpq); + hfvoice_mpq = NULL; + } + if (hfopt1_mpq) { + SFileCloseArchive(hfopt1_mpq); + hfopt1_mpq = NULL; + } + if (hfopt2_mpq) { + SFileCloseArchive(hfopt2_mpq); + hfopt2_mpq = NULL; + } +#endif + UiDestroy(); + effects_cleanup_sfx(); + sound_cleanup(); + NetClose(); + dx_cleanup(); + engine_debug_trap(show_cursor); + StormDestroy(); + + if (show_cursor) + ShowCursor(TRUE); } -void init_kill_mom_parent() +static void init_strip_trailing_slash(char *path) { - HWND handle; + char *result; - handle = init_find_mom_parent(); - if (handle != NULL) { - PostMessage(handle, WM_CLOSE, 0, 0); - killed_mom_parent = TRUE; + result = strrchr(path, '\\'); + if (result) { + if (!result[1]) + *result = 0; + } +} + +static BOOL init_read_test_file(char *pszPath, const char *pszArchive, int dwPriority, HANDLE *phArchive) +{ + DWORD dwSize; + char *pszDrive, *pszRoot; + char szDrive[MAX_PATH]; + + dwSize = GetLogicalDriveStrings(sizeof(szDrive), szDrive); + if (dwSize == 0 || dwSize > sizeof(szDrive)) { + return FALSE; + } + + while (*pszArchive == '\\') { + pszArchive++; + } + + pszDrive = szDrive; + if (*pszDrive == '\0') { + return FALSE; + } + while (1) { + pszRoot = pszDrive; + while (*pszDrive++ != '\0') + ; + if (GetDriveType(pszRoot) == DRIVE_CDROM) { + strcpy(pszPath, pszRoot); + strcat(pszPath, pszArchive); + if (SFileOpenArchive(pszPath, dwPriority, FS_CD, phArchive)) { + break; + } + } + if (*pszDrive == '\0') { + return FALSE; + } } + + return TRUE; } -HWND init_find_mom_parent() +static HANDLE init_test_access(char *mpq_path, const char *mpq_name, const char *reg_loc, int dwPriority, int fs) { - HWND i, handle; - char ClassName[256]; + char *last_slash_pos; + char Filename[MAX_PATH]; + char Buffer[MAX_PATH]; + char archive_path[MAX_PATH]; + HANDLE archive; - for (i = GetForegroundWindow();; i = GetWindow(handle, GW_HWNDNEXT)) { - handle = i; - if (!i) - break; - GetClassName(i, ClassName, 255); - if (!_strcmpi(ClassName, "MOM Parent")) - break; + if (!GetCurrentDirectory(sizeof(Buffer), Buffer)) + app_fatal("Can't get program path"); + init_strip_trailing_slash(Buffer); + if (!SFileSetBasePath(Buffer)) + app_fatal("SFileSetBasePath"); + if (!GetModuleFileName(ghInst, Filename, sizeof(Filename))) + app_fatal("Can't get program name"); + last_slash_pos = strrchr(Filename, '\\'); + if (last_slash_pos) + *last_slash_pos = '\0'; + init_strip_trailing_slash(Filename); + strcpy(mpq_path, Buffer); + strcat(mpq_path, mpq_name); + if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) + return archive; + if (strcmp(Filename, Buffer)) { + strcpy(mpq_path, Filename); + strcat(mpq_path, mpq_name); + if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) + return archive; } - return handle; + archive_path[0] = '\0'; + if (reg_loc) { + if (SRegLoadString("Archives", reg_loc, 0, archive_path, sizeof(archive_path))) { + init_strip_trailing_slash(archive_path); + strcpy(mpq_path, archive_path); + strcat(mpq_path, mpq_name); + if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) + return archive; + } + } + if (fs != FS_PC && init_read_test_file(archive_path, mpq_name, dwPriority, &archive)) { + strcpy(mpq_path, archive_path); + return archive; + } + return NULL; } -void init_await_mom_parent_exit() +static void init_get_file_info() { - DWORD tick; + DWORD dwLen; + void *pBlock; + unsigned int uBytes; + DWORD dwHandle; + VS_FIXEDFILEINFO *lpBuffer; - tick = GetTickCount(); - if (!init_find_mom_parent()) { - return; + if (GetModuleFileName(ghInst, diablo_exe_path, sizeof(diablo_exe_path))) { + dwLen = GetFileVersionInfoSize(diablo_exe_path, &dwHandle); + if (dwLen) { + pBlock = DiabloAllocPtr(dwLen); + if (GetFileVersionInfo(diablo_exe_path, 0, dwLen, pBlock)) { + if (VerQueryValue(pBlock, "\\", (LPVOID *)&lpBuffer, &uBytes)) + sprintf( + gszVersionNumber, + "version %d.%d.%d.%d", + lpBuffer->dwProductVersionMS >> 16, + lpBuffer->dwProductVersionMS & 0xFFFF, + lpBuffer->dwProductVersionLS >> 16, + lpBuffer->dwProductVersionLS & 0xFFFF); + } + mem_free_dbg(pBlock); + } } - do { - Sleep(250); - } while (GetTickCount() - tick <= 4000 && init_find_mom_parent() != NULL); } -void init_archives() +static void init_archives() { HANDLE fh; #ifdef COPYPROT @@ -339,125 +419,66 @@ void init_archives() #endif } -HANDLE init_test_access(char *mpq_path, const char *mpq_name, const char *reg_loc, int dwPriority, int fs) -{ - char *last_slash_pos; - char Filename[MAX_PATH]; - char Buffer[MAX_PATH]; - char archive_path[MAX_PATH]; - HANDLE archive; - - if (!GetCurrentDirectory(sizeof(Buffer), Buffer)) - app_fatal("Can't get program path"); - init_strip_trailing_slash(Buffer); - if (!SFileSetBasePath(Buffer)) - app_fatal("SFileSetBasePath"); - if (!GetModuleFileName(ghInst, Filename, sizeof(Filename))) - app_fatal("Can't get program name"); - last_slash_pos = strrchr(Filename, '\\'); - if (last_slash_pos) - *last_slash_pos = '\0'; - init_strip_trailing_slash(Filename); - strcpy(mpq_path, Buffer); - strcat(mpq_path, mpq_name); - if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) - return archive; - if (strcmp(Filename, Buffer)) { - strcpy(mpq_path, Filename); - strcat(mpq_path, mpq_name); - if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) - return archive; - } - archive_path[0] = '\0'; - if (reg_loc) { - if (SRegLoadString("Archives", reg_loc, 0, archive_path, sizeof(archive_path))) { - init_strip_trailing_slash(archive_path); - strcpy(mpq_path, archive_path); - strcat(mpq_path, mpq_name); - if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) - return archive; - } - } - if (fs != FS_PC && init_read_test_file(archive_path, mpq_name, dwPriority, &archive)) { - strcpy(mpq_path, archive_path); - return archive; - } - return NULL; -} - -void init_strip_trailing_slash(char *path) +void init_create_window(int nCmdShow) { - char *result; + int nWidth, nHeight; + HWND hWnd; + WNDCLASSEXA wcex; - result = strrchr(path, '\\'); - if (result) { - if (!result[1]) - *result = 0; - } + init_kill_mom_parent(); + pfile_init_save_directory(); + memset(&wcex, 0, sizeof(wcex)); + wcex.cbSize = sizeof(wcex); + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = WindowProc; + wcex.hInstance = ghInst; + wcex.hIcon = LoadIcon(ghInst, MAKEINTRESOURCE(IDI_ICON1)); + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); + wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wcex.lpszMenuName = GAME_NAME; + wcex.lpszClassName = "DIABLO"; + wcex.hIconSm = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); + if (!RegisterClassEx(&wcex)) + app_fatal("Unable to register window class"); + if (GetSystemMetrics(SM_CXSCREEN) < SCREEN_WIDTH) + nWidth = SCREEN_WIDTH; + else + nWidth = GetSystemMetrics(SM_CXSCREEN); + if (GetSystemMetrics(SM_CYSCREEN) < SCREEN_HEIGHT) + nHeight = SCREEN_HEIGHT; + else + nHeight = GetSystemMetrics(SM_CYSCREEN); + hWnd = CreateWindowEx(0, "DIABLO", GAME_NAME, WS_POPUP, 0, 0, nWidth, nHeight, NULL, NULL, ghInst, NULL); + if (!hWnd) + app_fatal("Unable to create main window"); + ShowWindow(hWnd, SW_SHOWNORMAL); // nCmdShow used only in beta: ShowWindow(hWnd, nCmdShow) + UpdateWindow(hWnd); + init_await_mom_parent_exit(); + dx_init(hWnd); + BlackPalette(); + snd_init(hWnd); + init_archives(); + init_disable_screensaver(TRUE); } -BOOL init_read_test_file(char *pszPath, const char *pszArchive, int dwPriority, HANDLE *phArchive) +static void init_activate_window(HWND hWnd, BOOL bActive) { - DWORD dwSize; - char *pszDrive, *pszRoot; - char szDrive[MAX_PATH]; - - dwSize = GetLogicalDriveStrings(sizeof(szDrive), szDrive); - if (dwSize == 0 || dwSize > sizeof(szDrive)) { - return FALSE; - } - - while (*pszArchive == '\\') { - pszArchive++; - } + LONG dwNewLong; - pszDrive = szDrive; - if (*pszDrive == '\0') { - return FALSE; - } - while (1) { - pszRoot = pszDrive; - while (*pszDrive++ != '\0') - ; - if (GetDriveType(pszRoot) == DRIVE_CDROM) { - strcpy(pszPath, pszRoot); - strcat(pszPath, pszArchive); - if (SFileOpenArchive(pszPath, dwPriority, FS_CD, phArchive)) { - break; - } - } - if (*pszDrive == '\0') { - return FALSE; - } - } + gbActive = bActive; + UiAppActivate(bActive); + dwNewLong = GetWindowLong(hWnd, GWL_STYLE); - return TRUE; -} + if (gbActive && fullscreen) + dwNewLong &= ~WS_SYSMENU; + else + dwNewLong |= WS_SYSMENU; -void init_get_file_info() -{ - DWORD dwLen; - void *pBlock; - unsigned int uBytes; - DWORD dwHandle; - VS_FIXEDFILEINFO *lpBuffer; + SetWindowLong(hWnd, GWL_STYLE, dwNewLong); - if (GetModuleFileName(ghInst, diablo_exe_path, sizeof(diablo_exe_path))) { - dwLen = GetFileVersionInfoSize(diablo_exe_path, &dwHandle); - if (dwLen) { - pBlock = DiabloAllocPtr(dwLen); - if (GetFileVersionInfo(diablo_exe_path, 0, dwLen, pBlock)) { - if (VerQueryValue(pBlock, "\\", (LPVOID *)&lpBuffer, &uBytes)) - sprintf( - gszVersionNumber, - "version %d.%d.%d.%d", - lpBuffer->dwProductVersionMS >> 16, - lpBuffer->dwProductVersionMS & 0xFFFF, - lpBuffer->dwProductVersionLS >> 16, - lpBuffer->dwProductVersionLS & 0xFFFF); - } - mem_free_dbg(pBlock); - } + if (gbActive) { + force_redraw = 255; + ResetPal(); } } @@ -503,27 +524,6 @@ LRESULT __stdcall MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) return DefWindowProc(hWnd, Msg, wParam, lParam); } -void init_activate_window(HWND hWnd, BOOL bActive) -{ - LONG dwNewLong; - - gbActive = bActive; - UiAppActivate(bActive); - dwNewLong = GetWindowLong(hWnd, GWL_STYLE); - - if (gbActive && fullscreen) - dwNewLong &= ~WS_SYSMENU; - else - dwNewLong |= WS_SYSMENU; - - SetWindowLong(hWnd, GWL_STYLE, dwNewLong); - - if (gbActive) { - force_redraw = 255; - ResetPal(); - } -} - LRESULT __stdcall WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { if (CurrentProc) diff --git a/Source/init.h b/Source/init.h index 70babe5b6..898772ecf 100644 --- a/Source/init.h +++ b/Source/init.h @@ -8,53 +8,19 @@ extern _SNETVERSIONDATA fileinfo; extern int gbActive; -extern char diablo_exe_path[MAX_PATH]; -extern HANDLE hellfire_mpq; -extern char patch_rt_mpq_path[MAX_PATH]; extern WNDPROC CurrentProc; extern HANDLE diabdat_mpq; -extern char diabdat_mpq_path[MAX_PATH]; -extern HANDLE patch_rt_mpq; -extern BOOL killed_mom_parent; -extern BOOLEAN screensaver_enabled_prev; #ifdef HELLFIRE -extern char hellfire_mpq_path[MAX_PATH]; -extern char hfmonk_mpq_path[MAX_PATH]; -extern char hfbard_mpq_path[MAX_PATH]; -extern char hfbarb_mpq_path[MAX_PATH]; -extern char hfmusic_mpq_path[MAX_PATH]; -extern char hfvoice_mpq_path[MAX_PATH]; -extern char hfopt1_mpq_path[MAX_PATH]; -extern char hfopt2_mpq_path[MAX_PATH]; -extern HANDLE hfmonk_mpq; extern HANDLE hfbard_mpq; extern HANDLE hfbarb_mpq; -extern HANDLE hfmusic_mpq; -extern HANDLE hfvoice_mpq; -extern HANDLE hfopt1_mpq; -extern HANDLE hfopt2_mpq; #endif void init_cleanup(BOOL show_cursor); -void init_run_office_from_start_menu(); -void init_run_office(char *dir); -void init_disable_screensaver(BOOLEAN disable); void init_create_window(int nCmdShow); -void init_kill_mom_parent(); -HWND init_find_mom_parent(); -void init_await_mom_parent_exit(); -void init_archives(); -HANDLE init_test_access(char *mpq_path, const char *mpq_name, const char *reg_loc, int flags, int fs); -void init_strip_trailing_slash(char *path); -BOOL init_read_test_file(char *pszPath, const char *pszArchive, int flags, HANDLE *phArchive); -void init_get_file_info(); LRESULT __stdcall MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); -void init_activate_window(HWND hWnd, BOOL bActive); LRESULT __stdcall WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); WNDPROC SetWindowProc(WNDPROC NewProc); -/* rdata */ - /* data */ extern char gszVersionNumber[MAX_PATH];