Example #1
0
        /// <summary>
        /// Creates a singleton settings object and loads parameters from file, if it exists.
        /// </summary>
        public static void GlobalSettingsInit()
        {
            Settings    = new GlobalSettings();
            appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            settingsFN  = Path.Combine(appDataPath, "furdown\\furdown.conf");
            // true portable mode
            if (File.Exists("./furdown-portable.conf"))
            {
                settingsFN = "./furdown-portable.conf";
            }
            // if settings file exists, load it
            bool needToSetDefaults = false;

            if (File.Exists(settingsFN))
            {
                using (Stream stream = File.Open(settingsFN, FileMode.Open))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    try
                    {
                        Settings = (GlobalSettings)bformatter.Deserialize(stream);
                    }
                    // settings file is present, but failed to be loaded
                    catch
                    {
                        needToSetDefaults = true;
                        Console.WriteLine("Settings file is incompatible or corrupted, restoring defaults.");
                    }
                }
            }
            else
            {
                needToSetDefaults = true;
            }
            // set defaults, if settings file does not exist or otherwise not loaded
            if (needToSetDefaults)
            {
                Settings.downloadPath          = Path.Combine(appDataPath, "furdown\\downloads");
                Settings.systemPath            = Path.Combine(appDataPath, "furdown\\system");
                Settings.filenameTemplate      = "%ARTIST%%SCRAPS%\\%SUBMID%.%FILEPART%";
                Settings.descrFilenameTemplate = "%ARTIST%%SCRAPS%\\%SUBMID%.%FILEPART%.dsc.htm";
                Settings.downloadOnlyOnce      = true;
                Settings.scrapsTemplateActive  = ".scraps";
                Settings.scrapsTemplateActive  = "";
                try
                {
                    Directory.CreateDirectory(Settings.downloadPath);
                    Directory.CreateDirectory(Settings.systemPath);
                }
                catch
                {
                    Console.WriteLine("[Error] Default paths are invalid!");
                }
            }
            else
            {
                if (!Directory.Exists(Settings.downloadPath))
                {
                    Directory.CreateDirectory(Settings.downloadPath);
                }
                if (!Directory.Exists(Settings.systemPath))
                {
                    Directory.CreateDirectory(Settings.systemPath);
                }
            }
            SubmissionsDB.Load();
        }