Den Download-Kram aus cl_parse.c nach cl_download.c

This commit is contained in:
Yamagi Burmeister 2010-06-18 16:34:07 +00:00
parent 974aeeea41
commit d7900e6333
2 changed files with 194 additions and 186 deletions

View File

@ -379,3 +379,189 @@ void CL_RequestNextDownload (void)
MSG_WriteString (&cls.netchan.message, va("begin %i\n", precache_spawncount) ); MSG_WriteString (&cls.netchan.message, va("begin %i\n", precache_spawncount) );
} }
void CL_DownloadFileName(char *dest, int destlen, char *fn) {
if (strncmp(fn, "players", 7) == 0)
Com_sprintf (dest, destlen, "%s/%s", BASEDIRNAME, fn);
else
Com_sprintf (dest, destlen, "%s/%s", FS_Gamedir(), fn);
}
/*
* Returns true if the file exists, otherwise it attempts
* to start a download from the server.
*/
qboolean CL_CheckOrDownloadFile (char *filename) {
FILE *fp;
char name[MAX_OSPATH];
char *ptr;
if (strstr (filename, "..")) {
Com_Printf ("Refusing to download a path with ..\n");
return true;
}
/* fix backslashes - this is mostly für UNIX comaptiblity */
while ((ptr=strchr(filename,'\\'))) {
*ptr = '/';
}
if (FS_LoadFile (filename, NULL) != -1) {
/* it exists, no need to download */
return true;
}
strcpy (cls.downloadname, filename);
/* download to a temp name, and only rename
to the real name when done, so if interrupted
a runt file wont be left */
COM_StripExtension (cls.downloadname, cls.downloadtempname);
strcat (cls.downloadtempname, ".tmp");
/* check to see if we already have a tmp for this file, if so, try to resume
and open the file if not opened yet */
CL_DownloadFileName(name, sizeof(name), cls.downloadtempname);
fp = fopen (name, "r+b");
if (fp) {
/* it exists */
int len;
fseek(fp, 0, SEEK_END);
len = ftell(fp);
cls.download = fp;
/* give the server an offset to start the download */
Com_Printf ("Resuming %s\n", cls.downloadname);
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message,
va("download %s %i", cls.downloadname, len));
} else {
Com_Printf ("Downloading %s\n", cls.downloadname);
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message,
va("download %s", cls.downloadname));
}
cls.downloadnumber++;
return false;
}
/*
* Request a download from the server
*/
void CL_Download_f (void) {
char filename[MAX_OSPATH];
if (Cmd_Argc() != 2) {
Com_Printf("Usage: download <filename>\n");
return;
}
Com_sprintf(filename, sizeof(filename), "%s", Cmd_Argv(1));
if (strstr (filename, "..")) {
Com_Printf ("Refusing to download a path with ..\n");
return;
}
if (FS_LoadFile (filename, NULL) != -1) {
/* it exists, no need to download */
Com_Printf("File already exists.\n");
return;
}
strcpy (cls.downloadname, filename);
Com_Printf ("Downloading %s\n", cls.downloadname);
/* download to a temp name, and only rename
to the real name when done, so if interrupted
a runt file wont be left */
COM_StripExtension (cls.downloadname, cls.downloadtempname);
strcat (cls.downloadtempname, ".tmp");
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message,
va("download %s", cls.downloadname));
cls.downloadnumber++;
}
/*
* A download message has been received from the server
*/
void CL_ParseDownload (void) {
int size, percent;
char name[MAX_OSPATH];
int r;
/* read the data */
size = MSG_ReadShort (&net_message);
percent = MSG_ReadByte (&net_message);
if (size == -1) {
Com_Printf ("Server does not have this file.\n");
if (cls.download) {
/* if here, we tried to resume a
file but the server said no */
fclose (cls.download);
cls.download = NULL;
}
CL_RequestNextDownload ();
return;
}
/* open the file if not opened yet */
if (!cls.download) {
CL_DownloadFileName(name, sizeof(name), cls.downloadtempname);
FS_CreatePath (name);
cls.download = fopen (name, "wb");
if (!cls.download) {
net_message.readcount += size;
Com_Printf ("Failed to open %s\n", cls.downloadtempname);
CL_RequestNextDownload ();
return;
}
}
fwrite (net_message.data + net_message.readcount, 1, size, cls.download);
net_message.readcount += size;
if (percent != 100) {
/* request next block */
cls.downloadpercent = percent;
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
SZ_Print (&cls.netchan.message, "nextdl");
} else {
char oldn[MAX_OSPATH];
char newn[MAX_OSPATH];
fclose (cls.download);
/* rename the temp file to it's final name */
CL_DownloadFileName(oldn, sizeof(oldn), cls.downloadtempname);
CL_DownloadFileName(newn, sizeof(newn), cls.downloadname);
r = rename (oldn, newn);
if (r)
Com_Printf ("failed to rename.\n");
cls.download = NULL;
cls.downloadpercent = 0;
/* get another file if needed */
CL_RequestNextDownload ();
}
}

View File

@ -15,11 +15,19 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 * this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* =======================================================================
* *
* This file implements the entity and network protocol parsing
*
* =======================================================================
*/ */
#include "header/client.h" #include "header/client.h"
void CL_DownloadFileName(char *dest, int destlen, char *fn);
void CL_ParseDownload (void);
int bitcounts[32]; /* just for protocol profiling */ int bitcounts[32]; /* just for protocol profiling */
char *svc_strings[256] = { char *svc_strings[256] = {
@ -48,118 +56,6 @@ char *svc_strings[256] = {
"svc_frame" "svc_frame"
}; };
void CL_DownloadFileName(char *dest, int destlen, char *fn) {
if (strncmp(fn, "players", 7) == 0)
Com_sprintf (dest, destlen, "%s/%s", BASEDIRNAME, fn);
else
Com_sprintf (dest, destlen, "%s/%s", FS_Gamedir(), fn);
}
/*
* Returns true if the file exists, otherwise it attempts
* to start a download from the server.
*/
qboolean CL_CheckOrDownloadFile (char *filename) {
FILE *fp;
char name[MAX_OSPATH];
char *ptr;
if (strstr (filename, "..")) {
Com_Printf ("Refusing to download a path with ..\n");
return true;
}
/* fix backslashes - this is mostly für UNIX comaptiblity */
while ((ptr=strchr(filename,'\\'))) {
*ptr = '/';
}
if (FS_LoadFile (filename, NULL) != -1) {
/* it exists, no need to download */
return true;
}
strcpy (cls.downloadname, filename);
/* download to a temp name, and only rename
to the real name when done, so if interrupted
a runt file wont be left */
COM_StripExtension (cls.downloadname, cls.downloadtempname);
strcat (cls.downloadtempname, ".tmp");
/* check to see if we already have a tmp for this file, if so, try to resume
and open the file if not opened yet */
CL_DownloadFileName(name, sizeof(name), cls.downloadtempname);
fp = fopen (name, "r+b");
if (fp) {
/* it exists */
int len;
fseek(fp, 0, SEEK_END);
len = ftell(fp);
cls.download = fp;
/* give the server an offset to start the download */
Com_Printf ("Resuming %s\n", cls.downloadname);
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message,
va("download %s %i", cls.downloadname, len));
} else {
Com_Printf ("Downloading %s\n", cls.downloadname);
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message,
va("download %s", cls.downloadname));
}
cls.downloadnumber++;
return false;
}
/*
* Request a download from the server
*/
void CL_Download_f (void) {
char filename[MAX_OSPATH];
if (Cmd_Argc() != 2) {
Com_Printf("Usage: download <filename>\n");
return;
}
Com_sprintf(filename, sizeof(filename), "%s", Cmd_Argv(1));
if (strstr (filename, "..")) {
Com_Printf ("Refusing to download a path with ..\n");
return;
}
if (FS_LoadFile (filename, NULL) != -1) {
/* it exists, no need to download */
Com_Printf("File already exists.\n");
return;
}
strcpy (cls.downloadname, filename);
Com_Printf ("Downloading %s\n", cls.downloadname);
/* download to a temp name, and only rename
to the real name when done, so if interrupted
a runt file wont be left */
COM_StripExtension (cls.downloadname, cls.downloadtempname);
strcat (cls.downloadtempname, ".tmp");
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message,
va("download %s", cls.downloadname));
cls.downloadnumber++;
}
void CL_RegisterSounds (void) { void CL_RegisterSounds (void) {
int i; int i;
@ -177,80 +73,6 @@ void CL_RegisterSounds (void) {
S_EndRegistration (); S_EndRegistration ();
} }
/*
* A download message has been received from the server
*/
void CL_ParseDownload (void) {
int size, percent;
char name[MAX_OSPATH];
int r;
/* read the data */
size = MSG_ReadShort (&net_message);
percent = MSG_ReadByte (&net_message);
if (size == -1) {
Com_Printf ("Server does not have this file.\n");
if (cls.download) {
/* if here, we tried to resume a
file but the server said no */
fclose (cls.download);
cls.download = NULL;
}
CL_RequestNextDownload ();
return;
}
/* open the file if not opened yet */
if (!cls.download) {
CL_DownloadFileName(name, sizeof(name), cls.downloadtempname);
FS_CreatePath (name);
cls.download = fopen (name, "wb");
if (!cls.download) {
net_message.readcount += size;
Com_Printf ("Failed to open %s\n", cls.downloadtempname);
CL_RequestNextDownload ();
return;
}
}
fwrite (net_message.data + net_message.readcount, 1, size, cls.download);
net_message.readcount += size;
if (percent != 100) {
/* request next block */
cls.downloadpercent = percent;
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
SZ_Print (&cls.netchan.message, "nextdl");
} else {
char oldn[MAX_OSPATH];
char newn[MAX_OSPATH];
fclose (cls.download);
/* rename the temp file to it's final name */
CL_DownloadFileName(oldn, sizeof(oldn), cls.downloadtempname);
CL_DownloadFileName(newn, sizeof(newn), cls.downloadname);
r = rename (oldn, newn);
if (r)
Com_Printf ("failed to rename.\n");
cls.download = NULL;
cls.downloadpercent = 0;
/* get another file if needed */
CL_RequestNextDownload ();
}
}
int CL_ParseEntityBits (unsigned *bits) { int CL_ParseEntityBits (unsigned *bits) {
unsigned b, total; unsigned b, total;
int i; int i;