Skip to content

Commit

Permalink
fix(pc): rename
Browse files Browse the repository at this point in the history
修复未创建角色卡时使用.pc nn导致骰娘崩溃的bug
  • Loading branch information
mystringEmpty committed Aug 27, 2024
1 parent 6192752 commit 3ace6d9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
5 changes: 2 additions & 3 deletions Dice/CharacterCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,16 +545,15 @@ int Player::removeCard(const string& name){
while (!mCardList.count(indexMax))indexMax--;
return 0;
}
int Player::renameCard(const string& name, const string& name_new) {
int Player::renameCard(PC pc, const string& name_new) {
std::lock_guard<std::mutex> lock_queue(cardMutex);
if (name_new.empty())return -3;
if (NameList.count(name_new))return -4;
if (name_new.find(":") != string::npos)return -6;
auto pc{ NameList[name] };
const auto i = pc->getID();
if (pc->locked("n"))return -22;
NameList[name_new] = pc;
NameList.erase(name);
NameList.erase(pc->getName());
pc->setName(name_new);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Dice/CharacterCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class Player

int removeCard(const string& name);

int renameCard(const string& name, const string& name_new);
int renameCard(PC pc, const string& name_new);

int copyCard(const string& name1, const string& name2, long long group = 0);

Expand Down
5 changes: 3 additions & 2 deletions Dice/DiceEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3414,8 +3414,9 @@ int DiceEvent::InnerOrder() {
}
else if (strOption == "nn") {
string& strPC{ (at("new_name") = strip(filter_CQcode(readRest(),fromChat.gid))).text};
set("old_name",pl[fromChat.gid]->getName());
if (!(resno = pl.renameCard(get_str("old_name"), strPC)))replyMsg("strPcCardRename");
auto pc = pl[fromChat.gid];
set("old_name", pc->getName());
if (!(resno = pl.renameCard(pc, strPC)))replyMsg("strPcCardRename");
else set("char", strPC);
}
else if (strOption == "del") {
Expand Down
80 changes: 40 additions & 40 deletions Dice/DiceLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ void lua_set_field(lua_State* L, int idx, const string& str) {
}
AttrIndex lua_to_index(lua_State* L, int idx){
if (lua_type(L, idx) == LUA_TSTRING) {
return lua_to_gbstring(L, idx);
return lua_to_u8string(L, idx);
}
else if (lua_isnumber(L, idx)) {
return lua_tonumber(L, idx);
}
return lua_to_gbstring(L, idx);
return lua_to_u8string(L, idx);
}

void lua_push_Context(lua_State* L, const ptr<AnysTable>& obj) {
Expand Down Expand Up @@ -237,7 +237,7 @@ AttrVar lua_to_attr(lua_State* L, int idx = -1) {
}
break;
case LUA_TSTRING:
return lua_to_gbstring(L, idx);
return lua_to_u8string(L, idx);
break;
case LUA_TFUNCTION:
return lua_to_chunk(L, idx);
Expand All @@ -256,7 +256,7 @@ AttrVar lua_to_attr(lua_State* L, int idx = -1) {
tab->list->push_back(lua_to_attr(L, -1));
}
else {
tab->dict.emplace(lua_to_gbstring(L, -2), lua_to_attr(L, -1));
tab->dict.emplace(lua_to_u8string(L, -2), lua_to_attr(L, -1));
}
lua_pop(L, 1);
}
Expand Down Expand Up @@ -295,7 +295,7 @@ AnysTable lua_to_table(lua_State* L, int idx = -1) {
lua_pushnil(L);
while (lua_next(L, idx)) {
if (lua_isstring(L, -2) && !lua_isnil(L, -1)) {
tab.set(lua_to_gbstring(L, -2),lua_to_attr(L, -1));
tab.set(lua_to_u8string(L, -2),lua_to_attr(L, -1));
}
else {
tab.set(lua_to_int(L, -2) - 1, lua_to_attr(L, -1));
Expand All @@ -309,7 +309,7 @@ AttrVars lua_to_dict(lua_State* L, int idx = -1) {
if (idx < 0)idx = lua_gettop(L) + idx + 1;
lua_pushnil(L);
while (lua_next(L, idx)) {
tab[lua_to_gbstring(L, -2)] = lua_to_attr(L, -1);
tab[lua_to_u8string(L, -2)] = lua_to_attr(L, -1);
lua_pop(L, 1);
}
return tab;
Expand Down Expand Up @@ -337,7 +337,7 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) {
if (!luaFunc) {
//执行文件
if (lua_pcall(L, 0, 2, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "运行" + filename + "失败:" + pErrorMsg, 0b10);
msg->set("lang", "Lua");
msg->reply(getMsg("strScriptRunErr"));
Expand All @@ -346,7 +346,7 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) {
}
//加载文件,执行全局函数
else if (lua_pcall(L, 0, 0, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "运行" + filename + "失败:" + pErrorMsg, 0b10);
msg->set("lang", "Lua");
msg->reply(getMsg("strScriptRunErr"));
Expand All @@ -356,7 +356,7 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) {
lua_getglobal(L, luaFunc.to_str().c_str());
lua_push_Context(L, msg->shared_from_this());
if (lua_pcall(L, 1, 2, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "调用" + filename + "函数" + luaFunc.to_str() + "失败!\n" + pErrorMsg, 0b10);
msg->set("lang", "Lua");
msg->reply(getMsg("strScriptRunErr"));
Expand All @@ -370,7 +370,7 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) {
if (bytes.isUTF8)UTF8Luas.insert(L);
if (lua_load(L, lua_reader, &bytes, msg->get_str("reply_title").c_str(), "bt")
|| (lua_push_Context(L, msg->shared_from_this()), lua_pcall(L, 1, 2, 0))) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "运行Lua字节码" + msg->get_str("reply_title") + "失败!\n" + pErrorMsg, 0b10);
msg->set("lang", "Lua");
msg->reply(getMsg("strScriptRunErr"));
Expand All @@ -380,7 +380,7 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) {
//执行语句
else if (luaFile.empty() &&
(luaL_loadstring(L, lua.to_str().c_str()) || lua_pcall(L, 0, 2, 0))) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "调用" + msg->get_str("reply_title") + "Lua代码失败!\n" + pErrorMsg, 0b10);
msg->set("lang", "Lua");
msg->reply(getMsg("strScriptRunErr"));
Expand All @@ -389,7 +389,7 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) {
if (lua_gettop(L)) {
if (!lua_isnoneornil(L, 1)) {
if (lua_isstring(L, 1)) {
msg->reply(lua_to_gbstring(L, 1));
msg->reply(lua_to_u8string(L, 1));
}
else {
console.log(getMsg("strSelfName") + "调用" + msg->get_str("reply_title") + "脚本返回值格式错误(" + LuaTypes[lua_type(L, 1)] + ")!", 0b10);
Expand All @@ -400,7 +400,7 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) {
}
if (!lua_isnoneornil(L, 2)) {
if (lua_isstring(L, 2)) {
msg->replyHidden(lua_to_gbstring(L, 2));
msg->replyHidden(lua_to_u8string(L, 2));
}
else {
console.log(getMsg("strSelfName") + "调用" + msg->get_str("reply_title") + "脚本返回值格式错误(" + LuaTypes[lua_type(L, 2)] + ")!", 1);
Expand All @@ -422,7 +422,7 @@ bool lua_call_event(const ptr<AnysTable>& eve, const AttrVar& lua) {
lua_setglobal(L, "event");
if (isFile) {
if (lua_pcall(L, 0, 2, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "运行" + luas + "失败:" + pErrorMsg, 0b10);
return 0;
}
Expand All @@ -432,13 +432,13 @@ bool lua_call_event(const ptr<AnysTable>& eve, const AttrVar& lua) {
if (bytes.isUTF8)UTF8Luas.insert(L);
if (lua_load(L, lua_reader, (void*)&bytes, eve->get_str("Type").c_str(), "bt")
|| lua_pcall(L, 0, 2, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "调用事件lua失败!\n" + pErrorMsg, 0b10);
return false;
}
}
else if (luaL_loadstring(L, luas.c_str()) || lua_pcall(L, 0, 2, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "调用事件lua失败!\n" + pErrorMsg, 0b10);
return false;
}
Expand All @@ -465,14 +465,14 @@ bool lua_call_task(const AttrVars& task) {
string fileGB18030(UTF8toGBK(file, true));
#endif
if (lua_pcall(L, 0, 0, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "运行lua文件" + fileGB18030 + "失败:" + pErrorMsg, 0b10);
return 0;
}
string func{ task.at("func").to_str() };
lua_getglobal(L, func.c_str());
if (lua_pcall(L, 0, 0, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "调用" + fileGB18030 + "函数" + func + "失败!\n" + pErrorMsg, 0b10);
return false;
}
Expand Down Expand Up @@ -516,7 +516,7 @@ int lua_Set_totable(lua_State* L) {
int lua_Set_index(lua_State* L) {
LUA2SET(L);
if (lua_type(L, 2) == LUA_TSTRING) {
string key{ lua_to_gbstring(L, 2) };
string key{ lua_to_u8string(L, 2) };
if (key == "in")lua_pushcfunction(L, lua_Set_in);
else if (key == "add")lua_pushcfunction(L, lua_Set_add);
else if (key == "remove")lua_pushcfunction(L, lua_Set_remove);
Expand Down Expand Up @@ -567,7 +567,7 @@ int selfData_get(lua_State* L) {
return 1;
}
else if(file.data.is_table()){
string key{ lua_to_gbstring(L, 2) };
string key{ lua_to_u8string(L, 2) };
if (file.data.table->has(key)) {
lua_push_attr(L, file.data.table->get(key));
return 1;
Expand All @@ -586,7 +586,7 @@ int selfData_set(lua_State* L) {
file.data = lua_to_attr(L, 2);
}
else if (std::lock_guard<std::mutex> lock(file.exWrite); lua_isstring(L, 2) && file.data.is_table()) {
string key{ lua_to_gbstring(L, 2) };
string key{ lua_to_u8string(L, 2) };
if (lua_isnoneornil(L, 3)) {
file.data.table->reset(key);
}
Expand All @@ -599,7 +599,7 @@ int selfData_set(lua_State* L) {
int SelfData_index(lua_State* L) {
if (lua_gettop(L) < 2)return 0;
SelfData& file{ **(SelfData**)luaL_checkudata(L, 1, "SelfData") };
string key{ lua_to_gbstring(L, 2) };
string key{ lua_to_u8string(L, 2) };
if (key == "get") {
lua_pushcfunction(L, selfData_get);
}
Expand All @@ -615,7 +615,7 @@ int SelfData_index(lua_State* L) {
int SelfData_newindex(lua_State* L) {
if (lua_gettop(L) < 2)return 0;
SelfData& file{ **(SelfData**)luaL_checkudata(L, 1, "SelfData") };
string key{ lua_to_gbstring(L, 2) };
string key{ lua_to_u8string(L, 2) };
if (file.data.is_null())file.data = AnysTable();
else if (!file.data.is_table())return 0;
if (std::lock_guard<std::mutex> lock(file.exWrite); lua_isnoneornil(L,3)) {
Expand Down Expand Up @@ -650,7 +650,7 @@ static int luaopen_SelfData(lua_State* L) {
#define LUADEF(name) static int lua_dice_##name(lua_State* L)
//输出日志
LUADEF(log) {
if (string info{ lua_to_gbstring(L, 1) }; !info.empty()) {
if (string info{ lua_to_u8string(L, 1) }; !info.empty()) {
int note_lv{ 0 };
for (int idx = lua_gettop(L); idx > 1; --idx) {
if (lua_isinteger(L, idx)) {
Expand Down Expand Up @@ -711,12 +711,12 @@ LUADEF(loadLua) {
return 0;
}
if (luaL_loadstring(L, strLua.c_str())) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "读取" + UTF8toGBK(pathFile.u8string()) + "失败:"+ pErrorMsg, 0b10);
return 0;
}
if (lua_pcall(L, 0, 1, 0)) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
console.log(getMsg("strSelfName") + "运行" + UTF8toGBK(pathFile.u8string()) + "失败:"+ pErrorMsg, 0b10);
return 0;
}
Expand Down Expand Up @@ -755,7 +755,7 @@ LUADEF(getGroupConf) {
if (top < 1)return 0;
string item;
if (lua_isstring(L, 2)) {
if ((item = lua_to_gbstring(L, 2))[0] == '&')item = fmt->format(item);
if ((item = lua_to_u8string(L, 2))[0] == '&')item = fmt->format(item);
}
if (lua_isnil(L, 1)) {
if (item.empty())return 0;
Expand Down Expand Up @@ -827,7 +827,7 @@ LUADEF(getGroupConf) {
}
LUADEF(setGroupConf) {
long long id{ lua_to_int_or_zero(L, 1) };
string item{ lua_to_gbstring(L, 2) };
string item{ lua_to_u8string(L, 2) };
if (!id || item.empty())return 0;
if (item[0] == '&')item = fmt->format(item);
Chat& grp{ chat(id) };
Expand All @@ -836,7 +836,7 @@ LUADEF(setGroupConf) {
if (size_t l{ item.find_first_of(chDigit) }; l != string::npos) {
uid = stoll(item.substr(l, item.find_first_not_of(chDigit, l) - l));
}
string card{ lua_to_gbstring(L, 3) };
string card{ lua_to_u8string(L, 3) };
DD::setGroupCard(id, uid, card);
return 0;
}
Expand All @@ -852,7 +852,7 @@ LUADEF(getUserConf) {
else if (top < 1)return 0;
string item;
if (lua_isstring(L, 2)) {
if ((item = lua_to_gbstring(L, 2))[0] == '&')item = fmt->format(item);
if ((item = lua_to_u8string(L, 2))[0] == '&')item = fmt->format(item);
}
if (lua_isnil(L, 1)) {
if (item.empty())return 0;
Expand Down Expand Up @@ -882,7 +882,7 @@ LUADEF(getUserConf) {
LUADEF(setUserConf) {
long long uid{ lua_to_int_or_zero(L, 1) };
if (!uid)return 0;
string item{ lua_to_gbstring(L, 2) };
string item{ lua_to_u8string(L, 2) };
if (item.empty())return 0;
if (item[0] == '&')item = fmt->format(item);
if (item == "trust") {
Expand All @@ -902,7 +902,7 @@ LUADEF(setUserConf) {
getUser(uid).rmNick(gid);
}
else {
getUser(uid).setNick(gid, lua_to_gbstring(L, 3));
getUser(uid).setNick(gid, lua_to_u8string(L, 3));
}
}
else if (lua_isnoneornil(L, 3)) {
Expand All @@ -917,7 +917,7 @@ LUADEF(getUserToday) {
else if (top < 1)return 0;
string item;
if (lua_isstring(L, 2)) {
if ((item = lua_to_gbstring(L, 2))[0] == '&')item = fmt->format(item);
if ((item = lua_to_u8string(L, 2))[0] == '&')item = fmt->format(item);
}
if (lua_isnil(L, 1)) {
if (item.empty())return 0;
Expand Down Expand Up @@ -950,7 +950,7 @@ LUADEF(getUserToday) {
}
LUADEF(setUserToday) {
long long uid{ lua_to_int_or_zero(L, 1) };
string item{ lua_to_gbstring(L, 2) };
string item{ lua_to_u8string(L, 2) };
if (item.empty())return 0;
if (item[0] == '&')item = fmt->format(item);
today->set(uid, item, lua_to_attr(L, 3));
Expand All @@ -963,7 +963,7 @@ LUADEF(getPlayerCardAttr) {
else if (argc < 3)return 0;
long long uid{ lua_to_int_or_zero(L, 1) };
long long group{ lua_to_int_or_zero(L, 2) };
string key{ lua_to_gbstring(L, 3) };
string key{ lua_to_u8string(L, 3) };
if (!uid || key.empty())return 0;
PC pc = getPlayer(uid)[group];
if (auto val{ pc->get(key) };!val.is_null()) {
Expand All @@ -981,7 +981,7 @@ LUADEF(getPlayerCardAttr) {
LUADEF(getPlayerCard) {
if (long long uid{ lua_to_int_or_zero(L, 1) }) {
if (lua_type(L, 2) == LUA_TSTRING) {
lua_push_Actor(L, getPlayer(uid).getCard(lua_to_gbstring(L, 2)));
lua_push_Actor(L, getPlayer(uid).getCard(lua_to_u8string(L, 2)));
}
else {
long long gid{ lua_to_int_or_zero(L, 2) };
Expand All @@ -994,12 +994,12 @@ LUADEF(getPlayerCard) {
LUADEF(setPlayerCardAttr) {
long long plQQ{ lua_to_int_or_zero(L, 1) };
long long group{ lua_to_int_or_zero(L, 2) };
string item{ lua_to_gbstring(L, 3) };
string item{ lua_to_u8string(L, 3) };
if (!plQQ || item.empty())return 0;
//参数4为空则视为删除,__Name除外
auto pc = getPlayer(plQQ)[group];
if (item == "__Name") {
getPlayer(plQQ).renameCard(pc->getName(), lua_to_gbstring(L, 4));
getPlayer(plQQ).renameCard(pc, lua_to_u8string(L, 4));
}
else if (lua_isnoneornil(L, 4)) {
pc->erase(item);
Expand All @@ -1024,7 +1024,7 @@ LUADEF(sleepTime) {
}

LUADEF(drawDeck) {
string nameDeck{ lua_to_gbstring(L, 3) };
string nameDeck{ lua_to_u8string(L, 3) };
if (nameDeck.empty())return 0;
long long fromGID{ lua_to_int_or_zero(L, 1) };
long long fromUID{ lua_to_int_or_zero(L, 2) };
Expand Down Expand Up @@ -1618,7 +1618,7 @@ void DiceModManager::loadPlugin(ResList& res) {
lua_newtable(L);
lua_setglobal(L, "task_call");
if (luaL_dofile(L, file.c_str())) {
string pErrorMsg = lua_to_gbstring_from_native(L, -1);
string pErrorMsg = lua_to_u8string_from_native(L, -1);
err << pErrorMsg;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion Dice/ManagerSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class User :public AnysTable

User& trust(int n);

[[nodiscard]] bool empty() const;
[[nodiscard]] bool empty() const override;

void setConf(const string& key, const AttrVar& val);
void rmConf(const string& key);
Expand Down

0 comments on commit 3ace6d9

Please sign in to comment.