Skip to content

Commit

Permalink
fix: DiceEvent
Browse files Browse the repository at this point in the history
修复eventMsg中DiceEvent未由shared_ptr管理内存导致的问题
  • Loading branch information
mystringEmpty committed Feb 11, 2024
1 parent 55ac9de commit ab81390
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 118 deletions.
33 changes: 19 additions & 14 deletions Dice/Dice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,25 @@ EVE_Enable(eventEnable){
}
loadData();
//初始化黑名单
blacklist = make_unique<DDBlackManager>();
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<DDBlackManager>();
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();
Expand Down Expand Up @@ -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<AnysTable>( AttrVars{{"Event","StartUp"}} ));
}

mutex GroupAddMutex;
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions Dice/DiceJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiceEvent> msg{ std::make_shared<DiceEvent>(*eve.p) };
msg->virtualCall();
// });
//th.detach();
return JS_TRUE;
}
QJSDEF(sendMsg) {
Expand Down
14 changes: 7 additions & 7 deletions Dice/DiceLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnysTable>& 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)) {
Expand Down Expand Up @@ -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<DiceEvent> msg{ std::make_shared<DiceEvent>(eve) };
msg->virtualCall();
//});
//th.detach();
return 0;
}
LUADEF(askExtra) {
Expand Down
2 changes: 1 addition & 1 deletion Dice/DiceLua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnysTable>& eve, const AttrVar&);
bool lua_call_task(const AttrVars&);
16 changes: 8 additions & 8 deletions Dice/DiceMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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");
Expand Down Expand Up @@ -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<AnysTable>& 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
Expand Down
4 changes: 2 additions & 2 deletions Dice/DiceMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -238,4 +238,4 @@ class DiceModManager {
};

extern std::shared_ptr<DiceModManager> fmt;
void call_event(AttrObject eve, const AttrObject& action);
void call_event(const ptr<AnysTable>& eve, const AttrObject& action);
10 changes: 5 additions & 5 deletions Dice/DicePython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiceEvent> e{ std::make_shared<DiceEvent>(*eve.p) };
e->virtualCall();
// });
//th.detach();
return Py_BuildValue("");
}
#define REG(name) #name,(PyCFunction)py_##name
Expand Down
2 changes: 1 addition & 1 deletion Dice/DiceRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Dice/DiceSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
93 changes: 19 additions & 74 deletions Dice/Jsonio.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,95 +75,40 @@ int readJMap(const fifo_json& j, Map& mapTmp)
}
template <typename T>
int readJson(const std::string& strJson, std::set<T>& 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 <typename T1, typename T2>
int readJson(const std::string& strJson, std::map<T1, T2>& mapTmp)
{
try
{
fifo_json j = fifo_json::parse(strJson);
return readJMap(j, mapTmp);
}
catch (...)
{
return -1;
}
}

template<class Map>
[[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<class Map>
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 <class C, class TKey, class TVal, TVal& (C::* U)(const TKey&) = &C::operator[]>
template <class C>
[[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 <class C>
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);
}

0 comments on commit ab81390

Please sign in to comment.