diff --git a/Dice/Dice.cpp b/Dice/Dice.cpp index 0772085c..4138efb0 100644 --- a/Dice/Dice.cpp +++ b/Dice/Dice.cpp @@ -304,20 +304,25 @@ EVE_Enable(eventEnable){ } loadData(); //初始化黑名单 - blacklist = make_unique(); - if (auto cnt = blacklist->loadJson(DiceDir / "conf" / "BlackList.json");cnt < 0) - { - blacklist->loadJson(fpFileLoc / "BlackMarks.json"); - cnt = blacklist->loadHistory(fpFileLoc); - if (cnt) { - blacklist->saveJson(DiceDir / "conf" / "BlackList.json"); - console.log("初始化不良记录" + to_string(cnt) + "条", 1); + try { + blacklist = make_unique(); + if (auto cnt = blacklist->loadJson(DiceDir / "conf" / "BlackList.json"); cnt < 0) + { + blacklist->loadJson(fpFileLoc / "BlackMarks.json"); + cnt = blacklist->loadHistory(fpFileLoc); + if (cnt) { + blacklist->saveJson(DiceDir / "conf" / "BlackList.json"); + console.log("初始化不良记录" + to_string(cnt) + "条", 1); + } + } + else { + DD::debugLog("读取不良记录" + to_string(cnt) + "条"); + if ((cnt = blacklist->loadJson(DiceDir / "conf" / "BlackListEx.json", true)) > 0) + DD::debugLog("合并外源不良记录" + to_string(cnt) + "条"); } } - else { - DD::debugLog("读取不良记录" + to_string(cnt) + "条"); - if ((cnt = blacklist->loadJson(DiceDir / "conf" / "BlackListEx.json", true)) > 0) - DD::debugLog("合并外源不良记录" + to_string(cnt) + "条"); + catch (const std::exception& e) { + console.log(string("读取/conf/BlackList.json失败!") + e.what(), 1, printSTNow()); } //读取用户数据 readUserData(); @@ -441,7 +446,7 @@ EVE_Enable(eventEnable){ getDiceList(); getExceptGroup(); isIniting.clear(); - fmt->call_hook_event(AnysTable{ AttrVars {{"Event","StartUp"}} }); + fmt->call_hook_event(std::make_shared( AttrVars{{"Event","StartUp"}} )); } mutex GroupAddMutex; @@ -622,7 +627,7 @@ EVE_PrivateMsg(eventPrivateMsg) }, chatInfo{ fromUID,0,0 })); return Msg->DiceFilter() || fmt->call_hook_event(Msg->merge({ {"hook","WhisperIgnored"}, - })); + }).shared_from_this()); } EVE_GroupMsg(eventGroupMsg) diff --git a/Dice/DiceJS.cpp b/Dice/DiceJS.cpp index 097a33ed..c6e1b020 100644 --- a/Dice/DiceJS.cpp +++ b/Dice/DiceJS.cpp @@ -488,10 +488,11 @@ QJSDEF(eventMsg) { ? AnysTable{ {{"fromMsg",fromMsg},{"gid",gid}, {"uid", uid}} } : AnysTable{ {{"fromMsg",fromMsg}, {"uid", uid}} }; } - std::thread th([=]() { - DiceEvent(*eve.p).virtualCall(); - }); - th.detach(); + //std::thread th([=]() { + shared_ptr msg{ std::make_shared(*eve.p) }; + msg->virtualCall(); + // }); + //th.detach(); return JS_TRUE; } QJSDEF(sendMsg) { diff --git a/Dice/DiceLua.cpp b/Dice/DiceLua.cpp index 196861b2..7eb17726 100644 --- a/Dice/DiceLua.cpp +++ b/Dice/DiceLua.cpp @@ -393,13 +393,13 @@ bool lua_msg_call(DiceEvent* msg, const AttrVar& lua) { } return true; } -bool lua_call_event(AttrObject eve, const AttrVar& lua) { +bool lua_call_event(const ptr& eve, const AttrVar& lua) { if (!Enabled)return false; string luas{ lua.to_str() }; bool isFile{ lua.is_character() && fmt->has_lua(luas) }; LuaState L{ fmt->lua_path(luas) }; if (!L)return false; - lua_push_Context(L, eve.p); + lua_push_Context(L, eve); lua_setglobal(L, "event"); if (isFile) { if (lua_pcall(L, 0, 2, 0)) { @@ -1064,11 +1064,11 @@ LUADEF(eventMsg) { ? AttrVars{ {"fromMsg",fromMsg},{"gid",fromGID}, {"uid", fromUID} } : AttrVars{ {"fromMsg",fromMsg}, {"uid", fromUID} }; } - std::thread th([=]() { - DiceEvent msg(eve); - msg.virtualCall(); - }); - th.detach(); + //std::thread th([=]() { + shared_ptr msg{ std::make_shared(eve) }; + msg->virtualCall(); + //}); + //th.detach(); return 0; } LUADEF(askExtra) { diff --git a/Dice/DiceLua.h b/Dice/DiceLua.h index 55636525..2c720268 100644 --- a/Dice/DiceLua.h +++ b/Dice/DiceLua.h @@ -11,5 +11,5 @@ class Lua_State; class DiceEvent; bool lua_msg_call(DiceEvent*, const AttrVar&); -bool lua_call_event(AttrObject eve, const AttrVar&); +bool lua_call_event(const ptr& eve, const AttrVar&); bool lua_call_task(const AttrVars&); \ No newline at end of file diff --git a/Dice/DiceMod.cpp b/Dice/DiceMod.cpp index b8dda411..2807e8d7 100644 --- a/Dice/DiceMod.cpp +++ b/Dice/DiceMod.cpp @@ -783,21 +783,21 @@ void DiceModManager::call_cycle_event(const string& id) { if (auto trigger{ eve->get_obj("trigger") }; trigger->has("cycle")) { sch.add_job_for(parse_seconds(trigger->at("cycle")), eve); } - if (auto action{ eve->get_obj("action") })call_event(eve, action); + if (auto action{ eve->get_obj("action") })call_event(eve.p, action); } void DiceModManager::call_clock_event(const string& id) { if (id.empty() || !global_events.count(id))return; AttrObject eve{ global_events[id] }; - if (auto action{ eve->get_obj("action") })call_event(eve, action); + if (auto action{ eve->get_obj("action") })call_event(eve.p, action); } -bool DiceModManager::call_hook_event(AttrObject eve) { +bool DiceModManager::call_hook_event(const AttrObject& eve) { string hookEvent{ eve->has("hook") ? eve->get_str("hook") : eve->get_str("Event") }; if (hookEvent.empty())return false; for (auto& [id, hook] : multi_range(hook_events, hookEvent)) { if (auto action{ hook->get_obj("action")}) { if (hookEvent == "StartUp" || hookEvent == "DayEnd" || hookEvent == "DayNew") { if (action->has("lua")) { - std::thread th(lua_call_event, eve, action->at("lua")); + std::thread th(lua_call_event, eve.p, action->at("lua")); th.detach(); } if (action->has("js")) { @@ -806,12 +806,12 @@ bool DiceModManager::call_hook_event(AttrObject eve) { } #ifdef DICE_PYTHON if (action->has("py")) { - std::thread th(py_call_event, eve, action->at("py")); + std::thread th(py_call_event, eve.p, action->at("py")); th.detach(); } #endif //DICE_PYTHON } - else call_event(eve, action); + else call_event(eve.p, action); } } return eve->is("blocked"); @@ -1356,9 +1356,9 @@ void DiceModManager::save() { remove(DiceDir / "conf" / "ModList.json"); } } -void call_event(AttrObject eve, const AttrObject& action) { +void call_event(const ptr& eve, const AttrObject& action) { if (action->has("lua"))lua_call_event(eve, action->at("lua")); - if (action->has("js"))js_call_event(eve.p, action->at("js")); + if (action->has("js"))js_call_event(eve, action->at("js")); #ifdef DICE_PYTHON if (action->has("py"))py_call_event(eve, action->at("py")); #endif //DICE_PYTHON diff --git a/Dice/DiceMod.h b/Dice/DiceMod.h index 74969fd3..c68a4159 100644 --- a/Dice/DiceMod.h +++ b/Dice/DiceMod.h @@ -209,7 +209,7 @@ class DiceModManager { void call_cycle_event(const string&); void call_clock_event(const string&); //return if event is blocked - bool call_hook_event(AttrObject); + bool call_hook_event(const AttrObject&); bool listen_order(DiceEvent* msg) { return final_reply.listen(msg, 1); } bool listen_reply(DiceEvent* msg) { return final_reply.listen(msg, 2); } @@ -238,4 +238,4 @@ class DiceModManager { }; extern std::shared_ptr fmt; -void call_event(AttrObject eve, const AttrObject& action); \ No newline at end of file +void call_event(const ptr& eve, const AttrObject& action); \ No newline at end of file diff --git a/Dice/DicePython.cpp b/Dice/DicePython.cpp index 6b38c790..dded94a9 100644 --- a/Dice/DicePython.cpp +++ b/Dice/DicePython.cpp @@ -1099,11 +1099,11 @@ PYDEFKEY(eventMsg) { ? AnysTable{ { {"fromMsg",fromMsg},{"gid",gid}, {"uid", uid} } } : AnysTable{ { {"fromMsg",fromMsg}, {"uid", uid} } }; } - std::thread th([=]() { - DiceEvent msg(*eve); - msg.virtualCall(); - }); - th.detach(); + //std::thread th([=]() { + shared_ptr e{ std::make_shared(*eve.p) }; + e->virtualCall(); + // }); + //th.detach(); return Py_BuildValue(""); } #define REG(name) #name,(PyCFunction)py_##name diff --git a/Dice/DiceRule.cpp b/Dice/DiceRule.cpp index 7d5f9b1b..085fa1de 100644 --- a/Dice/DiceRule.cpp +++ b/Dice/DiceRule.cpp @@ -37,7 +37,7 @@ bool DiceRule::listen_order(DiceEvent* eve) { } bool DiceRule::listen_cassette(const string& tape, DiceEvent* eve)const{ if (auto t{ cassettes.find(tape) }; t != cassettes.end() && t->second) { - call_event(*eve, t->second); + call_event(eve->shared_from_this(), t->second); return eve->is("blocked"); } return false; diff --git a/Dice/DiceSession.cpp b/Dice/DiceSession.cpp index 53f45040..d3161a97 100644 --- a/Dice/DiceSession.cpp +++ b/Dice/DiceSession.cpp @@ -254,9 +254,9 @@ void DiceSession::log_end(DiceEvent* msg) { msg->replyMsg("strLogEnd"); update(); msg->set("hook","LogEnd"); - if (!fmt->call_hook_event(*msg)) { + if (!fmt->call_hook_event(msg->shared_from_this())) { msg->set("cmd", "uplog"); - sch.push_job(*msg); + sch.push_job(msg->shared_from_this()); } } std::filesystem::path DiceSession::log_path()const { diff --git a/Dice/Jsonio.h b/Dice/Jsonio.h index 74ba4611..cd285e42 100644 --- a/Dice/Jsonio.h +++ b/Dice/Jsonio.h @@ -75,95 +75,40 @@ int readJMap(const fifo_json& j, Map& mapTmp) } template int readJson(const std::string& strJson, std::set& setTmp) { - try { - fifo_json j(fifo_json::parse(strJson)); - j.get_to(setTmp); - return j.size(); - } catch (...) { - return -1; - } + fifo_json j(fifo_json::parse(strJson)); + j.get_to(setTmp); + return j.size(); } template int readJson(const std::string& strJson, std::map& mapTmp) { - try - { - fifo_json j = fifo_json::parse(strJson); - return readJMap(j, mapTmp); - } - catch (...) - { - return -1; - } -} - -template -[[deprecated]] int loadJMap(const std::string& strLoc, Map& mapTmp) { - fifo_json j = freadJson(strLoc); - if (j.is_null())return -2; - try - { - return readJMap(j, mapTmp); - } - catch (...) - { - return -1; - } + return readJMap(fifo_json::parse(strJson), mapTmp); } template int loadJMap(const std::filesystem::path& fpLoc, Map& mapTmp) { - if (!std::filesystem::exists(fpLoc))return -2; - fifo_json j = freadJson(fpLoc); - if (j.is_null())return 0; - try - { - return readJMap(j, mapTmp); - } - catch (...) - { - return -1; - } -} - - -//template -template -[[deprecated]] void saveJMap(const std::string& strLoc, const C& mapTmp) -{ - if (mapTmp.empty()) { - remove(strLoc.c_str()); - return; - } - std::ofstream fout(strLoc); - if (fout) - { - fifo_json j; - for (auto& [key,val] : mapTmp) - { - j[GBKtoUTF8(key)] = GBKtoUTF8(val); + if (std::filesystem::exists(fpLoc)) { + if (fifo_json j = freadJson(fpLoc); !j.is_null()) { + return readJMap(j, mapTmp); } - fout << j.dump(2); - fout.close(); + return 0; } + return -2; } template void saveJMap(const std::filesystem::path& fpLoc, const C& mapTmp) { - if (mapTmp.empty()) { - remove(fpLoc); - return; - } - std::ofstream fout(fpLoc); - if (fout) - { - fifo_json j; - for (auto& [key,val] : mapTmp) - { - j[GBKtoUTF8(key)] = GBKtoUTF8(val); + if (!mapTmp.empty()) { + if (std::ofstream fout{ fpLoc }){ + fifo_json j; + for (auto& [key, val] : mapTmp) + { + j[GBKtoUTF8(key)] = GBKtoUTF8(val); + } + fout << j.dump(2); + fout.close(); } - fout << j.dump(2); - fout.close(); } + else remove(fpLoc); }