Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public string AddToRepository(string sourceDir, bool beta, string description)
        {
            if (!Configured())
            {
                return("Error : Not configured");
            }

            var build = Tools.GetFileRevision(sourceDir + @"\Yatse2.exe");

            if (build == 0)
            {
                return("Error : Invalid source dir");
            }

            if (Directory.Exists(_platformDir + @"\" + build))
            {
                return("Error : Build allready in repository");
            }

            Directory.CreateDirectory(_platformDir + @"\" + build);
            Directory.CreateDirectory(_platformDir + @"\" + build + @"\Files");

            var fileinfos = new Collection <FileInfo>();
            var files     = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                if (file.Contains(".pdb") || file.Contains(".vshost.") || file.Contains(".application"))
                {
                    continue;
                }
                var fileinfo = new FileInfo
                {
                    FilePath = file.Replace(sourceDir, ""),
                    FileHash = FileHash.GetFileHashString(file)
                };
                if (!File.Exists(_platformDir + @"\" + build + @"\Files\" +
                                 fileinfo.FileHash + ".dat"))
                {
                    File.Copy(file, _platformDir + @"\" + build + @"\Files\" +
                              fileinfo.FileHash + ".dat");
                }
                fileinfos.Add(fileinfo);
            }

            var versionFile = new VersionFile {
                Description = description, FileInfos = fileinfos
            };

            versionFile.Save(_repoDir + @"\xmls\Version_" + _platform + "_" + build + ".xml");

            var liste = new VersionList();

            liste.Load(_repoDir + @"\xmls\Versions_" + _platform + ".xml");
            liste.Version.Add(new VersionInfo {
                Beta = beta, Build = build
            });
            liste.Save(_repoDir + @"\xmls\Versions_" + _platform + ".xml");

            return(null);
        }