move Json* to Util, add Random, RandomRange, RandomIntRange, catch

errors in TPluginMonitor
This commit is contained in:
Lion Kortlepel 2022-03-18 01:52:31 +01:00
parent 39db1a5e42
commit c1e216957b
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 60 additions and 37 deletions

View File

@ -10,6 +10,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <queue> #include <queue>
#include <random>
#include <set> #include <set>
#include <toml11/toml.hpp> #include <toml11/toml.hpp>
#include <unordered_map> #include <unordered_map>
@ -213,6 +214,8 @@ private:
sol::state_view mStateView { mState }; sol::state_view mStateView { mState };
std::queue<fs::path> mPaths; std::queue<fs::path> mPaths;
std::recursive_mutex mPathsMutex; std::recursive_mutex mPathsMutex;
std::mt19937 mMersenneTwister;
std::uniform_real_distribution<double> mUniformRealDistribution01;
}; };
struct TimedEvent { struct TimedEvent {

View File

@ -676,15 +676,26 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
mEngine->CancelEventTimers(EventName, mStateId); mEngine->CancelEventTimers(EventName, mStateId);
}); });
MPTable.set_function("Set", &LuaAPI::MP::Set); MPTable.set_function("Set", &LuaAPI::MP::Set);
MPTable.set_function("JsonEncode", &LuaAPI::MP::JsonEncode);
MPTable.set_function("JsonDecode", [this](const std::string& str) { auto UtilTable = StateView.create_named_table("Util");
UtilTable.set_function("JsonEncode", &LuaAPI::MP::JsonEncode);
UtilTable.set_function("JsonDecode", [this](const std::string& str) {
return Lua_JsonDecode(str); return Lua_JsonDecode(str);
}); });
MPTable.set_function("JsonDiff", &LuaAPI::MP::JsonDiff); UtilTable.set_function("JsonDiff", &LuaAPI::MP::JsonDiff);
MPTable.set_function("JsonFlatten", &LuaAPI::MP::JsonFlatten); UtilTable.set_function("JsonFlatten", &LuaAPI::MP::JsonFlatten);
MPTable.set_function("JsonUnflatten", &LuaAPI::MP::JsonUnflatten); UtilTable.set_function("JsonUnflatten", &LuaAPI::MP::JsonUnflatten);
MPTable.set_function("JsonPrettify", &LuaAPI::MP::JsonPrettify); UtilTable.set_function("JsonPrettify", &LuaAPI::MP::JsonPrettify);
MPTable.set_function("JsonMinify", &LuaAPI::MP::JsonMinify); UtilTable.set_function("JsonMinify", &LuaAPI::MP::JsonMinify);
UtilTable.set_function("Random", [this] {
return mUniformRealDistribution01(mMersenneTwister);
});
UtilTable.set_function("RandomRange", [this](double min, double max) -> double {
return std::uniform_real_distribution(min, max)(mMersenneTwister);
});
UtilTable.set_function("RandomIntRange", [this](int64_t min, int64_t max) -> int64_t {
return std::uniform_int_distribution(min, max)(mMersenneTwister);
});
auto HttpTable = StateView.create_named_table("Http"); auto HttpTable = StateView.create_named_table("Http");
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) { HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
@ -934,7 +945,9 @@ void TPluginMonitor::operator()() {
beammp_info("PluginMonitor started"); beammp_info("PluginMonitor started");
while (!mShutdown) { while (!mShutdown) {
std::this_thread::sleep_for(std::chrono::seconds(3)); std::this_thread::sleep_for(std::chrono::seconds(3));
std::vector<std::string> ToRemove;
for (const auto& Pair : mFileTimes) { for (const auto& Pair : mFileTimes) {
try {
auto CurrentTime = fs::last_write_time(Pair.first); auto CurrentTime = fs::last_write_time(Pair.first);
if (CurrentTime != Pair.second) { if (CurrentTime != Pair.second) {
mFileTimes[Pair.first] = CurrentTime; mFileTimes[Pair.first] = CurrentTime;
@ -966,6 +979,13 @@ void TPluginMonitor::operator()() {
}*/ }*/
} }
} }
} catch (const std::exception& e) {
ToRemove.push_back(Pair.first);
}
}
for (const auto& File : ToRemove) {
mFileTimes.erase(File);
beammp_warn("file '" + File + "' couldn't be accessed, so it was removed from plugin hot reload monitor (probably got deleted)");
} }
} }
} }