Exemple #1
0
        /// <summary>
        /// Check for updates and returns the result.
        /// </summary>
        /// <param name="PatchVersionUrl">Url location of the version file</param>
        public async Task <PatchVersion> CheckForUpdates(string PatchVersionUrl)
        {
            // Set SSL/TLS is correctly being set
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;

            // Create web client to download the patch info
            using (WebClient web = new WebClient())
            {
                // Download, read and convert JSON to class
                PatchVersion patchVersion = JsonDeserializer <PatchVersion>(await web.DownloadStringTaskAsync(PatchVersionUrl));
                patchVersion.IsUpdateAvailable = CurrentVersion < new Version(patchVersion.LastestVersion);
                return(patchVersion);
            }
        }
Exemple #2
0
        /// <summary>
        /// Initialize the pachting files to be downloaded and updated
        /// </summary>
        /// <param name="Patch">Patch to prepare</param>
        /// <param name="SupportOldVersion">Check if your application supports versions that are not found on patch</param>
        public async Task InitializeUpdate(PatchVersion Patch, bool SupportOldVersion = false)
        {
            // Set SSL/TLS is correctly being set
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;

            // Create web client to check patch files
            using (WebClient web = new WebClient())
            {
                // Patch to download
                var patchRequiredUrl = Patch.PatchUrl;
                // Avoid repeating files
                PatchFiles = new Dictionary <string, PatchFile>();
                // Collect all files that will be used for patching
                Patches = new Dictionary <Version, Dictionary <string, PatchFile> >();

                // Create downloading folder and hide it from user
                if (!Directory.Exists(DownloadingPath))
                {
                    DirectoryInfo dirInfo = Directory.CreateDirectory(DownloadingPath);
                    dirInfo.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
                }

                // Start reading web patches
                Version patchVersion = null;
                do
                {
                    // Download, read and convert JSON to class
                    var patchInfo = JsonDeserializer <PatchInfo>(await web.DownloadStringTaskAsync(patchRequiredUrl));

                    // If the version is the same as my current version, stop it
                    patchVersion = new Version(patchInfo.Version);
                    if (patchVersion == CurrentVersion)
                    {
                        break;
                    }

                    // Create version patch if doesn't exist
                    if (!Patches.TryGetValue(patchVersion, out Dictionary <string, PatchFile> patch))
                    {
                        patch = new Dictionary <string, PatchFile>();
                        Patches[patchVersion] = patch;
                    }
                    // Collects non repeated files
                    foreach (string file in patchInfo.Files)
                    {
                        if (!PatchFiles.ContainsKey(file))
                        {
                            // Add file to this patch
                            var patchFile = new PatchFile(file, patchInfo.Host + file, Path.Combine(DownloadingPath, PatchFiles.Count.ToString().PadLeft(8, '0')));
                            patch.Add(file, patchFile);
                            PatchFiles.Add(file, patchFile);
                        }
                    }
                    // Remove it if this patch doesn't have files
                    if (patch.Count == 0)
                    {
                        Patches.Remove(patchVersion);
                    }

                    // Collect files from patch dependencies
                    patchRequiredUrl = patchInfo.PatchRequiredUrl;
                } while (!string.IsNullOrEmpty(patchRequiredUrl));

                // Check if the update version is matching with app
                if (!SupportOldVersion && patchVersion > CurrentVersion)
                {
                    throw new ApplicationException("The version of your application is too old to be updated.");
                }

                // Check if there is cache to synchronize
                LoadCache();

                m_PatchFilesMax   = PatchFiles.Count;
                m_PatchFilesCount = 0;
            }
        }