Пример #1
0
        /// <summary>Sets default settings as all statistics, share everything</summary>
        static void SetDefaultSettings()
        {
            string docsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string dataPath = Path.Combine(docsPath, "RomRepoMgr");

            Current = new SetSettings
            {
                DatabasePath    = Path.Combine(dataPath, "romrepo.db"),
                RepositoryPath  = Path.Combine(dataPath, "repo"),
                TemporaryFolder = Path.GetTempPath()
            };
        }
Пример #2
0
        /// <summary>Loads settings</summary>
        public static void LoadSettings()
        {
            Current = new SetSettings();
            PlatformID ptId     = DetectOS.GetRealPlatformID();
            string     homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

            FileStream   prefsFs = null;
            StreamReader prefsSr = null;

            try
            {
                switch (ptId)
                {
                // In case of macOS or iOS settings will be saved in ~/Library/Preferences/com.claunia.romrepomgr.plist
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");

                    string preferencesFilePath = Path.Combine(preferencesPath, "com.claunia.romrepomgr.plist");

                    if (!File.Exists(preferencesFilePath))
                    {
                        SetDefaultSettings();
                        SaveSettings();
                    }

                    prefsFs = new FileStream(preferencesFilePath, FileMode.Open, FileAccess.Read);

                    var parsedPreferences = (NSDictionary)BinaryPropertyListParser.Parse(prefsFs);

                    if (parsedPreferences != null)
                    {
                        NSObject obj;

                        Current.DatabasePath = parsedPreferences.TryGetValue("DatabasePath", out obj)
                                                       ? ((NSString)obj).ToString() : null;

                        Current.RepositoryPath = parsedPreferences.TryGetValue("RepositoryPath", out obj)
                                                         ? ((NSString)obj).ToString() : null;

                        Current.TemporaryFolder = parsedPreferences.TryGetValue("TemporaryFolder", out obj)
                                                          ? ((NSString)obj).ToString() : null;

                        Current.UnArchiverPath = parsedPreferences.TryGetValue("UnArchiverPath", out obj)
                                                         ? ((NSString)obj).ToString() : null;

                        prefsFs.Close();
                    }
                    else
                    {
                        prefsFs.Close();

                        SetDefaultSettings();
                        SaveSettings();
                    }
                }

                break;

                #if !NETSTANDARD2_0
                // In case of Windows settings will be saved in the registry: HKLM/SOFTWARE/Claunia.com/RomRepoMgr
                case PlatformID.Win32NT when OperatingSystem.IsWindows():
                case PlatformID.Win32S when OperatingSystem.IsWindows():
                case PlatformID.Win32Windows when OperatingSystem.IsWindows():
                case PlatformID.WinCE when OperatingSystem.IsWindows():
                case PlatformID.WindowsPhone when OperatingSystem.IsWindows():
                {
                    RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE")?.OpenSubKey("Claunia.com");

                    if (parentKey == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    RegistryKey key = parentKey.OpenSubKey("RomRepoMgr");

                    if (key == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    Current.DatabasePath    = key.GetValue("DatabasePath") as string;
                    Current.RepositoryPath  = key.GetValue("RepositoryPath") as string;
                    Current.TemporaryFolder = key.GetValue("TemporaryFolder") as string;
                    Current.UnArchiverPath  = key.GetValue("UnArchiverPath") as string;
                }

                break;
                #endif

                // Otherwise, settings will be saved in ~/.config/RomRepoMgr.json
                default:
                {
                    string xdgConfigPath =
                        Path.Combine(homePath,
                                     Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ??
                                     XDG_CONFIG_HOME_RESOLVED);

                    string settingsPath = Path.Combine(xdgConfigPath, "RomRepoMgr.json");

                    if (!File.Exists(settingsPath))
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    prefsSr = new StreamReader(settingsPath);

                    Current = JsonSerializer.Deserialize <SetSettings>(prefsSr.ReadToEnd(), new JsonSerializerOptions
                        {
                            AllowTrailingCommas         = true,
                            PropertyNameCaseInsensitive = true,
                            ReadCommentHandling         = JsonCommentHandling.Skip,
                            WriteIndented = true
                        });
                }

                break;
                }
            }
            catch
            {
                prefsFs?.Close();
                prefsSr?.Close();
                SetDefaultSettings();
                SaveSettings();
            }
        }