Exemple #1
0
 void Work()
 {
     while (true)
     {
         if (_result == false)
         {
             return;
         }
         DownloadFileInfo task = null;
         lock (_locker)
             if (_files.Count > 0)
             {
                 task = _files.Dequeue();
                 if (task == null)
                 {
                     EnqueueTask(null); // Requeue for other thread to close.
                     return;
                 }
             }
         if (task != null)
         {
             if (!DownloadFile(task.FileSource, task.FileDestination, task.FileHash))
             {
                 SetError();
             }
         }
         else
         {
             _wh.WaitOne();
         }
     }
 }
        public bool Install(VersionInfo build, string targetDirectory)
        {
            var filesinfo = GetVersionInfo(build);

            Log("Install : Build " + build.Build + " to : " + targetDirectory, true);
            DownloadFile(_repository + "/Download/" + _platform + "/" + build.Build + "/Install", "Install");
            bool res;

            using (var q = new ThreadedDownlads(Threads, _tempDirectory, false))
            {
                foreach (var files in filesinfo.FileInfos)
                {
                    var info = new DownloadFileInfo
                    {
                        FileSource =
                            _platformUrl + @"/" + build.Build + @"/Files/" + files.FileHash + ".dat",
                        FileDestination = targetDirectory + files.FilePath,
                        FileHash        = files.FileHash
                    };
                    q.EnqueueTask(info);
                }
                res = q.WaitEnd();
            }
            return(res);
        }
Exemple #3
0
 public void EnqueueTask(DownloadFileInfo task)
 {
     lock (_locker) _files.Enqueue(task);
     if (_wh != null)
     {
         _wh.Set();
     }
 }
Exemple #4
0
        private bool DownloadFile(string file, string destination, string hash)
        {
            var inCache  = false;
            var copyFile = new DownloadFileInfo();

            if (!String.IsNullOrEmpty(hash) && File.Exists(destination))
            {
                if (FileHash.CheckFileHashString(destination, hash))
                {
                    Logger.Instance().Log("ThreadDl" + Thread.CurrentThread.GetHashCode(), "Destination file already correct - " + destination);
                    return(true);
                }
                copyFile.FileHash = _backup ? "1" : "0";
            }
            file = file.Replace('\\', '/');
            var index    = file.LastIndexOf('/');
            var filename = file;

            if (index > -1)
            {
                filename = file.Substring(index);
            }
            var cont = true;

            for (var i = 0; i < 5; i++)
            {
                lock (_locker)
                {
                    if (_currents.Count > 0 && _currents.Contains(file))
                    {
                        cont = false;
                    }
                    else
                    {
                        cont = true;
                    }
                }
                if (cont)
                {
                    break;
                }
                Thread.Sleep(1000);
            }
            if (!cont)
            {
                Logger.Instance().Log("ThreadDl" + Thread.CurrentThread.GetHashCode(), "Source file downloaded by other thread but not released in 5 sec - " + file, true);
                return(false);
            }
            lock (_locker)
            {
                _currents.Add(file);
            }
            if (File.Exists(_tempdir + @"\" + filename))
            {
                if (!String.IsNullOrEmpty(hash))
                {
                    if (FileHash.CheckFileHashString(_tempdir + @"\" + filename, hash))
                    {
                        inCache = true;
                    }
                    else
                    {
                        File.Delete(_tempdir + @"\" + filename);
                    }
                }
                else
                {
                    File.Delete(_tempdir + @"\" + filename);
                }
            }
            try
            {
                if (!inCache)
                {
                    using (var client = new WebClient())
                    {
                        client.DownloadFile(file, _tempdir + @"\" + filename);
                    }
                }

                if (!String.IsNullOrEmpty(hash))
                {
                    var result = FileHash.CheckFileHashString(_tempdir + @"\" + filename, hash);
                    if (result)
                    {
                        Logger.Instance().Log("ThreadDl" + Thread.CurrentThread.GetHashCode(), "File downloaded - " + file);
                    }
                    else
                    {
                        Logger.Instance().Log("ThreadDl" + Thread.CurrentThread.GetHashCode(), "Error (Hash) - " + file, true);
                    }

                    lock (_locker)
                    {
                        if (result)
                        {
                            copyFile.FileSource      = _tempdir + @"\" + filename;
                            copyFile.FileDestination = destination;
                            _copies.Enqueue(copyFile);
                        }
                        _currents.Remove(file);
                    }
                    return(result);
                }
            }
            catch (Exception e)
            {
                Logger.Instance().Log("ThreadDl" + Thread.CurrentThread.GetHashCode(), "Error (" + e.Message + ") - " + file, true);
                lock (_locker)
                {
                    _currents.Remove(file);
                }
                return(false);
            }
            Logger.Instance().Log("ThreadDl" + Thread.CurrentThread.GetHashCode(), "File downloaded - " + filename);
            lock (_locker)
            {
                copyFile.FileSource      = _tempdir + @"\" + filename;
                copyFile.FileDestination = destination;
                _copies.Enqueue(copyFile);
                _currents.Remove(file);
            }
            return(true);
        }
        public bool Update(string directory, VersionInfo build)
        {
            if (!File.Exists(directory + @"\Yatse2.exe"))
            {
                return(false);
            }

            Tools.FindAndKillProcess("Yatse2");

            var version = Tools.GetFileRevision(directory + @"\Yatse2.exe");

            if (version == build.Build)
            {
                return(false);
            }

            var filesinfo = GetVersionInfo(new VersionInfo {
                Beta = true, Build = version
            });

            if (filesinfo != null)
            {
                Log("Update : Checking current install against web build : " + version);
                foreach (var files in filesinfo.FileInfos)
                {
                    if (!File.Exists(directory + files.FilePath))
                    {
                        continue;
                    }
                    if (FileHash.CheckFileHashString(directory + files.FilePath, files.FileHash))
                    {
                        continue;
                    }
                    if (File.Exists(directory + files.FilePath + ".bck"))
                    {
                        File.Delete(directory + files.FilePath + ".bck");
                    }
                    File.Move(directory + files.FilePath, directory + files.FilePath + ".bck");
                    Log("Update : Backup modified file : " + files.FilePath, true);
                }
            }
            else
            {
                Log("Update : Current build no more on server : " + version, true);
            }

            DownloadFile(_repository + "/Download/" + _platform + "/" + build.Build + "/Update", "Update");
            Log("Update : Updating to build : " + build.Build, true);
            filesinfo = GetVersionInfo(build);
            if (filesinfo != null)
            {
                bool res;
                using (var q = new ThreadedDownlads(Threads, _tempDirectory, false))
                {
                    foreach (var files in filesinfo.FileInfos)
                    {
                        var info = new DownloadFileInfo
                        {
                            FileSource =
                                _platformUrl + @"/" + build.Build + @"/Files/" + files.FileHash + ".dat",
                            FileDestination = directory + files.FilePath,
                            FileHash        = files.FileHash
                        };
                        q.EnqueueTask(info);
                    }
                    res = q.WaitEnd();
                }
                return(res);
            }

            Log("Update : New build no more on server : " + build.Build, true);
            return(false);
        }