package srvdata import ( "os" "path/filepath" "github.com/howeyc/fsnotify" "mongo.games.com/game/model" "mongo.games.com/goserver/core" "mongo.games.com/goserver/core/basic" "mongo.games.com/goserver/core/logger" ) // DatFileModifiedCommand dat文件加载 type DatFileModifiedCommand struct { fileName string } func (fmc *DatFileModifiedCommand) Done(o *basic.Object) error { fn := filepath.Base(fmc.fileName) logger.Logger.Info("modified file name ======", fn) // loader for k, v := range DataMgr.GetLoader(fn) { err := v.Reload(fmc.fileName) if err != nil { logger.Logger.Warnf("%v loader %v err: %v", fn, k, err) } } return nil } // JsonFileModifiedCommand json文件加载 type JsonFileModifiedCommand struct { fileName string } func (gmc *JsonFileModifiedCommand) Done(o *basic.Object) error { fn := filepath.Base(gmc.fileName) switch fn { case "gameparam.json": model.InitGameParam() case "gmac.json": model.InitGMAC() case "thrconfig.json": model.InitGameConfig() case "fishingparam.json": model.InitFishingParam() //case "bullfightparam.json": // model.InitBullFightParam() //case "winthreeparam.json": // model.InitWinThreeParam() //case "mahjongparam.json": // model.InitMahJongParam() case "normalparam.json": model.InitNormalParam() case "clientparam.json": model.InitClientParam() } return nil } var Config = Configuration{} type Configuration struct { RootPath string //文件目录 watcher *fsnotify.Watcher } func (this *Configuration) Name() string { return "data" } func (this *Configuration) Init() error { workDir, workingDirError := os.Getwd() if workingDirError != nil { return workingDirError } this.RootPath = filepath.Join(workDir, this.RootPath) var err error this.watcher, err = fsnotify.NewWatcher() if err != nil { logger.Logger.Error("fsnotify.NewWatcher err:", err) return err } // 监听文件变化 // Process events go func() { defer func() { logger.Logger.Warnf("watcher quit! %v", this.RootPath) if err := recover(); err != nil { logger.Logger.Error("watch data director modify goroutine err:", err) } }() logger.Logger.Infof("watcher start! %v", this.RootPath) for { select { case ev, ok := <-this.watcher.Event: logger.Logger.Info("fsnotify event:", ev) if !ok || ev == nil { continue } if ev.IsModify() || ev.IsRename() { obj := core.CoreObject() if obj == nil { continue } switch filepath.Ext(ev.Name) { case ".dat": obj.SendCommand(&DatFileModifiedCommand{fileName: ev.Name}, false) case ".json": obj.SendCommand(&JsonFileModifiedCommand{fileName: ev.Name}, false) } } case err := <-this.watcher.Error: logger.Logger.Error("fsnotify err:", err) } } }() // 收集 DataLoader 处理的文件 var loaderFiles []string // 文件地址,.dat文件 this.watcher.Watch(this.RootPath) filepath.Walk(this.RootPath, func(path string, info os.FileInfo, err error) error { if info.IsDir() { this.watcher.Watch(path) // 监听文件夹下的文件修改 } name := info.Name() if filepath.Ext(name) != ".dat" { return nil } loaderFiles = append(loaderFiles, name) return nil }) // 使用 DataLoader 加载 for _, fileName := range loaderFiles { for k, v := range DataMgr.GetLoader(fileName) { fullPath := filepath.Join(this.RootPath, fileName) err := v.Load(fullPath) if err != nil { logger.Logger.Warnf("%v loader %v err: %v", fileName, k, err) } else { logger.Logger.Infof("%v loader %v success", fileName, k) } } } return nil } func (this *Configuration) Close() error { if this.watcher != nil { this.watcher.Close() } return nil } func init() { core.RegistePackage(&Config) }