This is native Win32, so no DLL is required.
Don't get me wrong, I would prefer the libcurl solution if somehow both libcurl and zlib1 could be compiled into the binary without doing something crazy.
This function could easily be modified to replace the Curl-based download solutions (just feed it a callback function like Host_Frame or what not and do that instead of the update messages --- then it isn't synchronous anymore
Example: Sys_Download ("http://www.quaddicted.com/filebase/warpspasm.zip", "warpspasm.zip")
Code: Select all
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
qboolean Sys_Download (const char *strSourceUrl, const char *qdestpath)
{
HINTERNET hINet = NULL;
HINTERNET hFile = NULL;
FILE *fp;
int filesize;
char outfile[MAX_OSPATH];
qboolean success = false;
sprintf (outfile, "%s/%s", com_gamedir, qdestpath);
do
{
// Handle all the "fail" situations
hINet = InternetOpen("Quake", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hINet == NULL)
{
Con_Printf ("Download failed; no internet\n");
goto download_shutdown;
}
hFile = InternetOpenUrl(hINet, strSourceUrl, NULL, 0, INTERNET_FLAG_RELOAD, 0);
if (hFile == NULL)
{
Con_Printf ("Download failed: Unable to open URL: %s\n", strSourceUrl);
goto download_shutdown;
}
// Get size
{
char buffer[64];
DWORD length = sizeof(buffer);
HttpQueryInfo (hFile, HTTP_QUERY_CONTENT_LENGTH, (LPVOID)&buffer, &length, NULL);
filesize = Q_atoi (buffer);
}
// Load information to file.
fp = fopen(outfile, "wb");
if (fp == NULL)
{
Con_Printf ("Download failed: unable to open file for writing '%s'\n", qdestpath);
goto download_shutdown;
}
} while (0);
// Read file.
do
{
char buffer[1024];
DWORD dwRead;
int cumulativeRead = 0;
while (InternetReadFile(hFile, buffer, 1024, &dwRead))
{
// Every 256 KB, do an update message ....
if ( ((cumulativeRead + dwRead) /262144) > (cumulativeRead / 262144) )
{
Con_Printf ("Progress: %i of %i (%3.2f %%) ...\n", cumulativeRead, filesize, ((cumulativeRead / (float)filesize) * 100.0f) );
SCR_UpdateScreen ();
}
if (dwRead == 0)
break;
cumulativeRead += dwRead;
fwrite(buffer, dwRead, 1, fp);
}
fclose(fp);
success = true;
} while (0);
download_shutdown:
if (hFile) InternetCloseHandle(hFile);
if (hINet) InternetCloseHandle(hINet);
return success;
}