mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-02 15:55:30 +00:00
Add GetFilename, GetExtension,
This commit is contained in:
parent
32756ccc4a
commit
27b5c6d850
@ -28,6 +28,8 @@ namespace FS {
|
|||||||
std::pair<bool, std::string> Remove(const std::string& Path);
|
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> Rename(const std::string& Path, const std::string& NewPath);
|
||||||
std::pair<bool, std::string> Copy(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);
|
bool Exists(const std::string& Path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,19 @@ struct TLuaChunk {
|
|||||||
std::string PluginPath;
|
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 {
|
class TLuaEngine : IThreaded {
|
||||||
public:
|
public:
|
||||||
TLuaEngine();
|
TLuaEngine();
|
||||||
@ -92,6 +105,8 @@ public:
|
|||||||
std::set<std::string> GetEventHandlersForState(const std::string& EventName, TLuaStateId StateId);
|
std::set<std::string> GetEventHandlersForState(const std::string& EventName, TLuaStateId StateId);
|
||||||
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS);
|
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS);
|
||||||
void CancelEventTimers(const std::string& EventName, TLuaStateId StateId);
|
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";
|
static constexpr const char* BeamMPFnNotFoundError = "BEAMMP_FN_NOT_FOUND";
|
||||||
|
|
||||||
@ -147,6 +162,7 @@ private:
|
|||||||
|
|
||||||
TNetwork* mNetwork;
|
TNetwork* mNetwork;
|
||||||
TServer* mServer;
|
TServer* mServer;
|
||||||
|
TPluginMonitor mPluginMonitor;
|
||||||
std::atomic_bool mShutdown { false };
|
std::atomic_bool mShutdown { false };
|
||||||
fs::path mResourceServerPath;
|
fs::path mResourceServerPath;
|
||||||
std::vector<TLuaPlugin*> mLuaPlugins;
|
std::vector<TLuaPlugin*> mLuaPlugins;
|
||||||
|
@ -8,6 +8,7 @@ public:
|
|||||||
~TLuaPlugin() noexcept = default;
|
~TLuaPlugin() noexcept = default;
|
||||||
|
|
||||||
const TLuaPluginConfig& GetConfig() const { return mConfig; }
|
const TLuaPluginConfig& GetConfig() const { return mConfig; }
|
||||||
|
fs::path GetFolder() const { return mFolder; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TLuaPluginConfig mConfig;
|
TLuaPluginConfig mConfig;
|
||||||
|
@ -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) {
|
bool LuaAPI::FS::Exists(const std::string& Path) {
|
||||||
return fs::exists(fs::relative(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();
|
||||||
|
}
|
||||||
|
@ -13,7 +13,8 @@
|
|||||||
|
|
||||||
TLuaEngine* LuaAPI::MP::Engine;
|
TLuaEngine* LuaAPI::MP::Engine;
|
||||||
|
|
||||||
TLuaEngine::TLuaEngine() {
|
TLuaEngine::TLuaEngine()
|
||||||
|
: mPluginMonitor(fs::path(Application::Settings.Resource) / "Server", *this, mShutdown) {
|
||||||
LuaAPI::MP::Engine = this;
|
LuaAPI::MP::Engine = this;
|
||||||
if (!fs::exists(Application::Settings.Resource)) {
|
if (!fs::exists(Application::Settings.Resource)) {
|
||||||
fs::create_directory(Application::Settings.Resource);
|
fs::create_directory(Application::Settings.Resource);
|
||||||
@ -111,6 +112,28 @@ size_t TLuaEngine::CalculateMemoryUsage() {
|
|||||||
return Usage;
|
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) {
|
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results) {
|
||||||
for (const auto& Result : Results) {
|
for (const auto& Result : Results) {
|
||||||
while (!Result->Ready) {
|
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("Remove", &LuaAPI::FS::Remove);
|
||||||
FSTable.set_function("Rename", &LuaAPI::FS::Rename);
|
FSTable.set_function("Rename", &LuaAPI::FS::Rename);
|
||||||
FSTable.set_function("Copy", &LuaAPI::FS::Copy);
|
FSTable.set_function("Copy", &LuaAPI::FS::Copy);
|
||||||
|
FSTable.set_function("GetFilename", &LuaAPI::FS::GetFilename);
|
||||||
|
FSTable.set_function("GetExtension", &LuaAPI::FS::GetExtension);
|
||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -612,3 +637,59 @@ bool TLuaEngine::TimedEvent::Expired() {
|
|||||||
void TLuaEngine::TimedEvent::Reset() {
|
void TLuaEngine::TimedEvent::Reset() {
|
||||||
LastCompletion = std::chrono::high_resolution_clock::now();
|
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);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user