Exemplo n.º 1
0
        public bool LoadOrCreateConfig()
        {
            Logger.Info("Loading config...");
            var path = GetAbsolutePath(_filename);

            if (!File.Exists(path))
            {
                Logger.Warn("Config does not exist! Creating default...");
                CurrentConfig = ServiceConfig.CreateDefault();
                SaveConfig();
            }
            else
            {
                try
                {
                    using (var reader = new StreamReader(path))
                        CurrentConfig = JsonConvert.DeserializeObject <ServiceConfig>(reader.ReadToEnd());

                    _deviceConfigs = Directory.EnumerateFiles(GetAbsolutePath(@"Plugins\Devices\"), "*.json")
                                     .Select(f =>
                    {
                        using (var reader = new StreamReader(f))
                        {
                            return(Name: Path.GetFileNameWithoutExtension(f),
                                   Config: JsonConvert.DeserializeObject <DeviceConfig>(reader.ReadToEnd()));
                        }
                    })
                                     .Where(x => x != default)
                                     .ToDictionary(x => x.Name, x => x.Config);
                }
                catch (Exception e)
                {
                    Logger.Fatal(e);
                }

                if (CurrentConfig == null)
                {
                    Logger.Fatal("Failed to load the config!");
                    return(false);
                }
            }

            return(true);
        }