Add GetFilename, GetExtension,

This commit is contained in:
Lion Kortlepel 2021-09-20 22:45:12 +02:00
parent 32756ccc4a
commit 27b5c6d850
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
5 changed files with 110 additions and 2 deletions

View File

@ -28,6 +28,8 @@ namespace FS {
std::pair<bool, std::string> Remove(const std::string& Path);
std::pair<bool, std::string> Rename(const std::string& Path, const std::string& NewPath);
std::pair<bool, std::string> Copy(const std::string& Path, const std::string& NewPath);
std::string GetFilename(const std::string& Path);
std::string GetExtension(const std::string& Path);
bool Exists(const std::string& Path);
}
}

View File

@ -53,6 +53,19 @@ struct TLuaChunk {
std::string PluginPath;
};
class TPluginMonitor : IThreaded {
public:
TPluginMonitor(const fs::path& Path, TLuaEngine& Engine, std::atomic_bool& Shutdown);
void operator()();
private:
TLuaEngine& mEngine;
fs::path mPath;
std::atomic_bool& mShutdown;
std::unordered_map<std::string, fs::file_time_type> mFileTimes;
};
class TLuaEngine : IThreaded {
public:
TLuaEngine();
@ -92,6 +105,8 @@ public:
std::set<std::string> GetEventHandlersForState(const std::string& EventName, TLuaStateId StateId);
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS);
void CancelEventTimers(const std::string& EventName, TLuaStateId StateId);
sol::state_view GetStateForPlugin(const fs::path& PluginPath);
TLuaStateId GetStateIDForPlugin(const fs::path& PluginPath);
static constexpr const char* BeamMPFnNotFoundError = "BEAMMP_FN_NOT_FOUND";
@ -147,6 +162,7 @@ private:
TNetwork* mNetwork;
TServer* mServer;
TPluginMonitor mPluginMonitor;
std::atomic_bool mShutdown { false };
fs::path mResourceServerPath;
std::vector<TLuaPlugin*> mLuaPlugins;

View File

@ -8,6 +8,7 @@ public:
~TLuaPlugin() noexcept = default;
const TLuaPluginConfig& GetConfig() const { return mConfig; }
fs::path GetFolder() const { return mFolder; }
private:
TLuaPluginConfig mConfig;

View File

@ -311,3 +311,11 @@ std::pair<bool, std::string> LuaAPI::FS::Copy(const std::string& Path, const std
bool LuaAPI::FS::Exists(const std::string& Path) {
return fs::exists(fs::relative(Path));
}
std::string LuaAPI::FS::GetFilename(const std::string& Path) {
return fs::path(Path).filename().string();
}
std::string LuaAPI::FS::GetExtension(const std::string& Path) {
return fs::path(Path).extension().string();
}

View File

@ -13,7 +13,8 @@
TLuaEngine* LuaAPI::MP::Engine;
TLuaEngine::TLuaEngine() {
TLuaEngine::TLuaEngine()
: mPluginMonitor(fs::path(Application::Settings.Resource) / "Server", *this, mShutdown) {
LuaAPI::MP::Engine = this;
if (!fs::exists(Application::Settings.Resource)) {
fs::create_directory(Application::Settings.Resource);
@ -111,6 +112,28 @@ size_t TLuaEngine::CalculateMemoryUsage() {
return Usage;
}
sol::state_view TLuaEngine::GetStateForPlugin(const fs::path& PluginPath) {
for (const auto& Plugin : mLuaPlugins) {
if (fs::equivalent(Plugin->GetFolder(), PluginPath)) {
std::unique_lock Lock(mLuaStatesMutex);
return mLuaStates.at(Plugin->GetConfig().StateId)->State();
}
}
beammp_assert_not_reachable();
return mLuaStates.begin()->second->State();
}
TLuaStateId TLuaEngine::GetStateIDForPlugin(const fs::path& PluginPath) {
for (const auto& Plugin : mLuaPlugins) {
if (fs::equivalent(Plugin->GetFolder(), PluginPath)) {
std::unique_lock Lock(mLuaStatesMutex);
return Plugin->GetConfig().StateId;
}
}
beammp_assert_not_reachable();
return "";
}
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results) {
for (const auto& Result : Results) {
while (!Result->Ready) {
@ -432,6 +455,8 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
FSTable.set_function("Remove", &LuaAPI::FS::Remove);
FSTable.set_function("Rename", &LuaAPI::FS::Rename);
FSTable.set_function("Copy", &LuaAPI::FS::Copy);
FSTable.set_function("GetFilename", &LuaAPI::FS::GetFilename);
FSTable.set_function("GetExtension", &LuaAPI::FS::GetExtension);
Start();
}
@ -612,3 +637,59 @@ bool TLuaEngine::TimedEvent::Expired() {
void TLuaEngine::TimedEvent::Reset() {
LastCompletion = std::chrono::high_resolution_clock::now();
}
TPluginMonitor::TPluginMonitor(const fs::path& Path, TLuaEngine& Engine, std::atomic_bool& Shutdown)
: mEngine(Engine)
, mPath(Path)
, mShutdown(Shutdown) {
for (const auto& Entry : fs::recursive_directory_iterator(mPath)) {
// TODO: trigger an event when a subfolder file changes
if (Entry.is_regular_file()) {
mFileTimes[Entry.path().string()] = fs::last_write_time(Entry.path());
}
}
Start();
}
void TPluginMonitor::operator()() {
RegisterThread("PluginMonitor");
beammp_info("PluginMonitor started");
while (!mShutdown) {
std::this_thread::sleep_for(std::chrono::seconds(3));
for (const auto& Pair : mFileTimes) {
auto CurrentTime = fs::last_write_time(Pair.first);
if (CurrentTime != Pair.second) {
mFileTimes[Pair.first] = CurrentTime;
if (fs::path(Pair.first)) {
beammp_info("File \"" + Pair.first + "\" changed, reloading");
// is in root folder, so reload
std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary);
auto Size = std::filesystem::file_size(Pair.first);
auto Contents = std::make_shared<std::string>();
Contents->resize(Size);
FileStream.read(Contents->data(), Contents->size());
TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string());
auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path());
auto Res = mEngine.EnqueueScript(StateID, Chunk);
while (!Res->Ready) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (Res->Error) {
beammp_lua_error(Res->ErrorMessage);
}
} else {
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);
mEngine.WaitForAll(Results);
for (const auto& Result : Results) {
if (Result->Error) {
beammp_lua_error(Result->ErrorMessage);
}
}*/
}
}
}
}
}