go_dreamfactory/modules/comp_configure.go
2022-08-18 15:09:34 +08:00

138 lines
3.5 KiB
Go

package modules
import (
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
)
const (
game_global = "game_global.json" //全局配置表
game_initial = "game_initial.json" //初始化表
game_playerlv = "game_playerlv.json" //玩家等级
game_facemod = "game_facemod.json" // 形象配置表
)
///配置管理基础组件
type MCompConfigure struct {
cbase.ModuleCompBase
}
//组件初始化接口
func (this *MCompConfigure) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.ModuleCompBase.Init(service, module, comp, options)
err = this.LoadConfigure(game_global, cfg.NewGameGlobal)
err = this.LoadConfigure(game_initial, cfg.NewGameInitial)
err = this.LoadConfigure(game_playerlv, cfg.NewGamePlayerlv)
err = this.LoadConfigure(game_facemod, cfg.NewGameFacemod)
return
}
//加载一个配置文件
func (this *MCompConfigure) LoadConfigure(name string, fn interface{}) (err error) {
return configure.RegisterConfigure(name, fn, nil)
}
//加载多个配置文件
func (this *MCompConfigure) LoadMultiConfigure(confs map[string]interface{}) (err error) {
for k, v := range confs {
err = configure.RegisterConfigure(k, v, nil)
if err != nil {
log.Errorf("配置文件:%s解析失败!", k)
break
}
}
return
}
//读取配置数据
func (this *MCompConfigure) GetConfigure(name string) (v interface{}, err error) {
return configure.GetConfigure(name)
}
//全局配置
func (this *MCompConfigure) GetGlobalConf() *cfg.GameGlobalData {
var (
configure *cfg.GameGlobal
ok bool
)
if v, err := this.GetConfigure(game_global); err != nil {
log.Errorf("get global conf err:%v", err)
} else {
if configure, ok = v.(*cfg.GameGlobal); !ok {
log.Errorf("%T no is *cfg.Game_global", v)
}
}
return configure.GetDataList()[0] // 返回对象信息
}
func (this *MCompConfigure) GetGlobalInitConf() (configure *cfg.GameInitial, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_initial); err != nil {
return
} else {
if configure, ok = v.(*cfg.GameInitial); !ok {
err = fmt.Errorf("%T no is *cfg.Game_comInitial", v)
return
}
}
return
}
// 主角等级经验配置列表
func (this *MCompConfigure) GetPlayerlvConfList() (list []*cfg.GamePlayerlvData) {
if v, err := this.GetConfigure(game_playerlv); err != nil {
return
} else {
if configure, ok := v.(*cfg.GamePlayerlv); !ok {
err = fmt.Errorf("%T no is *cfg.Game_playerlv", v)
return
} else {
if configure != nil {
list = configure.GetDataList()
}
}
}
return
}
// 玩家等级经验配置表
func (this *MCompConfigure) GetPlayerlvConf(lv int32) (data *cfg.GamePlayerlvData) {
if v, err := this.GetConfigure(game_playerlv); err != nil {
return
} else {
if configure, ok := v.(*cfg.GamePlayerlv); !ok {
err = fmt.Errorf("%T no is *cfg.Game_playerlv", v)
return
} else {
if configure != nil {
data = configure.GetDataMap()[lv]
}
}
}
return
}
// 玩家形象预设配置
func (this *MCompConfigure) GetPlayerFigureConf() (list []*cfg.GameFacemodData) {
if v, err := this.GetConfigure(game_facemod); err != nil {
return
} else {
if configure, ok := v.(*cfg.GameFacemod); !ok {
err = fmt.Errorf("%T no is *cfg.Game_playerlv", v)
return
} else {
if configure != nil {
list = configure.GetDataList()
}
}
}
return
}