-
Notifications
You must be signed in to change notification settings - Fork 11
/
game_config.go
155 lines (142 loc) · 3.74 KB
/
game_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package component
import (
"github.com/cornelk/hashmap"
"github.com/thkhxm/tgf/db"
"github.com/thkhxm/tgf/log"
"github.com/thkhxm/tgf/util"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
)
//***************************************************
//@Link https://github.com/thkhxm/tgf
//@Link https://gitee.com/timgame/tgf
//@QQ群 7400585
//author tim.huang<thkhxm@gmail.com>
//@Description
//2023/4/10
//***************************************************
var confPath = "./conf/json"
var contextDataManager db.IAutoCacheService[string, []byte]
var cacheDataManager db.IAutoCacheService[string, *hashmap.Map[string, interface{}]]
var newLock = &sync.Mutex{}
func GetGameConf[Val any](id string) (res Val, h bool) {
t := util.ReflectType[Val]()
key := t.Name()
data := getCacheGameConfData[Val](key)
if tmp, x := data.Get(id); x {
res = tmp.([]Val)[0]
h = true
}
return
}
func GetGameConfBySlice[Val any](id string) (res []Val, h bool) {
t := util.ReflectType[Val]()
key := t.Name()
data := getCacheGameConfData[Val](key)
if tmp, x := data.Get(id); x {
res = tmp.([]Val)
h = true
}
return
}
// GetAllGameConf [Val any]
// @Description: 不建议使用这个函数除非特殊需求,建议使用RangeGameConf
func GetAllGameConf[Val any]() (res []Val) {
t := util.ReflectType[Val]()
key := t.Name()
data := getCacheGameConfData[Val](key)
tmp := make([]Val, 0, data.Len())
data.Range(func(s string, i interface{}) bool {
for _, v := range i.([]Val) {
tmp = append(tmp, v)
}
return true
})
res = tmp
return
}
// RangeGameConf [Val any]
// @Description:
// @param f
func RangeGameConf[Val any](f func(s string, i Val) bool) {
t := util.ReflectType[Val]()
key := t.Name()
data := getCacheGameConfData[Val](key)
ff := func(a string, b interface{}) bool {
for _, i := range b.([]Val) {
if !f(a, i) {
return false
}
}
return true
}
data.Range(ff)
}
func getCacheGameConfData[Val any](key string) *hashmap.Map[string, interface{}] {
data, _ := cacheDataManager.Get(key)
if data == nil {
newLock.Lock()
defer newLock.Unlock()
data = LoadGameConf[Val]()
}
return data
}
// LoadGameConf [Val any]
//
// 泛型传入自动生成的配置即可
// @Description: 预加载
func LoadGameConf[Val any]() *hashmap.Map[string, interface{}] {
t := util.ReflectType[Val]()
key := t.Name()
context, _ := contextDataManager.Get(key)
data, _ := util.StrToAny[[]Val](util.ConvertStringByByteSlice(context))
cc := hashmap.New[string, interface{}]()
for _, d := range data {
rd := reflect.ValueOf(d).Elem()
id := rd.Field(0)
uniqueId, _ := util.AnyToStr(id.Interface())
v, _ := cc.Get(uniqueId)
if v == nil {
v = make([]Val, 0)
}
v = append(v.([]Val), d)
cc.Set(uniqueId, v)
}
cacheDataManager.Set(cc, key)
log.DebugTag("GameConf", "load game conf , name=%v", t.Name())
return cc
}
func WithConfPath(path string) {
confPath, _ = filepath.Abs(path)
log.InfoTag("GameConf", "set game json file path=%v", confPath)
}
func InitGameConfToMem() {
builder := db.NewAutoCacheBuilder[string, []byte]()
builder.WithMemCache(0)
contextDataManager = builder.New()
//
cacheBuilder := db.NewAutoCacheBuilder[string, *hashmap.Map[string, interface{}]]()
cacheBuilder.WithMemCache(0)
cacheDataManager = cacheBuilder.New()
//
files := util.GetFileList(confPath, ".json")
for _, filePath := range files {
file, err := os.Open(filePath)
if err != nil {
log.WarnTag("GameConf", "game json file [%v] open error %v", filePath, err)
continue
}
context, err := io.ReadAll(file)
if err != nil {
log.WarnTag("GameConf", "game json file [%v] read error %v", filePath, err)
continue
}
_, fileName := filepath.Split(filePath)
contextDataManager.Set(context, strings.Split(fileName, `.`)[0]+"Conf")
file.Close()
}
}