Skip to content

Commit cb83fcc

Browse files
committed
feat: lua 添加一些 string 模块常用函数
1 parent 0f24c9a commit cb83fcc

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed

script/analysis_ani.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/zeromake/pkg/binaryutil"
88
)
99

10-
func main() {
10+
func main1() {
1111
input, err := os.Open(os.Args[1])
1212
if err != nil {
1313
log.Fatal(err)

script/analysis_stand.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strings"
8+
9+
"github.com/zeromake/pkg/binaryutil"
10+
)
11+
12+
func to_string(buf []uint8) string {
13+
offset := 32
14+
for offset > 0 && buf[offset-1] == 0 {
15+
offset--
16+
}
17+
return string(buf[:offset])
18+
}
19+
20+
func main() {
21+
input, err := os.Open(os.Args[1])
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
defer input.Close()
26+
r := binaryutil.NewDecoder(input)
27+
output, err := os.OpenFile(
28+
os.Args[2],
29+
os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
30+
0644,
31+
)
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
defer output.Close()
36+
output.WriteString("fill,img1,ani,img2,x,y\n")
37+
38+
for r.Error() == nil {
39+
r.Skip(4)
40+
if r.Error() != nil {
41+
break
42+
}
43+
imgBuf := make([]uint8, 32)
44+
r.ReadLE(imgBuf)
45+
output.WriteString(to_string(imgBuf))
46+
output.Write([]byte{','})
47+
r.ReadLE(imgBuf)
48+
output.WriteString(to_string(imgBuf))
49+
output.Write([]byte{','})
50+
r.ReadLE(imgBuf)
51+
output.WriteString(to_string(imgBuf))
52+
r.ReadLE(imgBuf)
53+
if imgBuf[0] == 0 {
54+
r.Skip(8)
55+
output.Write([]byte{'\n'})
56+
continue
57+
}
58+
img := to_string(imgBuf)
59+
output.Write([]byte{','})
60+
output.WriteString(img)
61+
output.Write([]byte{','})
62+
x := r.Read32LE()
63+
y := r.Read32LE()
64+
if strings.HasPrefix(img, "UMI_") {
65+
if strings.HasPrefix(img, "UMI_A") {
66+
x -= 185
67+
y -= 57
68+
} else if strings.HasPrefix(img, "UMI_0101") || strings.HasPrefix(img, "UMI_0201") {
69+
x -= 237
70+
y -= 57
71+
// x -= 237
72+
// y -= 57
73+
} else {
74+
x -= 280
75+
y -= 66
76+
}
77+
}
78+
output.WriteString(fmt.Sprintf("%d,%d", x, y))
79+
output.Write([]byte{'\n'})
80+
}
81+
}

src/LUAHandler.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ static int NSCall(lua_State *state) {
994994
lh->sh->enterExternalScript(cmd_buf);
995995
lh->ons->runScript();
996996
lh->sh->leaveExternalScript();
997+
return 0;
997998
}
998999

9991000
#define LUA_FUNC_LUT(s) \
@@ -1115,6 +1116,50 @@ static int fmt_println(lua_State *state) {
11151116
return 0;
11161117
}
11171118

1119+
static int string_split(lua_State* state) {
1120+
const char* input = luaL_checkstring(state, 1);
1121+
size_t sep_len = 0;
1122+
const char* sep = luaL_checklstring(state, 2, &sep_len);
1123+
size_t prev = 0;
1124+
lua_newtable(state);
1125+
size_t i = 0;
1126+
while (true) {
1127+
i++;
1128+
const char* next = strstr(input, sep);
1129+
if (next) {
1130+
lua_pushlstring(state, input, next - input);
1131+
lua_rawseti(state, -2, i);
1132+
next += sep_len;
1133+
input = next;
1134+
continue;
1135+
}
1136+
lua_pushstring(state, input);
1137+
lua_rawseti(state, -2, i);
1138+
break;
1139+
}
1140+
return 1;
1141+
}
1142+
1143+
static int string_startswith(lua_State* state) {
1144+
size_t cmp1_len = 0;
1145+
const char* cmp1 = luaL_checklstring(state, 1, &cmp1_len);
1146+
size_t cmp2_len = 0;
1147+
const char* cmp2 = luaL_checklstring(state, 2, &cmp2_len);
1148+
bool ok = !strncmp(cmp1, cmp2, cmp2_len);
1149+
lua_pushboolean(state, ok);
1150+
return 1;
1151+
}
1152+
1153+
static int string_endswith(lua_State* state) {
1154+
size_t cmp1_len = 0;
1155+
const char* cmp1 = luaL_checklstring(state, 1, &cmp1_len);
1156+
size_t cmp2_len = 0;
1157+
const char* cmp2 = luaL_checklstring(state, 2, &cmp2_len);
1158+
bool ok = !strncmp(cmp1+cmp1_len-cmp2_len, cmp2, cmp2_len);
1159+
lua_pushboolean(state, ok);
1160+
return 1;
1161+
}
1162+
11181163
static const struct luaL_Reg module_nsutf[] = {
11191164
LUA_FUNC_LUT(nsutf_from_ansi), LUA_FUNC_LUT(nsutf_to_ansi), {NULL, NULL}};
11201165

@@ -1187,6 +1232,14 @@ void LUAHandler::init(
11871232
luaL_register(state, "nsutf", module_nsutf);
11881233
luaL_register(state, "dpshadow", module_dpshadow);
11891234
luaL_register(state, "fmt", module_fmt);
1235+
1236+
lua_getglobal(state, "string");
1237+
lua_pushcfunction(state, string_split);
1238+
lua_setfield(state, -2, "split");
1239+
lua_pushcfunction(state, string_startswith);
1240+
lua_setfield(state, -2, "startswith");
1241+
lua_pushcfunction(state, string_endswith);
1242+
lua_setfield(state, -2, "endswith");
11901243
#endif
11911244

11921245
lua_pushlightuserdata(state, this);

src/ons_cache.h

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <optional>
1111
#include <string>
1212
#include <utility>
13-
#define USE_IMAGE_CACHE
1413

1514
namespace onscache {
1615
typedef caches::fixed_sized_cache<std::string,

src/private/uitls.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void split(std::vector<std::string> &output,
4141
while (true) {
4242
size_t pos = input.find(sep, prev);
4343
if (pos != std::string::npos) {
44-
output.push_back(std::move(input.substr(prev, pos)));
44+
output.push_back(std::move(input.substr(prev, pos - prev)));
4545
prev = pos + 1;
4646
continue;
4747
}

0 commit comments

Comments
 (0)