|
| 1 | +// Package abineundo provides an explicit "from the beginning" (Latin: "ab ineundō") |
| 2 | +// initialization anchor. |
| 3 | +// |
| 4 | +// Name origin: |
| 5 | +// |
| 6 | +// Latin phrase "ab ineundō" meaning "from which is to be begun". |
| 7 | +// |
| 8 | +// Purpose: |
| 9 | +// |
| 10 | +// Place this package at the very top of top-level main.go so its init (present |
| 11 | +// or future) executes before other plugin packages, filling in a predictable |
| 12 | +// plugin priority. |
| 13 | +// |
| 14 | +// Typical usage: |
| 15 | +// |
| 16 | +// import ( |
| 17 | +// _ "github.com/your/module/abineundo" // priority anchor |
| 18 | +// // ... other imports ... |
| 19 | +// ) |
| 20 | +// |
| 21 | +// A blank identifier import preserves ordering side-effects without expanding the |
| 22 | +// exported API surface. |
| 23 | +// |
| 24 | +// (No further code is required here; the package's presence alone defines ordering semantics.) |
| 25 | +package abineundo |
| 26 | + |
| 27 | +import ( |
| 28 | + "bufio" |
| 29 | + _ "embed" |
| 30 | + "regexp" |
| 31 | + "strings" |
| 32 | + |
| 33 | + "github.com/FloatTech/zbputils/control" |
| 34 | + "github.com/sirupsen/logrus" |
| 35 | +) |
| 36 | + |
| 37 | +//go:embed ref/main/main.go |
| 38 | +var maincode string |
| 39 | + |
| 40 | +//go:embed ref/custom/register.go |
| 41 | +var customcode string |
| 42 | + |
| 43 | +const ( |
| 44 | + statusnone = iota |
| 45 | + statushigh |
| 46 | + statushighend |
| 47 | + statusmid |
| 48 | + statusmidend |
| 49 | + statuslow |
| 50 | + statuslowend |
| 51 | +) |
| 52 | + |
| 53 | +var ( |
| 54 | + priore = regexp.MustCompile(`^\t// -{28}(高|中|低)优先级区-{28} //$`) |
| 55 | + mainpluginre = regexp.MustCompile(`^\t_ "github\.com/FloatTech/ZeroBot-Plugin/plugin/(\w+)"\s+// `) |
| 56 | + custpluginre = regexp.MustCompile(`^\t_ "github\.com/FloatTech/ZeroBot-Plugin/custom/plugin/(\w+)"\s+// `) |
| 57 | +) |
| 58 | + |
| 59 | +func init() { |
| 60 | + highprios := make([]string, 0, 64) |
| 61 | + midprios := make([]string, 0, 64) |
| 62 | + lowprios := make([]string, 0, 64) |
| 63 | + |
| 64 | + status := statusnone |
| 65 | + scanner := bufio.NewScanner(strings.NewReader(maincode)) |
| 66 | + for scanner.Scan() { |
| 67 | + line := scanner.Text() |
| 68 | + |
| 69 | + prioword := "" |
| 70 | + match := priore.FindStringSubmatch(line) |
| 71 | + if len(match) > 1 { |
| 72 | + prioword = match[1] |
| 73 | + } |
| 74 | + switch prioword { |
| 75 | + case "高": |
| 76 | + switch status { |
| 77 | + case statusnone: |
| 78 | + status = statushigh |
| 79 | + case statushigh: |
| 80 | + status = statushighend |
| 81 | + default: |
| 82 | + panic("unexpected") |
| 83 | + } |
| 84 | + case "中": |
| 85 | + switch status { |
| 86 | + case statushighend: |
| 87 | + status = statusmid |
| 88 | + case statusmid: |
| 89 | + status = statusmidend |
| 90 | + default: |
| 91 | + panic("unexpected") |
| 92 | + } |
| 93 | + case "低": |
| 94 | + switch status { |
| 95 | + case statusmidend: |
| 96 | + status = statuslow |
| 97 | + case statuslow: |
| 98 | + status = statuslowend |
| 99 | + default: |
| 100 | + panic("unexpected") |
| 101 | + } |
| 102 | + default: |
| 103 | + switch status { |
| 104 | + case statusnone: // 还未开始匹配 |
| 105 | + continue |
| 106 | + case statuslowend: // 匹配已结束 |
| 107 | + break |
| 108 | + default: // 继续匹配插件 |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // 在对应优先级区域内匹配插件 |
| 113 | + if matches := mainpluginre.FindStringSubmatch(line); len(matches) > 1 { |
| 114 | + name := matches[1] |
| 115 | + switch status { |
| 116 | + case statushigh: |
| 117 | + highprios = append(highprios, name) |
| 118 | + case statusmid: |
| 119 | + midprios = append(midprios, name) |
| 120 | + case statuslow: |
| 121 | + lowprios = append(lowprios, name) |
| 122 | + default: // 在不该匹配到插件的区域匹配到 |
| 123 | + panic("unexpected") |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + custprios := make([]string, 0, 64) |
| 129 | + |
| 130 | + scanner = bufio.NewScanner(strings.NewReader(customcode)) |
| 131 | + for scanner.Scan() { |
| 132 | + line := scanner.Text() |
| 133 | + |
| 134 | + if matches := custpluginre.FindStringSubmatch(line); len(matches) > 1 { |
| 135 | + custprios = append(custprios, matches[1]) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // 生成最终插件优先级表 |
| 140 | + m := make(map[string]uint64, 4*(len(highprios)+len(midprios)+len(lowprios)+len(custprios))) |
| 141 | + i := 0 |
| 142 | + for _, name := range highprios { |
| 143 | + m[name] = (uint64(i) + 1) * 10 |
| 144 | + logrus.Debugln("[ab] set high plugin", name, "prio to", m[name]) |
| 145 | + i++ |
| 146 | + } |
| 147 | + for _, name := range custprios { |
| 148 | + m[name] = (uint64(i) + 1) * 10 |
| 149 | + logrus.Debugln("[ab] set cust plugin", name, "prio to", m[name]) |
| 150 | + i++ |
| 151 | + } |
| 152 | + for _, name := range midprios { |
| 153 | + m[name] = (uint64(i) + 1) * 10 |
| 154 | + logrus.Debugln("[ab] set mid plugin", name, "prio to", m[name]) |
| 155 | + i++ |
| 156 | + } |
| 157 | + for _, name := range lowprios { |
| 158 | + m[name] = (uint64(i) + 1) * 10 |
| 159 | + logrus.Debugln("[ab] set low plugin", name, "prio to", m[name]) |
| 160 | + i++ |
| 161 | + } |
| 162 | + |
| 163 | + control.LoadCustomPriority(m) |
| 164 | +} |
0 commit comments