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,38 +945,47 @@ 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) {
auto CurrentTime = fs::last_write_time(Pair.first); try {
if (CurrentTime != Pair.second) { auto CurrentTime = fs::last_write_time(Pair.first);
mFileTimes[Pair.first] = CurrentTime; if (CurrentTime != Pair.second) {
// grandparent of the path should be Resources/Server mFileTimes[Pair.first] = CurrentTime;
if (fs::equivalent(fs::path(Pair.first).parent_path().parent_path(), mPath)) { // grandparent of the path should be Resources/Server
beammp_info("File \"" + Pair.first + "\" changed, reloading"); if (fs::equivalent(fs::path(Pair.first).parent_path().parent_path(), mPath)) {
// is in root folder, so reload beammp_info("File \"" + Pair.first + "\" changed, reloading");
std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary); // is in root folder, so reload
auto Size = std::filesystem::file_size(Pair.first); std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary);
auto Contents = std::make_shared<std::string>(); auto Size = std::filesystem::file_size(Pair.first);
Contents->resize(Size); auto Contents = std::make_shared<std::string>();
FileStream.read(Contents->data(), Contents->size()); Contents->resize(Size);
TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string()); FileStream.read(Contents->data(), Contents->size());
auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path()); TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string());
auto Res = mEngine.EnqueueScript(StateID, Chunk); auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path());
// TODO: call onInit auto Res = mEngine.EnqueueScript(StateID, Chunk);
mEngine.AddResultToCheck(Res); // TODO: call onInit
} else { mEngine.AddResultToCheck(Res);
// TODO: trigger onFileChanged event } else {
beammp_trace("Change detected in file \"" + Pair.first + "\", event trigger not implemented yet"); // TODO: trigger onFileChanged event
/* beammp_trace("Change detected in file \"" + Pair.first + "\", event trigger not implemented yet");
// is in subfolder, dont reload, just trigger an event /*
auto Results = mEngine.TriggerEvent("onFileChanged", "", Pair.first); // is in subfolder, dont reload, just trigger an event
mEngine.WaitForAll(Results); auto Results = mEngine.TriggerEvent("onFileChanged", "", Pair.first);
for (const auto& Result : Results) { mEngine.WaitForAll(Results);
if (Result->Error) { for (const auto& Result : Results) {
beammp_lua_error(Result->ErrorMessage); if (Result->Error) {
} beammp_lua_error(Result->ErrorMessage);
}*/ }
}*/
}
} }
} 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)");
}
} }
} }