public ConfigurationBuilder AddDefault() { Assembly ass = Assembly.GetEntryAssembly(); Stream stream = ass.LoadStream("HakeQuick.default.json"); RecordBase record = Converter.ReadJson(stream); stream.Dispose(); if (record is SetRecord set) { if (values == null) { values = set; } else { values.Combine(set); } } else { throw new Exception("invalid configuration format"); } return(this); }
public static void Initialize(ICurrentEnvironment env) { Stream defaultConfigStream = Assembly.GetExecutingAssembly().LoadStream("Chrome.BookmarkSearch.default.json"); SetRecord defaultConfig = Converter.ReadJson(defaultConfigStream) as SetRecord; string configPath = env.ConfigDirectory + "\\bookmarksearch.json"; if (File.Exists(configPath)) { try { Stream userConfigStream = File.OpenRead(configPath); SetRecord userConfig = Converter.ReadJson(userConfigStream) as SetRecord; userConfigStream.Dispose(); defaultConfig.Combine(userConfig); } catch { } } else { try { defaultConfigStream.Seek(0, SeekOrigin.Begin); FileStream configStream = File.Create(configPath); defaultConfigStream.CopyTo(configStream); configStream.Flush(); configStream.Dispose(); } catch { } } defaultConfigStream.Dispose(); ConfigurationRecord = defaultConfig; Config = new SearchConfig() { SearchUrl = ConfigurationRecord.ReadAs <bool>("allow_url_search"), EnableSearch = ConfigurationRecord.ReadAs <bool>("enable") }; Bookmarks = BookmarkCollection.Retrive(); SearchPatternErrorAction = new ErrorAction(null, "搜索模式错误", "搜索模式错误"); }