Skip to content

Commit 395fb86

Browse files
committed
继续优化emojimix存储占用
1 parent 83a4031 commit 395fb86

File tree

5 files changed

+43
-265
lines changed

5 files changed

+43
-265
lines changed

plugin/emojimix/emoji_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package emojimix
22

33
import (
4-
emoji_map "MiniBot/plugin/emojimix/proto"
4+
"encoding/json"
55
"fmt"
66
"io"
77
"net/http"
@@ -13,7 +13,6 @@ import (
1313
emoji "github.com/Andrew-M-C/go.emoji"
1414
"github.com/rs/zerolog/log"
1515
"github.com/tidwall/gjson"
16-
"google.golang.org/protobuf/proto"
1716
)
1817

1918
func TestUnicode(t *testing.T) {
@@ -45,35 +44,28 @@ func TestGenerateMap(t *testing.T) {
4544
}
4645

4746
datas := gjson.ParseBytes(body).Get("data").Map()
48-
outer := &emoji_map.OuterMap{OuterMap: map[string]*emoji_map.InnerMap{}}
47+
keyMap := map[string]string{}
4948

5049
for _, data := range datas {
5150
collections := data.Get("combinations").Map()
52-
cur := data.Get("emoji").String()
53-
outer.OuterMap[cur] = &emoji_map.InnerMap{InnerMap: map[string]string{}}
5451
for _, endData := range collections {
5552
// 只取第一个
5653
emojiFirst := endData.Array()[0]
57-
leftEmoji := emojiFirst.Get("leftEmoji").String()
58-
rightEmoji := emojiFirst.Get("rightEmoji").String()
59-
if cur != leftEmoji {
60-
leftEmoji, rightEmoji = rightEmoji, leftEmoji
61-
}
62-
outer.OuterMap[leftEmoji].InnerMap[rightEmoji] = strings.TrimLeft(emojiFirst.Get("gStaticUrl").String(), base_url)
54+
addValue(keyMap, emojiFirst.Get("leftEmoji").String(), emojiFirst.Get("rightEmoji").String(),
55+
strings.TrimLeft(emojiFirst.Get("gStaticUrl").String(), base_url))
6356
}
6457
}
65-
outFile, err := os.Create(filepath.Join(dataPath, "outer_map.bin"))
58+
outFile, err := os.Create(filepath.Join(dataPath, "key_map.json"))
6659
if err != nil {
6760
log.Error().Err(err).Msg("")
6861
return
6962
}
7063
defer outFile.Close()
7164

72-
data, err := proto.Marshal(outer)
65+
data, err := json.MarshalIndent(&keyMap, "", " ")
7366
if err != nil {
7467
log.Error().Err(err).Msg("")
7568
return
7669
}
7770
outFile.Write(data)
78-
7971
}

plugin/emojimix/map.proto

Lines changed: 0 additions & 15 deletions
This file was deleted.

plugin/emojimix/mix.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
package emojimix
33

44
import (
5-
emoji_map "MiniBot/plugin/emojimix/proto"
65
"MiniBot/utils/path"
6+
"encoding/json"
77
"os"
88
"path/filepath"
99
"strconv"
@@ -14,22 +14,19 @@ import (
1414

1515
emoji "github.com/Andrew-M-C/go.emoji"
1616
"github.com/rs/zerolog/log"
17-
"google.golang.org/protobuf/proto"
1817
)
1918

20-
var emojiMap = emoji_map.OuterMap{OuterMap: map[string]*emoji_map.InnerMap{}}
19+
var emojiMap = map[string]string{}
2120
var pluginName = "emojimix"
2221
var dataPath = path.GetPluginDataPath()
2322

24-
// const bed = "https://www.gstatic.com/android/keyboard/emojikitchen/%d/u%x/u%x_u%x.png"
25-
2623
func init() {
27-
data, err := os.ReadFile(filepath.Join(dataPath, "outer_map.bin"))
24+
data, err := os.ReadFile(filepath.Join(dataPath, "key_map.json"))
2825
if err != nil {
2926
log.Error().Err(err).Str("name", pluginName).Msg("")
3027
return
3128
}
32-
if err := proto.Unmarshal(data, &emojiMap); err != nil {
29+
if err := json.Unmarshal(data, &emojiMap); err != nil {
3330
log.Error().Err(err).Str("name", pluginName)
3431
return
3532
}
@@ -70,11 +67,10 @@ func match(ctx *zero.Ctx) bool {
7067
}
7168

7269
func setUrl(i string, j string, ctx *zero.Ctx) bool {
73-
if interMap, ok := emojiMap.OuterMap[i]; ok {
74-
if url, ok := interMap.InnerMap[j]; ok {
75-
ctx.State["emojimix"] = url
76-
return true
77-
}
70+
url := getValue(emojiMap, i, j)
71+
if url != "" {
72+
ctx.State["emojimix"] = url
73+
return true
7874
}
7975
return false
8076
}

plugin/emojimix/pair_key_map.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package emojimix
2+
3+
import "fmt"
4+
5+
type KeyPair struct {
6+
Key1 string
7+
Key2 string
8+
}
9+
10+
// 实现接口,以便可以用作 map 的键
11+
func (k KeyPair) String() string {
12+
// 确保 Key1 和 Key2 的顺序不影响组合键的唯一性
13+
if k.Key1 < k.Key2 {
14+
return fmt.Sprintf("%s|%s", k.Key1, k.Key2)
15+
}
16+
return fmt.Sprintf("%s|%s", k.Key2, k.Key1)
17+
}
18+
19+
// 添加值的函数
20+
func addValue(data map[string]string, key1 string, key2 string, value string) {
21+
pair := KeyPair{key1, key2}
22+
data[pair.String()] = value
23+
}
24+
25+
// 获取值的函数
26+
func getValue(data map[string]string, key1 string, key2 string) string {
27+
pair := KeyPair{key1, key2}
28+
return data[pair.String()]
29+
}

plugin/emojimix/proto/map.pb.go

Lines changed: 0 additions & 224 deletions
This file was deleted.

0 commit comments

Comments
 (0)