Ejemplo n.º 1
0
        public static HyperSearchSettings Instance()
        {
            if (_instance == null)
            {
                Load();

                if (_instance == null) _instance = new HyperSearchSettings();
            }

            return _instance;
        }
Ejemplo n.º 2
0
        public static void Load()
        {
            try
            {
                var filePath = Global.BuildFilePathInAppDir("Settings.json");
                string json;

                if (File.Exists(filePath))
                {
                    json = File.ReadAllText(filePath);
                }
                else
                { // create json using all top-level expected objects so that the DefaultValue attribute can take over
                    //var allExpectedSections = (from p in typeof(HyperSearchSettings).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                    //                           select string.Format("\"{0}\": {{ }}", p.Name));

                    var highLevelProperties = (from p in typeof(HyperSearchSettings).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                                               select new
                                               {
                                                   p.Name,
                                                   Children = (from c in p.GetGetMethod().ReturnType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                                                               where c.IsDefined(typeof(SettingCreateEmptyAttribute),false)
                                                               select new { c.Name }
                                                               )
                                               });

                    var allExpectedSections = from p in highLevelProperties
                              select string.Format("\"{0}\": {{ {1} }}", p.Name, (
                                string.Join(",", (from c in p.Children
                                select string.Format("\"{0}\": {{  }}", c.Name)))
                              ));

                    json = "{" + string.Join(",", allExpectedSections.ToArray()) + "}";
                }

                JsonSerializerSettings ss = new JsonSerializerSettings();

                ss.Converters = new List<JsonConverter> { new KeyListConverter() };
                ss.DefaultValueHandling = DefaultValueHandling.Populate;

                var settings = JsonConvert.DeserializeObject<HyperSearchSettings>(json, ss);

                var nullSections = (from p in settings.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                                    where p.GetValue(settings, null) == null
                                    select p);

                foreach(var p in nullSections)
                {
                    // create an empty section
                    var emptySection = Activator.CreateInstance(p.PropertyType);

                    p.SetValue(settings, emptySection, null);
                }

                _instance = settings;

                // TODO: Find a better way to create these objects on demand
                if (_instance.Input.Triggers == null) _instance.Input.Triggers = new Triggers();

            }
            catch (Exception ex)
            {
                ErrorHandler.HandleException(ex);
            }
        }