Skip to content

Commit

Permalink
feat: 添加一个 NSReadFile lua 调用
Browse files Browse the repository at this point in the history
- feat: lua 的 NSCOM 支持自动参数传递
- feat: lua 的 NSSpLoad 支持 lsp 最后面的可选位置参数
  • Loading branch information
zeromake committed Dec 3, 2023
1 parent 3da0b9c commit 889a0a1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
46 changes: 44 additions & 2 deletions src/LUAHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,16 @@ int NSSpLoad(lua_State *state) {

int no = luaL_checkinteger(state, 1);
const char *str = luaL_checkstring(state, 2);
int x = lh->ons->getWidth() + 1, y = 0;
if (lua_isnumber(state, 3)) {
x = luaL_checkinteger(state, 3);
}
if (lua_isnumber(state, 4)) {
y = luaL_checkinteger(state, 4);
}

CMD_BUF_SNPRINTF(
cmd_buf, "_lsp %d, \"%s\", %d, 0", no, str, lh->ons->getWidth() + 1);
cmd_buf, "_lsp %d, \"%s\", %d, %d", no, str, x, y);
lh->sh->enterExternalScript(cmd_buf);
lh->ons->runScript();
lh->sh->leaveExternalScript();
Expand All @@ -873,7 +880,7 @@ int NSSpMove(lua_State *state) {
int no = luaL_checkinteger(state, 1);
int x = luaL_checkinteger(state, 2);
int y = luaL_checkinteger(state, 3);
int alpha = luaL_checkinteger(state, 4);
int alpha = lua_isnumber(state, 4) ? luaL_checkinteger(state, 4) : 255;

CMD_BUF_SNPRINTF(cmd_buf, "_amsp %d, %d, %d, %d", no, x, y, alpha);
lh->sh->enterExternalScript(cmd_buf);
Expand Down Expand Up @@ -947,6 +954,24 @@ static int NSCurrentDir(lua_State *state) {
return 1;
}

static int NSReadFile(lua_State *state) {
lua_getglobal(state, ONS_LUA_HANDLER_PTR);
LUAHandler *lh = (LUAHandler *)lua_topointer(state, -1);
const char *str = luaL_checkstring(state, 1);
unsigned long length = lh->sh->cBR->getFileLength(str);
if (length == 0) {
utils::printInfo("cannot open %s\n", str);
return 0;
}
std::vector<unsigned char> buffer;
buffer.resize(length+1);
int location;
lh->sh->cBR->getFile(str, buffer.data(), &location);
buffer[length] = 0;
lua_pushstring(state, (char *)buffer.data());
return 1;
}

#define LUA_FUNC_LUT(s) \
{ #s, s }
#define LUA_FUNC_LUT_DUMMY(s) \
Expand Down Expand Up @@ -1010,6 +1035,7 @@ static const struct luaL_Reg lua_lut[] = {LUA_FUNC_LUT(NSCurrentDir),
LUA_FUNC_LUT(NSSp2Visible),
LUA_FUNC_LUT(NSTimer),
LUA_FUNC_LUT(NSUpdate),
LUA_FUNC_LUT(NSReadFile),
{NULL, NULL}};

static int nsutf_from_ansi(lua_State *state) {
Expand Down Expand Up @@ -1188,6 +1214,22 @@ int LUAHandler::callFunction(bool is_callback, const char *cmd, void *data) {
buf = new char[strlen(p) + 1]{0};
memcpy(buf, p, strlen(p) + 1);
lua_pushstring(state, buf);
} else {
// 自动读取变量
sh->nextParam();
while (sh->current_variable.type != ScriptHandler::VAR_NONE) {
if (sh->current_variable.type == ScriptHandler::VAR_INT) {
lua_pushinteger(state, sh->current_variable_data.num);
num_argument_value++;
} else if (sh->current_variable.type == ScriptHandler::VAR_STR) {
lua_pushstring(state, sh->current_variable_data.str);
num_argument_value++;
}
if (!(sh->getEndStatus() & ScriptHandler::END_COMMA)) {
break;
}
sh->nextParam();
}
}

if (lua_pcall(state, num_argument_value, num_return_value, 0) != 0) {
Expand Down
18 changes: 18 additions & 0 deletions src/ScriptHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ ScriptHandler::~ScriptHandler() {
}

void ScriptHandler::reset() {
current_variable_data.reset(true);
for (int i = 0; i < variable_range; i++) variable_data[i].reset(true);

if (extended_variable_data) delete[] extended_variable_data;
Expand Down Expand Up @@ -478,6 +479,23 @@ bool ScriptHandler::readColor(utils::uchar4 *color) {
return true;
}

void ScriptHandler::nextParam() {
end_status = END_NONE;
current_variable.type = VAR_NONE;
current_script = next_script;
SKIP_SPACE(current_script);
if (*current_script == '%' || *current_script == '$') {
readVariable();
current_variable_data = getVariableData(current_variable.var_no);
} else if (*current_script >= '0' && *current_script <= '9') {
current_variable_data.num = readInt();
current_variable.type = VAR_INT;
} else {
utils::setStr(&current_variable_data.str, readStr(), -1);
current_variable.type = VAR_STR;
}
}

void ScriptHandler::skipToken() {
SKIP_SPACE(current_script);
char *buf = current_script;
Expand Down
2 changes: 2 additions & 0 deletions src/ScriptHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class ScriptHandler {
void readVariable(bool reread_flag = false);
void skipAnyVariable();
bool readColor(utils::uchar4 *color);
void nextParam();

// function for string access
inline char *getStringBuffer() { return string_buffer; };
Expand Down Expand Up @@ -262,6 +263,7 @@ class ScriptHandler {
int global_variable_border;

BaseReader *cBR;
struct VariableData current_variable_data;

private:
enum {
Expand Down
18 changes: 1 addition & 17 deletions src/ScriptParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,22 +769,6 @@ void ScriptParser::deleteNestInfo() {
last_nest_info = &root_nest_info;
}

void ScriptParser::setStr(char **dst, const char *src, int num) {
if (*dst) delete[] *dst;
*dst = NULL;

if (src) {
if (num >= 0) {
*dst = new char[num + 1]{0};
memcpy(*dst, src, num);
(*dst)[num] = '\0';
} else {
*dst = new char[strlen(src) + 1]{0};
strcpy(*dst, src);
}
}
}

void ScriptParser::setCurrentLabel(const char *label) {
current_label_info = script_h.lookupLabel(label);
current_line = script_h.getLineByAddress(current_label_info.start_address);
Expand Down Expand Up @@ -1009,4 +993,4 @@ bool ScriptParser::isEndKinsoku(const char *str) {
return true;
}
return false;
}
}
4 changes: 3 additions & 1 deletion src/ScriptParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ class ScriptParser {
char *save_dir_envdata;

void deleteNestInfo();
void setStr(char **dst, const char *src, int num = -1);
inline void setStr(char **dst, const char *src, int num = -1) {
utils::setStr(dst, src, num);
}

void readToken();

Expand Down
15 changes: 15 additions & 0 deletions src/private/uitls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,19 @@ int readColor(const char *buf, uchar4 *color) {
offset += size;
return offset;
}

void setStr(char **dst, const char *src, int num) {
if (*dst) delete[] *dst;
*dst = NULL;
if (src) {
if (num >= 0) {
*dst = new char[num + 1]{0};
memcpy(*dst, src, num);
(*dst)[num] = '\0';
} else {
*dst = new char[strlen(src) + 1]{0};
strcpy(*dst, src);
}
}
}
} // namespace utils
1 change: 1 addition & 0 deletions src/private/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ int readColor(const char *buf, uchar4 *color);
void split(std::vector<std::string> &output,
std::string &input,
const char sep);
void setStr(char **dst, const char *src, int num);

template <typename T>
T(min)
Expand Down

0 comments on commit 889a0a1

Please sign in to comment.