Exemplo n.º 1
0
        private static T LoadInternal(string filePath, List <string> fallbackFilePaths = null)
        {
            string typeName = typeof(T).Name;

            if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
            {
                DebugHelper.WriteLine($"{typeName} load started: {filePath}");

                try
                {
                    using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        if (fileStream.Length > 0)
                        {
                            T settings;

                            using (StreamReader streamReader = new StreamReader(fileStream))
                                using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                                {
                                    JsonSerializer serializer = new JsonSerializer();
                                    serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver();
                                    serializer.Converters.Add(new StringEnumConverter());
                                    serializer.DateTimeZoneHandling   = DateTimeZoneHandling.Local;
                                    serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
                                    serializer.Error += Serializer_Error;
                                    settings          = serializer.Deserialize <T>(jsonReader);
                                }

                            if (settings == null)
                            {
                                throw new Exception($"{typeName} object is null.");
                            }

                            DebugHelper.WriteLine($"{typeName} load finished: {filePath}");

                            return(settings);
                        }
                        else
                        {
                            throw new Exception($"{typeName} file stream length is 0.");
                        }
                    }
                }
                catch (Exception e)
                {
                    DebugHelper.WriteException(e, $"{typeName} load failed: {filePath}");
                }
            }
            else
            {
                DebugHelper.WriteLine($"{typeName} file does not exist: {filePath}");
            }

            if (fallbackFilePaths != null && fallbackFilePaths.Count > 0)
            {
                filePath = fallbackFilePaths[0];
                fallbackFilePaths.RemoveAt(0);
                return(LoadInternal(filePath, fallbackFilePaths));
            }

            DebugHelper.WriteLine($"Loading new {typeName} instance.");

            return(new T());
        }
Exemplo n.º 2
0
        public override void CheckUpdate()
        {
            try
            {
                List <GitHubRelease> releases = GetReleases();

                if (releases != null && releases.Count > 0)
                {
                    GitHubRelease latestRelease;

                    if (IncludePreRelease)
                    {
                        latestRelease = releases[0];
                    }
                    else
                    {
                        latestRelease = releases.FirstOrDefault(x => !x.prerelease);
                    }

                    if (latestRelease != null && !string.IsNullOrEmpty(latestRelease.tag_name) && latestRelease.tag_name.Length > 1 && latestRelease.tag_name[0] == 'v')
                    {
                        LatestVersion = new Version(latestRelease.tag_name.Substring(1));

                        if (latestRelease.assets != null && latestRelease.assets.Count > 0)
                        {
                            string extension;

                            if (IsPortable)
                            {
                                extension = "portable.zip";
                            }
                            else
                            {
                                extension = ".exe";
                            }

                            foreach (GitHubAsset asset in latestRelease.assets)
                            {
                                if (asset != null && !string.IsNullOrEmpty(asset.name) && asset.name.EndsWith(extension, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    Filename = asset.name;

                                    if (IsPortable)
                                    {
                                        DownloadURL = asset.browser_download_url;
                                    }
                                    else
                                    {
                                        DownloadURL = asset.url;
                                    }

                                    RefreshStatus();
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                DebugHelper.WriteException(e, "GitHub update check failed");
            }

            Status = UpdateStatus.UpdateCheckFailed;
        }
Exemplo n.º 3
0
        private bool SaveInternal(string filePath)
        {
            string typeName = GetType().Name;

            DebugHelper.WriteLine($"{typeName} save started: {filePath}");

            bool isSuccess = false;

            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    lock (this)
                    {
                        Helpers.CreateDirectoryFromFilePath(filePath);

                        string tempFilePath = filePath + ".temp";

                        using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
                            using (StreamWriter streamWriter = new StreamWriter(fileStream))
                                using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                                {
                                    JsonSerializer serializer = new JsonSerializer();

                                    if (SupportDPAPIEncryption)
                                    {
                                        serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver();
                                    }
                                    else
                                    {
                                        serializer.ContractResolver = new WritablePropertiesOnlyResolver();
                                    }

                                    serializer.Converters.Add(new StringEnumConverter());
                                    serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                                    serializer.Formatting           = Formatting.Indented;
                                    serializer.Serialize(jsonWriter, this);
                                    jsonWriter.Flush();
                                }

                        if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
                        {
                            throw new Exception($"{typeName} file is corrupt: {tempFilePath}");
                        }

                        if (File.Exists(filePath))
                        {
                            string backupFilePath = null;

                            if (CreateBackup)
                            {
                                string fileName = Path.GetFileName(filePath);
                                backupFilePath = Path.Combine(BackupFolder, fileName);
                                Helpers.CreateDirectoryFromDirectoryPath(BackupFolder);
                            }

                            File.Replace(tempFilePath, filePath, backupFilePath);
                        }
                        else
                        {
                            File.Move(tempFilePath, filePath);
                        }

                        if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
                        {
                            Helpers.BackupFileWeekly(filePath, BackupFolder);
                        }

                        isSuccess = true;
                    }
                }
            }
            catch (Exception e)
            {
                DebugHelper.WriteException(e);

                OnSettingsSaveFailed(e);
            }
            finally
            {
                string status = isSuccess ? "successful" : "failed";
                DebugHelper.WriteLine($"{typeName} save {status}: {filePath}");
            }

            return(isSuccess);
        }