Exemple #1
0
        /**
         * CL_CheckOrDownloadFile returns true if the file exists,
         * otherwise it attempts to start a
         * download from the server.
         */
        public static bool CheckOrDownloadFile(string filename)
        {
            if (filename.IndexOf("..") != -1)
            {
                Com.Printf("Refusing to download a path with ..\n");

                return(true);
            }

            if (FS.FileLength(filename) > 0)
            {
                // it exists, no need to download
                return(true);
            }

            Globals.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
            Globals.cls.downloadtempname  = Com.StripExtension(Globals.cls.downloadname);
            Globals.cls.downloadtempname += ".tmp";

            //	  ZOID
            // check to see if we already have a tmp for this file, if so, try to
            // resume
            // open the file if not opened yet
            var    name = CL_parse.DownloadFileName(Globals.cls.downloadtempname);
            Stream fp   = File.OpenWrite(name);

            if (fp != null)
            {
                // it exists
                long len = 0;

                try
                {
                    len = fp.Length;
                }
                catch (IOException)
                {
                }

                Globals.cls.download = fp;

                // give the server an offset to start the download
                Com.Printf("Resuming " + Globals.cls.downloadname + "\n");
                MSG.WriteByte(Globals.cls.netchan.message, Defines.clc_stringcmd);
                MSG.WriteString(Globals.cls.netchan.message, "download " + Globals.cls.downloadname + " " + len);
            }
            else
            {
                Com.Printf("Downloading " + Globals.cls.downloadname + "\n");
                MSG.WriteByte(Globals.cls.netchan.message, Defines.clc_stringcmd);
                MSG.WriteString(Globals.cls.netchan.message, "download " + Globals.cls.downloadname);
            }

            Globals.cls.downloadnumber++;

            return(false);
        }
Exemple #2
0
        /*
         * ===================== CL_ParseDownload
         *
         * A download message has been received from the server
         * =====================
         */
        public static void ParseDownload()
        {
            // read the data
            int size    = MSG.ReadShort(Globals.net_message);
            var percent = MSG.ReadByte(Globals.net_message);

            if (size == -1)
            {
                Com.Printf("Server does not have this file.\n");

                if (Globals.cls.download != null)
                {
                    // if here, we tried to resume a file but the server said no
                    try
                    {
                        Globals.cls.download.Close();
                    }
                    catch (IOException)
                    {
                    }

                    Globals.cls.download = null;
                }

                Cl.RequestNextDownload();

                return;
            }

            // open the file if not opened yet
            if (Globals.cls.download == null)
            {
                var name = CL_parse.DownloadFileName(Globals.cls.downloadtempname).ToLower();
                FS.CreatePath(name);
                Globals.cls.download = File.OpenWrite(name);

                if (Globals.cls.download == null)
                {
                    Globals.net_message.readcount += size;
                    Com.Printf("Failed to open " + Globals.cls.downloadtempname + "\n");
                    Cl.RequestNextDownload();

                    return;
                }
            }

            try
            {
                Globals.cls.download.Write(Globals.net_message.data, Globals.net_message.readcount, size);
            }
            catch (Exception)
            {
            }

            Globals.net_message.readcount += size;

            if (percent != 100)
            {
                // request next block
                //	   change display routines by zoid
                Globals.cls.downloadpercent = percent;
                MSG.WriteByte(Globals.cls.netchan.message, Defines.clc_stringcmd);
                SZ.Print(Globals.cls.netchan.message, "nextdl");
            }
            else
            {
                try
                {
                    Globals.cls.download.Close();
                }
                catch (IOException)
                {
                }

                // rename the temp file to it's final name
                var oldn = CL_parse.DownloadFileName(Globals.cls.downloadtempname);
                var newn = CL_parse.DownloadFileName(Globals.cls.downloadname);
                File.Move(oldn, newn);
                Globals.cls.download        = null;
                Globals.cls.downloadpercent = 0;

                // get another file if needed
                Cl.RequestNextDownload();
            }
        }