|
| 1 | +/* |
| 2 | +=========================================================================== |
| 3 | +Copyright (C) 2025 Tim Angus (tim@ngus.net) |
| 4 | +
|
| 5 | +This file is part of Quake III Arena source code. |
| 6 | +
|
| 7 | +Quake III Arena source code is free software; you can redistribute it |
| 8 | +and/or modify it under the terms of the GNU General Public License as |
| 9 | +published by the Free Software Foundation; either version 2 of the License, |
| 10 | +or (at your option) any later version. |
| 11 | +
|
| 12 | +Quake III Arena source code is distributed in the hope that it will be |
| 13 | +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +GNU General Public License for more details. |
| 16 | +
|
| 17 | +You should have received a copy of the GNU General Public License |
| 18 | +along with Quake III Arena source code; if not, write to the Free Software |
| 19 | +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +=========================================================================== |
| 21 | +*/ |
| 22 | + |
| 23 | +#ifdef USE_HTTP |
| 24 | + |
| 25 | +#include "client.h" |
| 26 | + |
| 27 | +#include <windows.h> |
| 28 | +#include <wininet.h> |
| 29 | + |
| 30 | +static HINTERNET hInternet = NULL; |
| 31 | +static HINTERNET hUrl = NULL; |
| 32 | + |
| 33 | +static void DropIf(qboolean condition, const char *fmt, ...) |
| 34 | +{ |
| 35 | + char buffer[1024]; |
| 36 | + |
| 37 | + if (!condition) |
| 38 | + return; |
| 39 | + |
| 40 | + va_list argptr; |
| 41 | + va_start(argptr, fmt); |
| 42 | + Q_vsnprintf(buffer, sizeof(buffer), fmt, argptr); |
| 43 | + va_end(argptr); |
| 44 | + |
| 45 | + Com_Error(ERR_DROP, "Download Error: %s URL: %s", buffer, clc.downloadURL); |
| 46 | +} |
| 47 | + |
| 48 | +/* |
| 49 | +================= |
| 50 | +CL_HTTP_Init |
| 51 | +================= |
| 52 | +*/ |
| 53 | +qboolean CL_HTTP_Init() |
| 54 | +{ |
| 55 | + OSVERSIONINFO osvi = {sizeof(OSVERSIONINFO)}; |
| 56 | + const char *windowsVersion = GetVersionEx(&osvi) ? va("Windows %lu.%lu (build %lu)", |
| 57 | + osvi.dwMajorVersion, |
| 58 | + osvi.dwMinorVersion, |
| 59 | + osvi.dwBuildNumber) |
| 60 | + : "Windows"; |
| 61 | + |
| 62 | + hInternet = InternetOpenA( |
| 63 | + va("%s %s", Q3_VERSION, windowsVersion), |
| 64 | + INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); |
| 65 | + |
| 66 | + return hInternet != NULL; |
| 67 | +} |
| 68 | + |
| 69 | +/* |
| 70 | +================= |
| 71 | +CL_HTTP_Available |
| 72 | +================= |
| 73 | +*/ |
| 74 | +qboolean CL_HTTP_Available() |
| 75 | +{ |
| 76 | + return hInternet != NULL; |
| 77 | +} |
| 78 | + |
| 79 | +/* |
| 80 | +================= |
| 81 | +CL_HTTP_Shutdown |
| 82 | +================= |
| 83 | +*/ |
| 84 | +void CL_HTTP_Shutdown(void) |
| 85 | +{ |
| 86 | + if (hInternet) |
| 87 | + { |
| 88 | + InternetCloseHandle(hInternet); |
| 89 | + hInternet = NULL; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/* |
| 94 | +================= |
| 95 | +CL_HTTP_BeginDownload |
| 96 | +================= |
| 97 | +*/ |
| 98 | +void CL_HTTP_BeginDownload(const char *remoteURL) |
| 99 | +{ |
| 100 | + DWORD httpCode = 0; |
| 101 | + DWORD contentLength = 0; |
| 102 | + DWORD len = sizeof(httpCode); |
| 103 | + DWORD zero = 0; |
| 104 | + BOOL success; |
| 105 | + |
| 106 | + hUrl = InternetOpenUrlA(hInternet, remoteURL, NULL, 0, |
| 107 | + INTERNET_FLAG_HYPERLINK | |
| 108 | + INTERNET_FLAG_NO_CACHE_WRITE | |
| 109 | + INTERNET_FLAG_NO_COOKIES | |
| 110 | + INTERNET_FLAG_NO_UI | |
| 111 | + INTERNET_FLAG_RESYNCHRONIZE | |
| 112 | + INTERNET_FLAG_RELOAD, |
| 113 | + 0); |
| 114 | + |
| 115 | + DropIf(hUrl == NULL, "InternetOpenUrlA failed %lu", GetLastError()); |
| 116 | + |
| 117 | + success = HttpQueryInfo(hUrl, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &httpCode, &len, &zero); |
| 118 | + DropIf(!success, "Get HTTP_QUERY_STATUS_CODE failed %lu", GetLastError()); |
| 119 | + |
| 120 | + DropIf(httpCode >= 400, "HTTP code %lu", httpCode); |
| 121 | + DropIf(httpCode != 200, "Unhandled HTTP code %lu", httpCode); |
| 122 | + |
| 123 | + success = HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &contentLength, &len, &zero); |
| 124 | + DropIf(!success, "Get HTTP_QUERY_CONTENT_LENGTH failed %lu", GetLastError()); |
| 125 | + |
| 126 | + clc.downloadSize = (int)contentLength; |
| 127 | + Cvar_SetValue("cl_downloadSize", clc.downloadSize); |
| 128 | +} |
| 129 | + |
| 130 | +/* |
| 131 | +================= |
| 132 | +CL_HTTP_PerformDownload |
| 133 | +================= |
| 134 | +*/ |
| 135 | +qboolean CL_HTTP_PerformDownload(void) |
| 136 | +{ |
| 137 | + static BYTE readBuffer[256 * 1024]; |
| 138 | + DWORD bytesRead = 0; |
| 139 | + BOOL success; |
| 140 | + |
| 141 | + DropIf(hUrl == NULL, "hUrl is NULL"); |
| 142 | + |
| 143 | + success = InternetReadFile(hUrl, readBuffer, sizeof(readBuffer), &bytesRead); |
| 144 | + DropIf(!success, "InternetReadFile failed %lu", GetLastError()); |
| 145 | + |
| 146 | + if (bytesRead > 0) |
| 147 | + { |
| 148 | + clc.downloadCount += bytesRead; |
| 149 | + Cvar_SetValue("cl_downloadCount", clc.downloadCount); |
| 150 | + |
| 151 | + int bytesWritten = FS_Write(readBuffer, bytesRead, clc.download); |
| 152 | + DropIf(bytesWritten != bytesRead, "bytesWritten != bytesRead"); |
| 153 | + |
| 154 | + return qfalse; |
| 155 | + } |
| 156 | + |
| 157 | + InternetCloseHandle(hUrl); |
| 158 | + return qtrue; |
| 159 | +} |
| 160 | + |
| 161 | +#endif /* USE_HTTP */ |
0 commit comments