private void Button_Save_Click(object sender, RoutedEventArgs e) { DeleteBtn.IsEnabled = true; StartBtn.IsEnabled = true; StopBtn.IsEnabled = true; WatcherConfig cfg = GetWatcherConfig(); Settings.AddModWatcherConfig(cfg); _main.AddWatcherLabel(cfg); }
public static FileSystemWatcher WatchUserConfig(string filePath, UserConfigFileChangedHandler OnUserConfigFileChanged) { FileInfo info = new FileInfo(filePath); FileSystemWatcher userConfigWatcher; var lastRead = File.GetLastWriteTime(info.FullName); userConfigWatcher = new FileSystemWatcher(info.Directory.FullName, info.Name); userConfigWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Size | NotifyFilters.Attributes; userConfigWatcher.Changed += delegate { DateTime lastWriteTime = File.GetLastWriteTime(info.FullName); if (lastWriteTime.Subtract(lastRead).TotalMilliseconds > 200) { WatcherConfig watchConfig = null; try { watchConfig = ConfigPool[filePath]; var encoding = watchConfig.Encode; string content = LoadText(info.FullName, encoding); watchConfig.Config = TinyJsonConfig.Parse(content, encoding); watchConfig.LastChangeTime = DateTime.Now; Console.WriteLine("User configuration has changed, updated successfully :" + info.FullName); } catch (Exception) { Console.WriteLine("Updating user config failed:" + info.FullName); throw; } // trigger our event if (OnUserConfigFileChanged != null) { OnUserConfigFileChanged(watchConfig); } } lastRead = lastWriteTime; }; userConfigWatcher.EnableRaisingEvents = true; return(userConfigWatcher); }
//public static event UserConfigFileChangedHandler OnUserConfigFileChanged; /// <summary> /// 加载json配置文件 /// </summary> /// <param name="filePath"></param> /// <param name="enableWatch"></param> /// <param name="encoding"></param> /// <returns></returns> public static dynamic LoadConfig(string filePath, Encoding encoding = null, bool enableWatch = true, UserConfigFileChangedHandler OnUserConfigFileChanged = null) { filePath = FormatAbsFilePath(filePath); if (ConfigPool.ContainsKey(filePath)) { return(ConfigPool[filePath].Config); } if (!File.Exists(filePath)) { throw new ArgumentException("不存在Json配置文件:" + filePath); } if (encoding == null) { encoding = Encoding.Unicode; } string content = LoadText(filePath, encoding); var arrayJson = TinyJsonConfig.Parse(content, encoding); FileSystemWatcher userConfigWatcher = null; if (enableWatch) { userConfigWatcher = WatchUserConfig(filePath, OnUserConfigFileChanged);//监听配置文件变化 } WatcherConfig _watchConfig = new WatcherConfig(); _watchConfig.FilePath = filePath; _watchConfig.Encode = encoding; _watchConfig.Config = arrayJson; _watchConfig.Watcher = userConfigWatcher; _watchConfig.LastChangeTime = DateTime.Now; ConfigPool[filePath] = _watchConfig; return(arrayJson); }
void Awake() { instance = this; if (config == null) { config = Resources.Load <WatcherConfig>("Config/default"); } watcherStyle = new GUIStyle(); watcherStyle.font = config.font; watcherStyle.fontSize = 14; watcherStyle.normal.textColor = config.commandColor; // watcherStyle. var assembly = System.AppDomain.CurrentDomain.Load("Assembly-CSharp"); fields = assembly .GetTypes() .SelectMany(x => x.GetFields()) .Where(y => y.GetCustomAttributes(true).OfType <WatchAttribute>().Any()).ToList(); properties = assembly .GetTypes() .SelectMany(x => x.GetProperties()) .Where(y => y.GetCustomAttributes(true).OfType <WatchAttribute>().Any()).ToList(); }