| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package data
- import (
- "os"
- cherryFile "github.com/cherry-game/cherry/extend/file"
- clog "github.com/cherry-game/cherry/logger"
- cherryProfile "github.com/cherry-game/cherry/profile"
- "github.com/go-redis/redis/v8"
- )
- type (
- luaRow struct {
- Name string `json:"name"`
- Path string `json:"path"`
- Desc string `json:"desc"`
- }
- luaConfig struct {
- maps map[string]*redis.Script
- }
- )
- func (p *luaConfig) Name() string {
- return "lua"
- }
- func (p *luaConfig) Init() {
- p.maps = map[string]*redis.Script{}
- }
- func (p *luaConfig) OnLoad(maps interface{}, _ bool) (int, error) {
- var list []*luaRow
- if err := DecodeData(maps, &list); err != nil {
- return 0, err
- }
- loadMaps := map[string]*redis.Script{}
- for index, row := range list {
- bytes, err := row.readFileBytes()
- if err != nil {
- clog.Panicf("read script error. [row = %d, %v], err = %s", index+1, row, err)
- }
- loadMaps[row.Name] = redis.NewScript(string(bytes))
- }
- p.maps = loadMaps
- return len(list), nil
- }
- func (p *luaConfig) OnAfterLoad(_ bool) {
- }
- func (p *luaConfig) Get(name string) (*redis.Script, bool) {
- value, found := p.maps[name]
- return value, found
- }
- func (p *luaRow) readFileBytes() ([]byte, error) {
- fullPath, err := cherryFile.JoinPath(cherryProfile.Path(), p.Path)
- if err != nil {
- return nil, err
- }
- bytes, err := os.ReadFile(fullPath)
- if err != nil {
- return nil, err
- }
- return bytes, nil
- }
|