예제 #1
0
 public List <string> GetRemovedFiles(UpdateFile previousVersion)
 {
     return
         (previousVersion.Files.Where(f => !Files.Exists(x => x.SystemPath.Equals(f.SystemPath, StringComparison.CurrentCultureIgnoreCase)))
          .Select(f => f.SystemPath)
          .ToList());
 }
예제 #2
0
        public List <UpdateFileInfo> GetChangedFiles(UpdateFile previousVersion)
        {
            var changedFiles = new List <UpdateFileInfo>();

            foreach (var updateFileInfo in Files)
            {
                var file = previousVersion.Files.Find(uf => uf.SystemPath == updateFileInfo.SystemPath);

                if (file != null && file.Md5Hash == updateFileInfo.Md5Hash)
                {
                    continue;
                }

                changedFiles.Add(updateFileInfo);
            }

            return(changedFiles);
        }
예제 #3
0
        private new void Update()
        {
            try
            {
                Status("Load update info...");
                // get update index
                var req = WebRequest.Create("http://update.utopiarealms.com/index.update");

                using (var response = req.GetResponse())
                    using (var stream = response.GetResponseStream())
                        using (var zs = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            var reader = new BinaryReader(zs);
                            _updateFile = new UpdateFile();
                            _updateFile.Load(reader);
                        }

                var indexPath = Path.Combine(_basePath, "update.index");

                UpdateFile previousUpdate = null;

                if (File.Exists(indexPath))
                {
                    using (var fs = File.OpenRead(indexPath))
                        using (var zs = new GZipStream(fs, CompressionMode.Decompress))
                        {
                            var reader = new BinaryReader(zs);
                            previousUpdate = new UpdateFile();
                            previousUpdate.Load(reader);
                        }
                }

                Status("Check files to update...");

                var files = new List <UpdateFileInfo>();

                foreach (var file in _updateFile.Files)
                {
                    var filePath = Path.Combine(_basePath, file.SystemPath);

                    if (File.Exists(filePath))
                    {
                        using (var fs = File.OpenRead(filePath))
                        {
                            var hash = Md5Hash.Calculate(fs);
                            if (hash.ToString() == file.Md5Hash && fs.Length == file.Length)
                            {
                                continue;
                            }
                        }
                    }

                    files.Add(file);
                }

                float index = 0f;
                foreach (var file in files)
                {
                    Status("Updating " + file.SystemPath, index / files.Count);
                    try
                    {
                        var filePath = Path.Combine(_basePath, file.SystemPath);
                        if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        }

                        var fileReq = WebRequest.Create(file.DownloadUri);

                        using (var response = fileReq.GetResponse())
                            using (var fs = File.OpenWrite(filePath))
                            {
                                fs.SetLength(0);
                                using (var stream = response.GetResponseStream())
                                {
                                    if (file.Compressed)
                                    {
                                        using (var zip = new GZipStream(stream, CompressionMode.Decompress))
                                        {
                                            zip.CopyTo(fs);
                                        }
                                    }
                                    else
                                    {
                                        stream.CopyTo(fs);
                                    }
                                }
                            }
                    }
                    catch (Exception x)
                    {
                        MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Application.Exit();
                    }

                    index++;
                }

                // delete removed files
                if (previousUpdate != null)
                {
                    foreach (var removedFile in _updateFile.GetRemovedFiles(previousUpdate))
                    {
                        var fullPath = Path.Combine(_basePath, removedFile);
                        if (File.Exists(fullPath))
                        {
                            File.Delete(fullPath);
                        }
                    }
                }

                if (File.Exists(indexPath))
                {
                    File.Delete(indexPath);
                }

                // save index for future update
                using (var fs = File.OpenWrite(indexPath))
                    using (var zs = new GZipStream(fs, CompressionMode.Compress))
                    {
                        var writer = new BinaryWriter(zs);
                        _updateFile.Save(writer);
                    }

                // save current token
                File.WriteAllText(Path.Combine(_basePath, "update.token"), _updateFile.UpdateToken);

                StartGame();
            }
            catch (Exception x)
            {
                MessageBox.Show(string.Format("Exception occured: {0}\nPlease post this information to the forum at http://utopiarealms.com", x.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }