Example #1
0
        public static void DeserializeConfig(Config instance, BinaryReader r)
        {
            var fieldsCount = r.ReadInt16();

            for (var i = 0; i < fieldsCount; i++)
            {
                var fieldName = r.ReadString();
                var fieldType = r.ReadByte();
                switch (fieldName)
                {
                case "AfterCompletionCommand":
                    instance.AfterCompletionCommand = r.ReadString();
                    break;

                case "UserSelectedDownloadFolder":
                    instance.UserSelectedDownloadFolder = r.ReadString();
                    break;

                case "AntiVirusArgs":
                    instance.AntiVirusArgs = r.ReadString();
                    break;

                case "AntiVirusExecutable":
                    instance.AntiVirusExecutable = r.ReadString();
                    break;

                case "BlockedHosts":
                    var blockedHostsLength = r.ReadInt16();
                    instance.BlockedHosts = new string[blockedHostsLength];
                    for (int a = 0; a < blockedHostsLength; a++)
                    {
                        instance.BlockedHosts[a] = r.ReadString();
                    }
                    break;

                case "Categories":
                    var categoriesLength     = r.ReadInt16();
                    var categoriesFieldCount = r.ReadInt16();
                    var categories           = new List <Category>(categoriesLength);
                    for (int a = 0; a < categoriesLength; a++)
                    {
                        var cat = new Category();
                        for (int b = 0; b < categoriesFieldCount; b++)
                        {
                            var fieldName1 = r.ReadString();
                            var fieldType1 = r.ReadByte();
                            switch (fieldName1)
                            {
                            case "DefaultFolder":
                                cat.DefaultFolder = r.ReadString();
                                break;

                            case "DisplayName":
                                cat.DisplayName = r.ReadString();
                                break;

                            case "IsPredefined":
                                cat.IsPredefined = r.ReadBoolean();
                                break;

                            case "Name":
                                cat.Name = r.ReadString();
                                break;

                            case "FileExtensions":
                                cat.FileExtensions = new HashSet <string>();
                                var fileExtensionsLength1 = r.ReadInt16();
                                for (int m = 0; m < fileExtensionsLength1; m++)
                                {
                                    cat.FileExtensions.Add(r.ReadString());
                                }
                                break;

                            default:
                                SkipUnknownField(fieldType1, fieldName1, r);
                                break;
                            }
                        }
                        categories.Add(cat);
                        instance.Categories = categories;
                    }
                    break;

                case "DefaultDownloadFolder":
                    instance.DefaultDownloadFolder = r.ReadString();
                    break;

                case "EnableSpeedLimit":
                    instance.EnableSpeedLimit = r.ReadBoolean();
                    break;

                case "FetchServerTimeStamp":
                    instance.FetchServerTimeStamp = r.ReadBoolean();
                    break;

                case "FileConflictResolution":
                    instance.FileConflictResolution = (FileConflictResolution)r.ReadInt32();
                    break;

                case "FolderSelectionMode":
                    instance.FolderSelectionMode = (FolderSelectionMode)r.ReadInt32();
                    break;

                case "DefaltDownloadSpeed":
                    instance.DefaltDownloadSpeed = r.ReadInt32();
                    break;

                case "IsBrowserMonitoringEnabled":
                    instance.IsBrowserMonitoringEnabled = r.ReadBoolean();
                    break;

                case "KeepPCAwake":
                    instance.KeepPCAwake = r.ReadBoolean();
                    break;

                case "Language":
                    instance.Language = r.ReadString();
                    break;

                case "MaxParallelDownloads":
                    instance.MaxParallelDownloads = r.ReadInt32();
                    break;

                case "MaxRetry":
                    instance.MaxRetry = r.ReadInt32();
                    break;

                case "MaxSegments":
                    instance.MaxSegments = r.ReadInt32();
                    break;

                case "MinVideoSize":
                    instance.MinVideoSize = r.ReadInt32();
                    break;

                case "MonitorClipboard":
                    instance.MonitorClipboard = r.ReadBoolean();
                    break;

                case "NetworkTimeout":
                    instance.NetworkTimeout = r.ReadInt32();
                    break;

                case "RetryDelay":
                    instance.RetryDelay = r.ReadInt32();
                    break;

                case "RunCommandAfterCompletion":
                    instance.RunCommandAfterCompletion = r.ReadBoolean();
                    break;

                //case "RunOnLogon":
                //    instance.RunOnLogon = r.ReadBoolean();
                //    break;
                case "ScanWithAntiVirus":
                    instance.ScanWithAntiVirus = r.ReadBoolean();
                    break;

                case "ShowDownloadCompleteWindow":
                    instance.ShowDownloadCompleteWindow = r.ReadBoolean();
                    break;

                case "ShowProgressWindow":
                    instance.ShowProgressWindow = r.ReadBoolean();
                    break;

                case "ShutdownAfterAllFinished":
                    instance.ShutdownAfterAllFinished = r.ReadBoolean();
                    break;

                case "StartDownloadAutomatically":
                    instance.StartDownloadAutomatically = r.ReadBoolean();
                    break;

                case "TempDir":
                    instance.TempDir = r.ReadString();
                    break;

                case "AllowSystemDarkTheme":
                    instance.AllowSystemDarkTheme = r.ReadBoolean();
                    break;

                case "DoubleClickOpenFile":
                    instance.DoubleClickOpenFile = r.ReadBoolean();
                    break;

                case "FileExtensions":
                    var fileExtensionsLength = r.ReadInt16();
                    instance.FileExtensions = new string[fileExtensionsLength];
                    for (int a = 0; a < fileExtensionsLength; a++)
                    {
                        instance.FileExtensions[a] = r.ReadString();
                    }
                    break;

                case "RecentFolders":
                    var recentFoldersLength = r.ReadInt16();
                    instance.RecentFolders = new List <string>(recentFoldersLength);
                    for (int a = 0; a < recentFoldersLength; a++)
                    {
                        instance.RecentFolders.Add(r.ReadString());
                    }
                    break;

                case "VideoExtensions":
                    var videoExtensionsLength = r.ReadInt16();
                    instance.VideoExtensions = new string[videoExtensionsLength];
                    for (int a = 0; a < videoExtensionsLength; a++)
                    {
                        instance.VideoExtensions[a] = r.ReadString();
                    }
                    break;

                case "UserCredentials":
                    var userCredentialsLength    = r.ReadInt16();
                    var passwordEntryFieldLength = r.ReadInt16();
                    var passwordEntries          = new List <PasswordEntry>(userCredentialsLength);
                    for (int a = 0; a < userCredentialsLength; a++)
                    {
                        var passwordEntry = new PasswordEntry();
                        for (int b = 0; b < passwordEntryFieldLength; b++)
                        {
                            var fieldName1 = r.ReadString();
                            var fieldType1 = r.ReadByte();
                            switch (fieldName1)
                            {
                            case "Host":
                                passwordEntry.Host = r.ReadString();
                                break;

                            case "User":
                                passwordEntry.User = r.ReadString();
                                break;

                            case "Password":
                                passwordEntry.Password = r.ReadString();
                                break;

                            default:
                                SkipUnknownField(fieldType1, fieldName1, r);
                                break;
                            }
                        }
                        passwordEntries.Add(passwordEntry);
                        instance.UserCredentials = passwordEntries;
                    }
                    break;

                case "Proxy":
                    instance.Proxy = ProxyInfoSerializer.Deserialize(r);
                    break;

                default:
                    SkipUnknownField(fieldType, fieldName, r);
                    break;
                }
            }
        }
Example #2
0
        public static void SerializeConfig()
        {
            var instance = Config.Instance;

            using var ms = new MemoryStream();
            using var w  = new BinaryWriter(ms);

            w.Write((short)(instance.Proxy.HasValue ? 36 : 35)); //total fields

            WriteString(w, instance.AfterCompletionCommand, "AfterCompletionCommand");
            WriteString(w, instance.UserSelectedDownloadFolder, "UserSelectedDownloadFolder");
            WriteString(w, instance.AntiVirusArgs, "AntiVirusArgs");
            WriteString(w, instance.AntiVirusExecutable, "AntiVirusExecutable");
            WriteString(w, instance.DefaultDownloadFolder, "DefaultDownloadFolder");
            WriteString(w, instance.Language, "Language");
            WriteString(w, instance.TempDir, "TempDir");

            WriteBoolean(w, instance.EnableSpeedLimit, "EnableSpeedLimit");
            WriteBoolean(w, instance.FetchServerTimeStamp, "FetchServerTimeStamp");
            WriteBoolean(w, instance.IsBrowserMonitoringEnabled, "IsBrowserMonitoringEnabled");
            WriteBoolean(w, instance.KeepPCAwake, "KeepPCAwake");
            WriteBoolean(w, instance.MonitorClipboard, "MonitorClipboard");
            WriteBoolean(w, instance.RunCommandAfterCompletion, "RunCommandAfterCompletion");
            //WriteBoolean(w, instance.RunOnLogon, "RunOnLogon");
            WriteBoolean(w, instance.ScanWithAntiVirus, "ScanWithAntiVirus");
            WriteBoolean(w, instance.ShowDownloadCompleteWindow, "ShowDownloadCompleteWindow");
            WriteBoolean(w, instance.ShowProgressWindow, "ShowProgressWindow");
            WriteBoolean(w, instance.ShutdownAfterAllFinished, "ShutdownAfterAllFinished");
            WriteBoolean(w, instance.StartDownloadAutomatically, "StartDownloadAutomatically");
            WriteBoolean(w, instance.AllowSystemDarkTheme, "AllowSystemDarkTheme");
            WriteBoolean(w, instance.DoubleClickOpenFile, "DoubleClickOpenFile");

            WriteInt32(w, (int)instance.FileConflictResolution, "FileConflictResolution");
            WriteInt32(w, (int)instance.FolderSelectionMode, "FolderSelectionMode");

            WriteInt32(w, instance.DefaltDownloadSpeed, "DefaltDownloadSpeed");
            WriteInt32(w, instance.MaxParallelDownloads, "MaxParallelDownloads");
            WriteInt32(w, instance.MaxRetry, "MaxRetry");
            WriteInt32(w, instance.MaxSegments, "MaxSegments");
            WriteInt32(w, instance.MinVideoSize, "MinVideoSize");
            WriteInt32(w, instance.NetworkTimeout, "NetworkTimeout");
            WriteInt32(w, instance.RetryDelay, "RetryDelay");

            WriteStringArray(w, instance.BlockedHosts, "BlockedHosts", instance.BlockedHosts.Length);
            WriteStringArray(w, instance.FileExtensions, "FileExtensions", instance.FileExtensions.Length);
            WriteStringArray(w, instance.RecentFolders, "RecentFolders", instance.RecentFolders.Count);
            WriteStringArray(w, instance.VideoExtensions, "VideoExtensions", instance.VideoExtensions.Length);

            w.Write("Categories");
            w.Write(OBJECT_ARRAY);
            w.Write((short)instance.Categories.Count());
            w.Write((short)5); //no of fields in Category class
            foreach (var cat in instance.Categories)
            {
                WriteString(w, cat.DefaultFolder, "DefaultFolder");
                WriteString(w, cat.DisplayName, "DisplayName");
                WriteBoolean(w, cat.IsPredefined, "IsPredefined");
                WriteString(w, cat.Name, "Name");
                WriteStringArray(w, cat.FileExtensions, "FileExtensions", cat.FileExtensions.Count);
            }

            w.Write("UserCredentials");
            w.Write(OBJECT_ARRAY);
            w.Write((short)instance.UserCredentials.Count());
            w.Write((short)3); //no of fields in Category class
            foreach (var pe in instance.UserCredentials)
            {
                WriteString(w, pe.Host, "Host");
                WriteString(w, pe.User, "User");
                WriteString(w, pe.Password, "Password");
            }

            if (instance.Proxy.HasValue)
            {
                ProxyInfoSerializer.Serialize(instance.Proxy.Value, w);
            }

            w.Close();
            ms.Close();
            TransactedIO.WriteBytes(ms.ToArray(), "settings.dat", Config.DataDir);
        }